def create_block_list(self): self.clear_list() if (self.scale < 0.99) or (self.scale > 1.001): up = Upsample(scale_factor=self.scale, mode='nearest') self.add_block_list(up.get_name(), up, self.data_channel) stage1 = ConvBNActivationBlock(in_channels=self.data_channel, out_channels=self.out_channels[0], kernel_size=3, stride=2, padding=1, bias=False, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(stage1.get_name(), stage1, self.out_channels[0]) for index, number in enumerate(self.repeats): self.make_layers(number, self.out_channels[index], self.out_channels[index + 1], self.kernel_sizes[index], self.strides[index], self.expands[index]) activate = ActivationLayer(activation_name=self.activation_name) self.add_block_list(activate.get_name(), activate, self.block_out_channels[-1]) stage9 = ConvBNActivationBlock(in_channels=self.out_channels[7], out_channels=self.out_channels[8], kernel_size=1, stride=1, padding=0, bias=False, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(stage9.get_name(), stage9, self.out_channels[8])
def dsn_head(self, base_out_channels): route = RouteLayer('14') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in route.layers ]) self.add_block_list(route.get_name(), route, output_channel) conv1 = ConvBNActivationBlock(in_channels=output_channel, out_channels=512, kernel_size=3, stride=1, padding=1, bias=True, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(conv1.get_name(), conv1, 512) dropout = nn.Dropout2d(0.1) self.add_block_list(LayerType.Dropout, dropout, self.block_out_channels[-1]) out_conv = nn.Conv2d(512, self.class_number, kernel_size=1, stride=1, padding=0, bias=True) self.add_block_list(LayerType.Convolutional, out_conv, self.class_number) up1 = Upsample(scale_factor=16, mode='bilinear') self.add_block_list(up1.get_name(), up1, self.block_out_channels[-1])
def create_block_list(self): self.clear_list() self.lossList = [] backbone = self.factory.get_base_model(BackboneName.ResNet50) base_out_channels = backbone.get_outchannel_list() self.add_block_list(BlockType.BaseNet, backbone, base_out_channels[-1]) head = ASPPBlock(2048, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(head.get_name(), head, 512) out_conv = nn.Conv2d(512, self.class_number, kernel_size=1, stride=1, padding=0, bias=True) self.add_block_list(LayerType.Convolutional, out_conv, self.class_number) up = Upsample(scale_factor=32, mode='bilinear') self.add_block_list(up.get_name(), up, self.block_out_channels[-1]) self.loss_flag = 0 self.clear_list() self.dsn_head(base_out_channels) self.loss_flag = 1 self.clear_list()
def __init__(self, low_channels, high_channels, out_channels, scale, bn_name=NormalizationType.BatchNormalize2d, activation_name=ActivationType.ReLU): super().__init__(ICNetBlockName.CascadeFeatureFusion) self.up = Upsample(scale_factor=scale, mode='bilinear') self.conv_low = ConvBNActivationBlock( in_channels=low_channels, out_channels=out_channels, kernel_size=3, padding=2, dilation=2, bias=False, bnName=bn_name, activationName=ActivationType.Linear) self.conv_high = ConvBNActivationBlock( in_channels=high_channels, out_channels=out_channels, kernel_size=1, bias=False, bnName=bn_name, activationName=ActivationType.Linear) self.activation = ActivationLayer(activation_name=activation_name, inplace=False)
def make_layer(self, base_out_channels, conv_output_channel, scale_factor, route_layer_indexs): layer1 = Upsample(scale_factor=scale_factor, mode='bilinear') self.add_block_list(layer1.get_name(), layer1, self.block_out_channels[-1]) layer2 = RouteLayer(route_layer_indexs) output_channel = sum([base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer2.layers]) self.add_block_list(layer2.get_name(), layer2, output_channel) conv1 = ConvBNActivationBlock(self.block_out_channels[-1], conv_output_channel, kernel_size=1, stride=1, padding=0, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(conv1.get_name(), conv1, conv_output_channel) layer3 = RouteLayer('-1,-3') output_channel = sum([base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer3.layers]) self.add_block_list(layer3.get_name(), layer3, output_channel)
def create_block_list(self): self.clear_list() backbone = self.factory.get_base_model(BackboneName.ResNet101) base_out_channels = backbone.get_outchannel_list() self.add_block_list(BlockType.BaseNet, backbone, base_out_channels[-1]) if self.is_jpu: jup = JPUBlock(layers='4,8,31,34', in_planes=(512, 1024, 2048), width=512, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(jup.get_name(), jup, 512 + 512 + 512 + 512) scale_factor = 8 else: scale_factor = 32 psp = PyramidPooling(2048, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(psp.get_name(), psp, 2048 * 2) conv1 = ConvBNActivationBlock(in_channels=2048 * 2, out_channels=512, kernel_size=3, padding=1, bias=False, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(conv1.get_name(), conv1, 512) dropout = nn.Dropout(0.1) self.add_block_list(LayerType.Dropout, dropout, self.block_out_channels[-1]) conv2 = nn.Conv2d(512, self.class_number, 1) self.add_block_list(LayerType.Convolutional, conv2, self.class_number) layer = Upsample(scale_factor=scale_factor, mode='bilinear') self.add_block_list(layer.get_name(), layer, self.block_out_channels[-1]) self.create_loss()
def create_block_list(self): self.block_out_channels = [] self.index = 0 basic_model = MobileNetV2(bnName=self.bn_name, activationName=self.activation_name) base_out_channels = basic_model.get_outchannel_list() self.add_block_list(BlockType.BaseNet, basic_model, base_out_channels[-1]) input_channel = self.block_out_channels[-1] output_channel = base_out_channels[-1] // 2 self.make_conv_block(input_channel, output_channel) self.make_layer(base_out_channels, output_channel, 2, '13') input_channel = self.block_out_channels[-1] output_channel = base_out_channels[-1] // 4 self.make_conv_block(input_channel, output_channel) self.make_layer(base_out_channels, output_channel, 2, '6') input_channel = self.block_out_channels[-1] output_channel = base_out_channels[-1] // 8 self.make_conv_block(input_channel, output_channel) self.make_layer(base_out_channels, output_channel, 2, '3') input_channel = self.block_out_channels[-1] output_channel = base_out_channels[-1] // 16 self.make_conv_block(input_channel, output_channel) input_channel = self.block_out_channels[-1] output_channel = self.class_number conv4 = ConvActivationBlock(input_channel, output_channel, kernel_size=1, stride=1, padding=0, activationName=ActivationType.Linear) self.add_block_list(conv4.get_name(), conv4, output_channel) layer10 = Upsample(scale_factor=4, mode='bilinear') self.add_block_list(layer10.get_name(), layer10, self.block_out_channels[-1]) self.create_loss()
def __init__(self, in_planes, out_planes, stages, activation_name=ActivationType.ReLU): super().__init__(RefineNetBlockName.RefineNetBlock) self.activate = ActivationLayer(activation_name, inplace=False) self.mflow = CRPBlock(in_planes, out_planes, stages) self.conv1 = nn.Conv2d(out_planes, 256, 1, bias=False) self.up = Upsample(scale_factor=2, mode='bilinear') self.conv2 = nn.Conv2d(256, 256, 1, bias=False)
def create_block_list(self): self.block_out_channels = [] self.index = 0 self.learning_to_downsample() global_feature_extractor = GlobalFeatureExtractor(64, [64, 96, 128], 128, 6, [3, 3, 3], bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(global_feature_extractor.get_name(), global_feature_extractor, 128) feature_fusion = FeatureFusionBlock(64, 128, 128, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(feature_fusion.get_name(), feature_fusion, 128) self.classifer(128) layer = Upsample(scale_factor=8, mode='bilinear') self.add_block_list(layer.get_name(), layer, self.block_out_channels[-1]) self.create_loss()
def __init__(self, in_channels, out_channels, mode='bilinear', bn_name=NormalizationType.BatchNormalize2d, activation_name=ActivationType.ReLU): super().__init__(UNetBlockName.UpBlock) self.up = Upsample(scale_factor=2, mode=mode) self.conv = ConvBNActivationBlock(in_channels=in_channels, out_channels=out_channels, kernel_size=3, padding=1, bias=True, bnName=bn_name, activationName=activation_name)
def create_block_list(self): self.block_out_channels = [] self.index = 0 c1_channels = 24 c4_channels = 320 backbone = self.factory.get_base_model(BackboneName.MobileNetV2_1_0) base_out_channels = backbone.get_outchannel_list() self.add_block_list(BlockType.BaseNet, backbone, base_out_channels[-1]) aspp = ASPP(c4_channels, 256, bn_name=self.bn_name, activation_name=self.activation_name) out_channel = 256 self.add_block_list(aspp.get_name(), aspp, out_channel) up1 = Upsample(scale_factor=8, mode='bilinear') self.add_block_list(up1.get_name(), up1, self.block_out_channels[-1]) layer1 = RouteLayer('3') output_channel = sum([base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer1.layers]) self.add_block_list(layer1.get_name(), layer1, output_channel) conv1 = ConvBNActivationBlock(in_channels=c1_channels, out_channels=48, kernel_size=1, bias=False, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(conv1.get_name(), conv1, 48) layer2 = RouteLayer('-3, -1') output_channel = sum([base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer2.layers]) self.add_block_list(layer2.get_name(), layer2, output_channel) block1 = SeparableConv2dBNActivation(output_channel, 256, 3, relu_first=False, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(block1.get_name(), block1, 256) block2 = SeparableConv2dBNActivation(256, 256, 3, relu_first=False, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(block2.get_name(), block2, 256) conv2 = nn.Conv2d(256, self.class_number, 1) self.add_block_list(LayerType.Convolutional, conv2, self.class_number) layer3 = Upsample(scale_factor=4, mode='bilinear') self.add_block_list(layer3.get_name(), layer3, self.block_out_channels[-1]) self.create_loss()
def create_layer(self, module_def): if module_def['type'] == LayerType.MyMaxPool2d: kernel_size = int(module_def['size']) stride = int(module_def['stride']) maxpool = MyMaxPool2d(kernel_size, stride) self.addBlockList(LayerType.MyMaxPool2d, maxpool, self.filters) self.input_channels = self.filters elif module_def['type'] == LayerType.GlobalAvgPool: globalAvgPool = GlobalAvgPool2d() self.addBlockList(LayerType.GlobalAvgPool, globalAvgPool, self.filters) self.input_channels = self.filters elif module_def['type'] == LayerType.FcLayer: num_output = int(module_def['num_output']) self.filters = num_output layer = FcLayer(self.input_channels, num_output) self.addBlockList(LayerType.FcLayer, layer, num_output) self.input_channels = num_output elif module_def['type'] == LayerType.Upsample: scale = int(module_def['stride']) mode = module_def.get('model', 'bilinear') upsample = Upsample(scale_factor=scale, mode=mode) self.addBlockList(LayerType.Upsample, upsample, self.filters) self.input_channels = self.filters elif module_def['type'] == LayerType.MultiplyLayer: layer = MultiplyLayer(module_def['layers']) self.addBlockList(LayerType.MultiplyLayer, layer, self.filters) self.input_channels = self.filters elif module_def['type'] == LayerType.AddLayer: layer = AddLayer(module_def['layers']) self.addBlockList(LayerType.AddLayer, layer, self.filters) self.input_channels = self.filters elif module_def['type'] == LayerType.Dropout: probability = float(module_def['probability']) layer = nn.Dropout(p=probability, inplace=False) self.addBlockList(LayerType.Dropout, layer, self.filters) self.input_channels = self.filters elif module_def['type'] == LayerType.NormalizeLayer: bn_name = module_def['batch_normalize'].strip() layer = NormalizeLayer(bn_name, self.filters) self.addBlockList(LayerType.NormalizeLayer, layer, self.filters) self.input_channels = self.filters elif module_def['type'] == LayerType.ActivationLayer: activation_name = module_def['activation'].strip() layer = ActivationLayer(activation_name, inplace=False) self.addBlockList(LayerType.ActivationLayer, layer, self.filters) self.input_channels = self.filters
def __init__(self, in_channels, class_number, scale_factor, bn_name=NormalizationType.BatchNormalize2d, activation_name=ActivationType.ReLU): super().__init__(EncNetBlockName.FCNHeadBlock) inter_channels = in_channels // 4 self.conv1 = ConvBNActivationBlock(in_channels=in_channels, out_channels=inter_channels, kernel_size=3, padding=1, bias=False, bnName=bn_name, activationName=activation_name) self.dropout = nn.Dropout2d(0.1, False) self.conv2 = nn.Conv2d(inter_channels, class_number, 1) self.up = Upsample(scale_factor=scale_factor, mode='bilinear')
def __init__(self, in_channels, out_channels, scale_factor=2, bn_name=NormalizationType.BatchNormalize2d, activation_name=ActivationType.ReLU): super().__init__(BiSeNetBlockName.ContextPath) self.arm = AttentionRefinmentBlock(in_channels, out_channels, bn_name=bn_name, activation_name=activation_name) self.refine = ConvBNActivationBlock(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1, bnName=bn_name, activationName=activation_name) self.up = Upsample(scale_factor=scale_factor, mode='bilinear')
def create_head(self): cff_24 = CascadeFeatureFusion(2048, 512, 128, 7, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(cff_24.get_name(), cff_24, 128) cff_12 = CascadeFeatureFusion(128, 64, 128, 2, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(cff_12.get_name(), cff_12, 128) up1 = Upsample(scale_factor=2, mode='bilinear') self.add_block_list(up1.get_name(), up1, 128) conv_cls = nn.Conv2d(128, self.class_number, 1, bias=False) self.add_block_list(LayerType.Convolutional, conv_cls, self.class_number) up2 = Upsample(scale_factor=4, mode='bilinear') self.add_block_list(up2.get_name(), up2, self.class_number)
def __init__(self, in_channels, middle_channels, out_channels, activation_name=ActivationType.ReLU, is_deconv=True): super().__init__(TernausNetBlockName.DecoderBlock) if is_deconv: self.block = nn.Sequential( ConvActivationBlock(in_channels=in_channels, out_channels=middle_channels, kernel_size=3, padding=1, activationName=activation_name), # nn.ConvTranspose2d(middle_channels, out_channels, kernel_size=4, stride=2, # padding=1), nn.ConvTranspose2d(middle_channels, out_channels, kernel_size=3, stride=2, padding=1, output_padding=1), ActivationLayer(activation_name), ) else: self.block = nn.Sequential( Upsample(scale_factor=2, mode='bilinear'), ConvActivationBlock(in_channels=in_channels, out_channels=middle_channels, kernel_size=3, padding=1, activationName=activation_name), ConvActivationBlock(in_channels=middle_channels, out_channels=out_channels, kernel_size=3, padding=1, activationName=activation_name), )
def create_block_list(self): self.block_out_channels = [] self.index = 0 backbone = self.factory.get_base_model(BackboneName.ResNet101) base_out_channels = backbone.get_outchannel_list() self.add_block_list(BlockType.BaseNet, backbone, base_out_channels[-1]) dropout1 = nn.Dropout(p=0.5) self.add_block_list(LayerType.Dropout, dropout1, self.block_out_channels[-1]) layer1 = RouteLayer('31') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer1.layers ]) self.add_block_list(layer1.get_name(), layer1, output_channel) dropout2 = nn.Dropout(p=0.5) self.add_block_list(LayerType.Dropout, dropout2, self.block_out_channels[-1]) layer2 = RouteLayer('-3') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer1.layers ]) self.add_block_list(layer2.get_name(), layer2, output_channel) conv1 = nn.Conv2d(2048, 512, 1, bias=False) self.add_block_list(LayerType.Convolutional, conv1, 512) layer3 = RouteLayer('-3') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer1.layers ]) self.add_block_list(layer3.get_name(), layer3, output_channel) conv2 = nn.Conv2d(1024, 256, 1, bias=False) self.add_block_list(LayerType.Convolutional, conv2, 256) refinenet1 = RefineNetBlock(512, 512, 4, activation_name=self.activation_name) output_channel = 256 self.add_block_list(refinenet1.get_name(), refinenet1, output_channel) layer4 = RouteLayer('8') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer1.layers ]) self.add_block_list(layer4.get_name(), layer4, output_channel) conv3 = nn.Conv2d(512, 256, 1, bias=False) self.add_block_list(LayerType.Convolutional, conv3, 256) refinenet2 = RefineNetBlock(256, 256, 4, activation_name=self.activation_name) output_channel = 256 self.add_block_list(refinenet2.get_name(), refinenet2, output_channel) layer5 = RouteLayer('4') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer1.layers ]) self.add_block_list(layer5.get_name(), layer5, output_channel) conv4 = nn.Conv2d(256, 256, 1, bias=False) self.add_block_list(LayerType.Convolutional, conv4, 256) refinenet3 = RefineNetBlock(256, 256, 4, activation_name=self.activation_name) output_channel = 256 self.add_block_list(refinenet3.get_name(), refinenet3, output_channel) crp = CRPBlock(256, 256, 4) output_channel = 256 self.add_block_list(crp.get_name(), crp, output_channel) conv5 = nn.Conv2d(256, self.class_number, kernel_size=3, stride=1, padding=1, bias=True) self.add_block_list(LayerType.Convolutional, conv5, self.class_number) layer6 = Upsample(scale_factor=4, mode='bilinear') self.add_block_list(layer6.get_name(), layer6, self.block_out_channels[-1]) self.create_loss()
def __init__(self, mode='bilinear'): super().__init__(ICNetBlockName.InputDownSample) self.down1 = Upsample(scale_factor=0.5, mode=mode) self.down2 = Upsample(scale_factor=0.25, mode=mode)
def enc_head(self, in_channels, base_out_channels): if self.is_jpu: conv1 = ConvBNActivationBlock(in_channels=in_channels, out_channels=512, kernel_size=1, bias=False, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(conv1.get_name(), conv1, 512) else: conv1 = ConvBNActivationBlock(in_channels=in_channels, out_channels=512, kernel_size=3, padding=1, bias=False, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(conv1.get_name(), conv1, 512) if self.lateral: route1 = RouteLayer('8') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in route1.layers ]) self.add_block_list(route1.get_name(), route1, output_channel) connect1 = ConvBNActivationBlock( in_channels=output_channel, out_channels=512, kernel_size=1, bias=False, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(connect1.get_name(), connect1, 512) route2 = RouteLayer('14') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in route2.layers ]) self.add_block_list(route2.get_name(), route2, output_channel) connect2 = ConvBNActivationBlock( in_channels=output_channel, out_channels=512, kernel_size=1, bias=False, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(connect2.get_name(), connect2, 512) route3 = RouteLayer('-5,-3,-1') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in route2.layers ]) self.add_block_list(route3.get_name(), route3, output_channel) fusion = ConvBNActivationBlock(in_channels=output_channel, out_channels=512, kernel_size=3, padding=1, bias=False, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(fusion.get_name(), fusion, 512) encmodule = EncBlock(in_channels=512, nclass=self.class_number, se_loss=self.is_se_loss, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(encmodule.get_name(), encmodule, 512) dropout = nn.Dropout2d(0.1, False) self.add_block_list(LayerType.Dropout, dropout, self.block_out_channels[-1]) conv2 = nn.Conv2d(self.block_out_channels[-1], self.class_number, 1) self.add_block_list(LayerType.Convolutional, conv2, self.class_number) up = Upsample(scale_factor=8, mode='bilinear') self.add_block_list(up.get_name(), up, self.class_number)
def create_block_list(self): self.block_out_channels = [] self.index = 0 backbone = self.factory.get_base_model(BackboneName.Vgg16) base_out_channels = backbone.get_outchannel_list() self.add_block_list(BlockType.BaseNet, backbone, base_out_channels[-1]) layer1 = ConvBNActivationBlock(in_channels=base_out_channels[-1], out_channels=4096, kernel_size=7, padding=3, bias=True, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(layer1.get_name(), layer1, 4096) dropout1 = nn.Dropout() self.add_block_list(LayerType.Dropout, dropout1, 4096) layer2 = ConvBNActivationBlock(in_channels=4096, out_channels=4096, kernel_size=1, padding=0, bias=True, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(layer2.get_name(), layer2, 4096) dropout2 = nn.Dropout() self.add_block_list(LayerType.Dropout, dropout2, 4096) layer3 = nn.Conv2d(4096, self.class_number, kernel_size=1) self.add_block_list(LayerType.Convolutional, layer3, self.class_number) upsample1 = Upsample(scale_factor=2, mode='bilinear') self.add_block_list(LayerType.Upsample, upsample1, self.block_out_channels[-1]) layer4 = RouteLayer('13') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer4.layers ]) self.add_block_list(layer4.get_name(), layer4, output_channel) layer5 = nn.Conv2d(512, self.class_number, kernel_size=1) self.add_block_list(LayerType.Convolutional, layer5, self.class_number) layer6 = AddLayer('-1,-3') index = layer6.layers[0] output_channel = base_out_channels[ index] if index >= 0 else self.block_out_channels[index] self.add_block_list(layer6.get_name(), layer6, output_channel) layer7 = RouteLayer('9') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer7.layers ]) self.add_block_list(layer7.get_name(), layer7, output_channel) layer8 = nn.Conv2d(256, self.class_number, kernel_size=1) self.add_block_list(LayerType.Convolutional, layer8, self.class_number) layer9 = RouteLayer('-3') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer7.layers ]) self.add_block_list(layer9.get_name(), layer9, output_channel) upsample2 = Upsample(scale_factor=2, mode='bilinear') self.add_block_list(LayerType.Upsample, upsample2, self.block_out_channels[-1]) layer10 = AddLayer('-1,-3') index = layer10.layers[0] output_channel = base_out_channels[ index] if index >= 0 else self.block_out_channels[index] self.add_block_list(layer10.get_name(), layer10, output_channel) upsample3 = Upsample(scale_factor=8, mode='bilinear') self.add_block_list(LayerType.Upsample, upsample3, self.block_out_channels[-1]) self.create_loss()
def __init__(self, layers, in_planes, width=512, bn_name=NormalizationType.BatchNormalize2d, activation_name=ActivationType.ReLU): super().__init__(EncNetBlockName.JPUBlock) self.layers = [int(x) for x in layers.split(',') if x] assert len(self.layers) == 4 self.conv5 = ConvBNActivationBlock(in_channels=in_planes[-1], out_channels=width, kernel_size=3, padding=1, bias=False, bnName=bn_name, activationName=activation_name) self.conv4 = ConvBNActivationBlock(in_channels=in_planes[-2], out_channels=width, kernel_size=3, padding=1, bias=False, bnName=bn_name, activationName=activation_name) self.conv3 = ConvBNActivationBlock(in_channels=in_planes[-3], out_channels=width, kernel_size=3, padding=1, bias=False, bnName=bn_name, activationName=activation_name) self.up1 = Upsample(scale_factor=2, mode='bilinear') self.up2 = Upsample(scale_factor=4, mode='bilinear') self.dilation1 = SeperableConv2dNActivation( 3 * width, width, kernel_size=3, padding=1, dilation=1, bias=False, bn_name=bn_name, activation_name=activation_name) self.dilation2 = SeperableConv2dNActivation( 3 * width, width, kernel_size=3, padding=2, dilation=2, bias=False, bn_name=bn_name, activation_name=activation_name) self.dilation3 = SeperableConv2dNActivation( 3 * width, width, kernel_size=3, padding=4, dilation=4, bias=False, bn_name=bn_name, activation_name=activation_name) self.dilation4 = SeperableConv2dNActivation( 3 * width, width, kernel_size=3, padding=8, dilation=8, bias=False, bn_name=bn_name, activation_name=activation_name)
def create_block_list(self): self.clear_list() spatial_path = SpatialPath(self.data_channel, 128, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(spatial_path.get_name(), spatial_path, 128) backbone = self.factory.get_base_model(BackboneName.ResNet18) base_out_channels = backbone.get_outchannel_list() self.add_block_list(BlockType.BaseNet, backbone, base_out_channels[-1]) inter_channels = 128 global_context = GlobalAvgPooling(512, inter_channels, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(global_context.get_name(), global_context, base_out_channels[-1]) layer1 = RouteLayer('9') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer1.layers ]) self.add_block_list(layer1.get_name(), layer1, output_channel) context_path1 = ContextPath(512, inter_channels, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(context_path1.get_name(), context_path1, inter_channels) layer2 = RouteLayer('7') output_channel = sum([ base_out_channels[i] if i >= 0 else self.block_out_channels[i] for i in layer2.layers ]) self.add_block_list(layer2.get_name(), layer2, output_channel) context_path2 = ContextPath(256, inter_channels, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(context_path2.get_name(), context_path2, inter_channels) ffm = FeatureFusionBlock(256, 256, 4, bn_name=self.bn_name, activation_name=self.activation_name) self.add_block_list(ffm.get_name(), ffm, 256) conv1 = ConvBNActivationBlock(in_channels=256, out_channels=64, kernel_size=3, padding=1, bias=False, bnName=self.bn_name, activationName=self.activation_name) self.add_block_list(conv1.get_name(), conv1, 64) dropout = nn.Dropout(0.1) self.add_block_list(LayerType.Dropout, dropout, self.block_out_channels[-1]) conv2 = nn.Conv2d(64, self.class_number, 1) self.add_block_list(LayerType.Convolutional, conv2, self.class_number) layer = Upsample(scale_factor=8, mode='bilinear') self.add_block_list(layer.get_name(), layer, self.block_out_channels[-1]) self.create_loss()