Tensorflow 错误:层“model”的输入 0 与层不兼容:预期 shape=(None, 640, 360, 3),找到 shape=(32, 360, 3)

Tensorflow error : Input 0 of layer "model" is incompatible with the layer: expected shape=(None, 640, 360, 3), found shape=(32, 360, 3)

提问人:Tom Fiszelson 提问时间:11/12/2023 最后编辑:Tom Fiszelson 更新时间:11/12/2023 访问量:34

问:

我最近制作了一个 CNN,它用边界框检测人类,我训练了它,当我在图像上测试它时,我收到以下错误: ValueError:图层“模型”的输入 0 与图层不兼容:预期 shape=(None, 640, 360, 3),找到 shape=(32, 360, 3)

我使用此代码来创建和训练模型:

import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.preprocessing import image
from tensorflow.keras import layers, models
import pandas as pd
import numpy as np

print('1')

input_shape = (640, 360, 3)
Epochs = 5
Verbose = 1
Validation_split = 0.2


base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=input_shape)
base_model.trainable = False

inputs = keras.Input(shape=input_shape)
x = base_model(inputs, training=False)
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(256, activation='relu')(x)
x = tf.keras.layers.Dense(4, activation='relu')(x)

model = tf.keras.Model(inputs, x)

print('2')

label = np.load('labels.npy')
feed = np.load('array.npy')
print(label.shape)
print(feed.shape)
label = label.astype('float32')
print('ok')
feed = feed.astype('float32')
print('3')

with tf.device('/GPU:0'):
  model.compile(optimizer='adam', loss=tf.losses.Huber(delta=1.0), metrics=['accuracy'])
  model.summary()
  model.fit(feed,
            label,
            batch_size=128,
            validation_split=Validation_split,
            epochs=Epochs,
            verbose=Verbose)
  model.save('my_model(delta:0.1).keras')

这是我用来测试模型的代码:

import tensorflow as tf
import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as img
from matplotlib.patches import Rectangle

image_path = '/home/tom-fiszelson/PycharmProjects/test/image_de_moi.jpg'
image = cv2.imread(image_path)
model = tf.keras.models.load_model('/home/tom-fiszelson/PycharmProjects/test/my_model(delta:0.1).keras')

image = cv2.resize(image, (640, 360))
cv2.imwrite('resized pic of me.png', image)
image_as_array = np.asarray(image)
image_as_array = np.transpose(image_as_array, (1, 0, 2))
print(image_as_array.shape)
bbox = model.pr

诏书(image_as_array) 打印(bbox)

原始图像为 1280x720 像素。 正如我之前所说,我有一个奇怪的错误告诉我我的图像数组是 (32, 360, 3) 而不是 (640, 360, 3),即使我知道它是正确的形状,因为我的打印 (print(image_as_array.shape)) 输出 (640, 360, 3)。 对此有什么想法吗?

Python 熊猫 Linux TensorFlow 计算机视觉

评论


答: 暂无答案