【问题标题】:Tensorflow ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 20, 20, 3), found shape=(None, 20, 3)Tensorflow ValueError:层“顺序”的输入0与层不兼容:预期形状=(无,20,20,3),找到形状=(无,20,3)
【发布时间】:2021-11-04 07:22:51
【问题描述】:

所以我正在尝试测试我训练有素的模型(图像分类器)
tl;博士我有 2 种类型的照片(20x20 像素)。第一种是有粉碎飞机的照片,第二种是没有粉碎飞机的照片(从天空拍摄的照片) 我收到了包含文件名和标签的 csv 文件(1 - 照片上有飞机,0 - 没有飞机) 这就是我正在做的:

import tensorflow as tf
import pandas as pd
from tensorflow import keras


def read_image(image_file, label):
    image = tf.io.read_file(directory+image_file)
    image = tf.image.decode_image(image, channels=3, dtype=tf.float32)
    return image, label


def prepare_for_test(filepath):
    img_array = tf.io.read_file(filepath)
    img_array = tf.image.decode_image(img_array, channels=3, dtype=tf.float32)
    return img_array

这是我使用 csv 文件创建 tf 数据集的方式

directory = 'avia-train/'
df = pd.read_csv(directory+'train.csv')
df['filename'] = df['filename'].apply(lambda x: x+'.png')
filenames = df['filename'].values
signs = df['sign'].values
ds_train = tf.data.Dataset.from_tensor_slices((filenames, signs))
ds_train = ds_train.map(read_image).batch(32)

我的模特:

model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(20, 20, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])


model.compile(
    optimizer=keras.optimizers.Adam(),
    loss=[
        keras.losses.BinaryCrossentropy(),
    ],
    metrics=['accuracy'],
)

model.fit(ds_train,
    epochs=5,
    verbose=1)

据我了解,培训进展顺利
这是我得到的

Epoch 1/5
972/972 - 45s - loss: 0.2656 - accuracy: 0.8853
Epoch 2/5
972/972 - 7s - loss: 0.1417 - accuracy: 0.9447
Epoch 3/5
972/972 - 7s - loss: 0.1191 - accuracy: 0.9543
Epoch 4/5
972/972 - 7s - loss: 0.1030 - accuracy: 0.9608
Epoch 5/5
972/972 - 8s - loss: 0.0921 - accuracy: 0.9657

然后我尝试使用我的模型

prediction = model.predict([prepare_for_test('avia-test/00a90c41-965e-45d0-90c2-391e20cb25b7.png')])
print(prediction)

这就是我得到的

ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 20, 20, 3), found shape=(None, 20, 3)

我试图在这里找到一些东西:
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: [None, 2584]
ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28]
但对我来说没有什么有用的 如果您能提出简单的解决方案,那就太好了,但我会很感激任何帮助

【问题讨论】:

    标签: python tensorflow machine-learning keras deep-learning


    【解决方案1】:

    我找到了解决办法,prepare_for_test函数需要改一下。

    import cv2
    def prepare_for_test(filepath):
        IMG_SIZE = 20 # my pics are 20x20 px
        #if your pics are grayscaled you should use cv2.IMREAD_GRAYSCALE
        img_array = cv2.imread(filepath, cv2.IMREAD_COLOR)
        new_array = cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
        # if your pics are grayscaled you should use (-1, IMG_SIZE, IMG_SIZE,1)
        return new_array.reshape(-1, IMG_SIZE, IMG_SIZE,3)
    

    修改后一切正常

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 2022-06-18
      • 2023-02-07
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 2019-05-25
      • 2021-04-28
      • 2021-12-28
      相关资源
      最近更新 更多