问题描述
堆栈溢出的朋友你好:)
我训练了一个用于预测数字图像的 CNN 模型。
成功学习我想从一个包含多个数字的图像中预测一个数字。
但是我遇到了一个错误。
我会向你展示我的数据集和我的一些代码。
Dataset.py
batch_size = 32
img_height = 64
img_width = 64
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,validation_split=0.2,subset="training",seed=123,image_size=(img_height,img_width),batch_size=batch_size)
Model.py
num_classes = 10
model = Sequential([
layers.experimental.preprocessing.Rescaling(1./255,input_shape=(img_height,img_width,3)),layers.Conv2D(16,3,padding='same',activation='relu'),layers.MaxPooling2D(),layers.Conv2D(32,layers.Conv2D(64,layers.Flatten(),layers.Dense(128,layers.Dense(num_classes)
])
model.compile(optimizer='adam',loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])
预测几个 Numbers.py
import cv2 as cv
import numpy as np
from tensorflow.keras.models import load_model
img_color = cv.imread('mypath/test.PNG',cv.IMREAD_COLOR)
img_gray = cv.cvtColor(img_color,cv.COLOR_BGR2GRAY)
ret,img_binary = cv.threshold(img_gray,255,cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
kernel = cv.getStructuringElement( cv.MORPH_RECT,( 5,5 ) )
img_binary = cv.morphologyEx(img_binary,cv. MORPH_CLOSE,kernel)
contours,hierarchy = cv.findContours(img_binary,cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
for contour in contours:
x,y,w,h = cv.boundingRect(contour)
length = max(w,h) + 60
img_digit = np.zeros((length,length,1),np.uint8)
new_x,new_y = x-(length - w)//2,y-(length - h)//2
img_digit = img_binary[new_y:new_y+length,new_x:new_x+length]
model = load_model('mypath/saved_model.h5')
img_digit = cv.resize(img_digit,(64,64),interpolation=cv.INTER_AREA)
img_digit = img_digit / 255.0
img_input = np.array(img_digit).reshape(-1,64,3)
predictions = model.predict(img_input)
number = np.argmax(predictions)
print(number)
cv.rectangle(img_color,(x,y),(x+w,y+h),(255,0),2)
location = (x + int(w *0.5),y - 10)
font = cv.FONT_HERShey_COMPLEX
fontScale = 1.2
cv.putText(img_color,str(number),location,font,fontScale,(0,2)
cv.imshow('digit',img_digit)
cv.waitKey(0)
ValueError: 无法将大小为 4096 的数组重塑为形状 (64,3)
为什么会出现这个错误?帮帮我!!!
解决方法
您的问题是您将 im_digit 声明为 2D 数组,但将其重塑为 3D(3 个通道)。另请注意,您的 img_binary 也是单通道 (2D) 图像。您需要更改的只是继续使用灰度:
img_input = np.array(img_digit).reshape(1,64,1)
然而,这个答案假设您已经使用 64x64 输入训练了您的模型。