程序总是只预测一堂课

时间:2020-08-11 12:33:49

标签: tensorflow keras cnn multilabel-classification multiclass-classification

我正在研究叶子疾病,并尝试在SpMohanty's PlantVillage-dataset上实施CNN。
它有38 classes,每个类别中的图像可变,范围为1500至3000图像/类。 Total images = 54303

这是我的Colab notebook。问题是,在预测图像时,它总是抛出模型训练的第一类。我不明白为什么。
这是模型摘要:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 64, 64, 32)        896       
_________________________________________________________________
activation_1 (Activation)    (None, 64, 64, 32)        0         
_________________________________________________________________
batch_normalization_1 (Batch (None, 64, 64, 32)        128       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 21, 21, 32)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 21, 21, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 21, 21, 64)        18496     
_________________________________________________________________
activation_2 (Activation)    (None, 21, 21, 64)        0         
_________________________________________________________________
batch_normalization_2 (Batch (None, 21, 21, 64)        256       
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 21, 21, 64)        36928     
_________________________________________________________________
activation_3 (Activation)    (None, 21, 21, 64)        0         
_________________________________________________________________
batch_normalization_3 (Batch (None, 21, 21, 64)        256       
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 10, 10, 64)        0         
_________________________________________________________________
dropout_2 (Dropout)          (None, 10, 10, 64)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 10, 10, 128)       73856     
_________________________________________________________________
activation_4 (Activation)    (None, 10, 10, 128)       0         
_________________________________________________________________
batch_normalization_4 (Batch (None, 10, 10, 128)       512       
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 10, 10, 128)       147584    
_________________________________________________________________
activation_5 (Activation)    (None, 10, 10, 128)       0         
_________________________________________________________________
batch_normalization_5 (Batch (None, 10, 10, 128)       512       
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 5, 5, 128)         0         
_________________________________________________________________
dropout_3 (Dropout)          (None, 5, 5, 128)         0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 3200)              0         
_________________________________________________________________
dense_1 (Dense)              (None, 1024)              3277824   
_________________________________________________________________
activation_6 (Activation)    (None, 1024)              0         
_________________________________________________________________
batch_normalization_6 (Batch (None, 1024)              4096      
_________________________________________________________________
dropout_4 (Dropout)          (None, 1024)              0         
_________________________________________________________________
dense_2 (Dense)              (None, 38)                38950     
_________________________________________________________________
activation_7 (Activation)    (None, 38)                0         
=================================================================
Total params: 3,600,294
Trainable params: 3,597,414
Non-trainable params: 2,880
_________________________________________________________________

尽管该模型达到了98.94%的验证准确度和99.7%的火车准确度。
型号历史如下:

Epoch 1/10
678/678 [==============================] - 691s 1s/step - loss: 0.0527 - accuracy: 0.9836 - val_loss: 0.0521 - val_accuracy: 0.9840
Epoch 2/10
678/678 [==============================] - 705s 1s/step - loss: 0.0283 - accuracy: 0.9900 - val_loss: 0.0926 - val_accuracy: 0.9787
Epoch 3/10
678/678 [==============================] - 680s 1s/step - loss: 0.0205 - accuracy: 0.9925 - val_loss: 0.0228 - val_accuracy: 0.9924
Epoch 4/10
678/678 [==============================] - 692s 1s/step - loss: 0.0170 - accuracy: 0.9938 - val_loss: 0.0741 - val_accuracy: 0.9828
Epoch 5/10
678/678 [==============================] - 679s 1s/step - loss: 0.0148 - accuracy: 0.9946 - val_loss: 0.0503 - val_accuracy: 0.9860
Epoch 6/10
678/678 [==============================] - 682s 1s/step - loss: 0.0129 - accuracy: 0.9953 - val_loss: 0.0323 - val_accuracy: 0.9918
Epoch 7/10
678/678 [==============================] - 691s 1s/step - loss: 0.0110 - accuracy: 0.9960 - val_loss: 0.0393 - val_accuracy: 0.9890
Epoch 8/10
678/678 [==============================] - 701s 1s/step - loss: 0.0098 - accuracy: 0.9965 - val_loss: 0.0420 - val_accuracy: 0.9875
Epoch 9/10
678/678 [==============================] - 692s 1s/step - loss: 0.0090 - accuracy: 0.9967 - val_loss: 0.0687 - val_accuracy: 0.9855
Epoch 10/10
678/678 [==============================] - 690s 1s/step - loss: 0.0082 - accuracy: 0.9971 - val_loss: 0.0414 - val_accuracy: 0.9894

这看起来像是模型过度拟合的情况,但是我对各种train_test_splits做了一个混淆矩阵,发现其中80%的训练是最好的。因此,我认为该模型不适合。它还总是预测第一个类,因此我尝试将类的名称更改为[eg:Apple_scab更改为Z_Apple_Scab],然后开始将输出打印为:Apple_Blackrot(Apple_scab之后按字母顺序的下一个标签,现在该标签在训练中成为第一个标签。)

T.I.A。

[更新]:
这是我用来预测新图像的预测函数...

imAr  =  cvtim(impath)
savedclfmodel  =  pickle.load(open(model_file,'rb'))
pred  =  savedclfmodel.predict(imAr)
lb  =  pickle.load(open(lb_file,'rb'))
ret_data  =  lb.inverse_transform(pred)[0]
print("Predicted: ",ret_data)

其中cvtim(image_path)使用keras.preprocessing.image.img_to_array将图像转换为数组 然后savedclfmodel使用pickle.load()打开先前保存的模型权重文件并存储在lb中,然后使用该文件来查找inverse_transform以获得预测的标签

1 个答案:

答案 0 :(得分:0)

您的训练数据看起来很好,并且训练和验证准确性很高。我看不出过度拟合的迹象。验证损失在最低点附近小幅波动是正常的。您没有显示足够的代码来找出问题所在。我假设您在测试集上使用model.predict。像

predictions=model.predict(data,batch_size=batch_size, steps=steps, verbose=0 )
# data is your test data provided as an array or from a generator
# then use the code below to find the predicted class
for i in range (0,len(predictions):
        predicted_class_number=predictions[i].argmax()
        # this will be an integer denoting the class