I have a CNN model and using it to predict the class of an image:
model = load_model(modelName)
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
img = image.load_img(filename1, target_size=(img_width, img_height), color_mode="grayscale")
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
predict = model.predict(images, batch_size=8)
classes = np.argmax(predict, axis=1)
print(classes)
Output: [25]. I have 36 folders, each with a different name (https://imgur.com/a/rJdEmfJ), full of images that I used to train the CNN and I didn't label them in preprocessing. (class_names were left as comment, shown below):
ds_train = tf.keras.preprocessing.image_dataset_from_directory(
directory = 'D:/dataset2/',
labels = 'inferred',
label_mode = 'int',
# class_names=['0', '1', '2', '3', ...]
color_mode = 'grayscale',
batch_size = batchSize,
image_size = (imgHeight, imgWidth),
shuffle = True,
seed = 123,
validation_split = 0.2,
subset = "training",
)
How do I know which class is which, does it sort the folders in some way or what?
CodePudding user response:
If you set labels = 'inferred'
and do not specify class_names
, then the ordering of the classes is alphanumeric. If you specify class_names
then the order of the classes will be the order of the list of class_names
.
For example, I have a directory that contains 30 subdirectories and each of these subdirectories contains image files of musical instruments. In the code below train_data
is a data set where the class_names
are not specified so the order will be alphanumeric.
The dataset reverse_train_data is created by first listing the content of the main directory, then using the python function sorted with reverse=True
to get a reversed alphanumeric list, and then setting class_names
equal to that reversed list.
The print-out in the code shows the resultant order of the classes for each case. Note you can get the class name order using class_names=train_data.class_names
train_dir=r'C:\Temp\instruments\train'
classlist=os.listdir(train_dir)# Note per python documentation list_dir returns an arbitrary ordered list
sorted_classlist=sorted(classlist, reverse=True) # this is a list in reverse alphanumeric order
train_data=tf.keras.utils.image_dataset_from_directory(train_dir, labels='inferred', label_mode='categorical', class_names=None,
color_mode='rgb', batch_size=32, image_size=(224,224), shuffle=False,
seed=None, validation_split=None, subset=None, interpolation='bilinear',
follow_links=False, crop_to_aspect_ratio=False)
reverse_train_data=tf.keras.utils.image_dataset_from_directory(train_dir, labels='inferred', label_mode='categorical', class_names=sorted_classlist,
color_mode='rgb', batch_size=32, image_size=(224,224), shuffle=False,
seed=None, validation_split=None, subset=None, interpolation='bilinear',
follow_links=False, crop_to_aspect_ratio=False)
class_names=train_data.class_names
reverse_class_names=reverse_train_data.class_names
print('{0:^25s}{1:^25s}{2:^25s}'.format('CLASS NAMES', 'REVERSE CLASS NAMES', 'SORTED CLASS LIST'))
for i in range (len(class_names)):
print('{0:^25s}{1:^25s}{2:^25s}'.format(class_names[i], reverse_class_names[i], sorted_classlist[i]))
The result of the printout is shown below
Found 4793 files belonging to 30 classes.
Found 4793 files belonging to 30 classes.
CLASS NAMES REVERSE CLASS NAMES SORTED CLASS LIST
Didgeridoo violin violin
Tambourine tuba tuba
Xylophone trumpet trumpet
acordian trombone trombone
alphorn steel drum steel drum
bagpipes sitar sitar
banjo saxaphone saxaphone
bongo drum piano piano
casaba ocarina ocarina
castanets marakas marakas
clarinet harp harp
clavichord harmonica harmonica
concertina guitar guitar
drums guiro guiro
dulcimer flute flute
flute dulcimer dulcimer
guiro drums drums
guitar concertina concertina
harmonica clavichord clavichord
harp clarinet clarinet
marakas castanets castanets
ocarina casaba casaba
piano bongo drum bongo drum
saxaphone banjo banjo
sitar bagpipes bagpipes
steel drum alphorn alphorn
trombone acordian acordian
trumpet Xylophone Xylophone
tuba Tambourine Tambourine
violin Didgeridoo Didgeridoo