基于Tensorflow2.x的ResNet-50的病虫害分类讲解(有界面):
基于Tensorflow2.x的CNN的病虫害分类j讲解(有界面)讲解:(39条消息) 基于Tensorflow2.x的CNN的病虫害分类(有界面)_天道酬勤者的博客-CSDN博客
基于Tensorflow2.x的MobileNet的病虫害分类(有界面)讲解:
(41条消息) 基于Tensorflow2.x的MobileNet的病虫害分类(有界面)_songyang66的博客-CSDN博客
基于Tensorflow2.x的ResNet的病虫害分类(有界面)文件下载:
基于Tensorflow2.x的CNN的病虫害分类(有界面)文件下载:(41条消息) 基于Tensorflow2.x的CNN的病虫害分类(有界面)-深度学习文档类资源-CSDN文库
基于Tensorflow2.x的mobilenet的病虫害分类(有界面)文件下载:(41条消息) 基于Tensorflow2.x的mobilenet的病虫害分类(有界面)-深度学习文档类资源-CSDN文库
datasort.py用于对数据集图片进行排序重命名,代码如下:
import os
class BatchRename():
def __init__(self):
self.path = "B:/BaiduNetdiskDownload/class/testing_data/Leaf-ulcer" #图片的路径
def rename(self):
filelist = os.listdir(self.path)
filelist.sort()
total_num = len(filelist)
i = 0
for item in filelist:
item = item.lower()
if item.endswith('.png'):
src = os.path.join(self.path, item)
s = str(i)
s = s.zfill(2) #Python zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0
dst = os.path.join(os.path.abspath(self.path), s + '.png')
'''
概述:
os.rename() 方法用于重命名文件或目录,从 src 到 dst,如果dst是一个存在的目录, 将抛出OSError
语法:
rename()方法语法格式如下:os.rename(src, dst)
参数:
src 要修改的目录名
dst 修改后的目录名
返回值:
该方法没有返回值
'''
try:
os.rename(src, dst)
print ('converting %s to %s ...' % (src, dst))
i = i + 1
except Exception as e:
print(e)
print('rename dir fail\r\n')
print ('total %d to rename & converted %d jpgs' % (total_num, i))
if __name__ == '__main__':
demo = BatchRename()
demo.rename()
datasort.py效果如下:
data_progress.py用于对数据集进行分类,具体代码如下:
import os
import random
from shutil import copy2
def data_set_split(initial_data_folder, target_data_folder, train_scale=0.8, val_scale=0.2, test_scale=0.0):
'''
读取源数据文件夹,生成划分好的文件夹,分为trian、val、test三个文件夹进行
:param initial_data_folder: 源文件夹 E:/biye/gogogo/note_book/torch_note/data/utils_test/data_split/initial_data
:param target_data_folder: 目标文件夹 E:/biye/gogogo/note_book/torch_note/data/utils_test/data_split/target_data
:param train_scale: 训练集比例
:param val_scale: 验证集比例
:param test_scale: 测试集比例
:return:
'''
print("开始数据集划分")
class_names = os.listdir(initial_data_folder)#用于返回initial_data_folder文件夹包含的图片文件的名字的列表
# 在目标目录下创建文件夹
split_names = ['train', 'val', 'test']
for split_name in split_names:
split_path = os.path.join(target_data_folder, split_name)
if os.path.isdir(split_path): #os.path.isdir()函数判断某一路径是否为目录
pass
else:
os.mkdir(split_path) #os.mkdir()函数创建目录(创建一级目录),其参数path 为要创建目录的路径 拓展:使用os.rmdir()函数删除目录。
# 然后在split_path的目录下创建类别文件夹,生成Fruit-anthrax等五个文件夹
for class_name in class_names:
class_split_path = os.path.join(split_path, class_name)
if os.path.isdir(class_split_path):
pass
else:
os.mkdir(class_split_path)
# 按照比例划分数据集,并进行数据图片的复制
# 首先进行分类遍历
for class_name in class_names:
current_class_data_path = os.path.join(initial_data_folder, class_name)
current_all_data = os.listdir(current_class_data_path)#用于返回current_class_data_path文件夹包含的图片文件的名字的列表
current_data_length = len(current_all_data)
current_data_index_list = list(range(current_data_length))
random.shuffle(current_data_index_list)
train_folder = os.path.join(os.path.join(target_data_folder, 'train'), class_name)
val_folder = os.path.join(os.path.join(target_data_folder, 'val'), class_name)
test_folder = os.path.join(os.path.join(target_data_folder, 'test'), class_name)
train_stop_flag = current_data_length * train_scale
val_stop_flag = current_data_length * (train_scale + val_scale)
current_idx = 0
train_num = 0
val_num = 0
test_num = 0
for i in current_data_index_list:
initial_img_path = os.path.join(current_class_data_path, current_all_data[i])
if current_idx <= train_stop_flag:
copy2(initial_img_path, train_folder)
train_num = train_num + 1
elif (current_idx > train_stop_flag) and (current_idx <= val_stop_flag):
copy2(initial_img_path, val_folder)
val_num = val_num + 1
else:
copy2(initial_img_path, test_folder)
test_num = test_num + 1
current_idx = current_idx + 1
print("*********************************{}*************************************".format(class_name))
print(
"{}类按照{}:{}:{}的比例划分完成,一共{}张图片".format(class_name, train_scale, val_scale, test_scale, current_data_length))
print("训练集{}:{}张".format(train_folder, train_num))
print("验证集{}:{}张".format(val_folder, val_num))
print("测试集{}:{}张".format(test_folder, test_num))
if __name__ == '__main__':
initial_data_folder = "training_data" # 原始数据集路径
target_data_folder = "split_data" # 目标存放的路径
data_set_split(initial_data_folder, target_data_folder)
data_progress.py效果展示:
train_cnn.py用于训练cnn网络
import tensorflow as tf
import matplotlib.pyplot as plt
from time import *
# 数据集加载函数,指明数据集的位置并统一处理为imgheight*imgwidth的大小,同时设置batch
def data_load(data_dir, test_data_dir, img_height, img_width, batch_size):
# 加载训练集
#tf.keras.preprocessing.image_dataset_from_directory从目录中的图像文件生成一个 tf.data.Dataset
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir, # 数据所在目录。如果标签是“inferred”(默认),则它应该包含子目录,每个目录包含一个类的图像。否则,将忽略目录结构。
#省略labels: “inferred”(标签从目录结构生成),或者是整数标签的列表/元组,其大小与目录中找到的图像文件的数量相同。标签应根据图像文件路径的字母顺序排序(通过Python中的os.walk(directory)获得)。
label_mode='categorical',# 'int':表示标签被编码成整数(例如:sparse_categorical_crossentropy loss)。‘categorical’指标签被编码为分类向量(例如:categorical_crossentropy loss)。‘binary’意味着标签(只能有2个)被编码为值为0或1的float32标量(例如:binary_crossentropy)。None(无标签)。
seed=123, #用于shuffle和转换的可选随机种子
image_size=(img_height, img_width), #数据批次的大小。默认值:32
batch_size=batch_size) #数据批次的大小。默认值:32
# 加载验证集
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
test_data_dir,
label_mode='categorical',
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = train_ds.class_names
# 返回处理之后的训练集、验证集和类名
return train_ds, val_ds, class_names
#拓展:
#如果label_mode 是 int, labels是形状为(batch_size, )的int32张量
#如果label_mode 是 binary, labels是形状为(batch_size, 1)的1和0的float32张量。
#如果label_mode 是 categorial, labels是形状为(batch_size, num_classes)的float32张量,表示类索引的one-hot编码。
# 构建CNN模型
def model_load(IMG_SHAPE=(224, 224, 3), class_num=5):
# 搭建模型
model = tf.keras.models.Sequential([
# 对模型做归一化的处理,将0-255之间的数字统一处理到0到1之间
tf.keras.layers.experimental.preprocessing.Rescaling(1. / 255, input_shape=IMG_SHAPE),
# 卷积层,该卷积层的输出为32个通道,卷积核的大小是3*3,激活函数为relu
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
# 添加池化层,池化的kernel大小是2*2
tf.keras.layers.MaxPooling2D(2, 2),
# Add another convolution
# 卷积层,输出为64个通道,卷积核大小为3*3,激活函数为relu
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
# 池化层,最大池化,对2*2的区域进行池化操作
tf.keras.layers.MaxPooling2D(2, 2),
# 将二维的输出转化为一维
tf.keras.layers.Flatten(),
# The same 128 dense layers, and 10 output layers as in the pre-convolution example:
tf.keras.layers.Dense(128, activation='relu'),
# 通过softmax函数将模型输出为类名长度的神经元上,激活函数采用softmax对应概率值
tf.keras.layers.Dense(class_num, activation='softmax')
])
# 输出模型信息
model.summary()
# 指明模型的训练参数,优化器为sgd优化器,损失函数为交叉熵损失函数
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
# 返回模型
return model
# 展示训练过程的曲线
def show_loss_acc(history):
# 从history中提取模型训练集和验证集准确率信息和误差信息
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
# 按照上下结构将图画输出
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.ylabel('Accuracy')
plt.ylim([min(plt.ylim()), 1])
plt.title('Training and Validation Accuracy')
plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.ylabel('Cross Entropy')
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.savefig('results/results_cnn.png', dpi=100)
plt.show()
def train(epochs):
print("开始训练,记录开始时间。。。")
# 开始训练,记录开始时间
begin_time = time()
# todo 加载数据集, 修改为目标的数据集的路径
print("加载数据集中。。。")
train_ds, val_ds, class_names = data_load("split_data/train",
"split_data/val", 224, 224, 16)
print(class_names)
print("加载模型中。。。")
# 加载模型
model = model_load(class_num=len(class_names))
# 指明训练的轮数epoch,开始训练
print("指明训练的轮数epoch,开始训练中。。。")
history = model.fit(train_ds, validation_data=val_ds, epochs=epochs)
print("保存模型中。。。")
# todo 保存模型, 修改为你要保存的模型的名称
model.save("results/cnn_orange.h5")
print("记录结束时间中。。。")
# 记录结束时间
end_time = time()
run_time = end_time - begin_time
print('该循环程序运行时间:', run_time, "s") # 该循环程序运行时间: 1.4201874732
# 绘制模型训练过程图
show_loss_acc(history)
if __name__ == '__main__':
train(epochs=30)
train_cnn.py训练结果:
model_test.py用于用测试集图片对cnn模型进行预测,并观察准确率
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
#测试之后在命令行中会输出每个模型的准确率,并且会在results目录下生成相应的热力图
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['SimHei']
# 数据加载,分别从训练的数据集的文件夹和测试的文件夹中加载训练集和验证集
def data_load(test_data_dir, img_height, img_width, batch_size):
# 加载测试集
test_ds = tf.keras.preprocessing.image_dataset_from_directory(
test_data_dir,
label_mode='categorical',
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
class_names = test_ds.class_names
# 返回处理之后的训练集、验证集和类名
return test_ds, class_names
# 测试cnn模型准确率
def test_cnn():
# todo 加载数据, 修改为你自己的数据集的路径
test_ds, class_names = data_load("B:\\class\\testing_data", 224, 224, 16)
# todo 加载模型,修改为你的模型名称
model = tf.keras.models.load_model("results/cnn_orange.h5")
# model.summary()
# 测试
loss, accuracy = model.evaluate(test_ds)
# 输出结果
print('CNN test accuracy :', accuracy)
# 对模型分开进行推理
test_real_labels = []
test_pre_labels = []
for test_batch_images, test_batch_labels in test_ds:
test_batch_labels = test_batch_labels.numpy()
test_batch_pres = model.predict(test_batch_images)
# print(test_batch_pres)
test_batch_labels_max = np.argmax(test_batch_labels, axis=1)
test_batch_pres_max = np.argmax(test_batch_pres, axis=1)
# print(test_batch_labels_max)
# print(test_batch_pres_max)
# 将推理对应的标签取出
for i in test_batch_labels_max:
test_real_labels.append(i)
for i in test_batch_pres_max:
test_pre_labels.append(i)
# break
# print(test_real_labels)
# print(test_pre_labels)
class_names_length = len(class_names)
heat_maps = np.zeros((class_names_length, class_names_length))
for test_real_label, test_pre_label in zip(test_real_labels, test_pre_labels):
heat_maps[test_real_label][test_pre_label] = heat_maps[test_real_label][test_pre_label] + 1
print(heat_maps)
heat_maps_sum = np.sum(heat_maps, axis=1).reshape(-1, 1)
# print(heat_maps_sum)
print()
heat_maps_float = heat_maps / heat_maps_sum
print(heat_maps_float)
# title, x_labels, y_labels, harvest
show_heatmaps(title="heatmap", x_labels=class_names, y_labels=class_names, harvest=heat_maps_float,
save_name="results/heatmap_cnn.png")
def show_heatmaps(title, x_labels, y_labels, harvest, save_name):
# 这里是创建一个画布
fig, ax = plt.subplots()
# cmap https://blog.csdn.net/ztf312/article/details/102474190
im = ax.imshow(harvest, cmap="OrRd")
# 这里是修改标签
# We want to show all ticks...
ax.set_xticks(np.arange(len(y_labels)))
ax.set_yticks(np.arange(len(x_labels)))
# ... and label them with the respective list entries
ax.set_xticklabels(y_labels)
ax.set_yticklabels(x_labels)
# 因为x轴的标签太长了,需要旋转一下,更加好看
# Rotate the tick labels and set their alignment.
plt.setp(ax.get_xticklabels(), rotation=45, ha="right",
rotation_mode="anchor")
# 添加每个热力块的具体数值
# Loop over data dimensions and create text annotations.
for i in range(len(x_labels)):
for j in range(len(y_labels)):
text = ax.text(j, i, round(harvest[i, j], 2),
ha="center", va="center", color="black")
ax.set_xlabel("Predict label")
ax.set_ylabel("Actual label")
ax.set_title(title)
fig.tight_layout()
plt.colorbar(im)
plt.savefig(save_name, dpi=100)
plt.show()
if __name__ == '__main__':
test_cnn()
model_test.py用于用测试集图片对cnn模型进行预测,并观察准确率
design.py用于测试界面
import tensorflow as tf
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
import cv2
from PIL import Image
import numpy as np
import shutil
class MainWindow(QTabWidget):
# 初始化
def __init__(self):
super().__init__()
self.setWindowIcon(QIcon('images/logo.png'))
self.setWindowTitle('大田柑橘病虫害识别系统') # todo 修改系统名称
# 模型初始化
self.model = tf.keras.models.load_model("results/mobilenet_orange.h5") # todo 修改模型名称:cnn_orange.h5、mobilenet_orange.h5、resnet_orange.h5
self.to_predict_name = "images/background.jpg" # todo 修改初始图片,这个图片要放在images目录下
self.class_names = ['Fruit-anthrax', 'Fruit-ulcer', 'Leaf-anthrax','Leaf-ulcer', 'leaf_thyroid'] # todo 修改类名,这个数组在模型训练的开始会输出
self.resize(900, 700)
self.initUI()
# 界面初始化,设置界面布局
def initUI(self):
main_widget = QWidget()
main_layout = QHBoxLayout()
font = QFont('楷体', 18)
# 主页面,设置组件并在组件放在布局上
left_widget = QWidget()
left_layout = QVBoxLayout()
img_title = QLabel("样本")
img_title.setFont(font)
img_title.setAlignment(Qt.AlignCenter)
self.img_label = QLabel()
img_init = cv2.imread(self.to_predict_name)
h, w, c = img_init.shape
scale = 400 / h
img_show = cv2.resize(img_init, (0, 0), fx=scale, fy=scale)
cv2.imwrite("images/show.png", img_show)
img_init = cv2.resize(img_init, (224, 224))
cv2.imwrite('images/target.png', img_init)
self.img_label.setPixmap(QPixmap("images/show.png"))
left_layout.addWidget(img_title)
left_layout.addWidget(self.img_label, 1, Qt.AlignCenter)
left_widget.setLayout(left_layout)
right_widget = QWidget()
right_layout = QVBoxLayout()
btn_change = QPushButton(" 上传图片 ")
btn_change.clicked.connect(self.change_img)
btn_change.setFont(font)
btn_predict = QPushButton(" 开始识别 ")
btn_predict.setFont(font)
btn_predict.clicked.connect(self.predict_img)
label_result = QLabel(' 柑橘病虫害名称 ')
self.result = QLabel("等待识别")
label_result.setFont(QFont('楷体', 16))
self.result.setFont(QFont('楷体', 24))
right_layout.addStretch()
right_layout.addWidget(label_result, 0, Qt.AlignCenter)
right_layout.addStretch()
right_layout.addWidget(self.result, 0, Qt.AlignCenter)
right_layout.addStretch()
right_layout.addStretch()
right_layout.addWidget(btn_change)
right_layout.addWidget(btn_predict)
right_layout.addStretch()
right_widget.setLayout(right_layout)
main_layout.addWidget(left_widget)
main_layout.addWidget(right_widget)
main_widget.setLayout(main_layout)
# 关于页面,设置组件并把组件放在布局上
about_widget = QWidget()
about_layout = QVBoxLayout()
about_title = QLabel('欢迎使用柑橘病虫害识别系统') # todo 修改欢迎词语
about_title.setFont(QFont('楷体', 18))
about_title.setAlignment(Qt.AlignCenter)
about_img = QLabel()
about_img.setPixmap(QPixmap('images/bj.jpg'))
about_img.setAlignment(Qt.AlignCenter)
label_super = QLabel("作者:宋扬") # todo 更换作者信息
label_super.setFont(QFont('楷体', 15))
# label_super.setOpenExternalLinks(True)
label_super.setAlignment(Qt.AlignRight)
about_layout.addWidget(about_title)
about_layout.addStretch()
about_layout.addWidget(about_img)
about_layout.addStretch()
about_layout.addWidget(label_super)
about_widget.setLayout(about_layout)
# 添加注释
self.addTab(main_widget, '主页')
self.addTab(about_widget, '关于')
self.setTabIcon(0, QIcon('images/主页面.png'))
self.setTabIcon(1, QIcon('images/关于.png'))
# 上传并显示图片
def change_img(self):
openfile_name = QFileDialog.getOpenFileName(self, 'chose files', '',
'Image files(*.jpg *.png *jpeg)') # 打开文件选择框选择文件
img_name = openfile_name[0] # 获取图片名称
if img_name == '':
pass
else:
target_image_name = "images/tmp_up." + img_name.split(".")[-1] # 将图片移动到当前目录
shutil.copy(img_name, target_image_name)
self.to_predict_name = target_image_name
img_init = cv2.imread(self.to_predict_name) # 打开图片
h, w, c = img_init.shape
scale = 400 / h
img_show = cv2.resize(img_init, (0, 0), fx=scale, fy=scale) # 将图片的大小统一调整到400的高,方便界面显示
cv2.imwrite("images/show.png", img_show)
img_init = cv2.resize(img_init, (224, 224)) # 将图片大小调整到224*224用于模型推理
cv2.imwrite('images/target.png', img_init)
self.img_label.setPixmap(QPixmap("images/show.png"))
self.result.setText("等待识别")
# 预测图片
def predict_img(self):
img = Image.open('images/target.png') # 读取图片
img = np.asarray(img) # 将图片转化为numpy的数组
outputs = self.model.predict(img.reshape(1, 224, 224, 3)) # 将图片输入模型得到结果
result_index = int(np.argmax(outputs))
result = self.class_names[result_index] # 获得对应的水果名称
self.result.setText(result) # 在界面上做显示
# 界面关闭事件,询问用户是否关闭
def closeEvent(self, event):
reply = QMessageBox.question(self,
'退出',
"是否要退出程序?",
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No)
if reply == QMessageBox.Yes:
self.close()
event.accept()
else:
event.ignore()
if __name__ == "__main__":
app = QApplication(sys.argv)
x = MainWindow()
x.show()
sys.exit(app.exec_())
design.py用于测试界面
精彩链接
评论可见,请评论后查看内容,谢谢!!!
您阅读本篇文章共花了:
大家都在找:
python:python是做什么的
tensorflow:tensorflow教程
深度学习:深度学习走向核心素养读书体会
神经网络:神经网络模型
大家都在看: