Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 与CNN相同的预测不同_Python_Keras_Conv Neural Network - Fatal编程技术网

Python 与CNN相同的预测不同

Python 与CNN相同的预测不同,python,keras,conv-neural-network,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 = tr

我在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]
我得到了一个不同的结果。原因是什么?

Keras'ImageDataGenerator加载和调整图像大小。当您使用双线性插值(cv2.resize)调整大小时,默认情况下ImageDataGenerator使用最近邻插值

您可以使用调整大小的方法,直到获得相同的结果,或者使用keras preprocessing的和便利功能,以确保您所做的预处理与培训期间所做的完全相同:

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)
Keras的ImageDataGenerator用于加载和调整图像大小。当您使用双线性插值(cv2.resize)调整大小时,默认情况下ImageDataGenerator使用最近邻插值

您可以使用调整大小的方法,直到获得相同的结果,或者使用keras preprocessing的和便利功能,以确保您所做的预处理与培训期间所做的完全相同:

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)

默认情况下,
cv2
读入
BGR
。您确定要将
RGB
图像发送到网络吗?我添加了img=cv2.cvtColor(img,cv2.COLOR\u BGR2RGB),但默认情况下,它仍报告不同的输出
cv2
读取
BGR
。你确定要将
RGB
图像发送到网络吗?我添加了img=cv2.cvtColor(img,cv2.COLOR\u BGR2RGB),但它仍然报告不同的输出使用此设置它可以工作,但是有没有办法利用keras预处理库处理已经加载并经过一些先前预处理步骤的图像(例如去噪等)是的,您可以使用keras预处理中的
img_to_array
array_to_img
在numpy数组和PIL图像之间来回切换,并使用PIL的Image.resize()方法进行大小调整,方法与
load_img()中的方法相同
如果我使用load\u img保存并重新加载预处理的图像,它会工作,如果我使用img=cv2.cvtColor(img,cv2.COLOR\u BGR2RGB)img=array\u to\u img(img)img=img.resize((100120),image.NEAREST)img=img\u to\u array(img)/255.img=np.reformate(img,[1100,120,3])它没有什么意义,它不工作吗?你得到错误消息,或者只是不同的结果吗?我得到不同的结果使用这个设置它工作,但有没有一种方法可以利用keras预处理库与已经加载的图像,并已通过一些预处理步骤(例如去噪等)是的,您可以使用keras preprocessing中的
img_to_array
array_to_img
在numpy数组和PIL图像之间来回切换,并使用PIL的Image.resize()方法进行大小调整,方法与加载img()中的方法相同如果我使用load\u img保存并重新加载预处理的图像,它会工作,如果我使用img=cv2.cvtColor(img,cv2.COLOR\u BGR2RGB)img=array\u to\u img(img)img=img.resize((100120),image.NEAREST)img=img\u to\u array(img)/255.img=np.reformate(img,[1100,120,3])它没有什么意义,它不工作吗?你得到一个错误消息,或者只是不同的结果?我得到不同的结果