def __init__(self): super(DenseNet, self).__init__() # 卷积层部分 self.conv = nn.Sequential( nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) num_channels, growth_rate = 64, 32 # num_channels为当前的通道数 num_convs_in_dense_blocks = [4, 4, 4, 4] for i, num_convs in enumerate(num_convs_in_dense_blocks): DB = DenseBlock(num_convs, num_channels, growth_rate) self.conv.add_module("DenseBlosk_%d" % i, DB) # 上一个稠密块的输出通道数 num_channels = DB.out_channels # 在稠密块之间加入通道数减半的过渡层 if i != len(num_convs_in_dense_blocks) - 1: self.conv.add_module( "transition_block_%d" % i, self.transition_block(num_channels, num_channels // 2)) num_channels = num_channels // 2 self.conv.add_module("BN", nn.BatchNorm2d(num_channels)) self.conv.add_module("relu", nn.ReLU()) self.fc = nn.Sequential( utils.GlobalAvgPool2d(), utils.FlattenLayer(), nn.Linear(num_channels, 10)) # GlobalAvgPool2d的输出: (Batch, num_channels, 1, 1)
def __init__(self, ): super(ResNet, self).__init__() # 卷积层部分 self.conv = nn.Sequential( nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) self.conv.add_module("resnet_block1", self.resnet_block(64, 64, 2, first_block=True)) self.conv.add_module("resnet_block2", self.resnet_block(64, 128, 2)) self.conv.add_module("resnet_block3", self.resnet_block(128, 256, 2)) self.conv.add_module("resnet_block4", self.resnet_block(256, 512, 2)) # 全连接层部分 self.fc = nn.Sequential( utils.GlobalAvgPool2d(), utils.FlattenLayer(), nn.Linear(512, 10)) # GlobalAvgPool2d的输出: (Batch, 512, 1, 1)
nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) b3 = nn.Sequential(Inception(192, 64, (96, 128), (16, 32), 32), Inception(256, 128, (128, 192), (32, 96), 64), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) b4 = nn.Sequential(Inception(480, 192, (96, 208), (16, 48), 64), Inception(512, 160, (112, 224), (24, 64), 64), Inception(512, 128, (128, 256), (24, 64), 64), Inception(512, 112, (144, 288), (32, 64), 64), Inception(528, 256, (160, 320), (32, 128), 128), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) b5 = nn.Sequential(Inception(832, 256, (160, 320), (32, 128), 128), Inception(832, 384, (192, 384), (48, 128), 128), utils.GlobalAvgPool2d()) print('查看网络结构') net = nn.Sequential(b1, b2, b3, b4, b5, utils.FlattenLayer(), nn.Linear(1024, 10)) X = torch.rand(1, 1, 96, 96) for blk in net.children(): X = blk(X) print('output shape: ', X.shape) print('获取和读取数据,这里缩减尺寸为 96') batch_size = 256 train_iter, test_iter = utils.load_data_fashion_mnist(batch_size, resize=96) print('训练模型,只 1 轮')
stride=2)) else: blk.append(utils.Residual(out_channels, out_channels)) return nn.Sequential(*blk) net = nn.Sequential(nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3), nn.BatchNorm2d(64), nn.ReLU(), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) net.add_module("resnet_block1", resnet_block(64, 64, 2, first_block=True)) net.add_module("resnet_block2", resnet_block(64, 128, 2)) net.add_module("resnet_block3", resnet_block(128, 256, 2)) net.add_module("resent_block4", resnet_block(256, 512, 2)) net.add_module("global_avg_pool", utils.GlobalAvgPool2d()) net.add_module("fc", nn.Sequential(utils.FlattenLayer(), nn.Linear(512, 10))) # 训练模型 def train_with_data_aug(train_augs, test_augs, lr=0.001): batch_size = 256 optimizer = torch.optim.Adam(net.parameters(), lr=lr) loss = torch.nn.CrossEntropyLoss() train_iter = load_cifar10(True, train_augs, batch_size) test_iter = load_cifar10(False, test_augs, batch_size) utils.train(train_iter, test_iter, net, loss, optimizer,
for i, num_convs in enumerate(num_convs_in_dense_blocks): DB = DenseBlock(num_convs, num_channels, growth_rate) net.add_module("DenseBlosk_%d" % i, DB) # 上一个稠密块的输出通道数 num_channels = DB.out_channels # 在稠密块之间加入通道数减半的过渡层 if i != len(num_convs_in_dense_blocks) - 1: net.add_module("transition_block_%d" % i, transition_block(num_channels, num_channels // 2)) num_channels = num_channels // 2 net.add_module("BN", nn.BatchNorm2d(num_channels)) # 248 net.add_module("relu", nn.ReLU()) net.add_module( "global_avg_pool", d2l.GlobalAvgPool2d()) # GlobalAvgPool2d的输出: (Batch, num_channels, 1, 1) net.add_module("fc", nn.Sequential(d2l.FlattenLayer(), nn.Linear(num_channels, 10))) X = torch.rand((1, 1, 96, 96)) for name, layer in net.named_children(): X = layer(X) print(name, ' output shape:\t', X.shape) def load_data_fashion_mnist(batch_size, resize=None, root='input/FashionMNIST2065'): """Download the fashion mnist dataset and then load into memory.""" trans = [] if resize:
nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) b3 = nn.Sequential(Inception(192, 64, (96, 128), (16, 32), 32), Inception(256, 128, (128, 192), (32, 96), 64), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) b4 = nn.Sequential(Inception(480, 192, (96, 208), (16, 48), 64), Inception(512, 160, (112, 224), (24, 64), 64), Inception(512, 128, (128, 256), (24, 64), 64), Inception(512, 112, (144, 288), (32, 64), 64), Inception(528, 256, (160, 320), (32, 128), 128), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) b5 = nn.Sequential(Inception(832, 256, (160, 320), (32, 128), 128), Inception(832, 384, (192, 384), (48, 128), 128), d2l.GlobalAvgPool2d()) net = nn.Sequential(b1, b2, b3, b4, b5, d2l.FlattenLayer(), nn.Linear(1024, 10)) net = nn.Sequential(b1, b2, b3, b4, b5, d2l.FlattenLayer(), nn.Linear(1024, 10)) X = torch.rand(1, 1, 96, 96) for blk in net.children(): X = blk(X) print('output shape: ', X.shape) batch_size = 16 # 如出现“out of memory”的报错信息,可减小batch_size或resize
def nin_block(in_channels, out_channels, kernel_size, stride, padding): blk = nn.Sequential( nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding), nn.ReLU(), nn.Conv2d(out_channels, out_channels, kernel_size=1), nn.ReLU(), nn.Conv2d(out_channels, out_channels, kernel_size=1), nn.ReLU()) return blk net = nn.Sequential(nin_block(1, 96, kernel_size=11, stride=4, padding=0), nn.MaxPool2d(kernel_size=3, stride=2), nin_block(96, 256, kernel_size=5, stride=1, padding=2), nn.MaxPool2d(kernel_size=3, stride=2), nin_block(256, 384, kernel_size=3, stride=1, padding=1), nn.MaxPool2d(kernel_size=3, stride=2), nn.Dropout(0.5), nin_block(384, 10, kernel_size=3, stride=1, padding=1), utils.GlobalAvgPool2d(), utils.FlattenLayer()) X = torch.rand(1, 1, 224, 224) for name, blk in net.named_children(): X = blk(X) print(name, 'output shape: ', X.shape) # 训练模型 batch_size = 128 train_iter, test_iter = utils.load_data_fashion_mnist(batch_size, resize=224) lr, num_epochs = 0.002, 5 optimizer = torch.optim.Adam(net.parameters(), lr=lr) utils.train_ch5(net, train_iter, test_iter, batch_size, optimizer, device, num_epochs)
for i, num_convs in enumerate(num_convs_in_dense_block): DB = DenseBlock(num_convs, num_channels, growth_rate) net.add_module('DenseBlock_%d' % i, DB) # 上一个稠密块的输出通道数 num_channels = DB.out_channels # 在稠密块之间加入通道数减半的过渡层 if i != len(num_convs_in_dense_block) - 1: net.add_module('transition_block_%d' % i, transition_block(num_channels, num_channels // 2)) num_channels = num_channels // 2 # 加入全局平均池化和全连接 net.add_module('BN', nn.BatchNorm2d(num_channels)) net.add_module('relu', nn.ReLU()) net.add_module('global_avg_pool', utils.GlobalAvgPool2d()) net.add_module( 'fc', nn.Sequential(utils.FlattenLayer(), nn.Linear(num_channels, 10))) print('确保网络无误') X = torch.rand((1, 1, 96, 96)) for name, layer in net.named_children(): X = layer(X) print(name, 'output shape:\t', X.shape) print('获取和读取数据') batch_size = 256 train_iter, test_iter = utils.load_data_fashion_mnist(batch_size=batch_size, resize=96) print('训练模型')
assert in_channels == out_channels # 第一个模块的通道数同输入通道数一致 blk = [] for i in range(num_residuals): if i == 0 and not first_block: blk.append(Residual(in_channels, out_channels, use_1x1conv=True, stride=2)) else: blk.append(Residual(out_channels, out_channels)) return nn.Sequential(*blk) net.add_module("resnet_block1", resnet_block(64, 64, 2, first_block=True)) net.add_module("resnet_block2", resnet_block(64, 128, 2)) net.add_module("resnet_block3", resnet_block(128, 256, 2)) net.add_module("resnet_block4", resnet_block(256, 512, 2)) net.add_module("global_avg_pool", d2l.GlobalAvgPool2d()) # GlobalAvgPool2d的输出: (Batch, 512, 1, 1) net.add_module("fc", nn.Sequential(d2l.FlattenLayer(), nn.Linear(512, 10))) X = torch.rand((1, 1, 224, 224)) for name, layer in net.named_children(): X = layer(X) print(name, ' output shape:\t', X.shape) def load_data_fashion_mnist(batch_size, resize=None, root='input/FashionMNIST2065'): """Download the fashion mnist dataset and then load into memory.""" trans = [] if resize: trans.append(torchvision.transforms.Resize(size=resize)) trans.append(torchvision.transforms.ToTensor())