Keras / Tensorflow predicts: array error

I follow the Keras CIFAR10 study guide here . The only changes I made were:

[a] added at the end of the training file

model.save_weights('./weights.h5', overwrite=True) 

[b] changed ~. / keras / keras.json to

 {"floatx": "float32", "backend": "tensorflow", "epsilon": 1e-07} 

I can successfully run the model.

Then I want to test one image against a trained model. My code is:

 [... similar to tutorial file with model being created and compiled...] ... model = Sequential() ... model.compile() model.load_weights('./ddx_weights.h5') img = cv2.imread('car.jpeg', -1) # this is is a 32x32 RGB image img = np.array(img) y_pred = model.predict_classes(img, 1) print(y_pred) 

I get this error:

 ValueError: Cannot feed value of shape (1, 32, 3) for Tensor 'convolution2d_input_1:0', which has shape '(?, 3, 32, 32)' 

What is the correct way to change the input data for one test image?

I have not added "image_dim_ordering": "tf" to ./keras/keras.json .

+6
source share
2 answers

You must change the original image to the form [?, 3, 32, 32] , where ? - batch size. In your case, since you have 1 image, the batch size is 1, so you can do:

 img = np.array(img) img = img.reshape((1, 3, 32, 32)) 
+5
source

Now I'm working on cifar10 data, I found that a simple permutation will not work, use numpy.transpose instead.

0
source

All Articles