如何将keras在训练期间应用于图像的相同预处理步骤应用于numpy数组? 问题设置 我有一个float32numpy数组的集合(比如:(100100)),每个数组都属于几个类 通过使用matplotlib.image.imsave(,array,cmap='gray') 然后,我使用以下方法在该图像数据集上训练了一个预训练的VGG模型

如何将keras在训练期间应用于图像的相同预处理步骤应用于numpy数组? 问题设置 我有一个float32numpy数组的集合(比如:(100100)),每个数组都属于几个类 通过使用matplotlib.image.imsave(,array,cmap='gray') 然后,我使用以下方法在该图像数据集上训练了一个预训练的VGG模型,numpy,tensorflow,opencv,keras,deep-learning,Numpy,Tensorflow,Opencv,Keras,Deep Learning,扩展维度后,当我将其传递给model.predict()时,预测显然是错误的,尽管在训练期间表现良好 我认为这是因为上述输入与model.input在培训期间收到的不同。如果需要,我可以将输入数组保存到上面(2)中的图像中,但我想知道keras将对其应用的下一步 编辑: 根据@Lescurel在评论中的见解并查看的源代码,我使用了该函数,并通过将数组保存到图像并加载它来实现这一点(以重现上面的步骤2,并确保预处理\u函数获得0-255范围内的值) 下面是我如何让它工作的: input= np.r

扩展维度后,当我将其传递给
model.predict()
时,预测显然是错误的,尽管在训练期间表现良好

我认为这是因为上述
输入
model.input
在培训期间收到的不同。如果需要,我可以将
输入
数组保存到上面(2)中的图像中,但我想知道keras将对其应用的下一步

编辑: 根据@Lescurel在评论中的见解并查看的源代码,我使用了该函数,并通过将数组保存到图像并加载它来实现这一点(以重现上面的步骤2,并确保
预处理\u函数
获得0-255范围内的值)

下面是我如何让它工作的:

input= np.random.randn(100, 100).astype(np.float32)  # sample input array

# save `input` to an image and load it
temp_path = "temp_path.jpg"
matplotlib.image.imsave(temp_path, input, cmap='gray')
img = tf.keras.preprocessing.image.load_img(temp_path,
                                            color_mode='rgb',
                                            target_size=(224, 224)
                                        )
# convert to an array
input = tf.keras.preprocessing.image.img_to_array(img)

input = preprocess_input(input)

# the above `input` is passed to the model after adding the extra dimension.
# ...
input= np.random.randn(100, 100).astype(np.float32)  # sample input array

# save `input` to an image and load it
temp_path = "temp_path.jpg"
matplotlib.image.imsave(temp_path, input, cmap='gray')
img = tf.keras.preprocessing.image.load_img(temp_path,
                                            color_mode='rgb',
                                            target_size=(224, 224)
                                        )
# convert to an array
input = tf.keras.preprocessing.image.img_to_array(img)

input = preprocess_input(input)

# the above `input` is passed to the model after adding the extra dimension.
# ...   

对于我的用例,我仍然希望避免将其保存到图像中,并通过确保其值在(0255)范围内,直接将numpy数组转换为
预处理函数
,但这将是另一个问题的范围:)

,以便社区在此提供解决方案

基于@Lescurel在评论中的见解并查看来源 当然,我用过 函数,并通过将数组保存到图像,然后 加载它(复制上面的步骤2并确保
预处理函数
获取范围为0-255的值)

下面是我如何让它工作的:

input= np.random.randn(100, 100).astype(np.float32)  # sample input array

# save `input` to an image and load it
temp_path = "temp_path.jpg"
matplotlib.image.imsave(temp_path, input, cmap='gray')
img = tf.keras.preprocessing.image.load_img(temp_path,
                                            color_mode='rgb',
                                            target_size=(224, 224)
                                        )
# convert to an array
input = tf.keras.preprocessing.image.img_to_array(img)

input = preprocess_input(input)

# the above `input` is passed to the model after adding the extra dimension.
# ...
input= np.random.randn(100, 100).astype(np.float32)  # sample input array

# save `input` to an image and load it
temp_path = "temp_path.jpg"
matplotlib.image.imsave(temp_path, input, cmap='gray')
img = tf.keras.preprocessing.image.load_img(temp_path,
                                            color_mode='rgb',
                                            target_size=(224, 224)
                                        )
# convert to an array
input = tf.keras.preprocessing.image.img_to_array(img)

input = preprocess_input(input)

# the above `input` is passed to the model after adding the extra dimension.
# ...   

(摘自akilat90)

我正在努力理解这个问题。因此,您对图像进行了一些预处理,并用预处理后的图像训练CNN。现在,您正在测试CNN,但是您通过了测试图像,而没有经过相同的预处理阶段。现在,问题是CNN没有预测到输入。是这样吗?如果是这样,您必须通过培训期间使用的相同预处理管道运行图像,否则您将给CNN提供完全不同的数据。@stateMachine yes。问题是如何确保要预测的图像(numpy数组)经过与训练中相同的预处理步骤。如果这是一个自定义数据生成器,我将知道执行的确切步骤。但是,我正在使用keras'
flow\u from\u directory
,如上所示。例如,我有一个
target\u size
参数。那么,
flow\u from\u directory
使用了什么图像大小调整方法?在每个步骤中,他们希望图像的
dtype
是什么?@stateMachine另一个例子:当
flow\u from\u directory
读取图像时,它使用了什么方法(是
opencv.imread
skimage.io
PIL
图像
还是其他什么?。@akilat90您加载图像(在训练时间内)在RGB中,这就是为什么在推断时间中使用
cv2
读取图像后,您还应该转换为
BGRtoRGB
。此外,如果您在训练时间中将样本重新缩放(
1/255.
),那么您还应该确保在推断时间中。
我有一个float32 numpy数组的集合(比如:(100,100)这属于几个类。
因此我假设它们与图像具有相同的范围(值介于0和255之间)?因为这是由
预处理输入所做的假设。