赞
踩
服饰识别功能展示:
基于卷积神经网络的水果成熟度识别系统是在tensorflow框架下实现的,系统中有两个模型可选--cnn模型和VGG16模型,这两个模型可用于模型效果对比。该系统涉及的技术栈有,UI界面:python + pyqt5,前端界面:python flask + vue
该项目是在pycharm和anaconda搭建的虚拟环境执行,pycharm和anaconda安装和配置可观看教程:
超详细的pycharm+anaconda搭建python虚拟环境-CSDN博客
pycharm+anaconda搭建python虚拟环境_哔哩哔哩_bilibili
- class Vgg16Model:
- def __init__(self, train_path, test_path, model_name):
- self.train_path = train_path
- self.test_path = test_path
- self.model_name = model_name
-
- # 模型训练主入口
- def train(self, epochs):
- # 记录训练过程
- log_file_name = './results/cnn训练过程.txt'
- # 记录正常的 print 信息
- sys.stdout = Logger(log_file_name)
-
- # 开始训练,记录开始时间
- begin_time = time()
- train_ds, val_ds, class_names = self.data_load(224, 224, 16)
-
- print(class_names)
- # 加载模型
- model = self.model_load(class_num=len(class_names))
- # 指明训练的轮数epoch,开始训练
- history = model.fit(train_ds, validation_data=val_ds, epochs=epochs)
- # 保存模型
- model.save(self.model_name)
- # 记录结束时间
- end_time = time()
- run_time = end_time - begin_time
- print('该循环程序运行时间:', run_time, "s")
- # 绘制模型训练过程图
- self.show_loss_acc(history)
-
-
- if __name__ == '__main__':
- # todo 加载数据集, 修改数据集的路径
- train_dir = r"D:\A-Project\MangoRec_Project\mango_maturity_datasets\train"
- test_dir = r"D:\A-Project\MangoRec_Project\mango_maturity_datasets\test"
-
- # todo 修改模型名称
- model_name0 = r"models/vgg16.h5"
-
- my_cnn = Vgg16Model(train_dir, test_dir, model_name0)
- # todo 指定模型训练轮数
- my_cnn.train(epochs=10)
-
- # todo 模型验证 跑此函数前先把上面的 my_cnn.train(epochs=10) 注释掉
- # my_cnn.test()
该系统可以训练自己的数据集,训练过程也比较简单,只需指定自己数据集中训练集和测试集的路径,训练后模型名称和指定训练的轮数即可
训练结束后可输出以下结果:
- class Vgg16Model:
- def __init__(self, train_path, test_path, model_name):
- self.train_path = train_path
- self.test_path = test_path
- self.model_name = model_name
-
- # 测试模型准确率
- def test(self):
- # 记录测试过程
- log_file_name = './results/vgg16测试过程.txt'
- # 记录正常的 print 信息
- sys.stdout = Logger(log_file_name)
-
- _, test_ds, class_names = self.data_load(224, 224, 16)
- # 加载已训练好的模型
- model = tf.keras.models.load_model(self.model_name)
- # 测试
- loss, accuracy = model.evaluate(test_ds)
- # 输出结果
- print('VGG16 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
- self.show_heatmaps(title="heatmap", x_labels=class_names, y_labels=class_names,
- harvest=heat_maps_float, save_name="results/heatmap_vgg16.png")
-
-
- if __name__ == '__main__':
- # todo 加载数据集, 修改数据集的路径
- train_dir = r"D:\A-Project\MangoRec_Project\mango_maturity_datasets\train"
- test_dir = r"D:\A-Project\MangoRec_Project\mango_maturity_datasets\test"
-
- # todo 修改模型名称
- model_name0 = r"models/vgg16.h5"
-
- my_cnn = Vgg16Model(train_dir, test_dir, model_name0)
- # todo 指定模型训练轮数
- # my_cnn.train(epochs=10)
-
- # todo 模型验证 跑此函数前先把上面的 my_cnn.train(epochs=10) 注释掉
- my_cnn.test()
模型评估和模型训练在同一程序,模型评估时只需将模型评估入口函数放开,同时注释掉模型训练入口函数即可
模型评估可输出:
资源主要包括以下内容:完整的程序代码文件、训练好的模型、数据集、UI界面、前端界面。欢迎大家咨询!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。