Tensorflow classes:哪个class是哪个

Tensorflow classes: which class is which

我有一个 CNN 模型并用它来预测图像的 class:

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)

输出:[25]。我有 36 个文件夹,每个文件夹都有不同的名称 (https://imgur.com/a/rJdEmfJ),里面装满了我用来训练 CNN 的图像,我没有在预处理中标记它们。 (class_names 被留下作为评论,如下所示):

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",
)

我怎么知道哪个 class 是哪个,它是以某种方式对文件夹进行排序还是什么?

如果您设置 labels = 'inferred' 而未指定 class_names,则 class 的顺序为 字母数字 。如果您指定 class_names,那么 class 的顺序将是 class_names.

列表的顺序

例如,我有一个包含30个子目录的目录,每个子目录都包含乐器的图像文件。在下面的代码中 train_data 是一个未指定 class_names 的数据集,因此顺序将是 字母数字 .

数据集reverse_train_data的创建方式是先列出主目录的内容,然后使用python函数用reverse=True排序得到一个颠倒的字母数字列表,然后设置class_names 等于反向列表。

代码中的 print-out 显示每个案例的 classes 的结果顺序。请注意,您可以使用 class_names=train_data.class_names

获得 class 名称顺序
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]))

打印结果如下图

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