Pytorch学习笔记-Advanced_CNN(Using Inception_Module)实现Mnist数据集分类-(注释及结果)

目录

程序代码

Inception框图

执行结果

​​​​​​​

程序代码

import torch
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.optim as optim

batch_size = 64
transform = transforms.Compose([
    transforms.ToTensor(), #将shape为(H, W, C)的img转为shape为(C, H, W)的tensor,将每一个数值归一化到[0,1]
    transforms.Normalize((0.1307, ), (0.3081, )) #按通道进行数据标准化
])

train_dataset = datasets.MNIST(root = '../Pycharm/dataset/mnist/', train = True, download = True, transform = transform)

train_loader = DataLoader(train_dataset, shuffle = True, batch_size = batch_size)

test_dataset = datasets.MNIST(root = '../Pycharm/dataset/mnist/', train = False, download = True, transform = transform)

test_loader = DataLoader(test_dataset, shuffle = False, batch_size = batch_size)

class InceptionA(torch.nn.Module):
    def __init__(self, in_channels):
        super(InceptionA, self).__init__()
        self.branch1x1 = torch.nn.Conv2d(in_channels, 16, kernel_size = 1) # 1x1卷积

        self.branch5x5_1 = torch.nn.Conv2d(in_channels, 16, kernel_size = 1) # 先1x1卷积
        self.branch5x5_2 = torch.nn.Conv2d(16, 24, kernel_size = 5, padding = 2) # 再5x5卷积 padding = 2是为了保证图像尺寸不变

        self.branch3x3_1 = torch.nn.Conv2d(in_channels, 16, kernel_size = 1) # 先1x1卷积
        self.branch3x3_2 = torch.nn.Conv2d(16, 24, kernel_size = 3, padding = 1) # 再3x3卷积
        self.branch3x3_3 = torch.nn.Conv2d(24, 24, kernel_size = 3, padding = 1) # # 再3x3卷积 注意输入输出维度大小

        self.branch_pool = torch.nn.Conv2d(in_channels, 24, kernel_size = 1) # 池化后再1x1卷积

    def forward(self, x):
        branch1x1 = self.branch1x1(x) # Module1

        branch5x5 = self.branch5x5_1(x) # Module2
        branch5x5 = self.branch5x5_2(branch5x5)

        branch3x3 = self.branch3x3_1(x) # Module3
        branch3x3 = self.branch3x3_2(branch3x3)
        branch3x3 = self.branch3x3_3(branch3x3)

        branch_pool = F.avg_pool2d(x, kernel_size = 3, stride = 1, padding = 1) # 平均池化
        branch_pool = self.branch_pool(branch_pool) # Module4 

        outputs = [branch1x1, branch5x5, branch3x3, branch_pool] 

        return torch.cat(outputs, dim = 1)


class Net(torch.nn.Module):
    def __init__(self):    # 构造函数
        super(Net, self).__init__()
        self.conv1 = torch.nn.Conv2d(1, 10, kernel_size = 5) # 卷积层1
        self.conv2 = torch.nn.Conv2d(88, 20, kernel_size = 5) # 卷积层2

        self.incep1 = InceptionA(in_channels = 10)
        self.incep2 = InceptionA(in_channels = 20)

        self.mp = torch.nn.MaxPool2d(2)
        self.fc = torch.nn.Linear(1408, 10)

    def forward(self, x):
        in_size = x.size(0)
        x = F.relu(self.mp(self.conv1(x))) # 卷积、池化、激活函数
        x = self.incep1(x)
        
        x = F.relu(self.mp(self.conv2(x))) # 卷积、池化、激活函数
        x = self.incep2(x)
        
        x = x.view(in_size, -1) # reshape
        x = self.fc(x) # 全连接层
        return x

model = Net()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # 调用GPU或CPU
model.to(device)

criterion = torch.nn.CrossEntropyLoss()  # 计算交叉熵损失
optimizer = optim.SGD(model.parameters(), lr = 0.01, momentum = 0.5) #构建优化器,lr为学习率,momentum为冲量因子

def train(epoch):
    running_loss = 0.0
    for batch_idx, data in enumerate(train_loader, 0): # 遍历函数,0表示从第0个元素开始,返回数据下标和数值
        inputs, target = data #特征,标签
        inputs, target = inputs.to(device), target.to(device)
        optimizer.zero_grad() #梯度归零

        # forward + backward + updata
        outputs = model(inputs)
        loss = criterion(outputs, target) #计算损失
        loss.backward() #反向传播梯度值
        optimizer.step() #更新参数

        running_loss += loss.item() #得到元素张量的一个元素值,将张量转换成浮点数
        if batch_idx % 300 == 299:
            print('[%d, %5d] loss: %.3f' % (epoch + 1, batch_idx + 1, running_loss / 300))
            running_loss = 0.0

def test():
    correct = 0
    total = 0
    with torch.no_grad():  #数据不计算梯度
        for data in test_loader:
            images, labels = data
            images, labels = images.to(device), labels.to(device)
            outputs = model(images)
            _, predicted = torch.max(outputs.data, dim = 1) #predicted为tensor每行最大值的索引
            total += labels.size(0) # 总样本
            correct += (predicted == labels).sum().item() #预测准确的样本数
    print('Accuracy on test set: %d %%' % (100 * correct / total)) #准确率

def main():
    for epoch in range(10):
        train(epoch)
        test()

main()

Inception框图

​​​​​​​

执行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值