Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras loading color images

I have 3 folders with color images. Name of the folder is label for the images inside.

cls1
  |____img_0.png
  |____ ...
  |____img_n.png
cls2
  |____img_0.png
  |____ ...
  |____img_n.png   
cls3
  |____img_0.png
  |____ ...
  |____img_n.png

I would like to use Keras library to create Convolutional neural network for classification, but I can't find, how to create dataset from color images. Can you help me?

like image 952
Krivers Avatar asked May 07 '16 12:05

Krivers


1 Answers

Please consider this gist of pre-trained VGG-16 model with example usage which I find illustrative:

To summarize:

  1. Load your images with OpenCV or scikit-image, etc. (L58)
  2. Resize and/or crop your images to fit the input size (224*224 for VGG-16)(L58)
  3. Calculate and subtract for each image the mean value (L59~L61)
  4. Swap the color dimension before height and width dimensions (L62)
  5. If you use scikit-image, you need to swap the 3 color channels because OpenCV load images as BGR channels but scikit-image load it as RGB channels.
  6. Add the batch size dimension (L63)
  7. Shuffle, partition and concatenate them (along dimension 0) to form the training data X_train, test data X_test etc. along with ground truth Y_train, Y_test etc.
  8. If your data-set is too big to fit in the memory, use a generator and the function fit_generator to do the training instead. (Keras also has evaluate_generator and predict_generator)
  9. You are now ready for training.
like image 97
sytrus Avatar answered Oct 27 '22 11:10

sytrus