实战:利用pytorch搭建VGG-16实现从数据获取到模型训练的猫狗分类网络

在这里插入图片描述

在学习了卷积神经网络的理论基础和阅读了VGG的论文之后,对卷积有了大致的了解,但这都只是停留在理论上,动手实践更为重要,于是便开始了0基础学习pytorch、图像处理,搭建模型。
pytorch学习视频 https://www.bilibili.com/video/BV1hE411t7RN
代码参考https://blog.csdn.net/aa330233789/article/details/106411301
数据集来源https://www.cnblogs.com/xiximayou/p/12372969.html

  • 收获最大的是对于pycharm的了解更进一步,利用help 和 ?? 在python console 中查看帮助文档,是一个很实用的技巧,并且这也是离线操作,不会就查,函数的参数、参数的含义、数学原理等等一应俱全,配上Makedown的编辑器,绝了。
  • 惊讶于pytorch的封装能力,就从一个BP函数来说,简单的几个参数,包含了 L 2 L^2 L2正则化(权重衰减)、动态学习率调整,对于一个萌新来说,大概连原理也不要明白,只需要知道参数的作用便可以搭建网络。
  • 实际上这个实战还有很多地方没有完善,比如数据增强部分,数据可视化,模型的训练(没算力啥也不是),还有很多地方值得进一步挖掘。
  • 整体搭建一个类似的项目,虽然伤眼睛、废手,收获确实很大,对于Pytorch的理解比上一个星期的视频可要来得深刻,当然如果想要深入学习pytorch那又是另一个问题。

话不多说,细节全在注释中。
show the code
read_data.py

from torch.utils.data import Dataset
import cv2
import os
import numpy as np
import torch
from torchvision import transforms
class Mydata(Dataset):

    def __init__(self,img_path):# 用于设置类中的变量
        self.img_path=img_path
        self.img_list=os.listdir(img_path)
        print(self.img_list[0])

    def __getitem__(self, idx):
        img_name = self.img_list[idx]
        img_item_path = os.path.join(self.img_path,img_name)
        img = cv2.imread(img_item_path)
        img = cv2.resize(img,(224,224),interpolation=cv2.INTER_LINEAR)
        # 这里需要注意opencv独特图像存储方式
        trans = transforms.ToTensor()
        img = trans(img)
        #img = torch.from_numpy(img)
        #print(img.shape)
        label = 0
        if img_name[0]=='c':
            label=1
        return img,label

    def __len__(self):
        return len(self.img_list)

model

from torch import nn


class VGG16Net(nn.Module):# 继承父类nn.Module
    def __init__(self):
        super(VGG16Net,self).__init__()
        '''
        如果不用super,每次调用父类的方法是需要使用父类的名字
        使用super就避免这个麻烦
        super()实际上的含义远比这个要复杂。
        有兴趣可以通过这篇博客学习:https://blog.csdn.net/zhangjg_blog/article/details/83033210
        '''
        '''
        A sequential container.
        Modules will be added to it in the order they are passed in the
        constructor. Alternatively, an ``OrderedDict`` of modules can be
        passed in. The ``forward()`` method of ``Sequential`` accepts any
        input and forwards it to the first module it contains. It then
        "chains" outputs to inputs sequentially for each subsequent module,
        专业术语:一个有序的容器,神经网络模块将按照在传入构造器的顺序依次被添加到计算图中执行,同时以神经网络模块为元素的有序字典也可以作为传入参数
        这是一个有序模型容器,输入会按照顺序逐层通过每一模型,最终会返回最后一个模型的输出。
        实现原理:利用for循环 将所有的参数(即子模块)加入到self._module,然后在__call__中调用forward(),
        而forward()函数则会将self.module中的子模块推理一遍,返回值也就是最终结果。
        参考博客:https://blog.csdn.net/dss_dssssd/article/details/82980222 
        '''
        # 第一层,2个卷积层和一个最大池化层
        self.layer1 = nn.Sequential
  • 8
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
好的,首先我们需要安装PyTorch,可以通过以下命令安装: ``` pip install torch torchvision ``` 然后我们加载预训练VGG-16模型,可以使用以下代码: ```python import torch import torch.nn as nn import torchvision.models as models # 加载预训练VGG-16模型 vgg16 = models.vgg16(pretrained=True) ``` 接下来,我们需要对图像进行预处理,使其能够被VGG-16模型接受。VGG-16模型需要输入的图像大小为224x224,而且需要进行标准化处理。我们可以使用以下代码对图像进行预处理: ```python import torchvision.transforms as transforms # 预处理图像 transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) ``` 接下来,我们可以使用预处理后的图像作为输入,通过VGG-16模型进行前向传播得到预测结果。我们可以使用以下代码: ```python from PIL import Image # 加载图像 image = Image.open("image.jpg") # 预处理图像 image = transform(image) # 增加一个维度 image = image.unsqueeze(0) # 前向传播 output = vgg16(image) # 输出预测结果 print(output) ``` 其中,"image.jpg"是我们要分类的图像,可以根据需要替换为其他图像的路径。输出的结果是一个向量,表示图像属于1000个类别中的每一个类别的概率。我们可以使用以下代码获取最终的预测结果: ```python # 加载标签 with open("imagenet_classes.txt", "r") as f: categories = [s.strip() for s in f.readlines()] # 获取概率最大的类别 probs, indices = torch.topk(output, 5) for i in indices[0]: print(categories[i]) ``` 其中,"imagenet_classes.txt"是包含1000个类别标签的文件,可以在https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a下载到。输出的结果是概率最大的5个类别,我们可以根据需要修改输出的数量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值