2. Requirements on reading training set and validation set are different.
This tutorial is quite complicated because you do need this knowledge of hardware & system to run fast on ImageNet-sized dataset.
In training it's OK to reorder, regroup, or even duplicate some datapoints, as long as the
However, for __smaller datasets__ (e.g. several GBs of space, or lightweight preprocessing), a simple reader plus some prefetch should work well enough.
distribution roughly stays the same.
But in validation we often need the exact set of data, to be able to compute the correct error.
This will affect how we build the DataFlow.
3. The actual performance would depend on not only the disk, but also memory (for caching) and CPU (for data processing).
You may need to tune the parameters (#processes, #threads, size of buffer, etc.)
or change the pipeline for new tasks and new machines to achieve the best performance.
4. This tutorial could be too complicated for people new to system architectures, but you do need these to be able to run fast enough on ImageNet-sized dataset.
However, for smaller datasets (e.g. several GBs of images with lightweight preprocessing), a simple reader plus some prefetch should work well enough.
Figure out the bottleneck first, before trying to optimize any piece in the whole system.
will stack the datapoints into an `numpy.ndarray`, but since original images are of different shapes, we use
will stack the datapoints into an `numpy.ndarray`, but since original images are of different shapes, we use
`use_list=True` so that it just produces lists.
`use_list=True` so that it just produces lists.
On an SSD you probably can already observe good speed here (e.g. 5 it/s, that is 1280 images/s), but on HDD the speed may be just 1 it/s,
On a good filesystem you probably can already observe good speed here (e.g. 5 it/s, that is 1280 images/s), but on HDD the speed may be just 1 it/s,
because we are doing heavy random read on the filesystem (regardless of whether `shuffle` is True).
because we are doing heavy random read on the filesystem (regardless of whether `shuffle` is True).
Image decoding in `cv2.imread` could also be a bottleneck at this early stage.
We will now add the cheapest pre-processing now to get an ndarray in the end instead of a list
We will now add the cheapest pre-processing now to get an ndarray in the end instead of a list
(because training will need ndarray eventually):
(because training will need ndarray eventually):
...
@@ -68,13 +77,13 @@ Now it's time to add threads or processes:
...
@@ -68,13 +77,13 @@ Now it's time to add threads or processes:
ds = PrefetchDataZMQ(ds1, nr_proc=25)
ds = PrefetchDataZMQ(ds1, nr_proc=25)
ds = BatchData(ds, 256)
ds = BatchData(ds, 256)
```
```
Here we start 25 processes to run `ds1`, and collect their output through ZMQ IPC protocol.
Here we start 25 processes to run `ds1`, and collect their output through ZMQ IPC protocol,
Using ZMQ to transfer data is faster than `multiprocessing.Queue`, but data copy (even
which is faster than `multiprocessing.Queue`. You can also apply prefetch after batch, of course.
within one process) can still be quite expensive when you're dealing with large data.
For example, to reduce copy overhead, the ResNet example deliberately moves certain pre-processing (the mean/std normalization) from DataFlow to the graph.
The above DataFlow might be fast, but since it forks the ImageNet reader (`ds0`),
This way the DataFlow only transfers uint8 images as opposed float32 which takes 4x more memory.
it's **not a good idea to use it for validation** (for reasons mentioned at top).
Alternatively, you can use multi-threaded preprocessing like this:
Alternatively, you can use multi-threading like this:
```eval_rst
```eval_rst
.. code-block:: python
.. code-block:: python
:emphasize-lines: 3-6
:emphasize-lines: 3-6
...
@@ -83,56 +92,69 @@ Alternatively, you can use multi-threading like this:
...
@@ -83,56 +92,69 @@ Alternatively, you can use multi-threading like this: