Home > Software engineering >  Keras CNN Model Typevalue errors when using predict method
Keras CNN Model Typevalue errors when using predict method

Time:04-21

I am have a keras model that is supposed to take a (150, 150, 1) grayscale image as it's input and output an array of length 8.

Here is my model code:

from tensorflow.python import keras
model = keras.Sequential([
    keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation="relu", padding='same', input_shape=(150,150,1)),
    keras.layers.MaxPool2D(pool_size=(2,2), padding='same', data_format='channels_last'),
    keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding='same'),
    keras.layers.MaxPool2D(pool_size=(2,2), padding='same', data_format='channels_last'),
    keras.layers.Flatten(),
    keras.layers.Dense(8, activation="softmax")
])

When I try to use the .predict() method, I get this error:

Traceback (most recent call last):
  File "KerasCNN.py", line 152, in <module>
    ga.run()
  File "/home/User/Documents/Projects/2022/Keras_CNN/Trial1/env/lib/python3.6/site-packages/pygad/pygad.py", line 1192, in run
    self.last_generation_fitness = self.cal_pop_fitness()
  File "/home/User/Documents/Projects/2022/Keras_CNN/Trial1/env/lib/python3.6/site-packages/pygad/pygad.py", line 1159, in cal_pop_fitness
    fitness = self.fitness_func(sol, sol_idx)
  File "KerasCNN.py", line 112, in fitness
    prediction = model.predict(obs)
  File "/home/User/Documents/Projects/2022/Keras_CNN/Trial1/env/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/models.py", line 966, in predict
    return self.model.predict(x, batch_size=batch_size, verbose=verbose)
  File "/home/User/Documents/Projects/2022/Keras_CNN/Trial1/env/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 1813, in predict
    f, ins, batch_size=batch_size, verbose=verbose, steps=steps)
  File "/home/User/Documents/Projects/2022/Keras_CNN/Trial1/env/lib/python3.6/site-packages/tensorflow/python/keras/_impl/keras/engine/training.py", line 1300, in _predict_loop
    index_array = np.arange(num_samples)
TypeError: unsupported operand type(s) for /: 'Dimension' and 'int'

I had an ANN (non-CNN) model running earlier that was working fine. When I did some research I could find anything about this error either.

Here is the code I am using to make the prediction:

img = get_image() # (150, 150, 3)
g_img = cv2.cvtColor(obs, cv2.COLOR_BGR2GRAY) # (150, 150, 1)
g_img = tf.expand_dim(g_img, axis=0)
g_img = tf.expand_dim(g_img, axis=-1) # (1, 150, 150, 1)
prediction = model.predict(g_img)

Here are my version numbers:

tensorflow: 1.5.0

python: 3.69

numpy: 1.19.5

Ubuntu: 18.04

Let me know if theres any other info I can provide! Thanks!

CodePudding user response:

in code you wrote, the input shape is (224,256,1) so change it to (150,150,1) try this:

from tensorflow.python import keras
model = keras.Sequential([
    keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation="relu", padding='same', input_shape=(150,150,1)),
    keras.layers.MaxPool2D(pool_size=(2,2), padding='same', data_format='channels_last'),

do the math to get the desired output.

CodePudding user response:

You actually do not need to add a new dimension at the axis=-1. Maybe try:

img = get_image() # (150, 150, 3)
g_img = cv2.cvtColor(obs, cv2.COLOR_BGR2GRAY) # (150, 150, 1)
g_img = tf.expand_dims(g_img, axis=0)
prediction = model.predict(g_img)
  • Related