Reputation: 50
I trained a model on a dataset containing images belonging to two different categories and am now attempting to get some predictions from this model on new images. I used the saved_model format for saving, and am attempting to load and predict one image on my model. My code is as follows
loaded = tf.keras.models.load_model('/Library/...')
loaded.compile(loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.1),
optimizer=tf.keras.optimizers.SGD(lr=0.005, momentum=0.9), metrics=['accuracy'])
test_image = image.load_img(img_path, target_size=(img_width, img_height))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
test_image = test_image.reshape(img_width, img_height)
result = loaded.predict(test_image)
print(loaded.predict(test_image))
print(result)
and I get the error:
Traceback (most recent call last):
File "/Users/...", line 73, in <module>
test_image = test_image.reshape(img_width, img_height)
ValueError: cannot reshape array of size 268203 into shape (299,299)
I thought this was an issue with the image file, but it from the same source as the images I used to train, and I experienced no issues there. All the files are RGB png images (I thought the issue was that they were RGBA, however, this is not the case). Any help would be greatly appreciated!
Upvotes: 1
Views: 43
Reputation: 1098
You have to check your image dimensions. The number of elements of your array has to be a product of the arguments to .reshape
. 299*299 does not equal 268203.
An example is
a = np.arange(6)
These are valid reshapes:
a.reshape(1,6)
a.reshape(2,3)
a.reshape(3,2)
a.reshape(6,1)
since the product of the arguments are 6, which is the length of the array.
Upvotes: 1