import os
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout
from keras.optimizers import Adam
from PIL import Image
from sklearn.model_selection import train_test_split
# 存储CSV文件的路径和文件名
csv_file = 'data.csv'
# 存储所有图像数据的数组和标签
images = []
labels = []
# 遍历文件夹中所有图像
for file_name in os.listdir('new'):
# 读取图像并将其转换为灰度图像
img = Image.open(os.path.join('new', file_name)).convert('L')
# 将图像转换为NumPy数组并添加到列表中
img_data = np.asarray(img).astype(np.float32) / 255.0
images.append(img_data)
# 添加对应的标签
labels.append(0 if int(file_name.split('_')[0]) < 5 else 1)
# 将图像数据和标签保存到CSV文件中
data = np.vstack((labels, np.array(images)))
np.savetxt(csv_file, data.T, delimiter=',', fmt='%.6f')
# 读取数据集并预处理
def preprocess_data():
# 读取CSV文件
data = np.genfromtxt('data.csv', delimiter=',', dtype=float)
# 提取标签和图像数据
labels = data[:, 0].astype(int)
images = data[:, 1:].reshape(-1, 64, 64, 1).astype(np.float32)
# 将标签转换为one-hot编码
labels = tf.keras.utils.to_categorical(labels, num_classes=2)
return images, labels
# 构建卷积神经网络模型
def build_model():
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Conv2D(128, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(64, activation='relu'),
Dropout(0.5),
Dense(2, activation='softmax')
])
model.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
return model
# 训练模型
def train_model(model, X_train, y_train, X_test, y_test):
history = model.fit(X_train, y_train, batch_size=32, epochs=20, validation_data=(X_test, y_test))
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
score = model.evaluate(X_test, y_test)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
# 预测新图像
def predict_image(model, image_path):
img = Image.open(image_path).convert('L')
img = np.array(img.resize((64, 64))) / 255.0
img = img.reshape(1, 64, 64, 1)
pred = model.predict(img)
if pred[0][0] > pred[0][1]:
return 'Abnormal'
else:
return 'Normal'
# 主函数
if __name__ == '__main__':
# 读取和预处理数据集
X, y = preprocess_data()
header = 'label,' + ','.join(['pixel_{0}'.format(i) for i in range(64*64)])
np.savetxt(csv_file, data.T, delimiter=',', header=header, comments='', fmt='%.6f')
这段代码报这个错误应该怎么修改?