from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array, array_to_img
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
import numpy as np
import matplotlib.pyplot as plt
import glob, os, random
path = 'Micro_Organism'
os.path.join(path, '*/*.*')
# 使用 glob 模块批量匹配图像, * 代表匹配所有东西
img_list = glob.glob(os.path.join(path, '*/*.*'))
print('>>>图像数量:', len(img_list))
img_list[:5]
print(img_list[:5])
# 加载前面几张未感染的图像
for i, img_path in enumerate(img_list[10:16]):
img_plot = load_img(img_path) # 加载图像
arr = img_to_array(img_plot) # 将图像转换成数组
print(arr.shape) # 图像形状
plt.subplot(2, 3, i+1)
plt.imshow(img_plot)
plt.show()
# 统一定义图像像素的宽度和高度
img_width, img_height = 100, 100
# 定义训练集、验证集的图形路径(文件夹路径即可)
train_data_dir = 'Micro_Organism/'
validation_data_dir = 'Micro_Organism/'
# 模型训练的参数设置
nb_train_samples = 275
nb_validation_samples = 200
epochs = 20 # 迭代次数
batch_size = 64 # 每个批量观测数
# 图像输入维度设置
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
# 统一定义图像像素的宽度和高度
img_width, img_height = 100, 100
# 图像输入维度设置
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
train_datagen = ImageDataGenerator(rescale=1. / 255, # 重缩放因子
shear_range=0.2, # 剪切强度(以弧度逆时针方向剪切角度)
zoom_range=0.2, # 随机缩放范围
horizontal_flip=True # 随机水平翻转
)
train_generator = train_datagen.flow_from_directory(train_data_dir, # 训练数据的文件夹路径
target_size=(img_width, img_height), # 统一像素大小
batch_size=batch_size, # 每一批次的观测数
class_mode='categorical' # 指定分类模式,指定二分类
)
test_datagen = ImageDataGenerator(rescale=1. / 255,
shear_range=0.2, # 剪切强度(以弧度逆时针方向剪切角度)
zoom_range=0.2, # 随机缩放范围
horizontal_flip=True)# 随机水平翻转
validation_generator = test_datagen.flow_from_directory(validation_data_dir, # 验证集文件夹路径
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical' # 二分类
)
model = Sequential()
# -----------------------------------------------------
# 输入层:第一层
# 添加第一个卷积层/最大池化层(必选)
model.add(Conv2D(filters=32, # 32 个过滤器
kernel_size=(3, 3), # 卷积核大小 3 x 3
input_shape=input_shape, # 图像输入维度
activation='relu')) # 'relu' 激活函数
model.add(MaxPooling2D(pool_size=(2, 2))) # 池化核大小 2 x 2
# ----------------------------------------------------
# 隐藏层:介于第一层和最后一层之间
# 添加第二个卷积层/最大池化层(可选)
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# 添加第三个卷积层/最大池化层(可选)
model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# 由于卷积层是 2D 空间,训练时需要将数据展平为 1D 空间
model.add(Flatten()) # 添加展平层(必选)
model.add(Dense(units=64, activation='relu')) # 添加全连接层(必选) 64 个神经元
model.add(Dropout(0.5)) # 添加丢弃层,防止过拟合
# ---------------------------------------------------
# 输出层:最后一层,神经元控制输出的维度,并指定分类激活函数 类别
model.add(Dense(units=8, activation='sigmoid')) # 指定分类激活函数
print(model.summary())
model.compile(loss='categorical_crossentropy', # 指定损失函数类型
optimizer='rmsprop', # 优化器
metrics=['accuracy']) # 评价指标
history = model.fit_generator(train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size
)
# 保存整个模型
model.save('model.h5')
# 加载模型
from keras.models import load_model
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import os
# 指定要列出子文件和子文件夹的目录路径
folder_path = 'Micro_Organism' # 替换为你要获取子文件和子文件夹的目录路径
# 获取指定路径下的所有文件和文件夹列表
contents = os.listdir(folder_path)
print('contents',contents)
model = load_model('model.h5')
img_path = '测试1.jpg' # img_width, img_height = 100, 100
img = image.load_img(img_path, target_size=(100, 100)) # 例如,针对VGG模型,resize图像为224x224
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x) # 预处理图像数据,例如针对VGG模型
# 对测试图像进行预测
predictions = model.predict(x)
print(predictions.shape)
print('predictions:',predictions)
predictions=predictions[0]
max_indices = [i for i, value in enumerate(predictions) if value == max(predictions)][0]
print(contents[max_indices])
import matplotlib.pyplot as plt
training_loss = history.history['loss']
test_loss = history.history['val_loss']
# 创建迭代数量
epoch_count = range(1, len(training_loss) + 1)
# 可视化损失历史
plt.plot(epoch_count, training_loss, 'r--')
plt.plot(epoch_count, test_loss, 'b-')
plt.legend(['Training Loss', 'Test Loss'])
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.savefig('epochs_loss.png')
plt.show()
plt.clf()
# train_acc = history.history['acc']
train_acc = history.history['accuracy']
test_acc = history.history['val_accuracy']
epoch_counts = range(1, len(train_acc)+1)
plt.plot(epoch_counts, train_acc, 'r--', marker='^')
plt.plot(epoch_counts, test_acc, linestyle='-', marker='o', color='y')
plt.title('accuracy condition')
plt.legend(['train_acc', 'test_acc'])
plt.xlabel('epochs')
plt.ylabel('acc')
plt.savefig('epochs_acc.png')
plt.show()