卷积网络和残差网络

发布于:2024-12-06 ⋅ 阅读:(29) ⋅ 点赞:(0)

import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
# 设备配置
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# Hyper parameters
num_epochs = 5
num_classes = 10
batch_size = 100
learning_rate = 0.001
# 卷积神经网络
# CIFAR10 dataset
train_dataset = torchvision.datasets.CIFAR10(root='./datasets',
                                           train=True,
                                           transform=transforms.ToTensor(),
                                           download=True)

test_dataset = torchvision.datasets.CIFAR10(root='./datasets',
                                          train=False,
                                          transform=transforms.ToTensor())
# Data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=batch_size,
                                          shuffle=False)
img,label=next(iter(train_dataset))
aa=nn.Conv2d(1, 16, kernel_size=5, stride=1, padding=2)(img)
aa.shape
# 卷积神经网络 (两个卷积层)
class ConvNet(nn.Module):
    def __init__(self, num_classes=10):
        super(ConvNet, self).__init__()
        # 特征提取层1
        self.layer1 = nn.Sequential(
            # 因为核大小是5,不填充的话少两行两列,所以填充2,表示上下左右都填充两行(列)
            # 以保持尺寸不变
            nn.Conv2d(3, 16, kernel_size=5, stride=1, padding=2),
            nn.BatchNorm2d(16),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        # 特征提取层2
        self.layer2 = nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=2),
            nn.BatchNorm2d(32),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        # 输出层
        self.fc = nn.Linear(8*8*32, num_classes)
    def forward(self, x):
        out = self.layer1(x)  # (16,16,16)
        out = self.layer2(out) # (32,8,8)
        out = out.reshape(out.size(0), -1)
        out = self.fc(out)
        return out
model = ConvNet(num_classes).to(device)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train the model
total_step = len(train_loader) # 总批次数
for epoch in range(num_epochs):
    # 遍历数据加载器中的每个批次的数据
    for i, (images, labels) in enumerate(train_loader):
        images = images.to(device)
        labels = labels.to(device)
        # 前向传播,计算损失
        outputs = model(images)
        loss = criterion(outputs, labels)
        # 清理之前的梯度,反向传播,根据梯度更新参数
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if (i+1) % 100 == 0: # 打印日志
            print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'
                   .format(epoch+1, num_epochs, i+1, total_step, loss.item()))
# 评估模型
# 当模型被设置为评估模式时,其内部的Batch Normalization层会利用之前训练过程中积累下来的统计信息
# (移动平均值和方差)来进行归一化操作,而不是依据当前输入的具体小批量数据。这种做法有利于提高模型在
# 新数据上的泛化能力。
model.eval()
with torch.no_grad():
    correct = 0 # 用来统计模型预测正确的样本数
    total = 0 # 用来统计总样本数
    for images, labels in test_loader:
        images = images.to(device)
        labels = labels.to(device)
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
    print('Test Accuracy of the model on the 10000 test images: {} %'.format(100 * correct / total))
# Save the model checkpoint
torch.save(model.state_dict(), 'model.ckpt')
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Hyper-parameters
num_epochs = 60
batch_size = 100
learning_rate = 0.001
# 图像预处理模块(训练集上的增强)
transform = transforms.Compose([
    transforms.Pad(4),
    transforms.RandomHorizontalFlip(), # 随机水平翻转
    transforms.RandomCrop(32), # 随机裁剪
    transforms.ToTensor()]) # 0-1的归一化
# CIFAR-10 dataset
train_dataset = torchvision.datasets.CIFAR10(root='./datasets',
                                             train=True,
                                             transform=transform,
                                             download=True)

test_dataset = torchvision.datasets.CIFAR10(root='datasets',
                                            train=False,
                                            transform=transforms.ToTensor())
# Data loader
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True)

test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=batch_size,
                                          shuffle=False)
# 3x3 卷积,因为3x3的卷积,所以上下左右都设置为填充1,bias=False不计算截距
def conv3x3(in_channels, out_channels, stride=1):
    return nn.Conv2d(in_channels, out_channels, kernel_size=3,
                     stride=stride, padding=1, bias=False)
# 残差块
class ResidualBlock(nn.Module):
    def __init__(self, in_channels, out_channels, stride=1, downsample=None):
        super(ResidualBlock, self).__init__()
        self.conv1 = conv3x3(in_channels, out_channels, stride) # 3x3卷积
        self.bn1 = nn.BatchNorm2d(out_channels)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(out_channels, out_channels)
        self.bn2 = nn.BatchNorm2d(out_channels)
        self.downsample = downsample
    def forward(self, x):
        residual = x # 残差前段
        out = self.conv1(x) # 标准卷积
        out = self.bn1(out)
        out = self.relu(out)
        out = self.conv2(out)
        out = self.bn2(out)
        # 如果设置了downsample的话,就做下采样
        if self.downsample:
            residual = self.downsample(x)
        # 残差的两部分不会经过激活函数处理,这是为了维持卷积的线性
        out += residual # 残差连接
        out = self.relu(out)  # 残差后经过激活函数处理
        return out
m = nn.AvgPool2d((3, 2), stride=(2, 1))
input = torch.randn(20, 16, 50, 32)
output = m(input)
a=torch.randn(1,64,8,8)
b=nn.AvgPool2d(8)(a)
b.shape
# ResNet
class ResNet(nn.Module):
    def __init__(self, block, layers, num_classes=10):
        super(ResNet, self).__init__()
        self.in_channels = 16
        self.conv = conv3x3(3, 16)
        self.bn = nn.BatchNorm2d(16)
        # inplace=True:这个参数指定了是否进行就地操作。在就地操作模式下,ReLU直接修改输入张量的内容,
        # 而不是创建一个新的输出张量。这可以节省内存,因为不需要额外的空间来存储新的输出张量。但是,需要
        # 注意的是,就地操作可能会影响计算图的构建,特别是当你需要保留原始输入数据以供后续使用时。
        self.relu = nn.ReLU(inplace=True)
        self.layer1 = self.make_layer(block, 16, layers[0])
        self.layer2 = self.make_layer(block, 32, layers[1], 2)
        self.layer3 = self.make_layer(block, 64, layers[2], 2)
        self.avg_pool = nn.AvgPool2d(8)
        self.fc = nn.Linear(64, num_classes)
        
    def make_layer(self, block, out_channels, blocks, stride=1):
        downsample = None
        # 如果步长为2(内部要下采样)或者输入数据的通道数不等于最后的输出通道数
        if (stride != 1) or (self.in_channels != out_channels):
            # 设置残差前段转换块
            downsample = nn.Sequential(
                conv3x3(self.in_channels, out_channels, stride=stride),
                nn.BatchNorm2d(out_channels))
        layers = []
        # 添加第一个残差提取块
        layers.append(block(self.in_channels, out_channels, stride, downsample))
        self.in_channels = out_channels
        # 之后的提取块(不需要传入downsample),因为这时候输入输出通道相同,步长为1
        for i in range(1, blocks):
            layers.append(block(out_channels, out_channels))
        # 对列表进行拆包,构建多次提取块
        return nn.Sequential(*layers)
    # 整个前向传播可以看成嵌套复合函数
    def forward(self, x):
        out = self.conv(x) # 3-->16
        out = self.bn(out)
        out = self.relu(out)
        # 第一个特征提取块(不会改变特征图尺寸和通道)
        out = self.layer1(out)
        # 第二个提取块(改变特征图尺寸和通道)
        out = self.layer2(out) # 16-->32 (16,16)
        # 第三个提取块(改变特征图尺寸和通道)
        out = self.layer3(out) # 32-->64 (8,8)
        # 平均池化
        out = self.avg_pool(out)
        # 变形,把样本表示成向量形式
        out = out.view(out.size(0), -1)
        out = self.fc(out) # 输出层
        return out
# 第二个列表是残差块的提取深度列表
model = ResNet(ResidualBlock, [2, 2, 2]).to(device)
# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# 用于更新学习率
def update_lr(optimizer, lr):    
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr
# 训练模型
total_step = len(train_loader) #一个轮次的训练批次
curr_lr = learning_rate # 初始学习率
for epoch in range(num_epochs):
    # 遍历每个批次
    for i, (images, labels) in enumerate(train_loader):
        images = images.to(device)
        labels = labels.to(device)
        # 获取logits,计算预测和真实之间的误差
        outputs = model(images)
        loss = criterion(outputs, labels)
        # 清理之前梯度,反向传播,根据梯度更新模块参数
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        # 每割100次打印日志
        if (i+1) % 100 == 0:
            print ("Epoch [{}/{}], Step [{}/{}] Loss: {:.4f}"
                   .format(epoch+1, num_epochs, i+1, total_step, loss.item()))
    # 根据轮次改变学习率(每20个轮次)
    if (epoch+1) % 20 == 0:
        curr_lr /= 3
        update_lr(optimizer, curr_lr)
# 评估模式
model.eval()
# 禁用梯度是为了减少内存消耗
with torch.no_grad():
    correct = 0  # 统计预测正确的样本数
    total = 0 # 统计总样本数
    for images, labels in test_loader:
        images = images.to(device)
        labels = labels.to(device)
        outputs = model(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()
    print('Accuracy of the model on the test images: {} %'.format(100 * correct / total))
# Save the model checkpoint
torch.save(model.state_dict(), 'resnet.ckpt')