简介:
限制:
问题:期望是向模型提供图像并期望预测(概率)。但是,当调用
model.predict
时,会抛出以下错误:
“ValueError:层“sequential_7”的输入 0 与该层不兼容:预期形状=(None, 224, 224, 1),发现形状=(None, 244, 1)”
以下是按顺序排列的代码片段:
# 1. DATA PIPELINE
CLASS_NAMES = ['loose', 'control']
def decode_csv(csv_row): # csv_row consists of a file path and the image class
record_defaults = ["path", "image class"] # Default values for the dataset
filename, label_string = tf.io.decode_csv(csv_row, record_defaults) # tf.io.decode_csv reads every row in the csv
image_bytes = tf.io.read_file(filename=filename) # output: base64 image string
image_bytes = tf.image.decode_jpeg(image_bytes) # output: an integer array
image_bytes = tf.image.convert_image_dtype(image_bytes, tf.float32) # output: 0 - 1 range float
image_bytes = tf.image.resize(image_bytes, [224, 224]) # output: image dimension
label = tf.math.equal(CLASS_NAMES, label_string) # formats label to a boolean array with a truth value corresponding to the output class
return image_bytes, label # Returning a base64 image string and a boolean array with True corresponding to a particular class
def load_dataset(csv_file, batch_size, training=True):
ds = tf.data.TextLineDataset(filenames=csv_file).skip(1) # skip(1) will remove the top row i.e. header
ds = ds.map(decode_csv).cache()
ds = ds.batch(batch_size=batch_size)
if training:
ds = ds.shuffle(10).repeat()
return ds
train_ds = load_dataset("gs://qwiklabs-asl-04-06351f77b64f-hip-implant/hip-implant-data.csv", batch_size = 10)
validation_data = load_dataset("gs://qwiklabs-asl-04-06351f77b64f-hip-implant/hip-implant-data.csv", batch_size = 10, training=False)
# 2. CREATE MODEL
IMG_HEIGHT = 224
IMG_WIDTH = 224
IMG_CHANNELS = 64
model = Sequential([
Conv2D(name="first-Conv2D-layer",filters=64, kernel_size=3, input_shape=(IMG_WIDTH, IMG_HEIGHT, 1), padding='same', activation='relu'),
MaxPooling2D(name="first-pooling-layer",strides=2, padding='same'),
Conv2D(name="second-Conv2D-layer", filters=32, kernel_size=3, activation='relu'),
MaxPooling2D(name="second-pooling-layer", strides=2, padding='same'),
Flatten(),
Dense(units=400, activation='relu'),
Dense(units=100, activation='relu'),
Dropout(0.25),
Dense(2),
Softmax()
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
型号总结
# 3. PREDICTION USING A SINGLE IMAGE:
image_path = tf.io.read_file("gs://qwiklabs-asl-04-06351f77b64f-hip-implant/Control/control (25).png")
new_image = decode_img(image_path, [244, 244])
print(new_image.shape)
plt.imshow(new_image.numpy())
prediction = model.predict(new_image)
print(prediction)
已经尝试过的解决方案:
编辑1:漏掉了decode_img函数,它调整了测试图像(我们试图预测的单个图像)的大小
img = tf.io.read_file("gs://qwiklabs-asl-04-06351f77b64f-hip-implant/Control/control (25).png")
def decode_img(img, reshape_dims):
img = tf.image.decode_jpeg(img) # tf.image.decode_jpeg can decode Base64 image string into an integer array
#print("\n tf.image.decode_jpeg : Convert base64 image string into an integer array \n")
#print(img)
img = tf.image.convert_image_dtype(img, tf.float32) # tf.image.convert_image_dtype can cast the integer array into 0 -1 range float
#print("\n tf.image.convert_image_dtype : Cast the integer array into 0 - 1 range float \n")
#print(img)
img = tf.image.resize(img, reshape_dims) # tf.image.resize can make image dimensions consistent for our neural network
#print("\n tf.image.resize : Keep image dimensions consistent for our neural network \n")
#print(img)
return img
img = decode_img(img, [224, 224])
plt.imshow(img.numpy())
感谢用户的解决方案:https://www.reddit.com/r/learnmachinelearning/comments/18iaf0b/comment/kdbxjqw/?context=3
“当您输入网络的数据形状错误时,此错误最常见。如果每个样本的大小为 (224, 224, 1),则您的输入必须为等级 4,形状为 (n_batch, 224, 224, 1).
如果您尝试使用一张图像测试模型,您可能会意外地为其提供形状为 (224, 224, 1) 的张量。这是错误的,输入一张图像的正确方法是使用形状 (1, 224, 224, 1)。
您可以使用 numpy.stack,它给定几个 (224, 224, 1) 数组,可以将它们单独组合 axis = 0 为 (n_batch, 224, 224, 1) 形状。”
在这里,由于期望是提供单个图像来获得预测,因此我使用
numpy.stack((image,), axis=0)
单独堆叠单个图像并用它来进行预测。