A database is a good optimisation when your data is a huge text file, but for images stored in individual files it is probably overkill and will add some unnecessary overhead.
Another point to consider is that pickle might be a little slow to save/load pytorch Tensors.
As you say, the image decoding seems to take most of the time, so I would suggest writing a small script that loads each image_file.jpg into a torch Tensor, and then uses torch.save to save the result back into a file named image_file.pt or something like that.
Then you would need to modify your loader to torch.load the .pt files instead of loading the image files.
In my experience, I would first build an HDF5 file with all your images, which you can build easily following the documentation of h5py on http://docs.h5py.org/en/latest/. During training, build a class inheriting from Dataset which returns your images. Something along this line:
I have implemented dataset_h5. It runs quite well when I set the “num_workers” as 1 or 2. I meet some problem when I set “num_worker” bigger than 2. It seems related to the h5py version. My h5py now is 2.7.1.
File “/home/titan/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, line 281, in next
File “/home/titan/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, line 301, in _process_next_batch
KeyError: ‘Traceback (most recent call last):\n File “/home/titan/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, line 55, in _worker_loop\n samples = collate_fn([dataset[i] for i in batch_indices])\n File “/home/titan/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py”, line 55, in \n samples = collate_fn([dataset[i] for i in batch_indices])\n File “/home/titan/code/res-pytorch/AdobeData/AdobeData.py”, line 595, in getitem\n fgimg = self.fgfile[‘img’][index, …]\n File “h5py/_objects.pyx”, line 54, in h5py._objects.with_phil.wrapper\n File “h5py/_objects.pyx”, line 55, in h5py._objects.with_phil.wrapper\n File “/home/titan/anaconda3/lib/python3.6/site-packages/h5py/_hl/group.py”, line 167, in getitem\n oid = h5o.open(self.id, self._e(name), lapl=self._lapl)\n File “h5py/_objects.pyx”, line 54, in h5py._objects.with_phil.wrapper\n File “h5py/_objects.pyx”, line 55, in h5py._objects.with_phil.wrapper\n File “h5py/h5o.pyx”, line 190, in h5py.h5o.open\nKeyError: ‘Unable to open object (bad object header version number)’\n’
It looks like HDF5 has some concurrency issues. My suggestion of using it is probably not appropriate when you use several workers. I often use one worker because my networks are computationally heavy and I’m not limited by the data iterator. Perhaps you should try other approaches like zarr (http://zarr.readthedocs.io/en/stable/) which have been designed to be thread-safe.
After reading the some codes of torch.utils.data.dataloader, I find it does not work like caffe, which prefetches next batch data during the GPUs are working. I find a blog which tries to do it. I will try it.
After I read the blog, I made a mistake which the dataloader tries to prefetch the next batch data . But I find that it can NOT make full use of CPUs (it shows that it only uses about 60% ability of CPU). In the blog it shows that the data precoessing takes less than 18% of time. Actually, if it could fully achieve its target and made full use of CPUs and the disk is fast enough, it should be near 0%, not 18%.
For anyone reading this, Nvidia DALI is a great solution:
It’s got simple to use Pytorch integration.
I was running into the same problems with the pytorch dataloader. On ImageNet, I couldn’t seem to get above about 250 images/sec. On a Google cloud instance with 12 cores & a V100, I could get just over 2000 images/sec with DALI. However in cases where the dataloader isn’t the bottleneck, I found that using DALI would impact performance 5-10%. This makes sense I think, as you’re using the GPU to some of the decoding & preprocessing
Edit: Dali also has a CPU only mode, meaning no GPU performance hit
Just found this thread a few days ago and implemented NVIDIA DALI to load my data while doing some transfer learning with Alexnet.
On an AWS p2.x8large instance (8 Tesla K80 GPUs), using DALI speeds up my epoch time from 480 s / epoch to 10 s / epoch. No need for any code that explicitly prepares data while the GPUs are running. One important thing to note is that if you’re using a DALI dataloader from an external source (i.e. if you have your image classes grouped by folder), you have to manually reset the dataloader using loader_name.reset() at the end of every training epoch. That’s not how the pytorch dataloader works, so it took me a while to realize that was what was going on here.
The only irritating thing I’ve found about DALI is that there is no immediately obvious way (to me, anyway) to convert pixel values from uint8 with a 0-255 range to float with a 0-1 range, which is needed for transfer learning with pytorch’s pretrained models. Dividing by 255.0 within a pipeline runs into data type mismatch, the ops.Cast() function only converts to float but doesn’t rescale, and all the various flavors of normalize functions don’t allow for it either. The only way I was able to do it was by manually scaling the mean and std values given by pytorch.
Other than that, agree that the pytorch integration is simple and fairly clean.
I am facing the similar problem. However, I am more interested in knowing how to make my dataloader efficient.
Ideally, I want to read multiple temporal blocks for a video, perform transformations like random crop, rescale, extract I3D features for them, concat them in a tensor and return with other information like text embeddings for question, multiple answers.
For now, I am preprocessing my videos and extracting features beforehand, which does not allow me to use multiple random crops and not a good way. The video frames are 60GB size in total. Any tips?
I am using Ubuntu 16.04, 24gb RAM, 245gb/959gb disk space is free right now, 1 Titan Xp GPU, Python 3.6.4, Pytorch 0.4.1, cuda 8.0.61, cudnn 7102.
Dataset is on local disk.