简体   繁体   English

部署 CNN:训练和测试准确度高,但预测准确度低

[英]Deploying a CNN: High training and test accuracy but low prediction accuracy

Just starting out in ML and created my first CNN to detect the orientation of an image of a face.刚开始学习机器学习并创建了我的第一个 CNN 来检测人脸图像的方向。 I got the training and testing accuracy up to around 96-99% over 2 different sets of 1000 pictures (128x128 RGB).在 2 组不同的 1000 张图片(128x128 RGB)上,我得到了高达 96-99% 的训练和测试准确率。 However, when I go to predict an image from the test set on its own, the model rarely predicts correctly.但是,当我用 go 自己从测试集中预测图像时,model 很少能正确预测。 I think there must be a difference in the way I load data into the model during testing vs prediction.我认为在测试和预测期间将数据加载到 model 的方式肯定有所不同。 Here is how I load the data into the model to train and test:以下是我如何将数据加载到 model 进行训练和测试:

datagen = ImageDataGenerator()
train_it = datagen.flow_from_directory('twoThousandTransformed/', class_mode='categorical', batch_size=32, color_mode="rgb", target_size=(64,64))
val_it = datagen.flow_from_directory('validation/', class_mode='categorical', batch_size=32, color_mode="rgb", target_size=(64,64))
test_it = datagen.flow_from_directory('test/', class_mode='categorical', batch_size=32, color_mode='rgb', target_size=(64,64))

And here is how I load an image to make a prediction:这是我加载图像以进行预测的方式:

image_path='inputPicture/02001.png'
image = tf.keras.preprocessing.image.load_img(image_path)
input_arr = keras.preprocessing.image.img_to_array(image)
reshaped_image = np.resize(input_arr, (64,64,3))
input_arr = np.array([reshaped_image]) 
predictions = model.predict(input_arr)
print(predictions)
classes = np.argmax(predictions, axis = 1)
print(classes)

There must be some difference in the way the ImageDataGenerator handles the images vs. how I am doing it in the prediction. ImageDataGenerator 处理图像的方式与我在预测中的处理方式肯定存在一些差异。 Can y'all help a noobie out?你们都可以帮助一个菜鸟吗? Thanks!谢谢!

Edit: Below is my model编辑:下面是我的 model

imageInput = Input(shape=(64,64,3))
conv1 = Conv2D(128, kernel_size=16, activation='relu')(imageInput)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(64, kernel_size=12, activation='relu')(pool1)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(64, kernel_size=4, activation='relu')(pool2)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
flat = Flatten()(pool3)
hidden1 = Dense(16, activation='relu')(flat)
hidden2 = Dense(16, activation='relu')(hidden1)
hidden3 = Dense(10, activation='relu')(hidden2)
output = Dense(4, activation='softmax')(hidden3)
model = Model(inputs=imageInput, outputs=output)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(train_it, steps_per_epoch=16, validation_data=val_it, validation_steps=8, epochs=25)
print('here we go!')
_, accuracy = model.evaluate(test_it)
print('Accuracy: %.2f' % (accuracy*100))

One thing you can try is to replicate the chosen image to resemble the batch size with which you trained the model.您可以尝试的一件事是复制所选图像以类似于您训练 model 的批量大小。 Also, because of such high training accuracy, it seems your model must be overfitting.此外,由于训练精度如此之高,您的 model 似乎一定是过拟合的。 So, try adding dropout or reducing the number of layers in your network, if the first thing doesn't work out.因此,如果第一件事不起作用,请尝试添加 dropout 或减少网络中的层数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 高精度训练但低精度测试/预测 - High accuracy training but low accuracy test/prediction LSTM CNN训练和测试准确性相同,但预测概率较低 - LSTM CNN training and test accuracy are same with low prediction probability 训练精度高,验证精度低 CNN二元分类 keras - High training accuracy, low validation accuracy CNN binary classification keras Tensorflow Keras - 训练时精度高,预测时精度低 - Tensorflow Keras - High accuracy during training, low accuracy during prediction Keras CNN训练精度不错,但是测试精度很低 - Keras CNN training accuracy is good but test accuracy is very low 训练 CNN 后精度低 - Low accuracy after training a CNN 验证准确率非常低,但训练准确率很高 - Very low validation accuracy but high training accuracy 训练和验证期间的准确性高,使用相同数据集进行预测期间的准确性低 - High accuracy during training and validation, low accuracy during prediction with the same dataset 训练和验证的准确性很高,但测试集的准确性却很低 - High accuracy on both training and validation but very low on test set 在 CNN 中训练每个 epoch 期间,验证准确度很高,但分类报告中的最终准确度非常低,这是什么意思? - validation accuracy is high during training each epoch in a CNN but final accuracy in classification report very low what does it mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM