引入VGG16训练模型,并在代码中做出注释
时间: 2024-10-25 07:09:17 浏览: 34
好的,我将为您修改代码以引入 VGG16 模型,并添加相应的注释。以下是修改后的代码:
```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.applications.vgg16 import VGG16
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D, Dropout
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from sklearn.metrics import f1_score, accuracy_score
# 加载数据集
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)) # VGG16 输入尺寸为 224x224
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)
# 数据目录和标签路径
data_dir = "C:\\Users\\24067\\Desktop\\peach_split\\train"
label_path = "C:\\Users\\24067\\Desktop\\train_label.json"
# 加载训练数据
images, labels = load_data(data_dir, label_path)
# 标签映射
label_map = {'特级': 3, '一级': 2, '二级': 1, '三级': 0}
labels = np.array([label_map[label] for label in labels])
# 划分训练集和验证集
X_train, X_val, y_train, y_val = train_test_split(images, labels, test_size=0.2, random_state=42)
# 创建 VGG16 模型
def create_vgg16_model(input_shape=(224, 224, 3)):
# 加载预训练的 VGG16 模型,不包括顶层分类器
base_model = VGG16(weights='imagenet', include_top=False, input_shape=input_shape)
# 冻结基础模型的权重
for layer in base_model.layers:
layer.trainable = False
# 添加自定义顶层分类器
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.5)(x)
predictions = Dense(4, activation='softmax')(x)
# 构建新的模型
model = Model(inputs=base_model.input, outputs=predictions)
return model
# 实例化模型
model = create_vgg16_model()
# 编译模型
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(X_train, y_train, batch_size=32), epochs=1, validation_data=(X_val, y_val))
# 评估模型
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, X_val, y_val)
# 预测测试集并保存结果
def predict_and_save(test_data_dir, test_label_path, output_path):
test_images = []
test_file_names = []
# 读取测试集标签文件
with open(test_label_path, 'r') as f:
test_labels_list = json.load(f)
test_labels = {item['文件名']: item['标签'] for item in test_labels_list if '文件名' in item and '标签' in item}
# 加载测试集图像
for file_name in test_labels.keys():
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)) # VGG16 输入尺寸为 224x224
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)
# 测试集目录和标签路径
test_data_dir = "C:\\Users\\24067\\Desktop\\peach_split\\test"
test_label_path = "C:\\Users\\24067\\Desktop\\train_label.json"
output_path = "C:\\Users\\24067\\Desktop\\data\\submission.csv"
# 进行预测并保存结果
predict_and_save(test_data_dir, test_label_path, output_path)
```
### 主要改动点:
1. **调整图像尺寸**:VGG16 的输入尺寸为 `224x224`,因此需要将图像调整为该尺寸。
2. **创建 VGG16 模型**:使用 `VGG16` 预训练模型,并冻结其权重,然后在其基础上添加自定义的顶层分类器。
3. **编译和训练模型**:保持原有的编译和训练步骤不变。
希望这些修改对您有帮助!如果您有任何进一步的问题,请随时告诉我。
阅读全文