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),应该怎么改啊