为什么我在使用 np.array(Image) 时遇到重塑错误?

我在进行一个手写识别项目,使用 IamDB 时一切正常。然而,当我尝试拍摄自己的手写图片时,出现了这个错误,有人知道如何解决吗?我尝试将图片转换为灰度,但没有效果

images = []PTH = Path_to_test #filename  = os.path.basename(PTH[0])filename = PTH[0]#******************************************************print (filename)im = Image.open(filename)#print ("cu")cur_width = im.size[0]cur_height = im.size[1]    # print(cur_width, cur_height)​height_fac = 113 / cur_height​new_width = int(cur_width * height_fac)size = new_width, 113​imresize = im.resize((size), Image.ANTIALIAS)  # Resize so height = 113 while keeping aspect rationow_width = imresize.size[0]now_height = imresize.size[1]    # Generate crops of size 113x113 from this resized image and keep random 10% of crops​avail_x_points = list(range(0, now_width - 113 ))# total x start points are from 0 to width -113​    # Pick random x%factor = 0.1 pick_num = int(len(avail_x_points)*factor)random_startx = sample(avail_x_points,  pick_num)​for start in random_startx:  imcrop = imresize.crop((start, 0, start+113, 113))  images.append(np.asarray(imcrop))T_test = np.array(images)print (T_test.shape)T_test = T_test.reshape(T_test.shape[0], 113, 113, 1)    #convert to float and normalizeT_test = T_test.astype('float32')T_test /= 255shuffle(T_test)  print (T_test.shape)

T_test 的形状是 (3, 113, 113, 3),这是一张黑白手写图片。这就是错误:

     34 T_test = np.array(images)     35 print (T_test.shape)---> 36 T_test = T_test.reshape(T_test.shape[0], 113, 113, 1)     37     #convert to float and normalize     38 T_test = T_test.astype('float32')

ValueError: 无法将大小为 114921 的数组重塑为形状 (3,113,113,1) 我还使用了

predictions = model.predict(T_test, verbose =1)

所以我不能将其更改为 (T_test.shape[0], 113, 113, 3)


回答:

图片有 3 个通道,所以你需要使用 3 而不是 1:

T_test = T_test.reshape(T_test.shape[0], 113, 113, 3)

Related Posts

在使用k近邻算法时,有没有办法获取被使用的“邻居”?

我想找到一种方法来确定在我的knn算法中实际使用了哪些…

Theano在Google Colab上无法启用GPU支持

我在尝试使用Theano库训练一个模型。由于我的电脑内…

准确性评分似乎有误

这里是代码: from sklearn.metrics…

Keras Functional API: “错误检查输入时:期望input_1具有4个维度,但得到形状为(X, Y)的数组”

我在尝试使用Keras的fit_generator来训…

如何使用sklearn.datasets.make_classification在指定范围内生成合成数据?

我想为分类问题创建合成数据。我使用了sklearn.d…

如何处理预测时不在训练集中的标签

已关闭。 此问题与编程或软件开发无关。目前不接受回答。…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注