模型是以形状(None,180,180,3)作为输入构建的,但它是在具有不兼容形状(None,180,3)的输入上调用的?

2024-04-25 06:11:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我收到这个错误:

Error: WARNING:tensorflow:Model was constructed with shape (None, 180, 180, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 180, 180, 3), dtype=tf.float32, name='rescaling_2_input'), name='rescaling_2_input', description="created by layer 'rescaling_2_input'"), but it was called on an input with incompatible shape (None, 180, 3).

这是我的培训数据集类型:<BatchDataset shapes: ((None, 256, 256, 3), (None,)), types: (tf.float32, tf.int32)>。在创建顺序模型时,我重新塑造了这些训练数据。我想预测来自现场视频流的帧

batch_size = 32
img_height = 180
img_width = 180

num_classes = 4
  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, 3, padding='same', activation='relu'),
    layers.MaxPooling2D(),
    layers.Conv2D(64, 3, padding='same', activation='relu'),
    layers.MaxPooling2D(),
    layers.Flatten(),
    layers.Dense(128, activation='relu'),
    layers.Dense(num_classes)
  ])

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
rescaling_1 (Rescaling)      (None, 180, 180, 3)       0         
_________________________________________________________________
conv2d (Conv2D)              (None, 180, 180, 16)      448       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 90, 90, 16)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 90, 90, 32)        4640      
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 45, 45, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 45, 45, 64)        18496     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 22, 22, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 30976)             0         
_________________________________________________________________
dense (Dense)                (None, 128)               3965056   
_________________________________________________________________
dense_1 (Dense)              (None, 4)                 516       
=================================================================
Total params: 3,989,156
Trainable params: 3,989,156
Non-trainable params: 0

使用经过培训的模型预测:

vid = cv2.VideoCapture(0)
  
while(True):
    ret, frame = vid.read()

    frame= cv2.resize(frame, (180,180))
    frame = np.asarray(frame)
    frame = frame.astype('float32')
    frame /= 255.0
    print(frame.shape)
    new_model.predict(frame) #ERROR ON PREDICT FRAME

如何修复此错误


Tags: noneimginputlayerstfactivationframedense