Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default MaxPoolingOp only supports NHWC on device type CPU

I tried to run a prediction on a SegNet model, but when the predict function its call I received an error.

I tried also to run the prediction with the with tf.device('/cpu:0'):, but I received the same error

if __name__ == '__main__':
    # path to the model
    model = tf.keras.models.load_model('segnet_weightsONNXbackToKeras3.h5')

    model.compile(loss='categorical_crossentropy', optimizer='RMSprop', metrics=['accuracy'])

    model.summary()

    input_shape = [None, 360, 480, 3]
    output_shape = [None, 352, 480, 20]

    img = cv2.imread('test4.jpg')
    input_image = img
    img = cv2.resize(img, (input_shape[2], input_shape[1]))
    img = np.reshape(img, [1, input_shape[1], input_shape[2], input_shape[3]])

    if normalize:
        img = img.astype('float32') / 255

    model.summary()
    classes = model.predict(img)[0]
    colors = []
    for i in range(output_shape[3]):
        colors.append(generate_color())

    maxMatrix = np.amax(classes, axis=2)
    prediction = np.zeros((output_shape[1], output_shape[2], 3), dtype=np.uint8)

2019-10-25 19:32:03.126831: E tensorflow/core/common_runtime/executor.cc:642] Executor failed to create kernel. Invalid argument: Default MaxPoolingOp only supports NHWC on device type CPU
     [[{{node model/LAYER_7/MaxPool}}]]
Traceback (most recent call last):
  File "../mold_segmentation_h5VM.py", line 62, in <module>
    classes = model.predict(img)[0]
  File "..\anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 909, in predict
    use_multiprocessing=use_multiprocessing)
  File "..\anaconda3\lib\site-packages\tensorflow_core\python\eager\execute.py", line 67, in quick_execute
    six.raise_from(core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.InvalidArgumentError:  Default MaxPoolingOp only supports NHWC on device type CPU
     [[node model/LAYER_7/MaxPool (defined at D:\EB-AI\tools\anaconda3\lib\site-packages\tensorflow_core\python\framework\ops.py:1751) ]] [Op:__inference_distributed_function_4421]

Function call stack:
distributed_function
like image 706
Emanuel Covaci Avatar asked Sep 18 '25 04:09

Emanuel Covaci


1 Answers

Without test4.jpg it's difficult to test solutions. However, the error Default MaxPoolingOp only supports NHWC on device type CPU means that the model only can accept inputs of the form n_examples x height x width x channels. I think your cv2.resize and subsequent np.reshape lines are not outputting the image in the correct format. Try printing out the shape of the image before you call model.predict(), and make sure it's in the format n_examples x height x width x channels.

like image 108
Tom C Avatar answered Sep 20 '25 20:09

Tom C