第P4周:猴痘病识别

  • 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
  • 🍦 参考文章:Pytorch实战 | 第P4周:猴痘病识别
  • 🍖 原作者:K同学啊|接辅导、项目定制

一、前期准备

1.设置GPU 

''' 设置GPU '''
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Using {} device".format(device))

没有GPU则使用CPU 

 

2.导入数据、数据预处理

import os,PIL,random,pathlib
data_dir = r'D:\P4'
data_dir = pathlib.Path(data_dir)data_paths = list(data_dir.glob('*'))
class_names = [path.name for path in data_paths]
print(class_names)

 

 

import pathlib
import torchvision.transforms as transforms
from torchvision import datasets
total_datadir = r'D:\P4'train_transforms = transforms.Compose([transforms.Resize([224, 224]),transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])
])total_data = datasets.ImageFolder(total_datadir, transform=train_transforms)
print(total_data)

定义数据预处理操作 train_transforms

  • transforms.Resize([224, 224]):将输入图像的尺寸调整为 224x224 像素,这通常是训练深度学习模型所使用的常见图像尺寸。
  • transforms.ToTensor():将图像数据转换为PyTorch张量,并将像素值归一化到范围 [0, 1]。
  • transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]):对图像进行标准化处理,将像素值标准化为标准正态分布(高斯分布),这有助于模型更快地收敛。

使用 datasets.ImageFolder 加载图像数据集:

  • total_datadir 是包含图像数据集的目录路径。
  • transform=train_transforms 指定了要应用的数据预处理操作。

 3.标签映射

在DatasetFolder中,class_to_idx是一个字典,将类别名映射到类别标签(从0开始),其中类别名是文件夹的名称,类别标签是与之相关联的数字。

为什么要做标签映射呢?

  • 将类别名映射到类别标签是因为在训练深度学习模型时,通常使用类别标签来表示每个样本的类别。
  • 在训练模型时,输入数据被转换为张量,并且每个张量的标签是一个数字,表示与之相关联的类别。
  • 类别标签使得模型可以根据真实标签和预测标签之间的误差来更新模型权重,从而使模型学习到如何将输入数据映射到正确的输出标签。

 

print(total_data.class_to_idx)

二、划分数据集

import torch
from torch.utils.data import DataLoader# total_data 包含了数据集,我们可以从 total_data 中划分出训练集和测试集
# 例如,可以按照一定的比例划分数据集,或者根据需要自定义训练集和测试集# 划分数据集示例:
# 假设数据集总共有100个样本,可以将前80个样本用于训练,后20个用于测试
train_size = int(0.8 * len(total_data))
test_size = len(total_data) - train_sizetrain_dataset, test_dataset = torch.utils.data.random_split(total_data, [train_size, test_size])batch_size = 32train_dl = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=1)
test_dl = DataLoader(test_dataset, batch_size=batch_size, shuffle=True, num_workers=1)

三、 构建 CNN 网络

import torch
import torch.nn as nn
import torch.nn.functional as Fclass Network(nn.Module):def __init__(self):super(Network, self).__init__()self.conv1 = nn.Conv2d(3, 6, 5)self.conv2 = nn.Conv2d(6, 16, 5)self.fc1 = nn.Linear(16 * 53 * 53, 120)self.fc2 = nn.Linear(120, 84)self.fc3 = nn.Linear(84, 2)self.dropout = nn.Dropout(0.5)  # Adding dropout with a probability of 0.5def forward(self, x):x = F.max_pool2d(F.relu(self.conv1(x)), 2)x = F.max_pool2d(F.relu(self.conv2(x)), 2)x = x.view(-1, 16 * 53 * 53)x = F.relu(self.fc1(x))x = self.dropout(x)  # Applying dropout after the first fully connected layerx = F.relu(self.fc2(x))x = self.dropout(x)  # Applying dropout after the second fully connected layerx = self.fc3(x)x = F.log_softmax(x, dim=1)return xmodel = Network()
print(model)

四、训练模型

1. 设置超参数 

# 定义损失函数和优化器
loss_fn = nn.CrossEntropyLoss()
leaining_rate = 0.001
opt = torch.optim.Adam(model.parameters(),lr=leaining_rate)

2.编写训练函数

# 训练函数
def train(dataloader, model, loss_fn, optimizer):size = len(dataloader.dataset)num_batches = len(dataloader)train_loss, train_acc = 0, 0for x, y in dataloader:x, y  = x.to(device), y.to(device)# Compute prediction errorpred = model(x) # 网络输出loss = loss_fn(pred, y) # 计算损失optimizer.zero_grad() # 梯度清零loss.backward() # 反向传播optimizer.step() # 更新参数train_acc += (pred.argmax(1) == y).type(torch.float).sum().item()train_loss += loss.item()train_acc /= sizetrain_loss /= num_batchesreturn train_acc, train_loss# 测试函数
def test(dataloader, model, loss_fn):size = len(dataloader.dataset)num_batches = len(dataloader)test_loss, test_acc = 0, 0with torch.no_grad():for x, y in dataloader:x, y  = x.to(device), y.to(device)pred = model(x)loss = loss_fn(pred, y)test_loss += loss_fn(pred, y).item()test_acc += (pred.argmax(1) == y).type(torch.float).sum().item()test_acc /= sizetest_loss /= num_batchesreturn test_acc, test_loss

3.正式训练

epochs     = 20
train_loss = []
train_acc  = []
test_loss  = []
test_acc   = []for epoch in range(epochs):model.train()epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, opt)model.eval()epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn)train_acc.append(epoch_train_acc)train_loss.append(epoch_train_loss)test_acc.append(epoch_test_acc)test_loss.append(epoch_test_loss)template = ('Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%,Test_loss:{:.3f}')print(template.format(epoch+1, epoch_train_acc*100, epoch_train_loss, epoch_test_acc*100, epoch_test_loss))
print('Done')

 

代码在运行时出现下列问题可能是以下原因导致: 

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "D:\Python\lib\multiprocessing\spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "D:\Python\lib\multiprocessing\spawn.py", line 125, in _main
    prepare(preparation_data)
  File "D:\Python\lib\multiprocessing\spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "D:\Python\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "D:\Python\lib\runpy.py", line 289, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "D:\Python\lib\runpy.py", line 96, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "D:\Python\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\Users\刘鸿逸\Desktop\python\01.py", line 156, in <module>
    epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, opt)
  File "c:\Users\刘鸿逸\Desktop\python\01.py", line 105, in train
    for X, y in dataloader:  # 获取图片及其标签
  File "D:\Python\lib\site-packages\torch\utils\data\dataloader.py", line 441, in __iter__
    return self._get_iterator()
  File "D:\Python\lib\site-packages\torch\utils\data\dataloader.py", line 388, in _get_iterator
    return _MultiProcessingDataLoaderIter(self)
  File "D:\Python\lib\site-packages\torch\utils\data\dataloader.py", line 1042, in __init__
    w.start()
  File "D:\Python\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "D:\Python\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "D:\Python\lib\multiprocessing\context.py", line 336, in _Popen
    return Popen(process_obj)
  File "D:\Python\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "D:\Python\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "D:\Python\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

这个错误是由于在Windows操作系统上使用多进程时,未按照正确的方式设置了启动子进程的方法引起的。它提示需要在主模块中添加适当的if __name__ == '__main__':块以正确启动子进程。下面解释一下报错的含义以及如何解决它: 

  • 为了解决这个问题,应该确保在主模块中使用if __name__ == '__main__':块来包装主要的执行代码,这是一种在使用多进程时常见的做法。
  • 在主模块中包装代码后,子进程将只在主进程中执行,而不会在导入模块时执行。这可以防止上述报错。

修改后完整代码:

import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision
from torchvision import transforms, datasets
import os
import PIL
import pathlib# Your code for data loading, model definition, training, and testing should go hereif __name__ == '__main__':device = torch.device("cuda" if torch.cuda.is_available() else "cpu")print(device)import os,PIL,random,pathlibdata_dir = r'D:\P4'data_dir = pathlib.Path(data_dir)data_paths = list(data_dir.glob('*'))class_names = [path.name for path in data_paths]print(class_names)total_datadir = r'D:\P4'# 关于transforms.Compose的更多介绍可以参考:https://blog.csdn.net/qq_38251616/article/details/124878863train_transforms = transforms.Compose([transforms.Resize([224, 224]),  # 将输入图片resize成统一尺寸transforms.ToTensor(),          # 将PIL Image或numpy.ndarray转换为tensor,并归一化到[0,1]之间transforms.Normalize(           # 标准化处理-->转换为标准正太分布(高斯分布),使模型更容易收敛mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # 其中 mean=[0.485,0.456,0.406]与std=[0.229,0.224,0.225] 从数据集中随机抽样计算得到的。])total_data = datasets.ImageFolder(total_datadir, transform=train_transforms)print(total_data)train_size = int(0.8 * len(total_data))test_size  = len(total_data) - train_sizetrain_dataset, test_dataset = torch.utils.data.random_split(total_data, [train_size, test_size])print(train_dataset, test_dataset)batch_size = 32train_dl = torch.utils.data.DataLoader(train_dataset,batch_size=batch_size,shuffle=True,num_workers=1)test_dl = torch.utils.data.DataLoader(test_dataset,batch_size=batch_size,shuffle=True,num_workers=1)import torch.nn.functional as Fclass Network_bn(nn.Module):def __init__(self):super(Network_bn, self).__init__()"""nn.Conv2d()函数:第一个参数(in_channels)是输入的channel数量第二个参数(out_channels)是输出的channel数量第三个参数(kernel_size)是卷积核大小第四个参数(stride)是步长,默认为1第五个参数(padding)是填充大小,默认为0"""self.conv1 = nn.Conv2d(in_channels=3, out_channels=12, kernel_size=5, stride=1, padding=0)self.bn1 = nn.BatchNorm2d(12)self.conv2 = nn.Conv2d(in_channels=12, out_channels=12, kernel_size=5, stride=1, padding=0)self.bn2 = nn.BatchNorm2d(12)self.pool = nn.MaxPool2d(2,2)self.conv4 = nn.Conv2d(in_channels=12, out_channels=24, kernel_size=5, stride=1, padding=0)self.bn4 = nn.BatchNorm2d(24)self.conv5 = nn.Conv2d(in_channels=24, out_channels=24, kernel_size=5, stride=1, padding=0)self.bn5 = nn.BatchNorm2d(24)self.fc1 = nn.Linear(24*50*50, len(class_names))def forward(self, x):x = F.relu(self.bn1(self.conv1(x)))      x = F.relu(self.bn2(self.conv2(x)))     x = self.pool(x)                        x = F.relu(self.bn4(self.conv4(x)))     x = F.relu(self.bn5(self.conv5(x)))  x = self.pool(x)                        x = x.view(-1, 24*50*50)x = self.fc1(x)return xdevice = "cuda" if torch.cuda.is_available() else "cpu"print("Using {} device".format(device))model = Network_bn().to(device)print(model)loss_fn    = nn.CrossEntropyLoss() # 创建损失函数learn_rate = 1e-4 # 学习率opt        = torch.optim.SGD(model.parameters(),lr=learn_rate)# 训练循环def train(dataloader, model, loss_fn, optimizer):size = len(dataloader.dataset)  # 训练集的大小,一共60000张图片num_batches = len(dataloader)   # 批次数目,1875(60000/32)train_loss, train_acc = 0, 0  # 初始化训练损失和正确率for X, y in dataloader:  # 获取图片及其标签X, y = X.to(device), y.to(device)# 计算预测误差pred = model(X)          # 网络输出loss = loss_fn(pred, y)  # 计算网络输出和真实值之间的差距,targets为真实值,计算二者差值即为损失# 反向传播optimizer.zero_grad()  # grad属性归零loss.backward()        # 反向传播optimizer.step()       # 每一步自动更新# 记录acc与losstrain_acc  += (pred.argmax(1) == y).type(torch.float).sum().item()train_loss += loss.item()train_acc  /= sizetrain_loss /= num_batchesreturn train_acc, train_lossdef test (dataloader, model, loss_fn):size        = len(dataloader.dataset)  # 测试集的大小,一共10000张图片num_batches = len(dataloader)          # 批次数目,313(10000/32=312.5,向上取整)test_loss, test_acc = 0, 0# 当不进行训练时,停止梯度更新,节省计算内存消耗with torch.no_grad():for imgs, target in dataloader:imgs, target = imgs.to(device), target.to(device)# 计算losstarget_pred = model(imgs)loss        = loss_fn(target_pred, target)test_loss += loss.item()test_acc  += (target_pred.argmax(1) == target).type(torch.float).sum().item()test_acc  /= sizetest_loss /= num_batchesreturn test_acc, test_lossepochs     = 17train_loss = []train_acc  = []test_loss  = []test_acc   = []for epoch in range(epochs):model.train()epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, opt)model.eval()epoch_test_acc, epoch_test_loss = test(test_dl, model, loss_fn)train_acc.append(epoch_train_acc)train_loss.append(epoch_train_loss)test_acc.append(epoch_test_acc)test_loss.append(epoch_test_loss)template = ('Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%,Test_loss:{:.3f}')print(template.format(epoch+1, epoch_train_acc*100, epoch_train_loss, epoch_test_acc*100, epoch_test_loss))print('Done')

 

验证集正确率达到88%以上。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/76818.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

系统架构设计师-嵌入式系统

目录 一、嵌入式系统概述 1、基本概念 2、嵌入式系统软件组成架构 二、嵌入式软件开发 三、嵌入式硬件 1、嵌入式微处理器 2、人工智能芯片 3、嵌入式微处理器体系结构 4、总线 四、嵌入式操作系统 1、嵌入式实时操作系统 2、操作系统内核架构 3、鸿蒙操作系统 五、嵌入式…

hive的语言元素

参考文档地址 http://www.hplsql.org/doc 数据类型 可以在HPL/SQL程序中使用以下数据类型&#xff1a; 数据类型描述BIGINT / INT864位整数BINARY_DOUBLE双精度浮点数BINARY_FLOAT单精度浮点数BINARY_INTEGER32位整数BIT0、1或NULLBOOL / BOOLEAN真或假CHAR(n) / CHARACTER…

postman token 请求头添加

思路&#xff1a; 1、登录成功后将 得到的token设置为集合变量 2、在需要携带Authorization的请求头上使用该集合变量 关键代码 const responseData pm.response.json(); if(responseData.code 1) {// 获取tokenconst {data:{token}} responseData// 设置为集合变量pm.colle…

编译OpenWrt内核驱动

编译OpenWrt内核驱动可以参考OpenWrt内部其它驱动的编写例程&#xff0c;来修改成自己需要的驱动 一、OpenWrt源代码获取与编译 1.1、搭建环境 下载OpenWrt的官方源码&#xff1a; git clone https://github.com/openwrt/openwrt.git1.2、安装编译依赖项 sudo apt update -…

vue中如何给特殊字段设置插槽

大纲: <template><div><div><span>卡号</span><el-input type"text" v-model"cardNo" clearable placeholder"请输入卡号" /><el-button type"primary" plain icon"el-icon-search"…

Python操作Excel实战:Excel行转列

# 1、原始数据准备 样例数据准备 地区1m2-5m6-10m11-20m21-40m地区单价计费单位费用最小值费用最大值北京13012011010090     天津13012011010090     石家庄13012011010090     保定140130120110100     张家口170150130120110     邢台1401201101…

我们如何在工作与生活中找到平衡点?

找到工作与生活中的平衡点是每个人都必须面对的问题。以下是一些建议&#xff0c;可以帮助你在工作和生活之间找到平衡&#xff1a; 制定时间表&#xff1a;确保你有足够的时间来处理工作和生活中的各种任务。为工作、学习和个人生活设定优先级&#xff0c;并确保时间分配合理…

玩转Mysql系列 - 第15篇:详解视图

这是Mysql系列第15篇。 环境&#xff1a;mysql5.7.25&#xff0c;cmd命令中进行演示。 需求背景 电商公司领导说&#xff1a;给我统计一下&#xff1a;当月订单总金额、订单量、男女订单占比等信息&#xff0c;我们啪啦啪啦写了一堆很复杂的sql&#xff0c;然后发给领导。 …

ElasticSearch第三讲:ES详解 - Elastic Stack生态和场景方案

ElasticSearch第三讲&#xff1a;ES详解 - Elastic Stack生态和场景方案 本文是ElasticSearch第三讲&#xff0c;在了解ElaticSearch之后&#xff0c;我们还要了解Elastic背后的生态 即我们常说的ELK&#xff1b;与此同时&#xff0c;还会给你展示ElasticSearch的案例场景&…

k8s配置ingress访问集群外部资源

使用ingress访问外部资源&#xff0c;首先需要创建service指向我们需要访问的资源 而每个service包含一个endpoint endpoint是k8s集群中的一个资源对象&#xff0c;存储在etcd中&#xff0c;用来记录一个service对应的所有pod的访问地址。service配置selector&#xff0c;endpo…

浅谈限流式保护器在高校防火工作的应用

安科瑞 华楠 【摘要】摘要&#xff1a;为了预防火灾和减少火灾带来的危害&#xff0c;保护校园和师生生命财产安全&#xff0c; 建和谐安宁的校园环境&#xff0c;保障学校安全稳定发展&#xff0c;我们必须要时刻拧紧消防安全这弦&#xff0c;时刻注意这根高压线。随着近年来…

el-select下拉框定位问题

1.当el-select所在页面滚动时或者el-select上面区域高度发生变化时&#xff0c;定位存在偏差 2.解决办法&#xff1a; 1. el-select自带属性popper-append-to-body&#xff1a;true&#xff0c;可能会无效 2.设置ref,监听高度变化或者滚动时&#xff0c;手动执行刷新方法&…

应用在汽车新风系统中消毒杀菌的UVC灯珠

在病毒、细菌的传播可以说是一个让人敏感而恐惧的事情。而对于车内较小的空间&#xff0c;乘坐人员流动性大&#xff0c;更容易残留细菌病毒。车内缺少通风&#xff0c;残留的污垢垃圾也会滋生细菌&#xff0c;加快细菌的繁殖。所以对于车内消毒就自然不容忽视。 那么问题又来…

php如何处理高并发请求

PHP 处理高并发请求的方法&#xff1a; 使用异步框架&#xff1a;通过使用异步处理方式&#xff0c;可以有效地降低 PHP 处理请求的响应时间&#xff0c;避免因为 IO 操作而导致的等待阻塞。常用的异步框架有ReactPHP和Swoole等。 使用缓存&#xff1a;使用缓存可以减少每个请求…

算法:贪心---跳一跳

1、题目&#xff1a; 给你一个非负整数数组 nums &#xff0c;你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个下标&#xff0c;如果可以&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。 2…

【SpringMvc 丨跨域】

Spring MVC 支持跨域处理&#xff08;CORS&#xff09;。 CORS 简介处理CORS 过滤器CrossOrigin注解java配置xml配置 主页传送门&#xff1a;&#x1f4c0; 传送 简介 跨域是指在浏览器的同源策略下&#xff0c;不能执行其他网站的脚本。它是由浏览器的安全限制造成的&#xf…

每天40min,我们一起用70天稳扎稳打学完《JavaEE初阶》——32/70 第三十二天【JavaScript】

JavaScript JavaScript初识 JavaScript前置知识第一个程序JavaScript 的书写形式语法概览变量的使用基本数据类型number类型string 字符串类型强类型 和 动态类型boolean 布尔类型undefined 类型运算符\==\=和\=\=|| 和 && 的返回值条件,循环语句数组

C语言文件读写

C 文件读写介绍 一个文件&#xff0c;无论它是文本文件还是二进制文件&#xff0c;都是代表了一系列的字节。C 语言不仅提供了访问顶层的函数&#xff0c;也提供了底层&#xff08;OS&#xff09;调用来处理存储设备上的文件。本章将讲解文件管理的重要调用。 打开文件 您可…

Linux学习--MySQL学习之查询语句

所有实现基于mysql8.0.26实现&#xff0c;事例代码 1&#xff1a;常用函数 字符函数数学函数日期函数聚集函数数学计算if函数case函数 函数&#xff1a;MySQL服务内置命令 语法&#xff1a;函数名(表头名) select格式 SELECT 函数(表头名) FROM 库名.表名&#xff1b; SE…

2023-9-12 完全背包问题

题目链接&#xff1a;完全背包问题 初版(时间复杂度拉满) #include <iostream> #include <algorithm>using namespace std;const int N 1010;int n, m; int v[N], w[N]; int f[N][N];int main() {cin >> n >> m;for(int i 1; i < n; i ) cin >…