按照文件的内容修改代码,给出修改后完整代码
时间: 2024-10-24 13:13:03 浏览: 10
代码修改nacos内容.zip
以下是根据您提供的路径信息修改后的完整代码:
```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 Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
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"
# 加载数据集
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((128, 128)) # 调整图像大小
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)
# 加载训练数据
X_train, y_train = load_data(train_data_dir, train_label_path)
# 加载验证数据
X_val, y_val = load_data(val_data_dir, val_label_path)
# 标签映射
label_map = {'特级': 3, '一级': 2, '二级': 1, '三级': 0}
y_train = np.array([label_map[label] for label in y_train])
y_val = np.array([label_map[label] for label in y_val])
# 创建模型
def create_model(input_shape=(128, 128, 3)):
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=input_shape), # 第一个卷积层
MaxPooling2D((2
阅读全文