使用预测方法时 Keras CNN 模型类型值错误
Keras CNN Model Typevalue errors when using predict method
我有一个 keras 模型,它应该采用 (150, 150, 1)
灰度图像作为输入并输出长度为 8 的数组。
这是我的模型代码:
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")
])
当我尝试使用 .predict()
方法时,出现此错误:
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(g_img)
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'
我之前有一个 ANN(非 CNN)模型 运行,它运行良好。当我做一些研究时,我也能找到关于这个错误的任何信息。
这是我用来进行预测的代码:
img = get_image() # (150, 150, 3)
g_img = cv2.cvtColor(img, 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)
这是我的版本号:
张量流:1.5.0
python: 3.69
numpy: 1.19.5
Ubuntu: 18.04
如果我能提供任何其他信息,请告诉我!谢谢!
回答
用 np.expand_dim()
替换 tf.expand_dim()
修复了它!
在您编写的代码中,输入形状是 (224,256,1) 因此将其更改为 (150,150,1)
试试这个:
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'),
进行数学运算以获得所需的输出。
这似乎 运行 在 TF 1.15 上完全没问题:
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.python import keras
print(tf.__version__)
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")
])
# Create random image
img = np.zeros([150,150,3], dtype=np.uint8)
img[:,:,0] = np.ones([150,150])*64
img[:,:,1] = np.ones([150,150])*128
img[:,:,2] = np.ones([150,150])*192
g_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
g_img = np.expand_dims(g_img, axis=0)
g_img = np.expand_dims(g_img, axis=-1) # (1, 150, 150, 1)
prediction = model.predict(g_img)
print(prediction.shape)
1.15.2
(1, 8)
我有一个 keras 模型,它应该采用 (150, 150, 1)
灰度图像作为输入并输出长度为 8 的数组。
这是我的模型代码:
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")
])
当我尝试使用 .predict()
方法时,出现此错误:
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(g_img)
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'
我之前有一个 ANN(非 CNN)模型 运行,它运行良好。当我做一些研究时,我也能找到关于这个错误的任何信息。
这是我用来进行预测的代码:
img = get_image() # (150, 150, 3)
g_img = cv2.cvtColor(img, 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)
这是我的版本号:
张量流:1.5.0
python: 3.69
numpy: 1.19.5
Ubuntu: 18.04
如果我能提供任何其他信息,请告诉我!谢谢!
回答
用 np.expand_dim()
替换 tf.expand_dim()
修复了它!
在您编写的代码中,输入形状是 (224,256,1) 因此将其更改为 (150,150,1) 试试这个:
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'),
进行数学运算以获得所需的输出。
这似乎 运行 在 TF 1.15 上完全没问题:
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.python import keras
print(tf.__version__)
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")
])
# Create random image
img = np.zeros([150,150,3], dtype=np.uint8)
img[:,:,0] = np.ones([150,150])*64
img[:,:,1] = np.ones([150,150])*128
img[:,:,2] = np.ones([150,150])*192
g_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
g_img = np.expand_dims(g_img, axis=0)
g_img = np.expand_dims(g_img, axis=-1) # (1, 150, 150, 1)
prediction = model.predict(g_img)
print(prediction.shape)
1.15.2
(1, 8)