Reputation: 95
I am using my own image data generator. It generates 0 ,90, 180 and 270 degrees rotated versions of image batches and returns them with their classes. I use built in ImageDataGenerator
function to test the model. However flow_from_directory
generates different class indices. Output of train_generator.class_indices
is {'0': 0, '90': 1, '180': 2, '270': 3}
. But test_generator.class_indices
returns {'0': 0, '180': 1, '270': 2, '90': 3}
. Simply I can change order of rotation angles but this problem is caused by the file system of operating system and I will run the code on a different operating system. In this case I need an automated solution. Is there a way to change the class indices of flow_from_directory
method?
Upvotes: 2
Views: 2946
Reputation: 39
Absolutely you should be able to, and you can.
From Keras official document: https://keras.io/api/preprocessing/image/: There's an argument name classes for both flow_from_directory and flow_from_dataframe method.
For flow_from_directory it explained:
classes: Optional list of class subdirectories (e.g. ['dogs', 'cats']). Default: None. If not provided, the list of classes will be automatically inferred from the subdirectory names/structure under directory, where each subdirectory will be treated as a different class (and the order of the classes, which will map to the label indices, will be alphanumeric). The dictionary containing the mapping from class names to class indices can be obtained via the attribute class_indices.
This means, when indicated in your flow_from_directory method classes=['dogs', 'cats'], label "dogs" then "cats" will be mapped into label indices by list order, i.e., 0, 1...
By not doing so, they will be in alphanumeric order, "dogs" is 1 then "cats" is 0.
Upvotes: 1
Reputation: 150825
looks like you can do
flow_from_directory(directory,
classes={'0': 0,
'90': 1,
'180': 2,
'270': 3}
)
Upvotes: 5