历时显示的验证准确性比我实际得到的要高得多

时间:2018-11-11 15:47:36

标签: keras conv-neural-network

我是Keras和CNN的新手,因此在以下方面苦苦挣扎。

当我使用以下代码训练图像数据集时:

train_batches = gen1.flow_from_directory(train_path, target_size=(224,224),classes = ['garbage', 'recycled', 'organic'], batch_size = batch)    

valid_batches = gen1.flow_from_directory(valid_path, target_size=(224,224), classes = ['garbage', 'recycled', 'organic'], batch_size = batch)

test_batches = gen1.flow_from_directory(test_path, target_size=(224,224), classes = ['garbage', 'recycled', 'organic'], batch_size = batch)

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=( 224, 224, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(3))
model.add(Activation('softmax'))

model.compile(optimizer=Adam(lr=.0001), loss= 'categorical_crossentropy', metrics = ['accuracy'])

history = model.fit_generator(train_batches, steps_per_epoch =training_data_size//batch, validation_data = valid_batches, validation_steps=validation_data_size//batch, epochs=5, verbose=2)
model.save_weights('Try1.h5')

在5个周期后,我的验证精度达到了77%,如图所示:

enter image description here

但是当我尝试使用其创建混淆矩阵时,我的验证精度为35%:

predictions = model.predict_generator(valid_batches, steps=validation_data_size//batch,verbose=1)
conf_mat2 = confusion_matrix(valid_batches.classes, np.argmax(predictions,axis=1))

0 个答案:

没有答案