我正在处理以RGB格式保存的灰色图像的数据集。我对此数据集进行了VGG16训练,并通过以下方式对其进行了预处理:
train_data_gen = ImageDataGenerator(rescale=1./255,rotation_range = 20,
width_shift_range = 0.2,
height_shift_range = 0.2,
horizontal_flip = True)
validation_data_gen = ImageDataGenerator(rescale=1./255)
train_gen= train_data_gen.flow_from_directory(trainPath,
target_size=(224, 224),
batch_size = 64,
class_mode='categorical' )
validation_gen= validation_data_gen.flow_from_directory(validationPath, target_size=(224, 224),
batch_size = 64, class_mode='categorical' )
培训完成后,培训和验证的准确性均很高(92%)。在预测阶段,我首先尝试对图像进行预处理,如https://keras.io/applications/所示:
img = image.load_img(img_path, target_size=(image_size,image_size))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
但是,测试精度非常低!大约50%。我在训练样本和验证样本上再次尝试了预测,但准确率也很低,大约为50%,这意味着问题出在预测阶段。取而代之的是,我使用OpenCV库对图像进行了预处理,虽然精度更高,但仍然不如预期。我尝试对火车样本进行预测(训练期间的准确度为92%),而在预测期间我得到了82%。这是代码:
img = cv2.imread(imagePath)
#np.flip(img, axis=-1)
img= cv2.resize(img, (224, 224),
interpolation = cv2.INTER_AREA)
img = np.reshape(img,
(1, img.shape[0], img.shape[1], img.shape[2]))
img = img/255.
翻转/不翻转图像的结果相同。预处理步骤出了什么问题?谢谢
最佳答案
错误出在调整大小功能的插值参数中。应该是cv2.INTER_NEAREST
而不是cv2.INTER_AREA
。
关于tensorflow - 使用opencv预处理测试图像进行预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64625175/