Exemplo n.º 1
0
 def test_get_composed_model(self):
     for backbone_name in test_backbone_name_list:
         for classifier_name in test_classifier_name_list:
             print(backbone_name + '+' + classifier_name + ":")
             model = get_composed_model(backbone_name, classifier_name)
             testModel(model)
     for backbone_name in test_UNet_backbone_list:
         for classifier_name in test_UNet_classifier_list:
             print(backbone_name + '+' + classifier_name + ":")
             model = get_composed_model(backbone_name, classifier_name)
             testModel(model)
Exemplo n.º 2
0
class rSoftMax(nn.Module):
    def __init__(self, radix, cardinality):
        super().__init__()
        self.radix = radix
        self.cardinality = cardinality

    def forward(self, x):
        batch = x.size(0)
        if self.radix > 1:
            x = x.view(batch, self.cardinality, self.radix, -1).transpose(1, 2)
            x = F.softmax(x, dim=1)
            x = x.reshape(batch, -1)
        else:
            x = torch.sigmoid(x)
        return x


if __name__ == '__main__':
    model = ResNestBase([3, 4, 6, 3],
                        in_channels=3,
                        num_classes=1,
                        radix=2,
                        groups=1,
                        bottleneck_width=64,
                        deep_stem=True,
                        stem_width=32,
                        avg_down=True,
                        avd=True,
                        avd_first=False)
    testModel(model)
Exemplo n.º 3
0
            out = torch.cat((x, y), dim=1)
            out = self.fusion(out)
        elif self.fusion_function == 'max':
            out = torch.max(x, y)
        elif self.fusion_function == 'sum':
            out = torch.add(x, y)
        return out


if __name__ == '__main__':
    backbone = ResNet_BB(3, clf='PointRend')
    classifier = DeepLabV3Head(backbone.out_channels, 2)
    modelA = SimpleSegmentationModel(backbone,
                                     classifier,
                                     if_point_rend_upsample=True)
    testModel(modelA, eval=True)

    # backbone = TwoInput_NDDRLSC_BB(3, 3, clf='PointRend')
    # classifier = DeepLabV3Head(backbone.out_channels, 2)
    # modelA = TwoInputSegmentationModel(backbone, classifier, if_point_rend_upsample=True)
    # test2IModel(modelA, eval=True)

# 原始torchvision给出 backbone-classfier模型:backbone 将input(c, h, w) 变为 (2048, h/8, w/8), 二分类为(1, h/8, w/8), 最后再插值回去。
# 这样使得每个像素要对应之前的64个像素,显然是表达困难的... 需要重新实现!
# input output shapetorch.Size([8, 3, 224, 224])
# backbone output shapetorch.Size([8, 2048, 28, 28])
# classifier output shapetorch.Size([8, 1, 28, 28])
# result output shapetorch.Size([8, 1, 224, 224])
# input output shapetorch.Size([8, 3, 448, 448])
# backbone output shapetorch.Size([8, 2048, 56, 56])
# classifier output shapetorch.Size([8, 1, 56, 56])
Exemplo n.º 4
0
 def test_get_model(self):
     for model_name in model_name_list:
         print(model_name + ':')
         model = get_model(model_name)
         testModel(model)
Exemplo n.º 5
0
        x2 = self.down1(x1)
        x3 = self.down2(x2)
        x4 = self.down3(x3)
        x5 = self.down4(x4)
        x6 = self.up1(x5, x4)
        x7 = self.up2(x6, x3)
        x8 = self.up3(x7, x2)
        x9 = self.up4(x8, x1)

        x6 = F.interpolate(x6, size=size, mode='bilinear', align_corners=True)
        x6 = self.after_upsample6(x6)
        x6 = torch.mul(x6, self.layers_weights[0])
        x7 = F.interpolate(x7, size=size, mode='bilinear', align_corners=True)
        x7 = self.after_upsample7(x7)
        x7 = torch.mul(x7, self.layers_weights[1])
        x8 = F.interpolate(x8, size=size, mode='bilinear', align_corners=True)
        x8 = self.after_upsample8(x8)
        x8 = torch.mul(x8, self.layers_weights[2])
        x9 = F.interpolate(x9, size=size, mode='bilinear', align_corners=True)
        x9 = self.after_upsample9(x9)
        x9 = torch.mul(x9, self.layers_weights[3])

        x = torch.cat([x6, x7, x8, x9], dim=1)
        x = self.outc(x)

        return x


if __name__ == '__main__':
    testModel(DeepMadSeg(5, 1))
Exemplo n.º 6
0
                               filters_dict['contractive_' + str(i)][1],
                               filt_pair[0]))
            filters_dict['expansive_' + str(i)] = (filt_pair[1], filt_pair[0])
            filt_pair[1] = filt_pair[0]
            filt_pair[0] = filt_pair[0] // 2

        self.output = nn.Conv2d(filt_pair[1], n_labels, kernel_size=1)
        filters_dict['output'] = (filt_pair[1], n_labels)
        self.filters_dict = filters_dict

    # final_forward
    def forward(self, x):
        c00, c0 = self.contractive_0(x)
        c11, c1 = self.contractive_1(c0)
        c22, c2 = self.contractive_2(c1)
        c33, c3 = self.contractive_3(c2)
        bottle = self.bottleneck(c3)
        u3 = F.relu(self.expansive_3(bottle, c33))
        u2 = F.relu(self.expansive_2(u3, c22))
        u1 = F.relu(self.expansive_1(u2, c11))
        u0 = F.relu(self.expansive_0(u1, c00))
        return F.softmax(self.output(u0), dim=1)


if __name__ == '__main__':
    testModel(U_Net(5, 1))
    testModel(R2U_Net(5, 1))
    testModel(AttU_Net(5, 1))
    testModel(R2AttU_Net(5, 1))
    testModel(NestedUNet(5, 1))