Week4 猴痘病识别

本次采用的数据集为网络上下载的天气图像数据集,。
本人电脑配置
Python 3.8.0
Pytorch 1.8.1
torchvision 1.8.1+ cuda10.2

前期准备

1. 设置GPU/CPU

本次是在cpu上对网络进行训练和测试,同样需要先识别设备。

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

2. 导入数据

下载数据到主目录的文件夹的week4data文件夹里,文件夹下为2类的子文件夹,每一个文件夹为一类,其中Monkeypox和others的样本分别有980和1162张图片。

在这里把每张图片resize到224*224,并对数据进行归一化。在划分训练集和测试集时比例为0.8:0.2。在对数据进行封装时依旧采用的是dataloader。

import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision
from torchvision import transforms, datasets
import os, PIL, pathlib
from torch.optim.lr_scheduler import ReduceLROnPlateau
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)

import os, PIL, random, pathlib
data_dir = 'week4data/'
data_dir = pathlib.Path(data_dir)
data_paths = list(data_dir.glob('*'))
classNames = [str(path).split("\\")[1] for path in data_paths]
print(classNames)
total_datadir = 'week4data/'
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)

# split the data 
train_size = int(0.8*len(total_data))
test_size = len(total_data)-train_size
train_datasets, test_dataset = torch.utils.data.random_split(total_data, [train_size, test_size])
batch_size = 32
train_dl = torch.utils.data.DataLoader(train_datasets,
                                       batch_size = batch_size,
                                       shuffle = True)
test_dl = torch.utils.data.DataLoader(test_dataset, 
                                      batch_size = batch_size, 
                                      shuffle = True)

构建网络模型

1. 搭建模型

构建含有四层卷积的卷积神经网络,其中用到了batch normalization的操作,BN层一般位于激活函数前,可以保证激活单元的非线性表达能力,缓解梯度消失问题。

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.fc1 = nn.Linear(24*50*50, len(classeNames))

    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 x

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

model = Network_bn().to(device)
model

3. 编写训练函数

设置损失函数,这里采用的交叉熵损失函数,设置优化器为SGD优化,同时在其中加入了动量,也是为了防止过拟合。

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = Network_bn().to(device)
# setting hyperparameters
loss_fn    = nn.CrossEntropyLoss() 
learning_rate = 1e-2
opt           = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=0.78)#add mommentum
scheduler=torch.optim.lr_scheduler.ExponentialLR(opt, gamma=0.95)
# train
def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    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)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        train_acc += (pred.argmax(1)==y).type(torch.float).sum().item()
        train_loss += loss.item()
    scheduler.step()
    train_acc /= size
    train_loss /= num_batches
    return train_acc, train_loss

4. 编写测试函数

当不进行训练时,停止梯度更新,节省计算内存消耗。

def test(dataloader, model, loss_fn):
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    test_loss, test_acc = 0, 0
    with torch.no_grad():
        for imgs, target in dataloader:
            imgs, target = imgs.to(device), target.to(device)
            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

5. 主函数

设置迭代epoch次数,这里设定为100,并记录训练误差、精度,测试误差、精度。同时为了保存模型,选取训练过程中测试集上精度最大的模型进行保存。

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')

结果总结

(1)采用原始网络进行训练得到的训练结果如下
在这里插入图片描述
训练过程如下

Epoch: 1, Train_acc:59.3%, Train_loss:0.686, Test_acc:69.7%,Test_loss:0.621
Epoch: 2, Train_acc:69.5%, Train_loss:0.594, Test_acc:61.8%,Test_loss:0.741
Epoch: 3, Train_acc:73.2%, Train_loss:0.548, Test_acc:71.3%,Test_loss:0.594
Epoch: 4, Train_acc:76.4%, Train_loss:0.495, Test_acc:73.7%,Test_loss:0.542
Epoch: 5, Train_acc:78.3%, Train_loss:0.473, Test_acc:75.3%,Test_loss:0.515
Epoch: 6, Train_acc:80.9%, Train_loss:0.440, Test_acc:75.8%,Test_loss:0.503
Epoch: 7, Train_acc:82.4%, Train_loss:0.419, Test_acc:77.6%,Test_loss:0.490
Epoch: 8, Train_acc:83.8%, Train_loss:0.399, Test_acc:77.9%,Test_loss:0.475
Epoch: 9, Train_acc:85.8%, Train_loss:0.375, Test_acc:78.8%,Test_loss:0.459
Epoch:10, Train_acc:86.7%, Train_loss:0.364, Test_acc:79.5%,Test_loss:0.463
Epoch:11, Train_acc:88.0%, Train_loss:0.348, Test_acc:76.9%,Test_loss:0.489
Epoch:12, Train_acc:87.9%, Train_loss:0.346, Test_acc:77.4%,Test_loss:0.482
Epoch:13, Train_acc:89.4%, Train_loss:0.325, Test_acc:78.8%,Test_loss:0.435
Epoch:14, Train_acc:89.5%, Train_loss:0.312, Test_acc:80.4%,Test_loss:0.420
Epoch:15, Train_acc:90.4%, Train_loss:0.298, Test_acc:82.3%,Test_loss:0.406
Epoch:16, Train_acc:90.9%, Train_loss:0.289, Test_acc:82.8%,Test_loss:0.406
Epoch:17, Train_acc:91.5%, Train_loss:0.281, Test_acc:81.1%,Test_loss:0.403
Epoch:18, Train_acc:90.6%, Train_loss:0.280, Test_acc:81.1%,Test_loss:0.409
Epoch:19, Train_acc:92.3%, Train_loss:0.265, Test_acc:82.8%,Test_loss:0.397
Epoch:20, Train_acc:92.5%, Train_loss:0.255, Test_acc:81.1%,Test_loss:0.413

(2)改变网络结构如下,并增加动量和学习率变化机制

class Network_bn(nn.Module):
    def __init__(self):
        super(Network_bn, self).__init__()
        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)#24*50*50
        self.bn5 = nn.BatchNorm2d(24)
        self.conv6 = nn.Conv2d(in_channels=24, out_channels=48, kernel_size=3, stride=1, padding=1)#48*50*50
        self.bn6 = nn.BatchNorm2d(48)
        self.conv7 = nn.Conv2d(in_channels=48, out_channels=48, kernel_size=3, stride=1, padding=1)#96*50*50
        self.bn7 = nn.BatchNorm2d(48)
        # self.fc1 = nn.Linear(24*50*50, len(classNames))
        self.fc1 = nn.Linear(48*25*25, len(classNames))

    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 = F.relu(self.bn6(self.conv6(x)))
        x = F.relu(self.bn7(self.conv7(x))) 
        x = self.pool(x)                       
        x = x.view(-1, 48*25*25)
        x = self.fc1(x)

        return x
opt           = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=0.78)#add mommentum
scheduler=torch.optim.lr_scheduler.ExponentialLR(opt, gamma=0.8)
# train
def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    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)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        train_acc += (pred.argmax(1)==y).type(torch.float).sum().item()
        train_loss += loss.item()
    scheduler.step()
    train_acc /= size
    train_loss /= num_batches
    return train_acc, train_loss

得到的训练结果如下所示
在这里插入图片描述
每个epoch的精度和损失具体值如下,最终可以达到88%的精度。

Epoch:10, Train_acc:93.9%, Train_loss:0.167, Test_acc:87.4%,Test_loss:0.304
Epoch:11, Train_acc:95.6%, Train_loss:0.150, Test_acc:87.9%,Test_loss:0.314
Epoch:12, Train_acc:95.0%, Train_loss:0.155, Test_acc:87.9%,Test_loss:0.305
Epoch:13, Train_acc:95.0%, Train_loss:0.155, Test_acc:87.9%,Test_loss:0.312
Epoch:14, Train_acc:95.7%, Train_loss:0.147, Test_acc:88.3%,Test_loss:0.317
Epoch:15, Train_acc:95.9%, Train_loss:0.138, Test_acc:88.3%,Test_loss:0.297
Epoch:16, Train_acc:96.1%, Train_loss:0.139, Test_acc:88.6%,Test_loss:0.293
Epoch:17, Train_acc:95.6%, Train_loss:0.137, Test_acc:88.6%,Test_loss:0.324
Epoch:18, Train_acc:96.1%, Train_loss:0.147, Test_acc:87.9%,Test_loss:0.317
Epoch:19, Train_acc:96.6%, Train_loss:0.131, Test_acc:88.6%,Test_loss:0.307
Epoch:20, Train_acc:96.0%, Train_loss:0.136, Test_acc:88.1%,Test_loss:0.299
。。。
Epoch:40, Train_acc:96.1%, Train_loss:0.134, Test_acc:87.9%,Test_loss:0.303
Epoch:41, Train_acc:96.1%, Train_loss:0.133, Test_acc:89.0%,Test_loss:0.303
Epoch:42, Train_acc:96.7%, Train_loss:0.137, Test_acc:88.1%,Test_loss:0.309
Epoch:43, Train_acc:96.1%, Train_loss:0.137, Test_acc:88.1%,Test_loss:0.308
Epoch:44, Train_acc:96.1%, Train_loss:0.130, Test_acc:88.1%,Test_loss:0.305
Epoch:45, Train_acc:96.1%, Train_loss:0.136, Test_acc:88.3%,Test_loss:0.305
Epoch:46, Train_acc:96.5%, Train_loss:0.130, Test_acc:88.1%,Test_loss:0.312
Epoch:47, Train_acc:95.8%, Train_loss:0.137, Test_acc:87.6%,Test_loss:0.299
Epoch:48, Train_acc:96.0%, Train_loss:0.137, Test_acc:89.3%,Test_loss:0.302
Epoch:49, Train_acc:95.4%, Train_loss:0.139, Test_acc:88.3%,Test_loss:0.295
Epoch:50, Train_acc:96.8%, Train_loss:0.129, Test_acc:88.3%,Test_loss:0.301

加载训练模型并测试任意一张图片

采用原始网络训练得到的模型进行测试,测试代码如下

import torch
import torch.nn as nn
import torchvision.transforms as transforms
import torchvision
from torchvision import transforms, datasets
import os, PIL, pathlib
from torch.optim.lr_scheduler import ReduceLROnPlateau
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)

import os, PIL, random, pathlib
data_dir = 'week4data/'
data_dir = pathlib.Path(data_dir)
data_paths = list(data_dir.glob('*'))
classNames = [str(path).split("\\")[1] for path in data_paths]
print(classNames)
total_datadir = 'week4data/'
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)

# # split the data 
# train_size = int(0.8*len(total_data))
# test_size = len(total_data)-train_size
# train_datasets, test_dataset = torch.utils.data.random_split(total_data, [train_size, test_size])
# batch_size = 32
# train_dl = torch.utils.data.DataLoader(train_datasets,
#                                        batch_size = batch_size,
#                                        shuffle = True)
# test_dl = torch.utils.data.DataLoader(test_dataset, 
#                                       batch_size = batch_size, 
#                                       shuffle = True)

# construct the network
import torch.nn.functional as F

class Network_bn(nn.Module):
    def __init__(self):
        super(Network_bn, self).__init__()
        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)#24*50*50
        self.bn5 = nn.BatchNorm2d(24)
        # self.conv6 = nn.Conv2d(in_channels=24, out_channels=48, kernel_size=3, stride=1, padding=1)#48*50*50
        # self.bn6 = nn.BatchNorm2d(48)
        # self.conv7 = nn.Conv2d(in_channels=48, out_channels=48, kernel_size=3, stride=1, padding=1)#96*50*50
        # self.bn7 = nn.BatchNorm2d(48)
        self.fc1 = nn.Linear(24*50*50, len(classNames))
        # self.fc1 = nn.Linear(48*25*25, len(classNames))

    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 = F.relu(self.bn6(self.conv6(x)))
        # x = F.relu(self.bn7(self.conv7(x))) 
        # x = self.pool(x)                       
        x = x.view(-1, 24*50*50)
        x = self.fc1(x)

        return x
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = Network_bn().to(device)
# setting hyperparameters
loss_fn    = nn.CrossEntropyLoss() 
learning_rate = 1e-4
opt           = torch.optim.SGD(model.parameters(), lr=learning_rate)#add mommentum
# train
def train(dataloader, model, loss_fn, optimizer):
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    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)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        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

def test(dataloader, model, loss_fn):
    size = len(dataloader.dataset)
    num_batches = len(dataloader)
    test_loss, test_acc = 0, 0
    with torch.no_grad():
        for imgs, target in dataloader:
            imgs, target = imgs.to(device), target.to(device)
            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
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = Network_bn().to(device)



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        #分辨率

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}')

test_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])
])
if __name__ == '__main__':
    image_path = 'week4data/Monkeypox/M01_01_00.jpg'
    # 模型保存
    PATH = './model_p4.pth'  # 保存的参数文件名
    # torch.save(model.state_dict(), PATH)
    # 将参数加载到model当中
    model.load_state_dict(torch.load(PATH, map_location=device))
    predict_one_image(image_path, model, test_transforms, classNames)

加载训练集中的第一张图片进行测试,得到正确结果为预测结果是:Monkeypox,图片示例如下
在这里插入图片描述
从网上下载图片进行测试,得到的结果为预测结果是:Others
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值