-1

I found some code to create a Machine Learning Model with Keras, but i don't really know how i can put my own images in there. The model is created like that:

model = Model(inputs=base_model.input, outputs=predictions)
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics = ['accuracy'])

And the training_set is created like that:

train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)

training_set = train_datagen.flow_from_directory('/content/drive/My Drive/multiclass/train',
                                                 target_size = (224, 224),
                                                 batch_size = 32,
                                                 class_mode = 'categorical')

Here you can see a path to a google-drive location which the author connects like this at the beginning from google.colab import drive; drive.mount('/content/drive/') But i can't see how the data looks like in this google drive.

With this data the Training starts like that:

# Training the model for 5 epochs
model.fit_generator(training_set,
                     steps_per_epoch = 8000,
                     epochs = 5,
                     validation_data = test_set,
                     validation_steps = 200)

How can i put my own images in there? How does the Model know which image belongs to which class? Do i have to just put them in different folders and name them after my classes?

Another thing i wonder is, do the images have to be a certain size, like 224*224 pixels as the target_size is in the flow_from_directory method or are they transformed automatically in this model?

I am thankful for every answer!

Best regards Alex

lxg95
  • 553
  • 2
  • 8
  • 28
  • 1
    In order to use `flow_from_directory` each class needs to be in a subfolder. So its get automatically labeled. – Frightera May 20 '21 at 15:22
  • This is directly mentioned in the documentation of Keras: https://keras.io/api/preprocessing/image/#flowfromdirectory-method If you can find the answer in the software's documentation, there is no need to ask a SO question. – Dr. Snoopy May 20 '21 at 17:27

1 Answers1

1

Here is a simple example. Assume you want to build a CNN that can classify dogs, cats and mice. So you have 3 classes. Now assume you have gathered 1000 images of dogs, 1000 images of cats and 1000 images of mice. Now you want to take your image data set and split it into three datasets, One is for training, one is for validation and one is for test. So first create 3 directories. Lets call them train_dir, test_dir and valid_dir. Now in each of these directories create three subdirectories call them cats, dogs and mice. So you have the structure shown below

train_dir
      -- cats
      -- dogs
      -- mice

test_dir
      -- cats
      -- dogs
      -- mice

valid_dir
      -- cats
      -- dogs
      -- mice

Now say you want 80% of your data to be used for training, 10% for validation and 10% for testing. So take 800 images of cats and put them into the cats folder in the train_dir. Do the same for 800 dog images for the dog directory and 800 images for the mice directory. Then of the 600 images you have left, take 100 images of cats and put then in the cat directory under the test_dir. Do the same for 100 dog images and 100 mice images. Finally take the 300 images you have left and put 100 cat images in the cat directory under the valid_dir. Do the same for 100 dog images and 100 mice images. Your dataset is now complete. Then create a train, test and validation generator. For the train generator in flow_from_directory use the train_dir, for the valid generator use the valid_dir and for the test generator use the test_dir.

Gerry P
  • 7,662
  • 3
  • 10
  • 20