未捕获的错误:检查时出错:预期conv2d_input具有4个维,但数组的形状为[275,183,3] [英] Uncaught Error: Error when checking : expected conv2d_input to have 4 dimension(s), but got array with shape [275,183,3]

查看:42
本文介绍了未捕获的错误:检查时出错:预期conv2d_input具有4个维,但数组的形状为[275,183,3]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在训练我的keras模型之前,我对图像执行了以下操作:

I performed following operations on the images before training my keras model:

for img in os.listdir(path):    
    # convert to array
    img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE) 
    # resize to normalize data size
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) 
    # add this to our training_data list
    training_data.append([new_array, class_num]) 

#shuffle the data 
random.shuffle(training_data)

#empty lists (X for features, y for labels)
X = []
y = []

for features,label in tqdm(training_data):
    X.append(features)
    y.append(label)


X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)

然后我要训练模型.这是起始层:

I am then training the model. Here is the starting layer:

#start creating model 
model = Sequential()

model.add(Conv2D(256, (3, 3), input_shape=X.shape[1:])

我正在使用训练有素的模型来获得一些预测(我正在python中训练模型,然后将模型加载到Tensorflow.js )

I am using the using the trained model to get some predictions( I am training the model in python and then I am loading the model into Tensorflow.js)

用于预测的代码段

let imageTensor = tf.fromPixels(image);
model.predict(imageTensor).print();

我遇到以下错误:

未捕获的错误:检查时出错:预期conv2d_input具有4维,但数组的形状为[275,183,3]

Uncaught Error: Error when checking : expected conv2d_input to have 4 dimension(s), but got array with shape [275,183,3]

将上面的代码更改为

let imageTensor = tf.fromPixels(image).resizeNearestNeighbor([50,50]).mean(2).toInt().expandDims(2);
model.predict(imageTensor).print();

出现以下错误:

未捕获的错误:检查时出错:预期conv2d_input具有4维,但数组的形状为[50,50,1]

Uncaught Error: Error when checking : expected conv2d_input to have 4 dimension(s), but got array with shape [50,50,1]

最后,当我这样做

let imageTensor = tf.fromPixels(image).resizeNearestNeighbor([50,50]).toInt().expandDims();
 model.predict(imageTensor).print();

我收到以下错误:

检查时出错:预期conv2d_input具有形状[null,50,50,1],但数组的形状为[1,50,50,3].

Error when checking : expected conv2d_input to have shape [null,50,50,1] but got array with shape [1,50,50,3].

推荐答案

这与模型输入的尺寸和作为参数传递给预测方法的图像的尺寸不匹配有关.

It is related to a mismatch of the dimension of the input of the model and the dimension of the image passed in as parameter to the predict method.

一个人可以考虑通过以下方式重塑图像:

One might consider reshaping the image the following way:

imageTensor.reshape([-1, 50, 50, 3])

这篇关于未捕获的错误:检查时出错:预期conv2d_input具有4个维,但数组的形状为[275,183,3]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆