【问题标题】:Keras ImageDataGenerator Predicts More Than The Prediction SetKeras ImageDataGenerator 比预测集预测更多
【发布时间】:2021-08-25 19:58:53
【问题描述】:

我正在尝试制作一个分类美国手语的 CNN 模型。我已经创建并训练了我的模型。现在我正在尝试预测课程。我的预测集有 7250 个未标记的图像,但是当我进行预测时,模型执行 587250 个预测,而我需要它来执行 7250 个预测。我提供下面的代码。这是什么原因?我做错了吗?

代码块 1:

predict_set = data.flow_from_directory('/content/gdrive/MyDrive/test_data/')

输出 1:

Found 7250 images belonging to 1 classes.

代码块 2:

import numpy as np
predictions = model.predict_classes(predict_set)
print(len(predictions),"\n", predictions)

输出 2:

587250 
[27 27 27 ... 27  6 12]

编辑:

CNN 模型:

model = Sequential()

# First Layer
model.add(Conv2D(filters = 64, kernel_size = (4, 4), input_shape = (64, 64, 3), activation = 'relu'))
model.add(Conv2D(filters = 64, kernel_size = (4, 4), strides = 2,  activation = 'relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization(axis = 3, momentum = 0.8))

# Second Layer
model.add(Conv2D(filters = 128, kernel_size = (4, 4), activation = 'relu'))
model.add(Conv2D(filters = 128, kernel_size = (4, 4), strides = 2,  activation = 'relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization(axis = 3, momentum = 0.8))

# Third Layer
model.add(Conv2D(filters = 256, kernel_size = (4, 4), activation = 'relu'))
model.add(Conv2D(filters = 256, kernel_size = (4, 4), strides = 2,  activation = 'relu'))
model.add(Dropout(0.5))
model.add(BatchNormalization(axis = 3, momentum = 0.8))

# Flattening
model.add(Flatten())
model.add(Dropout(0.5))

model.add(Dense(units = 512, activation = 'relu')) # Hidden Layer
model.add(Dense(units = 29, activation = 'softmax')) # Output Layer

#Compiling the CNN
model.compile(optimizer= 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

model.fit_generator(training_set, steps_per_epoch = 350, epochs = 15, validation_data = test_set, validation_steps = 100)

test_set和training_set的batch size是64

模型总结:

Model: "sequential_24"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_142 (Conv2D)          (None, 61, 61, 64)        3136      
_________________________________________________________________
conv2d_143 (Conv2D)          (None, 29, 29, 64)        65600     
_________________________________________________________________
dropout_90 (Dropout)         (None, 29, 29, 64)        0         
_________________________________________________________________
batch_normalization_69 (Batc (None, 29, 29, 64)        256       
_________________________________________________________________
conv2d_144 (Conv2D)          (None, 26, 26, 128)       131200    
_________________________________________________________________
conv2d_145 (Conv2D)          (None, 12, 12, 128)       262272    
_________________________________________________________________
dropout_91 (Dropout)         (None, 12, 12, 128)       0         
_________________________________________________________________
batch_normalization_70 (Batc (None, 12, 12, 128)       512       
_________________________________________________________________
conv2d_146 (Conv2D)          (None, 9, 9, 256)         524544    
_________________________________________________________________
conv2d_147 (Conv2D)          (None, 3, 3, 256)         1048832   
_________________________________________________________________
dropout_92 (Dropout)         (None, 3, 3, 256)         0         
_________________________________________________________________
batch_normalization_71 (Batc (None, 3, 3, 256)         1024      
_________________________________________________________________
flatten_21 (Flatten)         (None, 2304)              0         
_________________________________________________________________
dropout_93 (Dropout)         (None, 2304)              0         
_________________________________________________________________
dense_42 (Dense)             (None, 512)               1180160   
_________________________________________________________________
dense_43 (Dense)             (None, 29)                14877     
=================================================================
Total params: 3,232,413
Trainable params: 3,231,517
Non-trainable params: 896
_________________________________________________________________

【问题讨论】:

  • 你的输出层有多少个神经元?你能附上你的模型结构吗?
  • 我会这样做的,抱歉信息不足。
  • 测试生成器的批量大小是多少?
  • 你应该打印 predictions.shape,我也不确定 predict_classes 是否适用于生成器。
  • @Frightera 我没有为生成器指定批量大小,因为当我尝试这样做时它给了我一个类型错误。

标签: python tensorflow machine-learning keras conv-neural-network


【解决方案1】:

predict_classes 的文档非常有限。如果您使用的是基于下面显示的文档的生成器,它可能无法正常工作

x: input data, as a Numpy array or list of Numpy arrays (if the model has multiple inputs).

所以我认为它不适用于生成器。所以你必须使用model.predict。

predictions=model.predict(predict_set)
for p in predictions:
    class_index=np.argmax(p) # this is the integer value assogned to a class.

如果您使用生成器来训练您的模型(我将其命名为 train_gen),那么您可以获得属于 for (class name, class_index) 的 class_indices 字典 如下

class_dict=train_gen.class_indices
# reverse the dictionary
for key,value in class_dict.items():
        new_dict[value]=key  
````
now you can use the new_dict to get the class name with the code below
```` 
for p in predictions:
    class_index=np.argmax(p)
    class_name=new_dict[class_index]
````

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-07
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多