def __init__(self, in_planes, planes, stride=1): super(Bottleneck, self).__init__() if stride != 1 or in_planes != self.expansion*planes: self.shortcut = conv_bn(in_planes, self.expansion*planes, kernel_size=1, stride=stride) else: self.shortcut = nn.Identity() self.conv1 = conv_bn_relu(in_planes, planes, kernel_size=1) self.conv2 = conv_bn_relu(planes, planes, kernel_size=3, stride=stride, padding=1) self.conv3 = conv_bn(planes, self.expansion*planes, kernel_size=1)
def create_stem(channels): stem = nn.Sequential() stem.add_module('conv1', conv_bn_relu(in_channels=3, out_channels=channels[0], kernel_size=11, stride=4, padding=2)) stem.add_module('maxpool1', nn.Maxpool2d(kernel_size=3, stride=2)) stem.add_module('conv2', conv_bn_relu(in_channels=channels[0], out_channels=channels[1], kernel_size=5, padding=2)) stem.add_module('maxpool2', nn.Maxpool2d(kernel_size=3, stride=2)) stem.add_module('conv3', conv_bn_relu(in_channels=channels[1], out_channels=channels[2], kernel_size=3, padding=1)) stem.add_module('conv4', conv_bn_relu(in_channels=channels[2], out_channels=channels[3], kernel_size=3, padding=1)) stem.add_module('conv5', conv_bn_relu(in_channels=channels[3], out_channels=channels[4], kernel_size=3, padding=1)) stem.add_module('maxpool3', nn.Maxpool2d(kernel_size=3, stride=2)) return stem
def __init__(self, block, num_blocks, num_classes=1000, width_multiplier=1): super(ResNet, self).__init__() self.in_planes = int(64 * width_multiplier) self.stage0 = nn.Sequential() self.stage0.add_module('conv1', conv_bn_relu(in_channels=3, out_channels=self.in_planes, kernel_size=7, stride=2, padding=3)) self.stage0.add_module('maxpool', nn.MaxPool2d(kernel_size=3, stride=2, padding=1)) self.stage1 = self._make_stage(block, int(64 * width_multiplier), num_blocks[0], stride=1) self.stage2 = self._make_stage(block, int(128 * width_multiplier), num_blocks[1], stride=2) self.stage3 = self._make_stage(block, int(256 * width_multiplier), num_blocks[2], stride=2) self.stage4 = self._make_stage(block, int(512 * width_multiplier), num_blocks[3], stride=2) self.gap = nn.AdaptiveAvgPool2d(output_size=1) self.linear = nn.Linear(int(512*block.expansion*width_multiplier), num_classes)
def __init__(self, num_classes): super(MobileV1, self).__init__() channels = MOBILE_CHANNELS assert len(channels) == 27 self.conv1 = conv_bn_relu(in_channels=3, out_channels=channels[0], kernel_size=3, stride=2, padding=1) blocks = [] for block_idx in range(13): depthwise_channels = int(channels[block_idx * 2 + 1]) pointwise_channels = int(channels[block_idx * 2 + 2]) stride = 2 if block_idx in [1, 3, 5, 11] else 1 blocks.append(MobileV1Block(in_planes=depthwise_channels, out_planes=pointwise_channels, stride=stride)) self.stem = nn.Sequential(*blocks) self.gap = nn.AdaptiveAvgPool2d(output_size=1) self.linear = nn.Linear(channels[-1], num_classes)
def __init__(self, in_planes, out_planes, stride=1): super(MobileV1Block, self).__init__() self.depthwise = conv_bn_relu(in_channels=in_planes, out_channels=in_planes, kernel_size=3, stride=stride, padding=1, groups=in_planes) self.pointwise = conv_bn_relu(in_channels=in_planes, out_channels=out_planes, kernel_size=1, stride=1, padding=0)