同一Keras CNN的不同预测

时间:2019-04-14 09:25:33

标签: python keras conv-neural-network

我在100x120的图像上训练了CNN,并创建了以下生成器:

train_datagen = ImageDataGenerator(
    rescale = 1. / 255,
    shear_range = 0.2,
    zoom_range = 0.2,
    horizontal_flip = True)

test_datagen = ImageDataGenerator(
    rescale=1/255)

#Apply them
train_generator = train_datagen.flow_from_directory(
    directory = train_data_dir,
    target_size=(parameters["img_width"], parameters["img_height"]),
    batch_size = parameters["batch_size"],
    class_mode= "categorical", 
    subset = "training", 
    color_mode = "rgb",
    seed = 42)

test_generator = test_datagen.flow_from_directory(
    directory = test_data_dir,
    target_size = (parameters["img_width"], parameters["img_height"]),
    color_mode = "rgb",
    batch_size=1,
    class_mode = None,
    shuffle = False,
    seed = 41)

因此,我将测试不同图像上的表现为:

# Method 1
test_generator.reset()
pred = model.predict_generator(test_generator,verbose = 1, steps = 1)
predicted_class_indices = np.argmax(pred, axis = 1)

# Convert the dictionary
labels = (train_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
predictions = [labels[k] for k in predicted_class_indices]

但是,如果我想使用以下方法对同一张图片生成预测:

# Method 2
crop_img = cv2.resize(img,(100, 120))                        
crop_img = np.reshape(crop_img,[1, 100, 120, 3])
crop_img = crop_img.astype('float32')
crop_img /= 255

# ID
pred = model.predict(crop_img)
predicted_class_indices = np.argmax(pred, axis = 1)
prediction = [labels[k] for k in predicted_class_indices]

我得到一个不同的结果。这可能是由于什么造成的?

1 个答案:

答案 0 :(得分:0)

Keras的ImageDataGenerator uses PIL and not openCV under the hood用于加载和调整图像大小。使用双线性插值(cv2.resize的default)调整大小时,ImageDataGenerator默认使用最近的邻居插值。

您可以使用调整大小的方法直到获得相同的结果,或者使用keras预处理中的load_imgimg_to_array便利函数来确保进行与之前完全相同的预处理在训练期间:

from keras.prepropressing.image import load_img, img_to_array

img = load_img(path_to_img, target_size=(100, 120)
img = img_to_array(img) / 255.
model.predict(img)