PyTorch深度学习实践(刘二大人)---第十一讲 卷积神经网络(高级篇)

 为了减少代码冗余,可以使用代码块,如函数、类等等

GoogleNet
代码
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision import transforms
import torch.nn.functional as F  # 激活函数relu
import torch.optim as optim


# # step1 准备数据集
batch_size = 64
transform = transforms.Compose([                                              # 将下列的内容构成pipline
                                transforms.ToTensor(),                        # 把原始数据转为张量   PIL图像0-255转为图像张量0-1
                                transforms.Normalize((0.1307, ), (0.3081, ))  # 归一化 做normalization, 前为均值,后为标准差  数据标准化
                                ])
train_dataset = datasets.MNIST(root='../dataset/mnist', train=True, download=False, transform=transform)
test_dataset = datasets.MNIST(root='../dataset/mnist', train=False, download=False, transform=transform)

train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)
test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size)


# # step2 模型GoogleNet
class Inception(nn.Module):
    def __init__(self, in_channels):
        super(Inception, self).__init__()
        self.branch_pool = nn.Conv2d(in_channels, 24, kernel_size=1)

        self.branch1x1 = nn.Conv2d(in_channels, 16, kernel_size=1)

        self.branch5x5_1 = nn.Conv2d(in_channels, 16, kernel_size=1)
        self.branch5x5_2 = nn.Conv2d(16, 24, kernel_size=5, padding=2)

        self.branch3x3_1 = nn.Conv2d(in_channels, 16, kernel_size=1)
        self.branch3x3_2 = nn.Conv2d(16, 24, kernel_size=3, padding=1)
        self.branch3x3_3 = nn.Conv2d(24, 24, kernel_size=3, padding=1)

    def forward(self, x):
        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
        branch_pool = self.branch_pool(branch_pool)

        branch1x1 = self.branch1x1(x)

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

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

        out = [branch1x1, branch5x5, branch3x3, branch_pool]
        return torch.cat(out, dim=1)  # b c w h  通道数:24+24+24+16=88


class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
        self.conv2 = nn.Conv2d(88, 20, kernel_size=5)

        self.cep1 = Inception(in_channels=10)
        self.cep2 = Inception(in_channels=20)

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

    def forward(self, x):
        in_size = x.size(0)
        x = F.relu(self.pool(self.conv1(x)))
        x = self.cep1(x)
        x = self.pool(F.relu(self.conv2(x)))
        x = self.cep2(x)
        x = x.view(in_size, -1)
        x = self.fc(x)
        return x


model = Model()
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')  # 使用GPU
model.to(device)   # 转为CUDA tensor

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)  # 冲量


# # step3 训练和测试
def train(epoch):
    running_loss = 0.0
    for batch_idx, data in enumerate(train_loader, 0):
        inputs, target = data
        inputs, target = inputs.to(device), target.to(device)
        optimizer.zero_grad()             # 优化器清零

        outputs = model(inputs)           # 前馈
        loss = criterion(outputs, target)
        loss.backward()                   # 反馈
        optimizer.step()                  # 更新

        running_loss += loss.item()
        if batch_idx % 300 == 299:        # 每300次输出一次loss
            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)                         # 输出是一个矩阵 10列
            _, predicted = torch.max(outputs.data, dim=1)  # 取出每一行的最大值,对应分类,返回最大值和最大值的下标
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
    print('Accuracy on test set: %d %%' % (100 * correct / total))


if __name__ == '__main__':
    for epoch in range(10):
        train(epoch)
        if epoch % 10 == 9:
            test()
结果图

 

 ResNet
代码
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision import transforms
import torch.nn.functional as F  # 激活函数relu
import torch.optim as optim


# # step1 准备数据集
batch_size = 64
transform = transforms.Compose([                                              # 将下列的内容构成pipline
                                transforms.ToTensor(),                        # 把原始数据转为张量   PIL图像0-255转为图像张量0-1
                                transforms.Normalize((0.1307, ), (0.3081, ))  # 归一化 做normalization, 前为均值,后为标准差  数据标准化
                                ])
train_dataset = datasets.MNIST(root='../dataset/mnist', train=True, download=False, transform=transform)
test_dataset = datasets.MNIST(root='../dataset/mnist', train=False, download=False, transform=transform)

train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)
test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size)


# # step2. 模型ResNet
class Residual(nn.Module):
    def __init__(self, channels):
        super(Residual, self).__init__()
        self.channel = channels
        self.conv1 = nn.Conv2d(channels, channels, kernel_size=3, padding=1)
        self.conv2 = nn.Conv2d(channels, channels, kernel_size=3, padding=1)

    def forward(self, x):
        y = F.relu(self.conv1(x))
        y = self.conv2(y)
        return F.relu(x + y)


class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(1, 16, kernel_size=5)
        self.conv2 = nn.Conv2d(16, 32, kernel_size=5)
        self.pool = nn.MaxPool2d(2)

        self.block1 = Residual(16)
        self.block2 = Residual(32)

        self.fc = nn.Linear(512, 10)

    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.block1(x)
        x = self.pool(F.relu(self.conv2(x)))
        x = self.block2(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)
        return x


model = Model()
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')  # 使用GPU
model.to(device)   # 转为CUDA tensor

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)  # 冲量


# # step3 训练和测试
def train(epoch):
    running_loss = 0.0
    for batch_idx, data in enumerate(train_loader, 0):
        inputs, target = data
        inputs, target = inputs.to(device), target.to(device)
        optimizer.zero_grad()             # 优化器清零

        outputs = model(inputs)           # 前馈
        loss = criterion(outputs, target)
        loss.backward()                   # 反馈
        optimizer.step()                  # 更新

        running_loss += loss.item()
        if batch_idx % 300 == 299:        # 每300次输出一次loss
            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)                         # 输出是一个矩阵 10列
            _, predicted = torch.max(outputs.data, dim=1)  # 取出每一行的最大值,对应分类,返回最大值和最大值的下标
            total += labels.size(0)
            correct += (predicted == labels).sum().item()
    print('Accuracy on test set: %d %%' % (100 * correct / total))


if __name__ == '__main__':
    for epoch in range(10):
        train(epoch)
        if epoch % 10 == 9:
            test()
结果图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值