4、猴痘病识别

 
 

学习要求

  • 训练过程中保存效果最好的模型参数。
  • 加载最佳模型参数识别本地的一张图片。
  • 调整网络结构使测试集accuracy到达88%(重点)。

学习提高

  • 调整模型参数并观察测试集的准确率变化。
  • 尝试设置动态学习率。
  • 测试集accuracy到达90%。
 

学习所得

通过加入dropout,对于learning rate和epochs的调参,很开心终于将accuracy提升到了90%!

 

一、前期工作准备部分

1、设置GPU

In [1]:
import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision
from torchvision import transforms, datasets
from sklearn.model_selection import KFold

import os,PIL,pathlib

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

device
Out[1]:
device(type='cuda')
 

2、导入数据

本次是进行本地数据的导入,首先下载到了本地,进行云的图像读取。 导入数据主要用的是pathlib的库,这个库在python3.4之后成为标准库模块,具有跨平台、面向对象等特点,解决了传统路径与字符串不等价的问题,使得在不同操作系统之间切换简单。

🐒pathlib主要包含两个类,PurePath和Path,两个类分别有两个子类,分别用于unix系统和windows系统 jupyter

对于数据标准化Normalize的进一步理解:https://blog.csdn.net/qq_40507857/article/details/116600119

In [2]:
data_dir = '../data/4-data'
# 通过Path类创建路径对象
data_dir = pathlib.Path(data_dir)
# 获取路径下所有文件路径
paths= list(data_dir.glob('*'))
# 获取所有文件夹的名字,也就是图片类别
classname = [str(path).split("\\")[3] for path in paths]
print(classname)

# 图像transforms
# 关于transforms.Compose的更多介绍可以参考:https://blog.csdn.net/qq_38251616/article/details/124878863
train_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和std是从数据中随机抽样计算得到的,其实这个数值是pytorch上给的通用的统计值
])
total_data = datasets.ImageFolder(data_dir, transform=train_transforms)
total_data
 
['Monkeypox', 'Others']
Out[2]:
Dataset ImageFolder
    Number of datapoints: 2142
    Root location: ..\data\4-data
    StandardTransform
Transform: Compose(
               Resize(size=[224, 224], interpolation=bilinear, max_size=None, antialias=None)
               ToTensor()
               Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
           )
In [3]:
total_data.class_to_idx
Out[3]:
{'Monkeypox': 0, 'Others': 1}
 

3、划分数据集

In [4]:
train_size = int(0.8 * len(total_data))
test_size  = len(total_data) - train_size
train_dataset, test_dataset = torch.utils.data.random_split(total_data, [train_size, test_size])
train_dataset, test_dataset
Out[4]:
(<torch.utils.data.dataset.Subset at 0x1bb613452e8>,
 <torch.utils.data.dataset.Subset at 0x1bb61345438>)
In [5]:
train_size,test_size
Out[5]:
(1713, 429)
In [6]:
batch_size = 32

train_dl = torch.utils.data.DataLoader(train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True,
                                           num_workers=0)
test_dl = torch.utils.data.DataLoader(test_dataset,
                                          batch_size=batch_size,
                                          shuffle=True,
                                          num_workers=0)
In [7]:
for X, y in test_dl:
    print("Shape of X [N, C, H, W]: ", X.shape)
    print("Shape of y: ", y.shape, y.dtype)
    break
 
Shape of X [N, C, H, W]:  torch.Size([32, 3, 224, 224])
Shape of y:  torch.Size([32]) torch.int64
 

二、构建简单的CNN网络

对于一般的CNN网络来说,都是由特征提取网络和分类网络构成,其中特征提取网络用于提取图片的特征,分类网络用于将图片进行分类。

In [8]:
import torch.nn.functional as F

class 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.drop1=nn.Dropout(p=0.2)
        self.fc1 = nn.Linear(24*50*50, 120)
        self.drop2=nn.Dropout(p=0.1)
        self.fc2 = nn.Linear(120, len(classname))

    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 = self.drop1(x)
        x = x.view(-1, 24*50*50)
        x = F.relu(self.fc1(x))
        x = self.drop2(x)
        x = self.fc2(x)

        return x

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

model = Network_bn().to(device)
model
 
Using cuda device
Out[8]:
Network_bn(
  (conv1): Conv2d(3, 12, kernel_size=(5, 5), stride=(1, 1))
  (bn1): BatchNorm2d(12, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (conv2): Conv2d(12, 12, kernel_size=(5, 5), stride=(1, 1))
  (bn2): BatchNorm2d(12, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (pool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
  (conv4): Conv2d(12, 24, kernel_size=(5, 5), stride=(1, 1))
  (bn4): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (conv5): Conv2d(24, 24, kernel_size=(5, 5), stride=(1, 1))
  (bn5): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (drop1): Dropout(p=0.2, inplace=False)
  (fc1): Linear(in_features=60000, out_features=120, bias=True)
  (drop2): Dropout(p=0.1, inplace=False)
  (fc2): Linear(in_features=120, out_features=2, bias=True)
)
 

三、 训练模型

1、设置超参数

In [9]:
loss_fn    = nn.CrossEntropyLoss() # 创建损失函数
learn_rate = 0.0005 # 学习率
opt        = torch.optim.SGD(model.parameters(),lr=learn_rate)
 

2、编写训练函数

In [10]:
# 训练循环
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与loss
        train_acc  += (pred.argmax(1) == y).type(torch.float).sum().item()
        train_loss += loss.item()
            
    train_acc  /= size
    train_loss /= num_batches

    return train_acc, train_loss
 

3、编写测试函数

In [11]:
def 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)
            
            # 计算loss
            target_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  /= size
    test_loss /= num_batches

    return test_acc, test_loss
 

4、正式训练

In [12]:
epochs     = 50
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')
 
Epoch: 1, Train_acc:62.9%, Train_loss:0.649, Test_acc:70.6%,Test_loss:0.593
Epoch: 2, Train_acc:69.8%, Train_loss:0.584, Test_acc:71.3%,Test_loss:0.585
Epoch: 3, Train_acc:72.7%, Train_loss:0.545, Test_acc:75.3%,Test_loss:0.514
Epoch: 4, Train_acc:78.1%, Train_loss:0.505, Test_acc:75.1%,Test_loss:0.503
Epoch: 5, Train_acc:79.2%, Train_loss:0.472, Test_acc:78.1%,Test_loss:0.464
Epoch: 6, Train_acc:81.6%, Train_loss:0.425, Test_acc:83.0%,Test_loss:0.427
Epoch: 7, Train_acc:83.3%, Train_loss:0.402, Test_acc:69.9%,Test_loss:0.610
Epoch: 8, Train_acc:85.5%, Train_loss:0.374, Test_acc:84.8%,Test_loss:0.383
Epoch: 9, Train_acc:86.9%, Train_loss:0.344, Test_acc:82.3%,Test_loss:0.433
Epoch:10, Train_acc:89.5%, Train_loss:0.316, Test_acc:84.4%,Test_loss:0.378
Epoch:11, Train_acc:90.4%, Train_loss:0.293, Test_acc:82.1%,Test_loss:0.406
Epoch:12, Train_acc:90.6%, Train_loss:0.282, Test_acc:87.6%,Test_loss:0.336
Epoch:13, Train_acc:91.0%, Train_loss:0.260, Test_acc:88.8%,Test_loss:0.314
Epoch:14, Train_acc:91.7%, Train_loss:0.256, Test_acc:85.3%,Test_loss:0.305
Epoch:15, Train_acc:93.6%, Train_loss:0.225, Test_acc:88.3%,Test_loss:0.292
Epoch:16, Train_acc:93.3%, Train_loss:0.217, Test_acc:88.1%,Test_loss:0.298
Epoch:17, Train_acc:94.0%, Train_loss:0.206, Test_acc:82.1%,Test_loss:0.378
Epoch:18, Train_acc:93.9%, Train_loss:0.202, Test_acc:89.5%,Test_loss:0.276
Epoch:19, Train_acc:94.8%, Train_loss:0.184, Test_acc:86.2%,Test_loss:0.301
Epoch:20, Train_acc:95.2%, Train_loss:0.174, Test_acc:86.9%,Test_loss:0.347
Epoch:21, Train_acc:96.4%, Train_loss:0.162, Test_acc:88.1%,Test_loss:0.286
Epoch:22, Train_acc:96.4%, Train_loss:0.157, Test_acc:89.3%,Test_loss:0.254
Epoch:23, Train_acc:97.3%, Train_loss:0.143, Test_acc:89.3%,Test_loss:0.256
Epoch:24, Train_acc:96.7%, Train_loss:0.137, Test_acc:89.3%,Test_loss:0.257
Epoch:25, Train_acc:96.6%, Train_loss:0.136, Test_acc:91.8%,Test_loss:0.256
Epoch:26, Train_acc:97.4%, Train_loss:0.124, Test_acc:90.7%,Test_loss:0.246
Epoch:27, Train_acc:97.1%, Train_loss:0.126, Test_acc:90.9%,Test_loss:0.255
Epoch:28, Train_acc:97.1%, Train_loss:0.122, Test_acc:90.4%,Test_loss:0.244
Epoch:29, Train_acc:97.8%, Train_loss:0.111, Test_acc:89.3%,Test_loss:0.248
Epoch:30, Train_acc:98.5%, Train_loss:0.101, Test_acc:90.4%,Test_loss:0.243
Epoch:31, Train_acc:97.9%, Train_loss:0.101, Test_acc:91.8%,Test_loss:0.230
Epoch:32, Train_acc:98.4%, Train_loss:0.096, Test_acc:89.7%,Test_loss:0.255
Epoch:33, Train_acc:98.5%, Train_loss:0.091, Test_acc:90.7%,Test_loss:0.231
Epoch:34, Train_acc:98.5%, Train_loss:0.087, Test_acc:91.6%,Test_loss:0.234
Epoch:35, Train_acc:98.7%, Train_loss:0.087, Test_acc:88.6%,Test_loss:0.265
Epoch:36, Train_acc:98.8%, Train_loss:0.080, Test_acc:89.7%,Test_loss:0.246
Epoch:37, Train_acc:98.8%, Train_loss:0.074, Test_acc:91.8%,Test_loss:0.221
Epoch:38, Train_acc:98.8%, Train_loss:0.074, Test_acc:90.2%,Test_loss:0.249
Epoch:39, Train_acc:98.9%, Train_loss:0.073, Test_acc:91.8%,Test_loss:0.248
Epoch:40, Train_acc:98.7%, Train_loss:0.071, Test_acc:91.6%,Test_loss:0.235
Epoch:41, Train_acc:99.1%, Train_loss:0.062, Test_acc:91.8%,Test_loss:0.214
Epoch:42, Train_acc:99.1%, Train_loss:0.064, Test_acc:91.6%,Test_loss:0.217
Epoch:43, Train_acc:99.2%, Train_loss:0.063, Test_acc:89.7%,Test_loss:0.240
Epoch:44, Train_acc:99.6%, Train_loss:0.056, Test_acc:92.3%,Test_loss:0.238
Epoch:45, Train_acc:99.3%, Train_loss:0.057, Test_acc:91.8%,Test_loss:0.232
Epoch:46, Train_acc:99.5%, Train_loss:0.054, Test_acc:91.1%,Test_loss:0.224
Epoch:47, Train_acc:99.2%, Train_loss:0.059, Test_acc:90.7%,Test_loss:0.228
Epoch:48, Train_acc:99.4%, Train_loss:0.049, Test_acc:91.8%,Test_loss:0.226
Epoch:49, Train_acc:99.6%, Train_loss:0.050, Test_acc:91.6%,Test_loss:0.226
Epoch:50, Train_acc:99.6%, Train_loss:0.046, Test_acc:91.6%,Test_loss:0.237
Done
 

四、结果可视化

1、Loss与Accuracy图

In [16]:
import matplotlib.pyplot as plt
#隐藏警告
import warnings
warnings.filterwarnings("ignore")               #忽略警告信息
plt.rcParams['font.sans-serif']    = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False      # 用来正常显示负号
plt.rcParams['figure.dpi']         = 100        #分辨率

epochs_range = range(epochs)

plt.figure(figsize=(12, 3))
plt.subplot(1, 2, 1)

plt.plot(epochs_range, train_acc, label='Training Accuracy')
plt.plot(epochs_range, test_acc, label='Test Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, train_loss, label='Training Loss')
plt.plot(epochs_range, test_loss, label='Test Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
 
 

2、数据维度处理

(1)torch.squeeze()详解

对数据的维度进行压缩,去掉维数为1的的维度

函数原型: torch.squeeze(input, dim=None, out=None)

关键参数说明

  • input (Tensor):输入Tensor

  • dim (int, optional):如果给定,输入将只在这个维度上被压缩

实战案例

In [14]:
>>> x = torch.zeros(2, 1, 2, 1, 2)
>>> x.size()
## torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x)
>>> y.size()
## torch.Size([2, 2, 2])
>>> y = torch.squeeze(x, 0)
>>> y.size()
## torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x, 1)
>>> y.size()
## torch.Size([2, 2, 1, 2])
Out[14]:
torch.Size([2, 2, 1, 2])
 

(2)torch.unsqueeze()

对数据维度进行扩充。给指定位置加上维数为一的维度

函数原型

torch.unsqueeze(input, dim)

关键参数说明

● input (Tensor):输入Tensor

● dim (int):插入单例维度的索引

In [15]:
>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
## tensor([[ 1,  2,  3,  4]])
>>> torch.unsqueeze(x, 1)
## tensor([[ 1],
        [ 2],
        [ 3],
        [ 4]])
 
  File "<ipython-input-15-1bdb034ef9aa>", line 6
    [ 2],
    ^
IndentationError: unexpected indent
 

3、指定图片进行预测

In [17]:
from PIL import Image 

classes = list(total_data.class_to_idx)

def predict_one_image(image_path, model, transform, classes):
    
    test_img = Image.open(image_path).convert('RGB')
    # plt.imshow(test_img)  # 展示预测的图片

    test_img = transform(test_img)
    img = test_img.to(device).unsqueeze(0)
    
    model.eval()
    output = model(img)

    _,pred = torch.max(output,1)
    pred_class = classes[pred]
    print(f'预测结果是:{pred_class}')
In [18]:
# 预测训练集中的某张照片
predict_one_image(image_path='../data/4-data/Monkeypox/M01_01_00.jpg', 
                  model=model, 
                  transform=train_transforms, 
                  classes=classes)
 
预测结果是:Monkeypox
 

五、保存并加载模型

In [19]:
# 模型保存
PATH = './model.pth'  # 保存的参数文件名
torch.save(model.state_dict(), PATH)

# 将参数加载到model当中
model.load_state_dict(torch.load(PATH, map_location=device))
Out[19]:
<All keys matched successfully>
posted @   CASTWJ  阅读(83)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示