简体   繁体   中英

CNN not predicting class of image

I have made a brain tumor detecting model using CNN, when I try to test a sample image by predicting it's class, an error occurs.

According to the error the input to the model should have 1 extra dimension. How do I predict the image's class. The code snippet that gives the error is:

best_model.predict(image)

The error is as follows

ValueError: Input 0 of layer zero_padding2d is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 240, 3]

The shape expected can be seen in the warning:

WARNING:tensorflow:Model was constructed with shape (None, 240, 240, 3) for input Tensor("input_1_1:0", shape=(None, 240, 240, 3), dtype=float32), but it was called on an input with incompatible shape (None, 240, 3).

I tried the solution but it is still giving the same error: Here is the image

So the problem is that the model assume the first dimension is the number of batchs. In your case it "thinks" that you have 240 batches where each image in size (240,3) .

What you need to do is expand the dimensions of the image before passing to the model. you can useexpand_dims

an example:

image = tf.zeros([240, 240, 3])
tf.expand_dims(image, axis=0)

This will add a batch dimension to image, and the model can operate it properly.

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