模型预测输入形状不兼容求助:预期(None,150,80,1)获(None,80,1)
问题:单张图像预测时输入形状不匹配报错
模型预期输入形状为(batchSize, imageWidth, imageHeight, imgChannels)即(1,150,80,1),但实际检测到的形状为(None,80,1),无法正常调用prediction_model.predict()。
错误信息
ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 150, 80, 1), found shape=(None, 80, 1)
预处理代码
# Create a tensor from an image # 1. Read the image img = tf.io.read_file('/content/drive/MyDrive/testImg.jpg') # 2. Decode and convert to grayscale img = tf.io.decode_jpeg(img, channels=1) #3. Convert to float32 in the range [0, 1] img = tf.image.convert_image_dtype(img, tf.float32) # 4. Change the size to the desired size img = tf.image.resize(img, [80, 150]) print(img.shape) #5. Move img = tf.transpose(img, perm=[1, 0, 2]) print(img.shape) pred = prediction_model.predict(img) pred_texts = decode_batch_predictions(pred)
控制台输出
(80, 150, 1) (150, 80, 1) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-19-e1b6d597bdc5> in <module> 14 print(img.shape) 15 ---> 16 preds = prediction_model.predict(img) 17 #pred_texts = decode_batch_predictions(preds) 1 frames /usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs) 65 except Exception as e: # pylint: disable=broad-except 66 filtered_tb = _process_traceback_frames(e.__traceback__) ---> 67 raise e.with_traceback(filtered_tb) from None 68 finally: 69 del filtered_tb /usr/local/lib/python3.8/dist-packages/keras/engine/training.py in tf__predict_function(iterator) 13 try: 14 do_return = True ---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope) 16 except: 17 do_return = False ValueError: in user code: File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1845, in predict_function * return step_function(self, iterator) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1834, in step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1823, in run_step ** outputs = model.predict_step(data) File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1791, in predict_step return self(x, training=False) File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler raise e.with_traceback(filtered_tb) from None File "/usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility raise ValueError(f'Input {input_index} of layer "{layer_name}" is ' ValueError: Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 150, 80, 1), found shape=(None, 80, 1)
模型摘要(prediction_model.summary())
Model: "model_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= image (InputLayer) [(None, 150, 80, 1)] 0 Conv1 (Conv2D) (None, 150, 80, 32) 320 pool1 (MaxPooling2D) (None, 75, 40, 32) 0 Conv2 (Conv2D) (None, 75, 40, 64) 18496 pool2 (MaxPooling2D) (None, 37, 20, 64) 0 reshape (Reshape) (None, 37, 1280) 0 dense1 (Dense) (None, 37, 64) 81984 dropout (Dropout) (None, 37, 64) 0 bidirectional (Bidirectiona (None, 37, 256) 197632 l) bidirectional_1 (Bidirectio (None, 37, 128) 164352 nal) dense2 (Dense) (None, 37, 12) 1548 ================================================================= Total params: 464,332 Trainable params: 464,332 Non-trainable params: 0 _________________________________________________________________
疑问
请问我哪里处理有误?为何无法将预处理后的单张图像传入prediction_model.predict()?
解答
问题出在缺少batch维度:
预处理后得到的img形状是(150,80,1),但Keras的predict()方法要求输入必须包含batch维度(即第一维是样本数)。模型预期的输入形状(None,150,80,1)中,None代表可变的batch大小,单张图片对应的batch大小是1,所以需要把输入形状变成(1,150,80,1)。
修改方法:在调用predict()之前,给img增加一个batch维度:
# 在transpose之后添加这行代码 img = tf.expand_dims(img, axis=0) print(img.shape) # 此时输出应为(1,150,80,1) pred = prediction_model.predict(img) pred_texts = decode_batch_predictions(pred)
另外验证一下resize和transpose步骤:
- 先用
tf.image.resize(img, [80,150])把图像改成(80,150,1),再用transpose(perm=[1,0,2])转成(150,80,1),这一步是正确的,符合模型要求的宽高顺序。
总结:Keras模型的predict()、fit()等方法都要求输入是批量数据,哪怕只有一张图片,也要手动添加batch维度。
内容的提问来源于stack exchange,提问作者Vipz
相关产品推荐
相关产品推荐

