1

I am using keras to load in my model.h5(a file of weights) for my CNN. I am using a VGG-16 Architecture. My training data consists of numpy array of size (2590, 3(for RGB), 875(pixels in width), 375(pixels in height)). I have finished training the data and now I am using the model.h5(with weights) to make a prediction. I am running into the following error.

expected zero_padding2d_1_input to have shape (None, 3, 875, 375) but got array with shape (1, 375, 875, 3)

Here is the top snippet of my VGG-16 CNN

def VGG_16(weights_path=None):
   model = Sequential()
   model.add(ZeroPadding2D((1,1),input_shape=(3,875,375)))
   model.add(Convolution2D(64, 3, 3, activation='relu'))
   model.add(ZeroPadding2D((1,1)))
   model.add(Convolution2D(64, 3, 3, activation='relu'))
   model.add(MaxPooling2D((2,2), strides=(2,2)))
   .......... Continued ..........

Here is what I've tried First: I looked at these posts: Error when checking target: expected dense_20 to have shape (None, 3) but got array with shape (1200, 1)

Keras Error when checking : expected embedding_1_input to have shape (None, 100) but got array with shape (1, 3)

Error when checking model target: expected dense_24 to have shape...but got array with shape... in Keras

Code I am trying:

model = VGG_16('/path/to/weights/file....../model.h5')
print("Created model")
img = np.array([np.array(Image.open(path_to_image_i_want_to_convert))])
img.reshape(1, 3, 875,375)
try:
    prediction = model.predict(img)
    print(prediction)
    print("I finished your prediction")
except Exception as e:
    print(str(e))

However, This always raises an error.

expected zero_padding2d_1_input to have shape (None, 3, 875, 375) but got array with shape (1, 375, 875, 3)

How can a numpy array even have a dimension of None? What am I doing wrong? How can I modify my code in order to make a prediction with only a single image.

Any help would be appreciated thanks!

Sreehari R
  • 919
  • 4
  • 11
  • 21

1 Answers1

1

I believe you need to use

model.add(ZeroPadding2D((1,1),input_shape=(3,875,375),data_format='channels_first'))

This is because the default is 'channels_last' as per the docs.

Also the first argument as None is just a representation of the batchsize, not something predetermined in the model architecture, so you do not need to worry about that. In that error message seeing None is expected

DJK
  • 8,924
  • 4
  • 24
  • 40