In this post, I will describe how to create HDF5 files for caffe using a set of images. For a full list of the formats supported by caffe, visit this url. Take into account that this is a very general way of creating the HDF5 files, you should be able to easily adapt the code to fulfil your needs (e.g. adding another dataset to the file, changing the names of the dataset).
The 3 major ways to input files to caffe are:
The 3 major ways to input files to caffe are:
- Using an HDF5 file.
- Using an LMDB file.
- Creating a list of the paths to the images in a txt file.
For this section, the code contained in the Python notebook is very self-explicative and most of the methods have less than 10 lines of code, so reading them should be straight forward. I'll just explain the high level idea of what the code is doing:
Most of the times we also want to create sub-images using a stride to get more samples. The method I provided to create the sub-images is getsubimages. You can configure how to create the sub-images using the parameters, by default, it creates 30 x 30 pixel images, with a stride of 10 and without cropping the image.
The python notebook also has a method to show the images contained in the h5 file, just for testing purposes, the method is called showh5images.
- Using a directory with images.
- Read all the images.
- Scale the images from 0 to 1.
- Create subimages of all the images.
- Create a numpy array with all the subimages.
- Give the array the shape required by caffe:
- Number of items.
- Number of channels.
- Height.
- Width.
- Create an H5 with this array.
Most of the times we also want to create sub-images using a stride to get more samples. The method I provided to create the sub-images is getsubimages. You can configure how to create the sub-images using the parameters, by default, it creates 30 x 30 pixel images, with a stride of 10 and without cropping the image.
The python notebook also has a method to show the images contained in the h5 file, just for testing purposes, the method is called showh5images.