PyTorch 中级篇(2):深度残差网络(Deep Residual Networks)
参考代码
yunjey的 pytorch tutorial系列
深度残差网络 学习资源
论文原文
Deep Residual Learning for Image Recognition
Kaiming He的深度残差网络PPT
秒懂!何凯明的深度残差网络PPT是这样的|ICML2016 tutorial
Pytorch实现
根据原文【4.2. CIFAR-10 and Analysis】一节设计的针对数据集CIFAR-10的深度残差网络。
预处理
1 2 3 4 5
| import torch import torch.nn as nn import torchvision import torchvision.transforms as transforms
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| torch.cuda.set_device(1) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
num_epochs = 80 learning_rate = 0.001
transform = transforms.Compose([ transforms.Pad(4), transforms.RandomHorizontalFlip(), transforms.RandomCrop(32), transforms.ToTensor()])
|
CIFAR-10 数据集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| train_dataset = torchvision.datasets.CIFAR10(root='../../../data/cifar-10', train=True, transform=transform, download=True)
test_dataset = torchvision.datasets.CIFAR10(root='../../../data/cifar-10', train=False, transform=transforms.ToTensor())
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=100, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=100, shuffle=False)
|
Files already downloaded and verified
深度残差网络模型设计
3x3卷积层
1 2 3 4
| def conv3x3(in_channels, out_channels, stride=1): return nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)
|
残差块(残差单元)(Residual block)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 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) 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) if self.downsample: residual = self.downsample(x) out += residual out = self.relu(out) return out
|
残差网络搭建
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| 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) self.relu = nn.ReLU(inplace=True) self.layer1 = self.make_layer(block, 16, layers[0]) self.layer2 = self.make_layer(block, 32, layers[0], 2) self.layer3 = self.make_layer(block, 64, layers[1], 2) self.avg_pool = nn.AvgPool2d(8,ceil_mode=False) self.fc = nn.Linear(64, num_classes) def make_layer(self, block, out_channels, blocks, stride=1): downsample = None 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 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) out = self.bn(out) out = self.relu(out) out = self.layer1(out) out = self.layer2(out) out = self.layer3(out) out = self.avg_pool(out) out = out.view(out.size(0), -1) out = self.fc(out) return out
|
实例化模型
1 2 3 4 5 6 7 8 9 10 11
| model = ResNet(ResidualBlock, [2, 2, 2, 2]).to(device)
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
|
训练模型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| 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) outputs = model(images) loss = criterion(outputs, labels) optimizer.zero_grad() loss.backward() optimizer.step() if ((i+1) % 100 == 0) and ((epoch+1) % 5 == 0): print ("Epoch [{}/{}], Step [{}/{}] Loss: {:.4f}" .format(epoch+1, num_epochs, i+1, total_step, loss.item()))
if (epoch+1) % 20 == 0: curr_lr /= 3 update_lr(optimizer, curr_lr)
|
Epoch [5/80], Step [100/500] Loss: 0.6474
Epoch [5/80], Step [200/500] Loss: 0.7043
Epoch [5/80], Step [300/500] Loss: 0.7472
Epoch [5/80], Step [400/500] Loss: 0.6662
Epoch [5/80], Step [500/500] Loss: 0.6378
Epoch [10/80], Step [100/500] Loss: 0.5786
Epoch [10/80], Step [200/500] Loss: 0.7229
Epoch [10/80], Step [300/500] Loss: 0.6183
Epoch [10/80], Step [400/500] Loss: 0.5043
Epoch [10/80], Step [500/500] Loss: 0.5799
Epoch [15/80], Step [100/500] Loss: 0.5295
Epoch [15/80], Step [200/500] Loss: 0.4475
Epoch [15/80], Step [300/500] Loss: 0.3896
Epoch [15/80], Step [400/500] Loss: 0.4869
Epoch [15/80], Step [500/500] Loss: 0.4973
Epoch [20/80], Step [100/500] Loss: 0.3953
Epoch [20/80], Step [200/500] Loss: 0.4542
Epoch [20/80], Step [300/500] Loss: 0.4003
Epoch [20/80], Step [400/500] Loss: 0.3863
Epoch [20/80], Step [500/500] Loss: 0.2813
Epoch [25/80], Step [100/500] Loss: 0.3860
Epoch [25/80], Step [200/500] Loss: 0.4341
Epoch [25/80], Step [300/500] Loss: 0.3384
Epoch [25/80], Step [400/500] Loss: 0.1694
Epoch [25/80], Step [500/500] Loss: 0.2215
Epoch [30/80], Step [100/500] Loss: 0.2096
Epoch [30/80], Step [200/500] Loss: 0.1695
Epoch [30/80], Step [300/500] Loss: 0.2272
Epoch [30/80], Step [400/500] Loss: 0.2907
Epoch [30/80], Step [500/500] Loss: 0.1764
Epoch [35/80], Step [100/500] Loss: 0.2971
Epoch [35/80], Step [200/500] Loss: 0.2568
Epoch [35/80], Step [300/500] Loss: 0.1824
Epoch [35/80], Step [400/500] Loss: 0.1700
Epoch [35/80], Step [500/500] Loss: 0.2449
Epoch [40/80], Step [100/500] Loss: 0.0951
Epoch [40/80], Step [200/500] Loss: 0.2217
Epoch [40/80], Step [300/500] Loss: 0.2020
Epoch [40/80], Step [400/500] Loss: 0.1849
Epoch [40/80], Step [500/500] Loss: 0.1752
Epoch [45/80], Step [100/500] Loss: 0.3183
Epoch [45/80], Step [200/500] Loss: 0.4195
Epoch [45/80], Step [300/500] Loss: 0.2002
Epoch [45/80], Step [400/500] Loss: 0.1956
Epoch [45/80], Step [500/500] Loss: 0.1547
Epoch [50/80], Step [100/500] Loss: 0.2431
Epoch [50/80], Step [200/500] Loss: 0.1655
Epoch [50/80], Step [300/500] Loss: 0.0941
Epoch [50/80], Step [400/500] Loss: 0.2437
Epoch [50/80], Step [500/500] Loss: 0.1340
Epoch [55/80], Step [100/500] Loss: 0.2455
Epoch [55/80], Step [200/500] Loss: 0.1532
Epoch [55/80], Step [300/500] Loss: 0.1303
Epoch [55/80], Step [400/500] Loss: 0.1286
Epoch [55/80], Step [500/500] Loss: 0.2082
Epoch [60/80], Step [100/500] Loss: 0.2705
Epoch [60/80], Step [200/500] Loss: 0.1413
Epoch [60/80], Step [300/500] Loss: 0.1149
Epoch [60/80], Step [400/500] Loss: 0.1146
Epoch [60/80], Step [500/500] Loss: 0.1569
Epoch [65/80], Step [100/500] Loss: 0.1463
Epoch [65/80], Step [200/500] Loss: 0.1799
Epoch [65/80], Step [300/500] Loss: 0.1485
Epoch [65/80], Step [400/500] Loss: 0.1690
Epoch [65/80], Step [500/500] Loss: 0.2135
Epoch [70/80], Step [100/500] Loss: 0.1388
Epoch [70/80], Step [200/500] Loss: 0.1783
Epoch [70/80], Step [300/500] Loss: 0.1284
Epoch [70/80], Step [400/500] Loss: 0.1675
Epoch [70/80], Step [500/500] Loss: 0.2066
Epoch [75/80], Step [100/500] Loss: 0.1681
Epoch [75/80], Step [200/500] Loss: 0.0998
Epoch [75/80], Step [300/500] Loss: 0.1553
Epoch [75/80], Step [400/500] Loss: 0.1153
Epoch [75/80], Step [500/500] Loss: 0.1365
Epoch [80/80], Step [100/500] Loss: 0.1176
Epoch [80/80], Step [200/500] Loss: 0.2006
Epoch [80/80], Step [300/500] Loss: 0.1738
Epoch [80/80], Step [400/500] Loss: 0.1613
Epoch [80/80], Step [500/500] Loss: 0.2003
模型测试和保存
ResNet(
(conv): Conv2d(3, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace)
(layer1): Sequential(
(0): ResidualBlock(
(conv1): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace)
(conv2): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(1): ResidualBlock(
(conv1): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace)
(conv2): Conv2d(16, 16, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(layer2): Sequential(
(0): ResidualBlock(
(conv1): Conv2d(16, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace)
(conv2): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(16, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): ResidualBlock(
(conv1): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace)
(conv2): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(layer3): Sequential(
(0): ResidualBlock(
(conv1): Conv2d(32, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(32, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): ResidualBlock(
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(avg_pool): AvgPool2d(kernel_size=8, stride=8, padding=0)
(fc): Linear(in_features=64, out_features=10, bias=True)
)
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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))
|
Accuracy of the model on the test images: 88.24 %
1 2
| torch.save(model.state_dict(), 'resnet.ckpt')
|
Pytorch模型可视化
导出ONNX模型
1 2 3 4 5 6
| import torch.onnx
dummy_input =torch.randn(1, 3, 32, 32).cuda()
torch.onnx.export(model,dummy_input, 'resnet.onnx',verbose=True)
|
graph(%0 : Float(1, 3, 32, 32)
%1 : Float(16, 3, 3, 3)
%2 : Float(16)
%3 : Float(16)
%4 : Float(16)
%5 : Float(16)
%6 : Long()
%7 : Float(16, 16, 3, 3)
%8 : Float(16)
%9 : Float(16)
%10 : Float(16)
%11 : Float(16)
%12 : Long()
%13 : Float(16, 16, 3, 3)
%14 : Float(16)
%15 : Float(16)
%16 : Float(16)
%17 : Float(16)
%18 : Long()
%19 : Float(16, 16, 3, 3)
%20 : Float(16)
%21 : Float(16)
%22 : Float(16)
%23 : Float(16)
%24 : Long()
%25 : Float(16, 16, 3, 3)
%26 : Float(16)
%27 : Float(16)
%28 : Float(16)
%29 : Float(16)
%30 : Long()
%31 : Float(32, 16, 3, 3)
%32 : Float(32)
%33 : Float(32)
%34 : Float(32)
%35 : Float(32)
%36 : Long()
%37 : Float(32, 32, 3, 3)
%38 : Float(32)
%39 : Float(32)
%40 : Float(32)
%41 : Float(32)
%42 : Long()
%43 : Float(32, 16, 3, 3)
%44 : Float(32)
%45 : Float(32)
%46 : Float(32)
%47 : Float(32)
%48 : Long()
%49 : Float(32, 32, 3, 3)
%50 : Float(32)
%51 : Float(32)
%52 : Float(32)
%53 : Float(32)
%54 : Long()
%55 : Float(32, 32, 3, 3)
%56 : Float(32)
%57 : Float(32)
%58 : Float(32)
%59 : Float(32)
%60 : Long()
%61 : Float(64, 32, 3, 3)
%62 : Float(64)
%63 : Float(64)
%64 : Float(64)
%65 : Float(64)
%66 : Long()
%67 : Float(64, 64, 3, 3)
%68 : Float(64)
%69 : Float(64)
%70 : Float(64)
%71 : Float(64)
%72 : Long()
%73 : Float(64, 32, 3, 3)
%74 : Float(64)
%75 : Float(64)
%76 : Float(64)
%77 : Float(64)
%78 : Long()
%79 : Float(64, 64, 3, 3)
%80 : Float(64)
%81 : Float(64)
%82 : Float(64)
%83 : Float(64)
%84 : Long()
%85 : Float(64, 64, 3, 3)
%86 : Float(64)
%87 : Float(64)
%88 : Float(64)
%89 : Float(64)
%90 : Long()
%91 : Float(10, 64)
%92 : Float(10)) {
%93 : Float(1, 16, 32, 32) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%0, %1), scope: ResNet/Conv2d[conv]
%94 : Float(1, 16, 32, 32) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%93, %2, %3, %4, %5), scope: ResNet/BatchNorm2d[bn]
%95 : Float(1, 16, 32, 32) = onnx::Relu(%94), scope: ResNet/ReLU[relu]
%96 : Float(1, 16, 32, 32) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%95, %7), scope: ResNet/Sequential[layer1]/ResidualBlock[0]/Conv2d[conv1]
%97 : Float(1, 16, 32, 32) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%96, %8, %9, %10, %11), scope: ResNet/Sequential[layer1]/ResidualBlock[0]/BatchNorm2d[bn1]
%98 : Float(1, 16, 32, 32) = onnx::Relu(%97), scope: ResNet/Sequential[layer1]/ResidualBlock[0]/ReLU[relu]
%99 : Float(1, 16, 32, 32) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%98, %13), scope: ResNet/Sequential[layer1]/ResidualBlock[0]/Conv2d[conv2]
%100 : Float(1, 16, 32, 32) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%99, %14, %15, %16, %17), scope: ResNet/Sequential[layer1]/ResidualBlock[0]/BatchNorm2d[bn2]
%101 : Float(1, 16, 32, 32) = onnx::Add(%100, %95), scope: ResNet/Sequential[layer1]/ResidualBlock[0]
%102 : Float(1, 16, 32, 32) = onnx::Relu(%101), scope: ResNet/Sequential[layer1]/ResidualBlock[0]/ReLU[relu]
%103 : Float(1, 16, 32, 32) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%102, %19), scope: ResNet/Sequential[layer1]/ResidualBlock[1]/Conv2d[conv1]
%104 : Float(1, 16, 32, 32) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%103, %20, %21, %22, %23), scope: ResNet/Sequential[layer1]/ResidualBlock[1]/BatchNorm2d[bn1]
%105 : Float(1, 16, 32, 32) = onnx::Relu(%104), scope: ResNet/Sequential[layer1]/ResidualBlock[1]/ReLU[relu]
%106 : Float(1, 16, 32, 32) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%105, %25), scope: ResNet/Sequential[layer1]/ResidualBlock[1]/Conv2d[conv2]
%107 : Float(1, 16, 32, 32) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%106, %26, %27, %28, %29), scope: ResNet/Sequential[layer1]/ResidualBlock[1]/BatchNorm2d[bn2]
%108 : Float(1, 16, 32, 32) = onnx::Add(%107, %102), scope: ResNet/Sequential[layer1]/ResidualBlock[1]
%109 : Float(1, 16, 32, 32) = onnx::Relu(%108), scope: ResNet/Sequential[layer1]/ResidualBlock[1]/ReLU[relu]
%110 : Float(1, 32, 16, 16) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[2, 2]](%109, %31), scope: ResNet/Sequential[layer2]/ResidualBlock[0]/Conv2d[conv1]
%111 : Float(1, 32, 16, 16) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%110, %32, %33, %34, %35), scope: ResNet/Sequential[layer2]/ResidualBlock[0]/BatchNorm2d[bn1]
%112 : Float(1, 32, 16, 16) = onnx::Relu(%111), scope: ResNet/Sequential[layer2]/ResidualBlock[0]/ReLU[relu]
%113 : Float(1, 32, 16, 16) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%112, %37), scope: ResNet/Sequential[layer2]/ResidualBlock[0]/Conv2d[conv2]
%114 : Float(1, 32, 16, 16) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%113, %38, %39, %40, %41), scope: ResNet/Sequential[layer2]/ResidualBlock[0]/BatchNorm2d[bn2]
%115 : Float(1, 32, 16, 16) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[2, 2]](%109, %43), scope: ResNet/Sequential[layer2]/ResidualBlock[0]/Sequential[downsample]/Conv2d[0]
%116 : Float(1, 32, 16, 16) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%115, %44, %45, %46, %47), scope: ResNet/Sequential[layer2]/ResidualBlock[0]/Sequential[downsample]/BatchNorm2d[1]
%117 : Float(1, 32, 16, 16) = onnx::Add(%114, %116), scope: ResNet/Sequential[layer2]/ResidualBlock[0]
%118 : Float(1, 32, 16, 16) = onnx::Relu(%117), scope: ResNet/Sequential[layer2]/ResidualBlock[0]/ReLU[relu]
%119 : Float(1, 32, 16, 16) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%118, %49), scope: ResNet/Sequential[layer2]/ResidualBlock[1]/Conv2d[conv1]
%120 : Float(1, 32, 16, 16) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%119, %50, %51, %52, %53), scope: ResNet/Sequential[layer2]/ResidualBlock[1]/BatchNorm2d[bn1]
%121 : Float(1, 32, 16, 16) = onnx::Relu(%120), scope: ResNet/Sequential[layer2]/ResidualBlock[1]/ReLU[relu]
%122 : Float(1, 32, 16, 16) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%121, %55), scope: ResNet/Sequential[layer2]/ResidualBlock[1]/Conv2d[conv2]
%123 : Float(1, 32, 16, 16) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%122, %56, %57, %58, %59), scope: ResNet/Sequential[layer2]/ResidualBlock[1]/BatchNorm2d[bn2]
%124 : Float(1, 32, 16, 16) = onnx::Add(%123, %118), scope: ResNet/Sequential[layer2]/ResidualBlock[1]
%125 : Float(1, 32, 16, 16) = onnx::Relu(%124), scope: ResNet/Sequential[layer2]/ResidualBlock[1]/ReLU[relu]
%126 : Float(1, 64, 8, 8) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[2, 2]](%125, %61), scope: ResNet/Sequential[layer3]/ResidualBlock[0]/Conv2d[conv1]
%127 : Float(1, 64, 8, 8) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%126, %62, %63, %64, %65), scope: ResNet/Sequential[layer3]/ResidualBlock[0]/BatchNorm2d[bn1]
%128 : Float(1, 64, 8, 8) = onnx::Relu(%127), scope: ResNet/Sequential[layer3]/ResidualBlock[0]/ReLU[relu]
%129 : Float(1, 64, 8, 8) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%128, %67), scope: ResNet/Sequential[layer3]/ResidualBlock[0]/Conv2d[conv2]
%130 : Float(1, 64, 8, 8) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%129, %68, %69, %70, %71), scope: ResNet/Sequential[layer3]/ResidualBlock[0]/BatchNorm2d[bn2]
%131 : Float(1, 64, 8, 8) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[2, 2]](%125, %73), scope: ResNet/Sequential[layer3]/ResidualBlock[0]/Sequential[downsample]/Conv2d[0]
%132 : Float(1, 64, 8, 8) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%131, %74, %75, %76, %77), scope: ResNet/Sequential[layer3]/ResidualBlock[0]/Sequential[downsample]/BatchNorm2d[1]
%133 : Float(1, 64, 8, 8) = onnx::Add(%130, %132), scope: ResNet/Sequential[layer3]/ResidualBlock[0]
%134 : Float(1, 64, 8, 8) = onnx::Relu(%133), scope: ResNet/Sequential[layer3]/ResidualBlock[0]/ReLU[relu]
%135 : Float(1, 64, 8, 8) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%134, %79), scope: ResNet/Sequential[layer3]/ResidualBlock[1]/Conv2d[conv1]
%136 : Float(1, 64, 8, 8) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%135, %80, %81, %82, %83), scope: ResNet/Sequential[layer3]/ResidualBlock[1]/BatchNorm2d[bn1]
%137 : Float(1, 64, 8, 8) = onnx::Relu(%136), scope: ResNet/Sequential[layer3]/ResidualBlock[1]/ReLU[relu]
%138 : Float(1, 64, 8, 8) = onnx::Conv[dilations=[1, 1], group=1, kernel_shape=[3, 3], pads=[1, 1, 1, 1], strides=[1, 1]](%137, %85), scope: ResNet/Sequential[layer3]/ResidualBlock[1]/Conv2d[conv2]
%139 : Float(1, 64, 8, 8) = onnx::BatchNormalization[epsilon=1e-05, is_test=1, momentum=1](%138, %86, %87, %88, %89), scope: ResNet/Sequential[layer3]/ResidualBlock[1]/BatchNorm2d[bn2]
%140 : Float(1, 64, 8, 8) = onnx::Add(%139, %134), scope: ResNet/Sequential[layer3]/ResidualBlock[1]
%141 : Float(1, 64, 8, 8) = onnx::Relu(%140), scope: ResNet/Sequential[layer3]/ResidualBlock[1]/ReLU[relu]
%142 : Dynamic = onnx::Pad[mode=constant, pads=[0, 0, 0, 0, 0, 0, 0, 0], value=0](%141), scope: ResNet/AvgPool2d[avg_pool]
%143 : Float(1, 64, 1, 1) = onnx::AveragePool[kernel_shape=[8, 8], pads=[0, 0, 0, 0], strides=[8, 8]](%142), scope: ResNet/AvgPool2d[avg_pool]
%144 : Dynamic = onnx::Shape(%143), scope: ResNet
%145 : Dynamic = onnx::Slice[axes=[0], ends=[1], starts=[0]](%144), scope: ResNet
%146 : Long() = onnx::Squeeze[axes=[0]](%145), scope: ResNet
%147 : Long() = onnx::Constant[value={-1}](), scope: ResNet
%148 : Dynamic = onnx::Unsqueeze[axes=[0]](%146), scope: ResNet
%149 : Dynamic = onnx::Unsqueeze[axes=[0]](%147), scope: ResNet
%150 : Dynamic = onnx::Concat[axis=0](%148, %149), scope: ResNet
%151 : Float(1, 64) = onnx::Reshape(%143, %150), scope: ResNet
%152 : Float(1, 10) = onnx::Gemm[alpha=1, beta=1, broadcast=1, transB=1](%151, %91, %92), scope: ResNet/Linear[fc]
return (%152);
}
模型可视化工具:NETRON
有几种方式:
- 安装ONNX客户端
- ONNX有测试网页可以加载显示模型 :Netron
- 安装netron服务,可以通过
import netron
和 netron.start('model.onnx')
来启动本地查看服务,打开指定端口即可看到。
1 2 3
| import netron
netron.start('resnet.onnx')
|
Stopping http://localhost:8080
Serving 'resnet.onnx' at http://localhost:8080
模型可视化结果