简体   繁体   中英

Model was constructed with shape (None, 180, 180, 3) for input, but it was called on an input with incompatible shape (None, 180, 3)?

I am receiving this error 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).

This is my training Dataset Type: <BatchDataset shapes: ((None, 256, 256, 3), (None,)), types: (tf.float32, tf.int32)>. I reshape this training data when creating the sequential model. I want to predict on frames from a live video stream.

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

Using trained model to predict:

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

在 new_model.predict 之前添加代码

frame=np.expand_dims(frame, axis=0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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