# -*- coding: utf-8 -*-
from MyDataSet import *
from torch.utils.data import DataLoader
import torch
import cv2
# 导入图像数据处理模块
from torchvision.transforms import transforms
from MyLeNet import *
import torch.optim as optim
# 定义训练的最大轮数
MAX_EPOCH = 10
# 定义学习率
LR = 0.01
# 定义一个批次数量
BATCH_SIZE = 16
# 图片数据的均值、标准差
# Image-net 大赛中使用的图片的均值、标准差
norm_mean = [0.485, 0.456, 0.406]
norm_std = [0.229, 0.224, 0.225]
# 1、数据部分------------------------------------------------------------------------------------------------------------
# a、准备数据---->图片特征和目标一一对应 --(自己做的时候,需要准备好图片---放置在对应的文件夹中)
# b、数据集拆分---> 训练集、验证集、测试集 = 8:1:1 --->训练集--训练数据构建模型;验证集--->验证模型的;测试集---> 测试模型的好坏
# c、数据读取和分批---->构建torch中的数据装载器
# d、图片数据处理----> 固定图片尺寸、转化为张量、图片的归一化(将图像像素转化到一个较小的范围内,避免像素过大)
# 确定 训练集路径
train_dir = './dataset/rmb_split/train'
# 确定 验证集路径
valid_dir = './dataset/rmb_split/valid'
# 确定 图像数据 处理对象
transform = transforms.Compose([
# 固定尺寸大小
transforms.Resize((32, 32)),
# 转化为张量
transforms.ToTensor(),
# 图像 归一化处理 ----> x = (x - mean) / std
transforms.Normalize(mean=norm_mean, std=norm_std)
])
# 确定 训练集的 DataSet对象
train_dataset = RMB_DataSet(dir_name=train_dir, transform=transform)
# 确定 验证集的 DataSet对象
valid_dataset = RMB_DataSet(dir_name=valid_dir, transform=transform)
# 确定 训练集 数据装载器---DataLoader对象
train_loader = DataLoader(
dataset=train_dataset, # 标志着数据的读取方式
batch_size=BATCH_SIZE, # 每批次样本数目
shuffle=True, # 打乱顺序
)
# 确定 验证集 数据装载器---DataLoader对象
valid_loader = DataLoader(
dataset=valid_dataset, # 标志着数据的读取方式
batch_size=BATCH_SIZE, # 每批次样本数目
shuffle=True, # 打乱顺序
)
# 2、网络部分------------------------------------------------------------------------------------------------------------
# 根据 LeNet-5 设置自己的卷积神经网络
# 实例化网络对象
net = LeNet_5(classes=2)
# 初始化网络对象
net.initialize_weight()
# 3、损失部分------------------------------------------------------------------------------------------------------------
# 构建 交叉熵损失对象
losses = nn.CrossEntropyLoss()
# 4、优化器部分----------------------------------------------------------------------------------------------------------
# 构建 随机梯度下降优化算法 对象
# 参数params=net.parameters() --->表示优化参数是网络中的参数
# 参数lr --->表示学习率
# 参数momentum=0.9---表示动量 ---->每次优化的时候,优化 1-momentum 的参数,保持 momentum=0.9 的参数不变
sgd = optim.SGD(params=net.parameters(), lr=LR, momentum=0.9)
# 5、训练---------------------------------------------------------------------------------------------------------------
# 如果测试模型,训练部分,需要注释掉
#
# 经过多轮训练
# 在每一轮中---->从训练集的数据装载器中获取数据--->输入到网络中--->正向传播--->预测值---->与真实值计算出交叉熵---->(清空上一次的梯度值)--->反向传播计算出梯度---> 梯度下降--更新参数
# 训练完一轮--->验证此时网络中的参数---->从验证集的数据装载器中获取 --->输入到网络中---->正向传播---得到预测值--与真实值对比--->此时模型的准确率
# 输出模型---->进行保存
# for epcoch in range(MAX_EPOCH):
# # epoch ---> 表示的 第 epoch 次的训练
#
# # 定义该轮预测准确的样本个数
# train_correct_num = 0
#
# # 定义该轮总的样本个数
# train_total_num = 0
#
# # 定义该轮的平均损失
# train_loss_mean = 0
#
# # 开始训练
# net.train()
#
# # 从训练数据集的数据装载器中 获取数据
# for train_data in train_loader:
# # train_data ---> 每批次的数据----> img,img_label,img_path --->这是一批次的数据
# train_img, train_img_label, train_img_path = train_data
# # print('train_img:\n', train_img)
# # print('train_img:\n', train_img.shape)
# #
# # print('train_img_label:\n', train_img_label)
# # print('train_img_label:\n', train_img_label.shape)
#
# # 正向传播 ---将 特征 输入到 网络
# train_output = net(train_img) # 等价于--- train_output = net.forward(train_img)
# # print('train_output:\n', train_output)
#
# # 计算交叉熵损失
# train_loss = losses(train_output, train_img_label)
# # print('该批次的误差损失为:', train_loss.item())
#
# # 清空上一批次的 梯度值
# net.zero_grad()
#
# # 反向传播、计算梯度
# train_loss.backward()
#
# # 梯度下降
# sgd.step()
#
# # 查看 该批次的预测类别
# # ret = torch.max(train_output.data,dim=1)
# # print('ret:\n',ret)
# # torch.return_types.max(
# # values=tensor([0.5677, 0.5101, 0.5511, 0.5707, 0.5394, 0.5706, 0.5704, 0.5728, 0.5325,
# # 0.5757, 0.5960, 0.5240, 0.5749, 0.5385, 0.5113, 0.6365]),
# # indices=tensor([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]))
#
# _, train_y_predict = torch.max(train_output.data, dim=1)
# # print('train_y_predict:\n', train_y_predict)
#
# # 获取该批次 预测准确的样本个数
# # print('预测准确的样本个数:\n', train_img_label == train_y_predict)
# # print('预测准确的样本个数:\n', (train_img_label == train_y_predict).sum())
# # print('预测准确的样本个数:\n', (train_img_label == train_y_predict).sum().numpy())
# # 加上每批次的训练准确的样本数
# train_correct_num += (train_img_label == train_y_predict).sum().numpy()
#
# # print('该批次总的样本个数为:\n',train_img_label.shape[0])
# # 加上每批次的样本数目
# train_total_num += train_img_label.shape[0]
#
# # 加损失
# train_loss_mean += train_loss.item()
#
# print('第 %d 轮训练,训练的准确率为 %f%%, 训练的平均误差为 %f' % (
# epcoch + 1, train_correct_num / train_total_num * 100, train_loss_mean / len(train_loader)
# ))
#
# # 验证
# # 定义该轮预测准确的样本个数
# valid_correct_num = 0
#
# # 定义该轮总的样本个数
# valid_total_num = 0
#
# # 定义该轮的平均损失
# valid_loss_mean = 0
#
# # 开始验证
# net.eval()
#
# with torch.no_grad():
# # 从验证数据的训练装载器中获取数据
# for valid_data in valid_loader:
# # valid_data ---> 每批次的数据----> img,img_label,img_path --->这是一批次的数据
# valid_img, valid_img_label, valid_img_path = valid_data
# # print('valid_img:\n', valid_img)
# # print('valid_img:\n', valid_img.shape)
# #
# # print('valid_img_label:\n', valid_img_label)
# # print('valid_img_label:\n', valid_img_label.shape)
#
# # 正向传播 ---将 特征 输入到 网络
# valid_output = net(valid_img) # 等价于--- valid_output = net.forward(valid_img)
# # print('valid_output:\n', valid_output)
#
# # 计算交叉熵损失
# valid_loss = losses(valid_output, valid_img_label)
# # print('该批�
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
python课程设计大作业—PyTorch实现图像识别基于卷积神经网络的识别方法RMB。已获导师指导并通过的高分项目。 python课程设计大作业—PyTorch实现图像识别基于卷积神经网络的识别方法RMB。已获导师指导并通过的高分项目。python课程设计大作业—PyTorch实现图像识别基于卷积神经网络的识别方法RMB。已获导师指导并通过的高分项目。python课程设计大作业—PyTorch实现图像识别基于卷积神经网络的识别方法RMB。已获导师指导并通过的高分项目。python课程设计大作业—PyTorch实现图像识别基于卷积神经网络的识别方法RMB。已获导师指导并通过的高分项目。python课程设计大作业—PyTorch实现图像识别基于卷积神经网络的识别方法RMB。已获导师指导并通过的高分项目。python课程设计大作业—PyTorch实现图像识别基于卷积神经网络的识别方法RMB。已获导师指导并通过的高分项目。python课程设计大作业—PyTorch实现图像识别基于卷积神经网络的识别方法RMB。已获导师指导并通过的高分项目。
资源推荐
资源详情
资源评论
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![thumb](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
收起资源包目录
![package](https://csdnimg.cn/release/downloadcmsfe/public/img/package.f3fc750b.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/UNKNOWN.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
![file-type](https://csdnimg.cn/release/download/static_files/pc/images/minetype/JPG.png)
共 413 条
- 1
- 2
- 3
- 4
- 5
![avatar](https://profile-avatar.csdnimg.cn/6f999e5eff2040dc8aaf911dc17b4077_weixin_55305220.jpg!1)
程序员张小妍
- 粉丝: 1w+
- 资源: 3134
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
安全验证
文档复制为VIP权益,开通VIP直接复制
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)
- 1
- 2
前往页