[英]CNN - Wrong prediction with multiclass classification
我正在建立一個卷積網絡來預測3類圖像(貓,狗和人)。 我訓練並訓練了它,但是隨后當我通過貓的圖像進行預測時,總是給出錯誤的輸出。 我試過其他貓的照片,但結果沒有改變。 對於人和狗來說,只貓就沒有問題。
cnn = Sequential()
#------------------- Convolução e Pooling
cnn.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation = 'relu'))
cnn.add(Dropout(0.5))
cnn.add(MaxPooling2D(pool_size = (2, 2)))
cnn.add(Conv2D(32, (3, 3), activation = 'relu'))
cnn.add(Dropout(0.5))
cnn.add(MaxPooling2D(pool_size = (2, 2)))
cnn.add(Conv2D(64, (3, 3), activation = 'relu'))
cnn.add(MaxPooling2D(pool_size = (2, 2)))
cnn.add(Conv2D(64, (3, 3), activation = 'relu'))
cnn.add(Dropout(0.5))
cnn.add(MaxPooling2D(pool_size = (2, 2)))
#Full connection
cnn.add(Flatten())
cnn.add(Dense(units = 128, activation = 'relu'))
cnn.add(Dense(units = 4, activation = 'softmax'))
# Compiling the CNN
cnn.compile(optimizer = OPTIMIZER, loss = 'categorical_crossentropy', metrics = ['accuracy'])
filepath="LPT-{epoch:02d}-{loss:.4f}.h5"
checkpoint = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
callbacks_list = [checkpoint]
12000張火車圖像-3000張測試圖像
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('data/train',
target_size = tgt_size,
batch_size = batch_size,
class_mode = 'categorical')
test_set = test_datagen.flow_from_directory('data/test',
target_size = tgt_size,
batch_size = batch_size,
class_mode = 'categorical')
cnn.fit_generator(training_set,
#steps_per_epoch = 12000,
steps_per_epoch = nb_train_samples // batch_size,
epochs = EPOCHS,
verbose = VERBOSE,
validation_data = test_set,
validation_steps = nb_validation_samples // batch_size,
callbacks = callbacks_list)
我最好的訓練結果:
loss: 0.6410 - acc: 0.7289 - val_loss: 0.6308 - val_acc: 0.7293
班級指標:
{'.ipynb_checkpoints': 0, 'cats': 1, 'dogs':2, 'person':3}
(我無法刪除該ipynb文件夾)
預測:
pred1 = 'single_prediction/ct.jpg'
pred2 = 'single_prediction/ps.jpg'
pred3 = 'data/single_prediction/dg.jpg'
test_img = image.load_img(pred1, target_size = tgt_size)
test_img = image.img_to_array(test_img)
test_img = np.expand_dims(test_img, axis = 0)
pred = new_model.predict(test_img)
print(pred)
if pred[0][1] == 1:
print('It is a cat!')
elif pred[0][2] == 1:
print('It is a dog!')
elif pred[0][3] == 1:
print('It is a Person!')
以及貓圖像的輸出:
[[0.000000e+00 0.000000e+00 8.265931e-34 1.000000e+00]]
我已經嘗試過:更改層數(添加和刪除),增加時期,減少批處理...我也嘗試過使用np.argmax()。 有人可以在這里給我點燈嗎?
更新 :我使用shutil.rmtree()命令刪除了jupyter筆記本的隱藏文件夾,並對其進行了大約40個時期的訓練,直到停止改進為止。 最后,我對預測圖像進行了縮放,並正確了。
test_img = image.img_to_array(test_img)/255
感謝您的所有幫助!
問題出在ipynb checkpoints文件夾。 這是一個隱藏的文件夾。 您需要先刪除它。 然后將您的輸出密集層更改為3個單位(類)。 改變這個
cnn.add(Dense(units = 4, activation = 'softmax'))
至
cnn.add(Dense(units = 3, activation = 'softmax'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.