Exemplo n.º 1
0
    def __init__(self, builder:ConvBuilder, in_planes, planes, stride=1):
        super(BasicBlock, self).__init__()
        self.conv1 = builder.Conv2dBNReLU(in_channels=in_planes, out_channels=planes, kernel_size=3, stride=stride, padding=1)
        self.conv2 = builder.Conv2dBN(in_channels=planes, out_channels=self.expansion * planes, kernel_size=3, stride=1, padding=1)

        if stride != 1 or in_planes != self.expansion * planes:
            self.shortcut = builder.Conv2dBN(in_channels=in_planes, out_channels=self.expansion * planes, kernel_size=1, stride=stride)
        else:
            self.shortcut = builder.ResIdentity(num_channels=in_planes)
Exemplo n.º 2
0
    def __init__(self, builder: ConvBuilder, in_planes, stage_deps, stride=1):
        super(ResNetBottleneckStage, self).__init__()
        print('building stage: in {}, deps {}'.format(in_planes, stage_deps))
        assert (len(stage_deps) - 1) % 3 == 0
        self.num_blocks = (len(stage_deps) - 1) // 3
        stage_out_channels = stage_deps[3]
        for i in range(2, self.num_blocks):
            assert stage_deps[3 * i] == stage_out_channels

        self.relu = builder.ReLU()

        self.projection = builder.Conv2dBN(in_channels=in_planes,
                                           out_channels=stage_deps[0],
                                           kernel_size=1,
                                           stride=stride)
        self.align_opr = builder.ResNetAlignOpr(channels=stage_deps[0])

        for i in range(self.num_blocks):
            in_c = in_planes if i == 0 else stage_out_channels
            block_stride = stride if i == 0 else 1
            self.__setattr__(
                'block{}'.format(i),
                BottleneckBranch(builder=builder,
                                 in_channels=in_c,
                                 deps=stage_deps[1 + i * 3:4 + i * 3],
                                 stride=block_stride))
Exemplo n.º 3
0
    def __init__(self,  builder:ConvBuilder, inplanes, planes, stride=1, downsample=None):
        super(Bottleneck, self).__init__()
        # self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
        # self.bn1 = nn.BatchNorm2d(planes)
        self.conv1 = builder.Conv2dBN(inplanes, planes, kernel_size=1)

        # self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride,
        #                        padding=1, bias=False)
        # self.bn2 = nn.BatchNorm2d(planes)
        self.conv2 = builder.Conv2dBN(planes, planes, kernel_size=3, stride=stride, padding=1)

        # self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
        # self.bn3 = nn.BatchNorm2d(planes * 4)
        self.conv3 = builder.Conv2dBN(planes, planes * 4, kernel_size=1)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride
Exemplo n.º 4
0
    def __init__(self,
                 builder: ConvBuilder,
                 in_planes,
                 stage_deps,
                 stride=1,
                 is_first=False):
        super(ResNetBasicStage, self).__init__()
        print('building stage: in {}, deps {}'.format(in_planes, stage_deps))
        self.num_blocks = len(stage_deps) // 2

        stage_out_channels = stage_deps[0]
        for i in range(0, self.num_blocks):
            assert stage_deps[i * 2 + 2] == stage_out_channels

        if is_first:
            self.conv1 = builder.Conv2dBN(in_channels=in_planes,
                                          out_channels=stage_out_channels,
                                          kernel_size=3,
                                          stride=1,
                                          padding=1)
            # self.projection = builder.ResIdentity(num_channels=stage_out_channels)
        else:
            self.projection = builder.Conv2dBN(in_channels=in_planes,
                                               out_channels=stage_out_channels,
                                               kernel_size=1,
                                               stride=stride)

        self.relu = builder.ReLU()
        self.align_opr = builder.ResNetAlignOpr(channels=stage_out_channels)

        for i in range(self.num_blocks):
            if i == 0 and is_first:
                in_c = stage_deps[0]
            elif i == 0:
                in_c = in_planes
            else:
                in_c = stage_out_channels
            block_stride = stride if i == 0 else 1
            self.__setattr__(
                'block{}'.format(i),
                BasicBranch(builder=builder,
                            in_channels=in_c,
                            deps=stage_deps[1 + i * 2:3 + i * 2],
                            stride=block_stride))
Exemplo n.º 5
0
 def __init__(self, builder: ConvBuilder, in_channels, deps, stride=1):
     super(BottleneckBranch, self).__init__()
     assert len(deps) == 3
     self.conv1 = builder.Conv2dBNReLU(in_channels, deps[0], kernel_size=1)
     self.conv2 = builder.Conv2dBNReLU(deps[0],
                                       deps[1],
                                       kernel_size=3,
                                       stride=stride,
                                       padding=1)
     self.conv3 = builder.Conv2dBN(deps[1], deps[2], kernel_size=1)
Exemplo n.º 6
0
    def __init__(self, builder:ConvBuilder, resnet_type):
	
        resnet_spec = {#18: (BasicBlock, [2, 2, 2, 2], [64, 64, 128, 256, 512], 'resnet18'),
		       #34: (BasicBlock, [3, 4, 6, 3], [64, 64, 128, 256, 512], 'resnet34'),
		       50: (Bottleneck, [3, 4, 6, 3], [64, 256, 512, 1024, 2048], 'resnet50'),
		       101: (Bottleneck, [3, 4, 23, 3], [64, 256, 512, 1024, 2048], 'resnet101'),
		       152: (Bottleneck, [3, 8, 36, 3], [64, 256, 512, 1024, 2048], 'resnet152')}
        block, layers, channels, name = resnet_spec[resnet_type]
        
        self.name = name
        self.inplanes = 64

        super(ResNetBackbone, self).__init__()
        # self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,
        #                        bias=False)
        # self.bn1 = nn.BatchNorm2d(64)
        self.bd = builder
        self.conv1 = builder.Conv2dBN(3, 64, kernel_size=7, stride=2, padding=3)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)