Msy20070905 2024-08-18 12:46 采纳率: 21.2%
浏览 3

Input 0 of layer "sequential" is incompatible with the layer


import os
import numpy as np
import tensorflow as tf
from PIL import Image
from sklearn.model_selection import train_test_split

np.set_printoptions(threshold=np.inf)

# 设置路径和文件名
train_path = './DATA_TEST/rename'
train_txt = './DATA_TEST/output/nose/nose_after_dealing_x.txt'
x_train_savepath = './X_Train.npy'
y_train_savepath = './Y_Train.npy'

# 定义数据增强的参数
datagen = tf.keras.preprocessing.image.ImageDataGenerator(
    rotation_range=0,
    width_shift_range=0,
    height_shift_range=0,
    shear_range=0.0,
    zoom_range=0.2,
    horizontal_flip=False,
    vertical_flip=False,
    fill_mode='nearest'
)

def generateds(path, txt, datagen=None):
    with open(txt, 'r') as f:
        contents = f.readlines()

    x, y_ = [], []
    for content in contents:
        value = content.split()
        img_path = os.path.join(path, value[0])
        img = Image.open(img_path)
        img = img.resize((256, 256))
        img = np.array(img) / 255.0
        img = img.astype(np.float32)  # 确保数据类型是 float32

        if datagen:
            img = img.reshape((1,) + img.shape)  # 增加批量维度
            for batch in datagen.flow(img, batch_size=1):
                x.append(batch[0])  # 仍然保持四维形状
                y_.append(float(value[1]))
                break
        else:
            x.append(img)
            y_.append(float(value[1]))

    x = np.array(x)
    y_ = np.array(y_)
    return x, y_

# 检查是否已经保存了训练数据
if os.path.exists(x_train_savepath) and os.path.exists(y_train_savepath):
    x_train = np.load(x_train_savepath)
    y_train = np.load(y_train_savepath)
else:
    x_train, y_train = generateds(train_path, train_txt, datagen)
    np.save(x_train_savepath, x_train)  # 保存四维数据
    np.save(y_train_savepath, y_train)

# 划分训练集和测试集,将20%作为测试集
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=0.2, random_state=42)

# 创建模型
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(filters=64, kernel_size=(3, 3), padding='valid', input_shape=(256, 256, 3)),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Conv2D(filters=128, kernel_size=(3, 3), padding='valid'),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Conv2D(filters=256, kernel_size=(3, 3), padding='valid'),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Conv2D(filters=243, kernel_size=(3, 3), padding='valid'),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Activation('relu'),
    tf.keras.layers.AveragePooling2D(pool_size=(5, 5), padding='valid'),
    tf.keras.layers.MaxPool2D(pool_size=(3, 3), padding='valid'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(256, activation='tanh', kernel_regularizer=tf.keras.regularizers.l1()),
    tf.keras.layers.Dense(64, activation='tanh', kernel_regularizer=tf.keras.regularizers.l1()),
    tf.keras.layers.Dense(10, activation='softmax', kernel_regularizer=tf.keras.regularizers.l1()),  # 根据实际任务调整
    tf.keras.layers.Dense(1, activation=None)
])

# 编译模型
model.compile(optimizer=tf.keras.optimizers.Adam(0.001),
              loss='mean_squared_error')

# 设置模型检查点
checkpoint_save_path = './checkpoint/cnn_head.ckpt'
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path, save_weights_only=True, save_best_only=True)

# 删除旧的检查点文件
if os.path.exists(checkpoint_save_path + '.index'):
    os.remove(checkpoint_save_path + '.index')
    os.remove(checkpoint_save_path + '.data-00000-of-00001')

# 如果检查点文件存在,则尝试加载模型权重
if os.path.exists(checkpoint_save_path + '.index'):
    print('-------------load the model-----------------')
    model.load_weights(checkpoint_save_path)

# 训练模型
history = model.fit(x_train, y_train, batch_size=2, epochs=200, validation_data=(x_test, y_test), validation_freq=5, callbacks=[cp_callback])

# 打印模型摘要信息
model.summary()

# 保存模型的可训练变量
with open('weights.txt', 'w') as file:
    for v in model.trainable_variables:
        file.write(f'{v.name}\n{v.shape}\n{v.numpy()}\n')


这段代码报 ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 196608),应该怎么改啊

  • 写回答

2条回答 默认 最新

  • 何事_ 2024-08-18 12:47
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    这个错误是由于模型输入的形状和数据实际提供的形状不匹配导致的。你的模型期望的输入形状是 (None, 256, 256, 3),但是实际提供的数据的形状却是 (None, 196608)。你需要确保数据在送入模型之前被重新整形为正确的四维形状。根据你代码中的操作,数据似乎经过了 Image.openimg.resize((256, 256)) 处理,这两个操作应该能够确保图像的高度和宽度为 256,并且假设图像是彩色的(即有 3 个颜色通道)。然而,在数据增强部分,你可能需要确保增强后的图像也保持正确的形状。

    在你的 generateds 函数中,你使用了 datagen 对图像进行增强,然后你可能在增强过程中改变了图像的形状。为了确保形状的一致性,你可以在增强之后再次对图像进行形状调整。这里是一个可能的修改方案:

    generateds 函数中,对于增强后的图像,你需要再次将其重塑为正确的四维形状:

    def generateds(path, txt, datagen=None):
        # ... 其他代码保持不变 ...
        if datagen:
            img = img.reshape((1,) + img.shape)  # 增加批量维度
            for batch in datagen.flow(img, batch_size=1):
                # 这里添加代码以确保输出形状为 (1, 256, 256, 3)
                batch_reshaped = batch[0].reshape((1, 256, 256, 3))  # 确保四维形状
                x.append(batch_reshaped)  # 添加重塑后的图像到列表
                y_.append(float(value[1]))
                break
        else:
            x.append(img.reshape((1, 256, 256, 3)))  # 确保图像形状正确
            y_.append(float(value[1]))
        # ... 其他代码保持不变 ...
    

    这样修改后,你的数据在进入模型之前应该会有正确的四维形状,从而避免这个错误。希望这能解决你的问题!

    评论

报告相同问题?

问题事件

  • 创建了问题 8月18日

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见