365天深度学习训练营-第P7周:咖啡豆识别

  • 本文为365天深度学习训练营 中的学习记录博客
  • 参考文章:Pytorch实战 | 第P7周:咖啡豆识别(训练营内部成员可读)
  • 原作者:K同学啊|接辅导、项目定制

目录

  • 一、课题背景和开发环境
    • 开发环境
  • 二、前期准备
    • 1.设置GPU
    • 2.导入数据并划分数据集
    • 3.加载数据及数据可视化
  • 三、手动搭建VGG-16模型
  • 四、训练模型
    • 1.编写训练函数
    • 2.编写测试函数
    • 3.正式训练&保存最优模型
  • 五、结果可视化
  • 六、加载模型&指定图片进行预测
  • 七、总结

一、课题背景和开发环境

第P7周:咖啡豆识别

  • 难度:夯实基础⭐⭐
  • 语言:Python3、Pytorch

要求:

  1. 自己搭建VGG-16网络框架
  2. 调用官方的VGG-16网络框架
  3. 如何查看模型的参数量以及相关指标

拔高(可选):

  1. 验证集准确率达到100%
  2. 使用PPT画出VGG-16算法框架图(发论文需要这项技能)

探索(难度有点大):

  1. 在不影响准确率的前提下轻量化模型
    – 目前VGG16的Total params是134,276,932

开发环境

  • 电脑系统:Windows 10
  • 语言环境:Python 3.8.2
  • 编译器:无(直接在cmd.exe内运行)
  • 深度学习环境:Pytorch
  • 显卡及显存:NVIDIA GeForce GTX 1660 Ti 12G
  • CUDA版本:Release 10.2, V10.2.89(cmd输入nvcc -Vnvcc --version指令可查看)
  • 数据:K同学啊的百度网盘

二、前期准备

1.设置GPU

如果设备上支持GPU就使用GPU,否则使用CPU

import torch
import torchvision

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

2.导入数据并划分数据集

''' 读取本地数据集并划分训练集与测试集 '''
def localDataset(data_dir):
    data_dir = pathlib.Path(data_dir)
    
    # 读取本地数据集
    data_paths = list(data_dir.glob('*'))
    classeNames = [str(path).split("\\")[1] for path in data_paths]
    
    # 关于transforms.Compose的更多介绍可以参考:https://blog.csdn.net/qq_38251616/article/details/124878863
    train_transforms = torchvision.transforms.Compose([
        torchvision.transforms.Resize([224, 224]),  # 将输入图片resize成统一尺寸
        # torchvision.transforms.RandomHorizontalFlip(), # 随机水平翻转
        torchvision.transforms.ToTensor(),          # 将PIL Image或numpy.ndarray转换为tensor,并归一化到[0,1]之间
        torchvision.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_dataset = torchvision.datasets.ImageFolder(data_dir,transform=train_transforms)
    print(total_dataset, '\n')
    print(total_dataset.class_to_idx, '\n')
    
    # 按比例划分训练集和测试集
    train_size = int(0.8 * len(total_dataset))
    test_size  = len(total_dataset) - train_size
    print('train_size', train_size, ', test_size', test_size, '\n')
    train_dataset, test_dataset = torch.utils.data.random_split(total_dataset, [train_size, test_size])
    
    return classeNames, train_dataset, test_dataset


if __name__=='__main__':
    ''' 加载数据 '''
    root = 'data'
    output = 'output'
    data_dir = os.path.join(root, '49-data')
    batch_size = 32
    classeNames, train_ds, test_ds = localDataset(data_dir)
    ''' 图片的类别数 '''
    num_classes = len(classeNames)
    print('num_classes {0}\n'.format(num_classes))
Dataset ImageFolder
    Number of datapoints: 1200
    Root location: data\49-data
    StandardTransform
Transform: Compose(
               Resize(size=[224, 224], interpolation=bilinear)
               ToTensor()
               Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
           )

{'Dark': 0, 'Green': 1, 'Light': 2, 'Medium': 3}

train_size 960 , test_size 240

num_classes 4

3.加载数据及数据可视化

''' 加载数据,并设置batch_size '''
def loadData(train_ds, test_ds, batch_size=32, root='', show_flag=False):
    # 从 train_ds 加载训练集
    train_dl = torch.utils.data.DataLoader(train_ds,
                                           batch_size=batch_size,
                                           shuffle=True,
                                           num_workers=1)
    # 从 test_ds 加载测试集
    test_dl  = torch.utils.data.DataLoader(test_ds,
                                           batch_size=batch_size,
                                           shuffle=True,
                                           num_workers=1)
    
    # 取一个批次查看数据格式
    # 数据的shape为:[batch_size, channel, height, weight]
    # 其中batch_size为自己设定,channel,height和weight分别是图片的通道数,高度和宽度。
    for X, y in test_dl:
        print('Shape of X [N, C, H, W]: ', X.shape)
        print('Shape of y: ', y.shape, y.dtype, '\n')
        break
    
    imgs, labels = next(iter(train_dl))
    print('Image shape: ', imgs.shape, '\n')
    # torch.Size([32, 3, 224, 224])  # 所有数据集中的图像都是224*224的RGB图
    displayData(imgs, root, show_flag)
    return train_dl, test_dl


''' 数据可视化 '''
def displayData(imgs, root='', flag=False):
    # 指定图片大小,图像大小为20宽、5高的绘图(单位为英寸inch)
    plt.figure('Data Visualization', figsize=(20, 5)) 
    for i, imgs in enumerate(imgs[:20]):
        # 维度顺序调整 [3, 224, 224]->[224, 224, 3]
        npimg = imgs.numpy().transpose((1, 2, 0))
        # 将整个figure分成2行10列,绘制第i+1个子图。
        plt.subplot(2, 10, i+1)
        plt.imshow(npimg)  # cmap=plt.cm.binary
        plt.axis('off')
    plt.savefig(os.path.join(root, 'DatasetDisplay.png'))
    if flag:
        plt.show()
    else:
        plt.close('all')


batch_size = 32
train_dl, test_dl = loadData(train_ds, test_ds, batch_size, root, True)
Shape of X [N, C, H, W]:  torch.Size([32, 3, 224, 224])
Shape of y:  torch.Size([32]) torch.int64

Image shape:  torch.Size([32, 3, 224, 224])

365天深度学习训练营-第P7周:咖啡豆识别_第1张图片


三、手动搭建VGG-16模型

VGG-16算法框架图
365天深度学习训练营-第P7周:咖啡豆识别_第2张图片

import torchsummary

class VGG16(nn.Module):
    def __init__(self):
        super(VGG16, self).__init__()
        # 卷积块1
        self.block1 = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
        )
        # 卷积块2
        self.block2 = nn.Sequential(
            nn.Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
        )
        # 卷积块3
        self.block3 = nn.Sequential(
            nn.Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
        )
        # 卷积块4
        self.block4 = nn.Sequential(
            nn.Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
        )
        # 卷积块5
        self.block5 = nn.Sequential(
            nn.Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=(2, 2), stride=(2, 2))
        )
        
        # 全连接网络层,用于分类
        self.classifier = nn.Sequential(
            nn.Linear(in_features=512*7*7, out_features=4096),
            nn.ReLU(),
            nn.Linear(in_features=4096, out_features=4096),
            nn.ReLU(),
            nn.Linear(in_features=4096, out_features=num_classes)
        )

    def forward(self, x):
        x = self.block1(x)      # 卷积-激活-卷积-激活-池化
        x = self.block2(x)      # 卷积-激活-卷积-激活-池化
        x = self.block3(x)      # 卷积-激活-卷积-激活-卷积-激活-池化
        x = self.block4(x)      # 卷积-激活-卷积-激活-卷积-激活-池化
        x = self.block5(x)      # 卷积-激活-卷积-激活-卷积-激活-池化
        x = torch.flatten(x, start_dim=1)
        x = self.classifier(x)  # 全链接

        return x


if __name__=='__main__':
    ''' 设置GPU '''
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print("Using {} device\n".format(device))
    
    ''' 调用并将模型转移到GPU中(我们模型运行均在GPU中进行) '''
    model = VGG16().to(device)
    ''' 显示网络结构 '''
    torchsummary.summary(model, (3, 224, 224))
    print(model)
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1         [-1, 64, 224, 224]           1,792
              ReLU-2         [-1, 64, 224, 224]               0
            Conv2d-3         [-1, 64, 224, 224]          36,928
              ReLU-4         [-1, 64, 224, 224]               0
         MaxPool2d-5         [-1, 64, 112, 112]               0
            Conv2d-6        [-1, 128, 112, 112]          73,856
              ReLU-7        [-1, 128, 112, 112]               0
            Conv2d-8        [-1, 128, 112, 112]         147,584
              ReLU-9        [-1, 128, 112, 112]               0
        MaxPool2d-10          [-1, 128, 56, 56]               0
           Conv2d-11          [-1, 256, 56, 56]         295,168
             ReLU-12          [-1, 256, 56, 56]               0
           Conv2d-13          [-1, 256, 56, 56]         590,080
             ReLU-14          [-1, 256, 56, 56]               0
           Conv2d-15          [-1, 256, 56, 56]         590,080
             ReLU-16          [-1, 256, 56, 56]               0
        MaxPool2d-17          [-1, 256, 28, 28]               0
           Conv2d-18          [-1, 512, 28, 28]       1,180,160
             ReLU-19          [-1, 512, 28, 28]               0
           Conv2d-20          [-1, 512, 28, 28]       2,359,808
             ReLU-21          [-1, 512, 28, 28]               0
           Conv2d-22          [-1, 512, 28, 28]       2,359,808
             ReLU-23          [-1, 512, 28, 28]               0
        MaxPool2d-24          [-1, 512, 14, 14]               0
           Conv2d-25          [-1, 512, 14, 14]       2,359,808
             ReLU-26          [-1, 512, 14, 14]               0
           Conv2d-27          [-1, 512, 14, 14]       2,359,808
             ReLU-28          [-1, 512, 14, 14]               0
           Conv2d-29          [-1, 512, 14, 14]       2,359,808
             ReLU-30          [-1, 512, 14, 14]               0
        MaxPool2d-31            [-1, 512, 7, 7]               0
           Linear-32                 [-1, 4096]     102,764,544
             ReLU-33                 [-1, 4096]               0
           Linear-34                 [-1, 4096]      16,781,312
             ReLU-35                 [-1, 4096]               0
           Linear-36                    [-1, 4]          16,388
================================================================
Total params: 134,276,932
Trainable params: 134,276,932
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 218.52
Params size (MB): 512.23
Estimated Total Size (MB): 731.32
----------------------------------------------------------------
VGG16(
  (block1): Sequential(
    (0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU()
    (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU()
    (4): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
  )
  (block2): Sequential(
    (0): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU()
    (2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU()
    (4): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
  )
  (block3): Sequential(
    (0): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU()
    (2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU()
    (4): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (5): ReLU()
    (6): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
  )
  (block4): Sequential(
    (0): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU()
    (2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU()
    (4): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (5): ReLU()
    (6): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
  )
  (block5): Sequential(
    (0): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (1): ReLU()
    (2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (3): ReLU()
    (4): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (5): ReLU()
    (6): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), padding=0, dilation=1, ceil_mode=False)
  )
  (classifier): Sequential(
    (0): Linear(in_features=25088, out_features=4096, bias=True)
    (1): ReLU()
    (2): Linear(in_features=4096, out_features=4096, bias=True)
    (3): ReLU()
    (4): Linear(in_features=4096, out_features=4, bias=True)
  )
)

四、训练模型

1.编写训练函数

optimizer.zero_grad()
loss.backward()
optimizer.step()
关于以上三个函数,我在之前的文章中有做说明,这里不再赘述

# 训练循环
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)  # 计算网络输出和真实值之间的差距,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

2.编写测试函数

测试函数和训练函数大致相同,但是由于不进行梯度下降对网络权重进行更新,所以不需要传入优化器

def test (dataloader, model, loss_fn):
    size        = len(dataloader.dataset)  # 测试集的大小
    num_batches = len(dataloader)          # 批次数目,(size/batch_size,向上取整)
    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

3.正式训练&保存最优模型

model.train()
model.eval()

关于以上两个个函数,我在之前的文章中有做说明,这里不再赘述

import time

''' 设置超参数 '''
start_epoch = 0
epochs      = 50
learn_rate  = 1e-4 # 初始学习率
loss_fn     = nn.CrossEntropyLoss()  # 创建损失函数
#optimizer   = torch.optim.SGD(model.parameters(), lr=learn_rate)
optimizer   = torch.optim.Adam(model.parameters(),lr=learn_rate)
# 调用官方动态学习率接口时使用
# lambda1 = lambda epoch: 0.92 ** (epoch // 4)
# scheduler   = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda1)  # 选定调整方法

train_loss  = []
train_acc   = []
test_loss   = []
test_acc    = []
epoch_best_acc = 0

''' 加载之前保存的模型 '''
if not os.path.exists(output) or not os.path.isdir(output):
    os.makedirs(output)
if start_epoch > 0:
    resumeFile = os.path.join(output, 'epoch'+str(start_epoch)+'.pkl')
    if not os.path.exists(resumeFile) or not os.path.isfile(resumeFile):
        start_epoch = 0
    else:
        model.load_state_dict(torch.load(resumeFile))  # 加载模型参数

''' 开始训练模型 '''
print('\nStart training...')
best_model = None
for epoch in range(start_epoch, epochs):
    # 更新学习率(使用自定义学习率时使用)
    # adjust_learning_rate(optimizer, epoch, learn_rate)
    
    model.train()
    epoch_train_acc, epoch_train_loss = train(train_dl, model, loss_fn, optimizer)
    # scheduler.step() # 更新学习率(调用官方动态学习率接口时使用)
    
    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)
    
    # 获取当前的学习率
    lr = optimizer.state_dict()['param_groups'][0]['lr']
    
    template = ('Epoch:{:2d}, Train_acc:{:.1f}%, Train_loss:{:.3f}, Test_acc:{:.1f}%, Test_loss:{:.3f}, Lr:{:.2E}')
    print(time.strftime('[%Y-%m-%d %H:%M:%S]'), template.format(epoch+1, epoch_train_acc*100, epoch_train_loss, epoch_test_acc*100, epoch_test_loss, lr))
    
    # 保存最佳模型
    if epoch_test_acc>epoch_best_acc:
        ''' 保存最优模型参数 '''
        epoch_best_acc = epoch_test_acc
        best_model = copy.deepcopy(model)
        print(('acc = {:.1f}%, saving model to best.pkl').format(epoch_best_acc*100))
        saveFile = os.path.join(output, 'best.pkl')
        torch.save(best_model.state_dict(), saveFile)
print('Done\n')
    
''' 保存最新模型参数 '''
saveFile = os.path.join(output, 'epoch'+str(epochs)+'.pkl')
torch.save(model.state_dict(), saveFile)

出现问题

程序运行时报错:
RuntimeError: CUDA out of memory. Tried to allocate 98.00 MiB (GPU 0; 6.00 GiB total capacity; 5.13 GiB already allocated; 0 bytes free; 5.23 GiB reserved in total by PyTorch)
显卡内存不足
调整 batch_size3216,即可正常运行。


使用 Adam 优化器的结果

Start training...
[2022-11-10 10:46:51] Epoch: 1, Train_acc:24.8%, Train_loss:1.388, Test_acc:23.8%, Test_loss:1.387, Lr:1.00E-04
acc = 23.8%, saving model to best.pkl
[2022-11-10 10:47:28] Epoch: 2, Train_acc:33.3%, Train_loss:1.314, Test_acc:55.8%, Test_loss:0.844, Lr:1.00E-04
acc = 55.8%, saving model to best.pkl
[2022-11-10 10:47:57] Epoch: 3, Train_acc:60.1%, Train_loss:0.794, Test_acc:71.7%, Test_loss:0.754, Lr:1.00E-04
acc = 71.7%, saving model to best.pkl
[2022-11-10 10:48:28] Epoch: 4, Train_acc:75.3%, Train_loss:0.562, Test_acc:86.2%, Test_loss:0.406, Lr:1.00E-04
acc = 86.2%, saving model to best.pkl
[2022-11-10 10:49:01] Epoch: 5, Train_acc:88.6%, Train_loss:0.266, Test_acc:93.8%, Test_loss:0.180, Lr:1.00E-04
acc = 93.8%, saving model to best.pkl
[2022-11-10 10:49:34] Epoch: 6, Train_acc:90.4%, Train_loss:0.271, Test_acc:89.2%, Test_loss:0.267, Lr:1.00E-04
[2022-11-10 10:49:58] Epoch: 7, Train_acc:95.5%, Train_loss:0.135, Test_acc:97.1%, Test_loss:0.062, Lr:1.00E-04
acc = 97.1%, saving model to best.pkl
[2022-11-10 10:50:59] Epoch: 8, Train_acc:95.3%, Train_loss:0.121, Test_acc:92.5%, Test_loss:0.157, Lr:1.00E-04
[2022-11-10 10:52:45] Epoch: 9, Train_acc:97.0%, Train_loss:0.081, Test_acc:98.8%, Test_loss:0.052, Lr:1.00E-04
acc = 98.8%, saving model to best.pkl
[2022-11-10 10:54:40] Epoch:10, Train_acc:91.0%, Train_loss:0.257, Test_acc:93.8%, Test_loss:0.175, Lr:1.00E-04
[2022-11-10 10:56:26] Epoch:11, Train_acc:97.4%, Train_loss:0.071, Test_acc:98.3%, Test_loss:0.046, Lr:1.00E-04
[2022-11-10 10:58:13] Epoch:12, Train_acc:97.8%, Train_loss:0.056, Test_acc:99.2%, Test_loss:0.031, Lr:1.00E-04
acc = 99.2%, saving model to best.pkl
[2022-11-10 11:00:07] Epoch:13, Train_acc:99.4%, Train_loss:0.033, Test_acc:98.3%, Test_loss:0.029, Lr:1.00E-04
[2022-11-10 11:01:54] Epoch:14, Train_acc:92.7%, Train_loss:0.177, Test_acc:95.8%, Test_loss:0.128, Lr:1.00E-04
[2022-11-10 11:03:40] Epoch:15, Train_acc:97.6%, Train_loss:0.079, Test_acc:92.9%, Test_loss:0.427, Lr:1.00E-04
[2022-11-10 11:05:27] Epoch:16, Train_acc:95.8%, Train_loss:0.160, Test_acc:98.3%, Test_loss:0.045, Lr:1.00E-04
[2022-11-10 11:07:14] Epoch:17, Train_acc:98.1%, Train_loss:0.053, Test_acc:91.7%, Test_loss:0.254, Lr:1.00E-04
[2022-11-10 11:09:00] Epoch:18, Train_acc:98.5%, Train_loss:0.037, Test_acc:97.1%, Test_loss:0.065, Lr:1.00E-04
[2022-11-10 11:10:47] Epoch:19, Train_acc:99.3%, Train_loss:0.025, Test_acc:98.8%, Test_loss:0.022, Lr:1.00E-04
[2022-11-10 11:12:33] Epoch:20, Train_acc:99.1%, Train_loss:0.017, Test_acc:98.8%, Test_loss:0.032, Lr:1.00E-04
[2022-11-10 11:14:20] Epoch:21, Train_acc:99.5%, Train_loss:0.013, Test_acc:97.9%, Test_loss:0.068, Lr:1.00E-04
[2022-11-10 11:16:06] Epoch:22, Train_acc:97.8%, Train_loss:0.087, Test_acc:98.3%, Test_loss:0.040, Lr:1.00E-04
[2022-11-10 11:17:53] Epoch:23, Train_acc:98.6%, Train_loss:0.041, Test_acc:98.8%, Test_loss:0.029, Lr:1.00E-04
[2022-11-10 11:19:39] Epoch:24, Train_acc:98.2%, Train_loss:0.050, Test_acc:99.2%, Test_loss:0.024, Lr:1.00E-04
[2022-11-10 11:21:25] Epoch:25, Train_acc:99.3%, Train_loss:0.019, Test_acc:98.8%, Test_loss:0.050, Lr:1.00E-04
[2022-11-10 11:23:11] Epoch:26, Train_acc:99.7%, Train_loss:0.006, Test_acc:97.5%, Test_loss:0.063, Lr:1.00E-04
[2022-11-10 11:24:58] Epoch:27, Train_acc:98.0%, Train_loss:0.054, Test_acc:98.3%, Test_loss:0.047, Lr:1.00E-04
[2022-11-10 11:26:44] Epoch:28, Train_acc:99.3%, Train_loss:0.029, Test_acc:97.5%, Test_loss:0.066, Lr:1.00E-04
[2022-11-10 11:28:31] Epoch:29, Train_acc:99.4%, Train_loss:0.014, Test_acc:98.3%, Test_loss:0.045, Lr:1.00E-04
[2022-11-10 11:30:18] Epoch:30, Train_acc:99.8%, Train_loss:0.009, Test_acc:97.9%, Test_loss:0.033, Lr:1.00E-04
[2022-11-10 11:32:05] Epoch:31, Train_acc:98.0%, Train_loss:0.046, Test_acc:100.0%, Test_loss:0.008, Lr:1.00E-04
acc = 100.0%, saving model to best.pkl
[2022-11-10 11:33:58] Epoch:32, Train_acc:97.7%, Train_loss:0.081, Test_acc:89.6%, Test_loss:0.327, Lr:1.00E-04
[2022-11-10 11:35:45] Epoch:33, Train_acc:98.1%, Train_loss:0.076, Test_acc:98.8%, Test_loss:0.035, Lr:1.00E-04
[2022-11-10 11:37:32] Epoch:34, Train_acc:99.8%, Train_loss:0.009, Test_acc:98.3%, Test_loss:0.053, Lr:1.00E-04
[2022-11-10 11:39:18] Epoch:35, Train_acc:99.0%, Train_loss:0.035, Test_acc:99.2%, Test_loss:0.030, Lr:1.00E-04
[2022-11-10 11:41:04] Epoch:36, Train_acc:97.8%, Train_loss:0.051, Test_acc:97.5%, Test_loss:0.065, Lr:1.00E-04
[2022-11-10 11:42:51] Epoch:37, Train_acc:98.6%, Train_loss:0.040, Test_acc:98.3%, Test_loss:0.080, Lr:1.00E-04
[2022-11-10 11:44:37] Epoch:38, Train_acc:98.1%, Train_loss:0.048, Test_acc:99.2%, Test_loss:0.023, Lr:1.00E-04
[2022-11-10 11:46:24] Epoch:39, Train_acc:98.9%, Train_loss:0.043, Test_acc:100.0%, Test_loss:0.008, Lr:1.00E-04
[2022-11-10 11:48:11] Epoch:40, Train_acc:99.1%, Train_loss:0.031, Test_acc:99.6%, Test_loss:0.011, Lr:1.00E-04
[2022-11-10 11:49:57] Epoch:41, Train_acc:98.6%, Train_loss:0.050, Test_acc:100.0%, Test_loss:0.010, Lr:1.00E-04
[2022-11-10 11:51:44] Epoch:42, Train_acc:96.9%, Train_loss:0.107, Test_acc:97.9%, Test_loss:0.065, Lr:1.00E-04
[2022-11-10 11:53:30] Epoch:43, Train_acc:99.3%, Train_loss:0.026, Test_acc:98.3%, Test_loss:0.072, Lr:1.00E-04
[2022-11-10 11:55:16] Epoch:44, Train_acc:99.9%, Train_loss:0.006, Test_acc:99.6%, Test_loss:0.008, Lr:1.00E-04
[2022-11-10 11:57:03] Epoch:45, Train_acc:99.0%, Train_loss:0.022, Test_acc:100.0%, Test_loss:0.004, Lr:1.00E-04
[2022-11-10 11:58:49] Epoch:46, Train_acc:99.8%, Train_loss:0.006, Test_acc:98.8%, Test_loss:0.032, Lr:1.00E-04
[2022-11-10 12:00:36] Epoch:47, Train_acc:100.0%, Train_loss:0.001, Test_acc:99.2%, Test_loss:0.015, Lr:1.00E-04
[2022-11-10 12:02:23] Epoch:48, Train_acc:99.8%, Train_loss:0.005, Test_acc:100.0%, Test_loss:0.004, Lr:1.00E-04
[2022-11-10 12:04:10] Epoch:49, Train_acc:100.0%, Train_loss:0.000, Test_acc:100.0%, Test_loss:0.003, Lr:1.00E-04
[2022-11-10 12:05:57] Epoch:50, Train_acc:100.0%, Train_loss:0.000, Test_acc:99.6%, Test_loss:0.005, Lr:1.00E-04
Done

365天深度学习训练营-第P7周:咖啡豆识别_第3张图片
最终结果,在第31轮时(Epoch:31的结果)的训练集准确率达到100.0%,测试集准确率达到98.0%
在第49轮时(Epoch:49的结果)的训练集准确率达到100.0%,测试集准确率达到100.0%


使用 SGD 优化器的结果

Start training...
[2022-11-10 12:41:04] Epoch: 1, Train_acc:24.8%, Train_loss:1.387, Test_acc:25.8%, Test_loss:1.386, Lr:1.00E-05
acc = 25.8%, saving model to best.pkl
[2022-11-10 12:41:34] Epoch: 2, Train_acc:24.8%, Train_loss:1.387, Test_acc:25.8%, Test_loss:1.386, Lr:1.00E-05
[2022-11-10 12:41:56] Epoch: 3, Train_acc:24.8%, Train_loss:1.387, Test_acc:25.8%, Test_loss:1.386, Lr:1.00E-05
[2022-11-10 12:42:19] Epoch: 4, Train_acc:24.8%, Train_loss:1.387, Test_acc:25.8%, Test_loss:1.386, Lr:1.00E-05
[2022-11-10 12:43:03] Epoch: 5, Train_acc:24.8%, Train_loss:1.387, Test_acc:25.8%, Test_loss:1.386, Lr:1.00E-05
...
[2022-11-10 13:03:01] Epoch: 50, Train_acc:24.8%, Train_loss:1.387, Test_acc:25.8%, Test_loss:1.386, Lr:1.00E-05
Done

365天深度学习训练营-第P7周:咖啡豆识别_第4张图片
改用SGD优化器后,模型完全不收敛


五、结果可视化

''' 结果可视化 '''
def displayResult(train_acc, test_acc, train_loss, test_loss, start_epoch, epochs, output=''):
    # 隐藏警告
    warnings.filterwarnings("ignore")                # 忽略警告信息
    plt.rcParams['font.sans-serif']    = ['SimHei']  # 用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False       # 用来正常显示负号
    plt.rcParams['figure.dpi']         = 100         # 分辨率
    
    epochs_range = range(start_epoch, epochs)
    
    plt.figure('Result Visualization', 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.savefig(os.path.join(output, 'AccuracyLoss.png'))
    plt.show()

''' 绘制准确率&损失率曲线图 '''
displayResult(train_acc, test_acc, train_loss, test_loss, start_epoch, epochs, output)

六、加载模型&指定图片进行预测

''' 预测函数 '''
def predict(model, img_path):
    img = Image.open(img_path)
    test_transforms = torchvision.transforms.Compose([
        torchvision.transforms.Resize([224, 224]),  # 将输入图片resize成统一尺寸
        torchvision.transforms.ToTensor(),          # 将PIL Image或numpy.ndarray转换为tensor,并归一化到[0,1]之间
        torchvision.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] 从数据集中随机抽样计算得到的。
    ])
    img = test_transforms(img)
    img = img.to(device).unsqueeze(0)
    output = model(img)
    #print(output.argmax(1))
    
    _, indices = torch.max(output, 1)
    percentage = torch.nn.functional.softmax(output, dim=1)[0] * 100
    perc = percentage[int(indices)].item()
    result = classeNames[indices]
    print('predicted:', result, perc)


if __name__=='__main__':
    classeNames = list({'Dark': 0, 'Green': 1, 'Light': 2, 'Medium': 3})
    num_classes = len(classeNames)
    
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print("Using {} device\n".format(device))
    
    model = VGG16().to(device)  # 加载自定义的VGG16模型
    model.load_state_dict(torch.load(os.path.join('output/adam', 'best.pkl')))
    model.eval()
    
    img_path = 'data/49-data/Dark/dark (7).png'
    predict(model, img_path)
Using cuda device

predicted: Dark 99.83885192871094

七、总结

轻量化模型思路:(时间问题,来不及做完该部分内容,下面简述以下我想法,后面有时间把结果再补上)

  1. 保持特征提取部分的网络不变,缩小FC层的大小
  2. 基于上一个思路的测试成果(如果训练效果不好则将FC层还原),依次对block5、block4、block3模块的卷积层的卷积核个数进行减少,并根据对应的测试训练效果来决定是否保留对网络的调整

你可能感兴趣的:(365天深度学习训练记录,深度学习,python,人工智能)