简体   繁体   English

为 Keras Model 推理将 2D 灰度重塑为 4D

[英]Reshaping 2D Grayscale into 4D for Keras Model Inference

I have a pre-trained Keras model that I need to use to classify a 512x 512 image that is originally in grayscale format.我有一个预先训练的 Keras model ,我需要用它来对最初是灰度格式的 512x 512 图像进行分类。 The input to the Keras model should be in the shape (None, 512, 512, 1). Keras model 的输入应该是形状(无、512、512、1)。 在此处输入图像描述 . .

I executed the following code:我执行了以下代码:

model=load_model('model.h5')
img = Image.open('img.jpg')
img_array = np.array (img)
img_array = img_array/255
model.predict (img_array)

However, I get the following error但是,我收到以下错误

Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (512, 512)检查输入时出错:预期 input_1 有 4 个维度,但得到了形状为 (512, 512) 的数组

I know that I need to reshape my grayscale image into 4D to match the desired input shape, however, I am not sure how to do this so that the image keeps its original features.我知道我需要将灰度图像重塑为 4D 以匹配所需的输入形状,但是,我不确定如何执行此操作以使图像保持其原始特征。 How can I make the grayscale image into 4D properly?如何正确地将灰度图像变成 4D?

Thanks.谢谢。

try reshaping the array尝试重塑数组

img_array = img_array.reshape((1, 512, 512, 1)) 

here 1st and last dimension are batch size and channels respectively这里第一个和最后一个维度分别是批量大小和通道

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM