def __init__(self, vgg_name='VGG19', in_channel=3, out_channel=10, drop_prob=0.0, block_size=5, forward='dropblock'): super(VGG2, self).__init__() # 修改了 make_layer 这个部分, 源码是在上面, 把中间分层处理 self.f1, self.f2, self.f3 = self._make_layers2(vggcfg[vgg_name], in_channel=in_channel) self.classifier = nn.Linear(512, out_channel) self.dropblock = LinearScheduler(DropBlock2D(drop_prob=drop_prob, block_size=block_size), start_value=0., stop_value=drop_prob, nr_steps=5e3) self.wh = 2 self.wh2 = 4 self.align_sche = False self.i = 0 # self.cr = CropAndResize(8, 8) # 8 is according to the real size self.cr = RoIAlign(self.wh, self.wh, transform_fpcoor=True) self.cr2 = RoIAlign(self.wh2, self.wh2, transform_fpcoor=True) # 注释掉 其中的一个, if forward == 'dropblock': self.forward = self._forward_dropblock print("------- VGG with Dropblock ---------\n") else: self.forward = self._forward_align print("------- VGG with ROiAlign ---------\n")
def __init__(self, block, layers, num_classes=1000, drop_prob=0., block_size=5): super(ResNet, self).__init__() self.inplanes = 64 self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False) self.bn1 = nn.BatchNorm2d(64) self.relu = nn.ReLU(inplace=True) self.dropblock = LinearScheduler(DropBlock2D(drop_prob=drop_prob, block_size=block_size), start_value=0., stop_value=drop_prob, nr_steps=5e3) 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) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(512 * block.expansion, num_classes) self.wh = 2 self.wh2 = 8 self.align_sche = False self.i = 0 # self.cr = CropAndResize(8, 8) # 8 is according to the real size self.cr = RoIAlign(self.wh, self.wh, transform_fpcoor=True) self.cr2 = RoIAlign(self.wh2, self.wh2, transform_fpcoor=True) print("--------------------------------------------------------" "\n-------- RoiAlign -------\n" "--------------------------------------------------------") for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, nn.BatchNorm2d): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0)