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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| import torch import torch.nn as nn import torch.nn.functional as F
class BasicBlock(nn.Module): def __init__(self, in_channels, out_channels, stride=1): super(BasicBlock, self).__init__()
self.expansion = 1 self.downsample = nn.Sequential() if in_channels != out_channels*self.expansion: self.downsample = nn.Sequential( nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(out_channels, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True), )
self.basicblock = nn.Sequential( nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False), nn.BatchNorm2d(out_channels, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True), nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False), nn.BatchNorm2d(out_channels, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) ) def forward(self, x): identity = x identity = self.downsample(x)
x = self.basicblock(x) x += identity x = F.relu(x)
return x
class BottleneckBlock(nn.Module): def __init__(self, in_channels, out_channels, stride=1): super(BottleneckBlock, self).__init__()
self.expansion = 4 self.downsample = nn.Sequential() if in_channels != out_channels*self.expansion: self.downsample = nn.Sequential( nn.Conv2d(in_channels, out_channels*self.expansion, kernel_size=1, stride=stride, bias=False), nn.BatchNorm2d(out_channels*self.expansion, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True), )
self.bottleneckblock = nn.Sequential( nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, bias=False), nn.BatchNorm2d(out_channels, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True), nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False), nn.BatchNorm2d(out_channels, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True), nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels*self.expansion, kernel_size=1, stride=1, bias=False), nn.BatchNorm2d(out_channels*self.expansion, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True) ) def forward(self, x): identity = x identity = self.downsample(x)
x = self.bottleneckblock(x) x += identity x = F.relu(x)
return x class ResNet(nn.Module):
def __init__(self, modeltype=18, in_channels=3, n_class=2): super(ResNet, self).__init__() self.in_channels = 64 if modeltype == 18: self.expansion = 1 else: self.expansion = 4
self.in_layer = nn.Sequential( nn.Conv2d(in_channels=in_channels, out_channels=self.in_channels, kernel_size=7, stride=2, padding=3, bias=False), nn.BatchNorm2d(self.in_channels, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=3, stride=2, padding=1) )
self.layers = self._network(modeltype)
self.out_layer = nn.Sequential( nn.AdaptiveAvgPool2d((1, 1)), nn.Flatten(), nn.Linear(512*self.expansion, n_class) ) def _layer(self, out_channels, n_blocks, BlockType, stride): layers = []
layers.append(BlockType(self.in_channels, out_channels, stride=stride)) self.in_channels = out_channels*self.expansion
for i in range(1, n_blocks): layers.append(BlockType(self.in_channels, out_channels, stride=1))
return nn.Sequential(*layers) def _network(self, modeltype):
if modeltype == 18: layers = [self._layer(64, 2, BasicBlock, stride=1), self._layer(128, 2, BasicBlock, stride=2), self._layer(256, 2, BasicBlock, stride=2), self._layer(512, 2, BasicBlock, stride=2)] elif modeltype == 50: layers = [self._layer(64, 3, BottleneckBlock, stride=1), self._layer(128, 4, BottleneckBlock, stride=2), self._layer(256, 6, BottleneckBlock, stride=2), self._layer(512, 3, BottleneckBlock, stride=2)] elif modeltype == 152: layers = [self._layer(64, 3, BottleneckBlock, stride=1), self._layer(128, 8, BottleneckBlock, stride=2), self._layer(256, 36, BottleneckBlock, stride=2), self._layer(512, 3, BottleneckBlock, stride=2)]
return nn.Sequential(*layers) def forward(self, x):
x = self.in_layer(x) x = self.layers(x) x = self.out_layer(x)
return x
model = ResNet(modeltype=18) x = torch.randn(1, 3, 224, 224) print(model) print(model(x))
|