def __init__(self, configer): super(PSPNet, self).__init__() self.configer = configer self.num_classes = self.configer.get('data', 'num_classes') self.backbone = BackboneSelector(configer).get_backbone() num_features = self.backbone.get_num_features() self.dsn = nn.Sequential( _ConvBatchNormReluBlock(num_features // 2, num_features // 4, 3, 1, bn_type=self.configer.get( 'network', 'bn_type')), nn.Dropout2d(0.1), nn.Conv2d(num_features // 4, self.num_classes, 1, 1, 0)) self.ppm = PPMBilinearDeepsup(fc_dim=num_features, bn_type=self.configer.get( 'network', 'bn_type')) self.cls = nn.Sequential( nn.Conv2d(num_features + 4 * 512, 512, kernel_size=3, padding=1, bias=False), ModuleHelper.BNReLU(512, bn_type=self.configer.get( 'network', 'bn_type')), nn.Dropout2d(0.1), nn.Conv2d(512, self.num_classes, kernel_size=1))
def __init__(self, configer): super(EmbedNet, self).__init__() self.configer = configer self.num_classes = self.configer.get('data', 'num_classes') self.backbone = BackboneSelector(configer).get_backbone() num_features = self.backbone.get_num_features() if 'caffe' in self.configer.get('network', 'backbone'): self.low_features = nn.Sequential( self.backbone.conv1, self.backbone.bn1, self.backbone.relu1, self.backbone.conv2, self.backbone.bn2, self.backbone.relu2, self.backbone.conv3, self.backbone.bn3, self.backbone.relu3, self.backbone.maxpool, self.backbone.layer1, ) else: self.low_features = nn.Sequential( self.backbone.conv1, self.backbone.bn1, self.backbone.relu, self.backbone.maxpool, self.backbone.layer1, ) self.high_features1 = nn.Sequential(self.backbone.layer2, self.backbone.layer3) self.high_features2 = nn.Sequential(self.backbone.layer4) self.embed_conv = EmbedModule(1024, bn_type=self.configer.get('network', 'bn_type')) self.decoder = PPMBilinearDeepsup(num_class=self.num_classes, fc_dim=num_features, bn_type=self.configer.get('network', 'bn_type'))
def __init__(self, configer): super(DenseASPP, self).__init__() self.configer = configer dropout0 = 0.1 dropout1 = 0.1 self.backbone = BackboneSelector(configer).get_backbone() num_features = self.backbone.get_num_features() self.trans = _Transition(num_input_features=self.num_features, num_output_features=self.num_features // 2, bn_type=self.configer.get('network', 'bn_type')) self.num_features = self.num_features // 2 self.ASPP_3 = _DenseAsppBlock(input_num=num_features, num1=256, num2=64, dilation_rate=3, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) self.ASPP_6 = _DenseAsppBlock(input_num=num_features + 64 * 1, num1=256, num2=64, dilation_rate=6, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) self.ASPP_12 = _DenseAsppBlock(input_num=num_features + 64 * 2, num1=256, num2=64, dilation_rate=12, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) self.ASPP_18 = _DenseAsppBlock(input_num=num_features + 64 * 3, num1=256, num2=64, dilation_rate=18, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) self.ASPP_24 = _DenseAsppBlock(input_num=num_features + 64 * 4, num1=256, num2=64, dilation_rate=24, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) num_features = num_features + 5 * 64 self.classification = nn.Sequential( nn.Dropout2d(p=dropout1), nn.Conv2d(in_channels=num_features, out_channels=self.configer.get('network', 'out_channels'), kernel_size=1, padding=0) ) self.upsample = nn.Sequential(nn.Upsample(scale_factor=8, mode='bilinear', align_corners=True)) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_uniform_(m.weight.data) elif isinstance(m, ModuleHelper.BatchNorm2d(bn_type=self.configer.get('network', 'bn_type'))): m.weight.data.fill_(1) m.bias.data.zero_()
def __init__(self, configer): super(DeepLabV3, self).__init__() self.configer = configer self.backbone = BackboneSelector(configer).get_backbone() self.backbone = nn.Sequential( self.backbone.conv1, self.backbone.bn1, self.backbone.relu1, self.backbone.conv2, self.backbone.bn2, self.backbone.relu2, self.backbone.conv3, self.backbone.bn3, self.backbone.relu3, self.backbone.maxpool, self.backbone.layer1, self.backbone.layer2, self.backbone.layer3) self.MG_features = _ResidualBlockMulGrid( inplanes=1024, midplanes=512, outplanes=2048, stride=1, dilation=2, mulgrid=self.configer.get('network', 'multi_grid'), bn_type=self.configer.get('network', 'bn_type')) pyramids = [6, 12, 18] self.aspp = _ASPPModule(2048, 256, pyramids, bn_type=self.configer.get( 'network', 'bn_type')) self.fc1 = nn.Sequential( nn.Conv2d(1280, 256, kernel_size=1), # 256 * 5 = 1280 ModuleHelper.BatchNorm2d( bn_type=self.configer.get('network', 'bn_type'))(256)) self.fc2 = nn.Conv2d(256, self.configer.get('data', 'num_classes'), kernel_size=1)
def __init__(self, configer): self.inplanes = 128 super(DeepLabV3, self).__init__() self.configer = configer self.num_classes = self.configer.get('data', 'num_classes') self.backbone = BackboneSelector(configer).get_backbone() self.head = nn.Sequential( ASPPModule(2048, bn_type=self.configer.get('network', 'bn_type')), nn.Conv2d(512, self.num_classes, kernel_size=1, stride=1, padding=0, bias=True)) self.dsn = nn.Sequential( nn.Conv2d(1024, 512, kernel_size=3, stride=1, padding=1), ModuleHelper.BNReLU(512, bn_type=self.configer.get( 'network', 'bn_type')), nn.Dropout2d(0.1), nn.Conv2d(512, self.num_classes, kernel_size=1, stride=1, padding=0, bias=True))
def __init__(self, configer): super(DarkNetYolov3, self).__init__() self.configer = configer self.backbone = BackboneSelector(configer).get_backbone() self.yolov3_head = Yolov3Head(configer, out_filters=self.backbone.num_features) self.yolo_detection_layer = YOLODetectionLayer(self.configer) self.yolov3_loss = YOLOv3Loss(self.configer)
def __init__(self, configer): super(DenseASPP, self).__init__() self.configer = configer dropout0 = MODEL_CONFIG['dropout0'] dropout1 = MODEL_CONFIG['dropout1'] d_feature0 = MODEL_CONFIG['d_feature0'] d_feature1 = MODEL_CONFIG['d_feature1'] self.backbone = BackboneSelector(configer).get_backbone() num_features = self.backbone.get_num_features() self.trans = _Transition(num_input_features=self.num_features, num_output_features=self.num_features // 2, norm_type=self.configer.get('network', 'norm_type')) self.num_features = self.num_features // 2 self.ASPP_3 = _DenseAsppBlock(input_num=num_features, num1=d_feature0, num2=d_feature1, dilation_rate=3, drop_out=dropout0, norm_type=self.configer.get('network', 'norm_type')) self.ASPP_6 = _DenseAsppBlock(input_num=num_features + d_feature1 * 1, num1=d_feature0, num2=d_feature1, dilation_rate=6, drop_out=dropout0, norm_type=self.configer.get('network', 'norm_type')) self.ASPP_12 = _DenseAsppBlock(input_num=num_features + d_feature1 * 2, num1=d_feature0, num2=d_feature1, dilation_rate=12, drop_out=dropout0, norm_type=self.configer.get('network', 'norm_type')) self.ASPP_18 = _DenseAsppBlock(input_num=num_features + d_feature1 * 3, num1=d_feature0, num2=d_feature1, dilation_rate=18, drop_out=dropout0, norm_type=self.configer.get('network', 'norm_type')) self.ASPP_24 = _DenseAsppBlock(input_num=num_features + d_feature1 * 4, num1=d_feature0, num2=d_feature1, dilation_rate=24, drop_out=dropout0, norm_type=self.configer.get('network', 'norm_type')) num_features = num_features + 5 * d_feature1 self.classification = nn.Sequential( nn.Dropout2d(p=dropout1), nn.Conv2d(num_features, self.configer.get('data', 'num_classes'), kernel_size=1, padding=0) )
def __init__(self, configer): super(DenseASPP, self).__init__() self.configer = configer dropout0 = 0.1 dropout1 = 0.1 self.backbone = BackboneSelector(configer).get_backbone() num_features = self.backbone.get_num_features() self.trans = _Transition(num_input_features=self.num_features, num_output_features=self.num_features // 2, bn_type=self.configer.get('network', 'bn_type')) self.num_features = self.num_features // 2 self.ASPP_3 = _DenseAsppBlock(input_num=num_features, num1=256, num2=64, dilation_rate=3, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) self.ASPP_6 = _DenseAsppBlock(input_num=num_features + 64 * 1, num1=256, num2=64, dilation_rate=6, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) self.ASPP_12 = _DenseAsppBlock(input_num=num_features + 64 * 2, num1=256, num2=64, dilation_rate=12, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) self.ASPP_18 = _DenseAsppBlock(input_num=num_features + 64 * 3, num1=256, num2=64, dilation_rate=18, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) self.ASPP_24 = _DenseAsppBlock(input_num=num_features + 64 * 4, num1=256, num2=64, dilation_rate=24, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) num_features = num_features + 5 * 64 self.classification = nn.Sequential( nn.Dropout2d(p=dropout1), nn.Conv2d(in_channels=num_features, out_channels=self.configer.get('network', 'out_channels'), kernel_size=1, padding=0) )
class EmbedNet(nn.Sequential): def __init__(self, configer): super(EmbedNet, self).__init__() self.configer = configer self.num_classes = self.configer.get('data', 'num_classes') self.backbone = BackboneSelector(configer).get_backbone() num_features = self.backbone.get_num_features() if 'caffe' in self.configer.get('network', 'backbone'): self.low_features = nn.Sequential( self.backbone.conv1, self.backbone.bn1, self.backbone.relu1, self.backbone.conv2, self.backbone.bn2, self.backbone.relu2, self.backbone.conv3, self.backbone.bn3, self.backbone.relu3, self.backbone.maxpool, self.backbone.layer1, ) else: self.low_features = nn.Sequential( self.backbone.conv1, self.backbone.bn1, self.backbone.relu, self.backbone.maxpool, self.backbone.layer1, ) self.high_features1 = nn.Sequential(self.backbone.layer2, self.backbone.layer3) self.high_features2 = nn.Sequential(self.backbone.layer4) self.embed_conv = EmbedModule(1024, bn_type=self.configer.get( 'network', 'bn_type')) self.decoder = PPMBilinearDeepsup(num_class=self.num_classes, fc_dim=num_features, bn_type=self.configer.get( 'network', 'bn_type')) def forward(self, x): low = self.low_features(x) aux = self.high_features1(low) incr = self.embed_conv(aux) x = self.high_features2(aux + incr) x, aux = self.decoder([x, aux]) x = F.interpolate(x, scale_factor=8, mode="bilinear", align_corners=False) return x, aux, incr
class OpenPose(nn.Module): def __init__(self, configer): super(OpenPose, self).__init__() self.configer = configer self.backbone = BackboneSelector(configer).get_backbone() self.pose_model = PoseModel(configer, self.backbone.get_num_features()) def forward(self, x): x = self.backbone(x) out = self.pose_model(x) return out
def __init__(self, configer): super(PSPNet, self).__init__() self.configer = configer self.num_classes = self.configer.get('data', 'num_classes') self.backbone = BackboneSelector(configer).get_backbone() num_features = self.backbone.get_num_features() self.low_features = nn.Sequential( self.backbone.conv1, self.backbone.bn1, self.backbone.relu, self.backbone.maxpool, self.backbone.layer1, ) self.high_features1 = nn.Sequential(self.backbone.layer2, self.backbone.layer3) self.high_features2 = nn.Sequential(self.backbone.layer4) self.decoder = PPMBilinearDeepsup(num_class=self.num_classes, fc_dim=num_features, bn_type=self.configer.get( 'network', 'bn_type'))
def __init__(self, configer): super(CPN, self).__init__() self.configer = configer self.backbone = BackboneSelector(configer).get_backbone() input_size = self.configer.get('data', 'input_size') stride = self.configer.get('network', 'stride') output_shape = (input_size[0] // stride, input_size[1] // stride) self.global_net = globalNet([2048, 1024, 512, 256], output_shape, self.configer.get('network', 'heatmap_out')) self.refine_net = refineNet(256, output_shape, self.configer.get('network', 'heatmap_out'))
class PSPNet(nn.Sequential): def __init__(self, configer): super(PSPNet, self).__init__() self.configer = configer self.num_classes = self.configer.get('data', 'num_classes') self.backbone = BackboneSelector(configer).get_backbone() num_features = self.backbone.get_num_features() self.dsn = nn.Sequential( _ConvBatchNormReluBlock(num_features // 2, num_features // 4, 3, 1, norm_type=self.configer.get( 'network', 'norm_type')), nn.Dropout2d(0.1), nn.Conv2d(num_features // 4, self.num_classes, 1, 1, 0)) self.ppm = PPMBilinearDeepsup(fc_dim=num_features, norm_type=self.configer.get( 'network', 'norm_type')) self.cls = nn.Sequential( nn.Conv2d(num_features + 4 * 512, 512, kernel_size=3, padding=1, bias=False), ModuleHelper.BNReLU(512, norm_type=self.configer.get( 'network', 'norm_type')), nn.Dropout2d(0.1), nn.Conv2d(512, self.num_classes, kernel_size=1)) def forward(self, data_dict): x = self.backbone(data_dict['img']) aux_x = self.dsn(x[-2]) x = self.ppm(x[-1]) x = self.cls(x) aux_x = F.interpolate(aux_x, size=(data_dict['img'].size(2), data_dict['img'].size(3)), mode="bilinear", align_corners=True) x = F.interpolate(x, size=(data_dict['img'].size(2), data_dict['img'].size(3)), mode="bilinear", align_corners=True) return dict(aux_out=aux_x, out=x)
def __init__(self, configer): super(asymmetric_non_local_network, self).__init__() self.configer = configer self.num_classes = self.configer.get('data', 'num_classes') self.backbone = BackboneSelector(configer).get_backbone() # low_in_channels, high_in_channels, out_channels, key_channels, value_channels, dropout self.fusion = AFNB(1024, 2048, 2048, 256, 256, dropout=0.05, sizes=([1]), norm_type=self.configer.get('network', 'norm_type')) # extra added layers self.context = nn.Sequential( nn.Conv2d(2048, 512, kernel_size=3, stride=1, padding=1), ModuleHelper.BNReLU(512, norm_type=self.configer.get( 'network', 'norm_type')), APNB(in_channels=512, out_channels=512, key_channels=256, value_channels=256, dropout=0.05, sizes=([1]), norm_type=self.configer.get('network', 'norm_type'))) self.cls = nn.Conv2d(512, self.num_classes, kernel_size=1, stride=1, padding=0, bias=True) self.dsn = nn.Sequential( nn.Conv2d(1024, 512, kernel_size=3, stride=1, padding=1), ModuleHelper.BNReLU(512, norm_type=self.configer.get( 'network', 'norm_type')), nn.Dropout2d(0.05), nn.Conv2d(512, self.num_classes, kernel_size=1, stride=1, padding=0, bias=True))
def __init__(self, configer): super(FpnRCNN, self).__init__() self.configer = configer self.backbone = BackboneSelector(configer).get_backbone() self.RCNN_layer0 = nn.Sequential(self.backbone.conv1, self.backbone.bn1, self.backbone.relu, self.backbone.maxpool) self.RCNN_layer1 = nn.Sequential(self.backbone.layer1) self.RCNN_layer2 = nn.Sequential(self.backbone.layer2) self.RCNN_layer3 = nn.Sequential(self.backbone.layer3) self.RCNN_layer4 = nn.Sequential(self.backbone.layer4) # Top layer self.RCNN_toplayer = nn.Conv2d(2048, 256, kernel_size=1, stride=1, padding=0) # reduce channel # Smooth layers self.RCNN_smooth1 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1) self.RCNN_smooth2 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1) self.RCNN_smooth3 = nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1) # Lateral layers self.RCNN_latlayer1 = nn.Conv2d(1024, 256, kernel_size=1, stride=1, padding=0) self.RCNN_latlayer2 = nn.Conv2d(512, 256, kernel_size=1, stride=1, padding=0) self.RCNN_latlayer3 = nn.Conv2d(256, 256, kernel_size=1, stride=1, padding=0) # ROI Pool feature downsampling self.RCNN_roi_feat_ds = nn.Conv2d(256, 256, kernel_size=3, stride=2, padding=1) self.RCNN_top = nn.Sequential( nn.Conv2d(256, 1024, kernel_size=self.configer.get('roi', 'pooled_height'), stride=self.configer.get('roi', 'pooled_height'), padding=0), nn.ReLU(True), nn.Conv2d(1024, 1024, kernel_size=1, stride=1, padding=0), nn.ReLU(True) ) self.rpn = NaiveRPN(configer) self.roi = FRRoiGenerator(configer) self.roi_sampler = FRRoiSampleLayer(configer) self.head = RoIHead(configer)
class PSPNet(nn.Sequential): def __init__(self, configer): super(PSPNet, self).__init__() self.configer = configer self.num_classes = self.configer.get('data', 'num_classes') self.backbone = BackboneSelector(configer).get_backbone() num_features = self.backbone.get_num_features() self.low_features = nn.Sequential( self.backbone.conv1, self.backbone.bn1, self.backbone.relu, self.backbone.maxpool, self.backbone.layer1, ) self.high_features1 = nn.Sequential(self.backbone.layer2, self.backbone.layer3) self.high_features2 = nn.Sequential(self.backbone.layer4) self.decoder = PPMBilinearDeepsup(num_class=self.num_classes, fc_dim=num_features, bn_type=self.configer.get( 'network', 'bn_type')) def forward(self, x_): low = self.low_features(x_) aux = self.high_features1(low) x = self.high_features2(aux) x, aux = self.decoder([x, aux]) x = F.interpolate(x, size=(x_.size(2), x_.size(3)), mode="bilinear", align_corners=False) return x, aux
def __init__(self, configer): super(DarkNetYolov3, self).__init__() self.configer = configer self.backbone = BackboneSelector(configer).get_backbone() self.yolov3_head = Yolov3Head(configer, out_filters=self.backbone.num_features)
def __init__(self, configer): super(MobilePose, self).__init__() self.configer = configer self.backbone = BackboneSelector(configer).get_backbone() self.pose_model = PoseModel(configer, in_channels = self.backbone.get_num_features())
class DenseASPP(nn.Module): def __init__(self, configer): super(DenseASPP, self).__init__() self.configer = configer det_features = self.configer.get('details', 'num_feature_list') self.num_classes = self.configer.get('data', 'num_classes') self.features = BackboneSelector(configer).get_backbone() num_features = self.features.get_num_features() self.ASPP_3 = nn.Sequential(nn.ReLU(inplace=True), nn.Conv2d(in_channels=num_features, out_channels=256, kernel_size=1), nn.BatchNorm2d(num_features=256), nn.ReLU(inplace=True), nn.Conv2d(in_channels=256, out_channels=64, kernel_size=3, dilation=3, padding=3), nn.Dropout2d(p=0.1)) self.ASPP_6 = nn.Sequential(nn.BatchNorm2d(num_features=num_features + 64 * 1), nn.ReLU(inplace=True), nn.Conv2d(in_channels=num_features + 64 * 1, out_channels=256, kernel_size=1), nn.BatchNorm2d(num_features=256), nn.ReLU(inplace=True), nn.Conv2d(in_channels=256, out_channels=64, kernel_size=3, dilation=6, padding=6), nn.Dropout2d(p=0.1)) self.ASPP_12 = nn.Sequential(nn.BatchNorm2d(num_features=num_features + 64 * 2), nn.ReLU(inplace=True), nn.Conv2d(in_channels=num_features + 64 * 2, out_channels=256, kernel_size=1), nn.BatchNorm2d(num_features=256), nn.ReLU(inplace=True), nn.Conv2d(in_channels=256, out_channels=64, kernel_size=3, dilation=12, padding=12), nn.Dropout2d(p=0.1)) self.ASPP_18 = nn.Sequential(nn.BatchNorm2d(num_features=num_features + 64 * 3), nn.ReLU(inplace=True), nn.Conv2d(in_channels=num_features + 64 * 3, out_channels=256, kernel_size=1), nn.BatchNorm2d(num_features=256), nn.ReLU(inplace=True), nn.Conv2d(in_channels=256, out_channels=64, kernel_size=3, dilation=18, padding=18), nn.Dropout2d(p=0.1)) self.ASPP_24 = nn.Sequential(nn.BatchNorm2d(num_features=num_features + 64 * 4), nn.ReLU(inplace=True), nn.Conv2d(in_channels=num_features + 64 * 4, out_channels=256, kernel_size=1), nn.BatchNorm2d(num_features=256), nn.ReLU(inplace=True), nn.Conv2d(in_channels=256, out_channels=64, kernel_size=3, dilation=24, padding=24)) num_features += 5 * 64 self.det_feature1 = _RevertedResDetBlock(num_features, det_features[0], stride=1, expand_ratio=2) self.det_feature2 = _RevertedResDetBlock(det_features[0], det_features[1], stride=2, expand_ratio=3) self.det_feature3 = _RevertedResDetBlock(det_features[1], det_features[2], stride=2, expand_ratio=3) self.det_feature4 = _RevertedResDetBlock(det_features[2], det_features[3], stride=2, expand_ratio=3) self.det_feature5 = _RevertedResDetBlock(det_features[3], det_features[4], stride=2, expand_ratio=3) self.multibox_layer = ShareMultiBoxLayer(configer) for m in self.modules(): if isinstance(m, nn.Conv2d): init.kaiming_uniform_(m.weight.data) if m.bias is not None: m.bias.data.zero_() elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_() def forward(self, _input): feature = self.features(_input) aspp3 = self.ASPP_3(feature) feature = torch.cat((aspp3, feature), dim=1) aspp6 = self.ASPP_6(feature) feature = torch.cat((aspp6, feature), dim=1) aspp12 = self.ASPP_12(feature) feature = torch.cat((aspp12, feature), dim=1) aspp18 = self.ASPP_18(feature) feature = torch.cat((aspp18, feature), dim=1) aspp24 = self.ASPP_24(feature) feature = torch.cat((aspp24, feature), dim=1) det_feature = [] feature = self.det_feature1(feature) det_feature.append(feature) feature = self.det_feature2(feature) det_feature.append(feature) feature = self.det_feature3(feature) det_feature.append(feature) feature = self.det_feature4(feature) det_feature.append(feature) feature = self.det_feature5(feature) det_feature.append(feature) loc_preds, conf_preds = self.multibox_layer(det_feature) return loc_preds, conf_preds @staticmethod def __extra_layer(num_in, num_out, num_c, stride, pad): layer = nn.Sequential( nn.Conv2d(num_in, num_c, kernel_size=1), nn.ReLU(), nn.Conv2d(num_c, num_out, kernel_size=3, stride=stride, padding=pad), nn.ReLU(), ) return layer def load_pretrained_weight(self, net): self.features.load_pretrained_weight(net)
def __init__(self, configer): super(DenseASPP, self).__init__() self.configer = configer det_features = self.configer.get('details', 'num_feature_list') self.num_classes = self.configer.get('data', 'num_classes') self.features = BackboneSelector(configer).get_backbone() num_features = self.features.get_num_features() self.ASPP_3 = nn.Sequential(nn.ReLU(inplace=True), nn.Conv2d(in_channels=num_features, out_channels=256, kernel_size=1), nn.BatchNorm2d(num_features=256), nn.ReLU(inplace=True), nn.Conv2d(in_channels=256, out_channels=64, kernel_size=3, dilation=3, padding=3), nn.Dropout2d(p=0.1)) self.ASPP_6 = nn.Sequential(nn.BatchNorm2d(num_features=num_features + 64 * 1), nn.ReLU(inplace=True), nn.Conv2d(in_channels=num_features + 64 * 1, out_channels=256, kernel_size=1), nn.BatchNorm2d(num_features=256), nn.ReLU(inplace=True), nn.Conv2d(in_channels=256, out_channels=64, kernel_size=3, dilation=6, padding=6), nn.Dropout2d(p=0.1)) self.ASPP_12 = nn.Sequential(nn.BatchNorm2d(num_features=num_features + 64 * 2), nn.ReLU(inplace=True), nn.Conv2d(in_channels=num_features + 64 * 2, out_channels=256, kernel_size=1), nn.BatchNorm2d(num_features=256), nn.ReLU(inplace=True), nn.Conv2d(in_channels=256, out_channels=64, kernel_size=3, dilation=12, padding=12), nn.Dropout2d(p=0.1)) self.ASPP_18 = nn.Sequential(nn.BatchNorm2d(num_features=num_features + 64 * 3), nn.ReLU(inplace=True), nn.Conv2d(in_channels=num_features + 64 * 3, out_channels=256, kernel_size=1), nn.BatchNorm2d(num_features=256), nn.ReLU(inplace=True), nn.Conv2d(in_channels=256, out_channels=64, kernel_size=3, dilation=18, padding=18), nn.Dropout2d(p=0.1)) self.ASPP_24 = nn.Sequential(nn.BatchNorm2d(num_features=num_features + 64 * 4), nn.ReLU(inplace=True), nn.Conv2d(in_channels=num_features + 64 * 4, out_channels=256, kernel_size=1), nn.BatchNorm2d(num_features=256), nn.ReLU(inplace=True), nn.Conv2d(in_channels=256, out_channels=64, kernel_size=3, dilation=24, padding=24)) num_features += 5 * 64 self.det_feature1 = _RevertedResDetBlock(num_features, det_features[0], stride=1, expand_ratio=2) self.det_feature2 = _RevertedResDetBlock(det_features[0], det_features[1], stride=2, expand_ratio=3) self.det_feature3 = _RevertedResDetBlock(det_features[1], det_features[2], stride=2, expand_ratio=3) self.det_feature4 = _RevertedResDetBlock(det_features[2], det_features[3], stride=2, expand_ratio=3) self.det_feature5 = _RevertedResDetBlock(det_features[3], det_features[4], stride=2, expand_ratio=3) self.multibox_layer = ShareMultiBoxLayer(configer) for m in self.modules(): if isinstance(m, nn.Conv2d): init.kaiming_uniform_(m.weight.data) if m.bias is not None: m.bias.data.zero_() elif isinstance(m, nn.BatchNorm2d): m.weight.data.fill_(1) m.bias.data.zero_()
class DenseASPP(nn.Module): """ * output_scale can only set as 8 or 16 """ def __init__(self, configer): super(DenseASPP, self).__init__() self.configer = configer dropout0 = 0.1 dropout1 = 0.1 self.backbone = BackboneSelector(configer).get_backbone() num_features = self.backbone.get_num_features() self.trans = _Transition(num_input_features=self.num_features, num_output_features=self.num_features // 2, bn_type=self.configer.get( 'network', 'bn_type')) self.num_features = self.num_features // 2 self.ASPP_3 = _DenseAsppBlock(input_num=num_features, num1=256, num2=64, dilation_rate=3, drop_out=dropout0, bn_type=self.configer.get( 'network', 'bn_type')) self.ASPP_6 = _DenseAsppBlock(input_num=num_features + 64 * 1, num1=256, num2=64, dilation_rate=6, drop_out=dropout0, bn_type=self.configer.get( 'network', 'bn_type')) self.ASPP_12 = _DenseAsppBlock(input_num=num_features + 64 * 2, num1=256, num2=64, dilation_rate=12, drop_out=dropout0, bn_type=self.configer.get( 'network', 'bn_type')) self.ASPP_18 = _DenseAsppBlock(input_num=num_features + 64 * 3, num1=256, num2=64, dilation_rate=18, drop_out=dropout0, bn_type=self.configer.get( 'network', 'bn_type')) self.ASPP_24 = _DenseAsppBlock(input_num=num_features + 64 * 4, num1=256, num2=64, dilation_rate=24, drop_out=dropout0, bn_type=self.configer.get( 'network', 'bn_type')) num_features = num_features + 5 * 64 self.classification = nn.Sequential( nn.Dropout2d(p=dropout1), nn.Conv2d(in_channels=num_features, out_channels=self.configer.get('network', 'out_channels'), kernel_size=1, padding=0)) self.upsample = nn.Sequential( nn.Upsample(scale_factor=8, mode='bilinear', align_corners=True)) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.xavier_uniform_(m.weight.data) elif isinstance( m, ModuleHelper.BatchNorm2d( bn_type=self.configer.get('network', 'bn_type'))): m.weight.data.fill_(1) m.bias.data.zero_() def forward(self, x): feature = self.backbone(x) aspp3 = self.ASPP_3(feature) feature = torch.cat((aspp3, feature), dim=1) aspp6 = self.ASPP_6(feature) feature = torch.cat((aspp6, feature), dim=1) aspp12 = self.ASPP_12(feature) feature = torch.cat((aspp12, feature), dim=1) aspp18 = self.ASPP_18(feature) feature = torch.cat((aspp18, feature), dim=1) aspp24 = self.ASPP_24(feature) feature = torch.cat((aspp24, feature), dim=1) cls = self.classification(feature) out = self.upsample(cls) res = [] res.append(out) return tuple(res)
class DenseASPP(nn.Module): """ * output_scale can only set as 8 or 16 """ def __init__(self, configer): super(DenseASPP, self).__init__() self.configer = configer dropout0 = MODEL_CONFIG['dropout0'] dropout1 = MODEL_CONFIG['dropout1'] d_feature0 = MODEL_CONFIG['d_feature0'] d_feature1 = MODEL_CONFIG['d_feature1'] self.backbone = BackboneSelector(configer).get_backbone() num_features = self.backbone.get_num_features() self.trans = _Transition(num_input_features=self.num_features, num_output_features=self.num_features // 2, norm_type=self.configer.get( 'network', 'norm_type')) self.num_features = self.num_features // 2 self.ASPP_3 = _DenseAsppBlock(input_num=num_features, num1=d_feature0, num2=d_feature1, dilation_rate=3, drop_out=dropout0, norm_type=self.configer.get( 'network', 'norm_type')) self.ASPP_6 = _DenseAsppBlock(input_num=num_features + d_feature1 * 1, num1=d_feature0, num2=d_feature1, dilation_rate=6, drop_out=dropout0, norm_type=self.configer.get( 'network', 'norm_type')) self.ASPP_12 = _DenseAsppBlock(input_num=num_features + d_feature1 * 2, num1=d_feature0, num2=d_feature1, dilation_rate=12, drop_out=dropout0, norm_type=self.configer.get( 'network', 'norm_type')) self.ASPP_18 = _DenseAsppBlock(input_num=num_features + d_feature1 * 3, num1=d_feature0, num2=d_feature1, dilation_rate=18, drop_out=dropout0, norm_type=self.configer.get( 'network', 'norm_type')) self.ASPP_24 = _DenseAsppBlock(input_num=num_features + d_feature1 * 4, num1=d_feature0, num2=d_feature1, dilation_rate=24, drop_out=dropout0, norm_type=self.configer.get( 'network', 'norm_type')) num_features = num_features + 5 * d_feature1 self.classification = nn.Sequential( nn.Dropout2d(p=dropout1), nn.Conv2d(num_features, self.configer.get('data', 'num_classes'), kernel_size=1, padding=0)) def forward(self, data_dict): x = self.backbone(data_dict['img']) feature = self.trans(x) aspp3 = self.ASPP_3(feature) feature = torch.cat((aspp3, feature), dim=1) aspp6 = self.ASPP_6(feature) feature = torch.cat((aspp6, feature), dim=1) aspp12 = self.ASPP_12(feature) feature = torch.cat((aspp12, feature), dim=1) aspp18 = self.ASPP_18(feature) feature = torch.cat((aspp18, feature), dim=1) aspp24 = self.ASPP_24(feature) feature = torch.cat((aspp24, feature), dim=1) x = self.classification(feature) x = F.interpolate(x, size=(data_dict['img'].size(2), data_dict['img'].size(3)), mode="bilinear", align_corners=True) return dict(out=out)
class DenseASPP(nn.Module): """ * output_scale can only set as 8 or 16 """ def __init__(self, configer): super(DenseASPP, self).__init__() self.configer = configer dropout0 = 0.1 dropout1 = 0.1 self.backbone = BackboneSelector(configer).get_backbone() num_features = self.backbone.get_num_features() self.trans = _Transition(num_input_features=self.num_features, num_output_features=self.num_features // 2, bn_type=self.configer.get('network', 'bn_type')) self.num_features = self.num_features // 2 self.ASPP_3 = _DenseAsppBlock(input_num=num_features, num1=256, num2=64, dilation_rate=3, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) self.ASPP_6 = _DenseAsppBlock(input_num=num_features + 64 * 1, num1=256, num2=64, dilation_rate=6, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) self.ASPP_12 = _DenseAsppBlock(input_num=num_features + 64 * 2, num1=256, num2=64, dilation_rate=12, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) self.ASPP_18 = _DenseAsppBlock(input_num=num_features + 64 * 3, num1=256, num2=64, dilation_rate=18, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) self.ASPP_24 = _DenseAsppBlock(input_num=num_features + 64 * 4, num1=256, num2=64, dilation_rate=24, drop_out=dropout0, bn_type=self.configer.get('network', 'bn_type')) num_features = num_features + 5 * 64 self.classification = nn.Sequential( nn.Dropout2d(p=dropout1), nn.Conv2d(in_channels=num_features, out_channels=self.configer.get('network', 'out_channels'), kernel_size=1, padding=0) ) def forward(self, x): feature = self.backbone(x) aspp3 = self.ASPP_3(feature) feature = torch.cat((aspp3, feature), dim=1) aspp6 = self.ASPP_6(feature) feature = torch.cat((aspp6, feature), dim=1) aspp12 = self.ASPP_12(feature) feature = torch.cat((aspp12, feature), dim=1) aspp18 = self.ASPP_18(feature) feature = torch.cat((aspp18, feature), dim=1) aspp24 = self.ASPP_24(feature) feature = torch.cat((aspp24, feature), dim=1) cls = self.classification(feature) out = F.interpolate(cls, scale_factor=8, mode='bilinear', align_corners=False) return out