将代码中的训练模型删除,替换为ResNet-50训练模型,并将训练模型保存在路基"C:\Users\24067\Desktop\data"下
时间: 2024-10-26 11:16:07 浏览: 26
ResNet50的预训练模型(昇思Mindspore学习25天打卡Day12:ResNet50迁移)
为了将代码中的训练模型从自定义的卷积神经网络(CNN)更改为预训练的ResNet-50模型,并将训练后的模型保存到指定路径,可以按照以下步骤进行修改:
1. **导入ResNet-50模型**:使用`tensorflow.keras.applications.ResNet50`来加载预训练的ResNet-50模型。
2. **调整模型结构**:将ResNet-50模型的顶部层移除,并添加新的分类层。
3. **编译和训练模型**:编译模型并进行训练。
4. **保存模型**:将训练好的模型保存到指定路径。
以下是修改后的代码:
```python
import os
import json
import numpy as np
import pandas as pd
from PIL import Image
import tensorflow as tf
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from sklearn.metrics import f1_score, accuracy_score
# 定义数据路径
TRAIN_DATA_DIR = "C:\\Users\\24067\\Desktop\\peach_split\\train"
VAL_DATA_DIR = "C:\\Users\\24067\\Desktop\\peach_split\\val"
TEST_DATA_DIR = "C:\\Users\\24067\\Desktop\\peach_split\\test"
TRAIN_LABEL_PATH = "C:\\Users\\24067\\Desktop\\train_label.json"
VAL_LABEL_PATH = "C:\\Users\\24067\\Desktop\\val_label.json"
OUTPUT_PATH = "C:\\Users\\24067\\Desktop\\data\\submission.csv"
MODEL_SAVE_PATH = "C:\\Users\\24067\\Desktop\\data\\resnet50_model.h5"
# 加载数据集
def load_data(data_dir, label_path):
with open(label_path, 'r') as f:
labels_list = json.load(f)
labels = {item['文件名']: item['标签'] for item in labels_list if '文件名' in item and '标签' in item}
images = []
targets = []
for file_name, label in labels.items():
img_path = os.path.join(data_dir, file_name)
if not os.path.exists(img_path):
continue
img = Image.open(img_path).resize((224, 224))
img_array = np.array(img) / 255.0
images.append(img_array)
targets.append(label)
if len(images) == 0:
raise ValueError("No valid images found.")
return np.array(images), np.array(targets)
# 加载训练数据
train_images, train_labels = load_data(TRAIN_DATA_DIR, TRAIN_LABEL_PATH)
# 加载验证数据
val_images, val_labels = load_data(VAL_DATA_DIR, VAL_LABEL_PATH)
# 标签映射
label_map = {'特级': 3, '一级': 2, '二级': 1, '三级': 0}
train_labels = np.array([label_map[label] for label in train_labels])
val_labels = np.array([label_map[label] for label in val_labels])
# 创建模型
def create_resnet50_model(input_shape=(224, 224, 3)):
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=input_shape)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(4, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
return model
# 实例化模型
model = create_resnet50_model()
# 冻结基础模型的层
for layer in model.layers[:-3]:
layer.trainable = False
# 编译模型
model.compile(optimizer=Adam(learning_rate=0.001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# 图像增强
datagen = ImageDataGenerator(
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
vertical_flip=True
)
# 训练模型
history = model.fit(datagen.flow(train_images, train_labels, batch_size=32),
epochs=10,
validation_data=(val_images, val_labels))
# 保存模型
model.save(MODEL_SAVE_PATH)
# 评估模型
def evaluate_model(model, X, y):
predictions = model.predict(X)
predicted_labels = np.argmax(predictions, axis=1)
accuracy = accuracy_score(y, predicted_labels)
f1 = f1_score(y, predicted_labels, average='weighted')
print(f'Accuracy: {accuracy:.4f}')
print(f'F1 Score: {f1:.4f}')
return accuracy, f1
evaluate_model(model, val_images, val_labels)
# 预测测试集并保存结果
def predict_and_save(test_data_dir, output_path):
test_images = []
test_file_names = []
for file_name in os.listdir(test_data_dir):
img_path = os.path.join(test_data_dir, file_name)
if not os.path.exists(img_path):
continue
img = Image.open(img_path).resize((224, 224))
img_array = np.array(img) / 255.0
test_images.append(img_array)
test_file_names.append(file_name)
test_images = np.array(test_images)
predictions = model.predict(test_images)
predicted_labels = np.argmax(predictions, axis=1)
label_map_inv = {v: k for k, v in label_map.items()}
predicted_labels = [label_map_inv[label] for label in predicted_labels]
submission_df = pd.DataFrame({'文件名': test_file_names, '标签': predicted_labels})
submission_df.to_csv(output_path, index=False)
# 进行预测并保存结果
predict_and_save(TEST_DATA_DIR, OUTPUT_PATH)
```
### 主要修改点:
1. **图像大小调整**:将图像大小从128x128调整为224x224,以适应ResNet-50的输入要求。
2. **创建ResNet-50模型**:使用`ResNet50`函数加载预训练的ResNet-50模型,并添加全局平均池化层和全连接层。
3. **冻结基础模型的层**:为了防止预训练权重被破坏,冻结了基础模型的大部分层。
4. **保存模型**:在训练完成后,将模型保存到指定路径`C:\Users\24067\Desktop\data\resnet50_model.h5`。
这样,代码就完成了从自定义CNN模型到预训练ResNet-50模型的转换,并且训练后的模型会被保存到指定路径。
阅读全文