예제 #1
0
    def __init__(self, num_classes, arg, pretrained=True):
        super(TubeTK, self).__init__()
        self.arg = arg
        if arg.backbone == 'res50':
            self.backbone = resnet50(freeze_stages=arg.freeze_stages,
                                     fst_l_stride=arg.model_stride[0][0])
        elif arg.backbone == 'res101':
            self.backbone = resnet101(freeze_stages=arg.freeze_stages,
                                      fst_l_stride=arg.model_stride[0][0])
        elif arg.backbone == 'resx101':
            self.backbone = resnext101(freeze_stages=arg.freeze_stages,
                                       fst_l_stride=arg.model_stride[0][0])
        else:
            raise NotImplementedError
        self.neck = FPN(in_channels=[512, 1024, 2048], arg=arg)
        self.tube_head = TrackHead(
            arg=arg,
            num_classes=num_classes,
            in_channels=self.neck.out_channels,
            strides=[[
                arg.model_stride[i][0] / (arg.forward_frames * 2) *
                arg.value_range,
                arg.model_stride[i][1] / arg.img_size[0] * arg.value_range,
                arg.model_stride[i][1] / arg.img_size[1] * arg.value_range
            ] for i in range(5)])

        if pretrained and arg.pretrain_model_path != '':
            self.load_pretrain(model_path=arg.pretrain_model_path)
        torch.cuda.empty_cache()
예제 #2
0
    def __init__(self, backbone='resnet101', n_classes=20, k_words=256):
        super(Net, self).__init__()

        if backbone == 'resnet50':
            self.resnet = resnet.resnet50(pretrained=True,
                                          strides=(2, 2, 2, 1))
        elif backbone == 'resnet101':
            self.resnet = resnet.resnet101(pretrained=True,
                                           strides=(2, 2, 2, 1))

        self.n_classes = n_classes
        self.k_words = k_words

        self.stage1 = nn.Sequential(self.resnet.conv1, self.resnet.bn1,
                                    self.resnet.relu, self.resnet.maxpool,
                                    self.resnet.layer1)
        self.stage2 = nn.Sequential(self.resnet.layer2)
        self.stage3 = nn.Sequential(self.resnet.layer3)
        self.stage4 = nn.Sequential(self.resnet.layer4)

        self.classifier = nn.Conv2d(2048, self.n_classes, 1, bias=False)
        self.fc_a = nn.Conv2d(self.k_words, self.n_classes, 1, bias=False)
        self.fc_b = nn.Conv2d(2048, self.k_words, 1, bias=False)
        self.VWE = VWE(k_words=self.k_words)

        self.backbone = nn.ModuleList(
            [self.stage1, self.stage2, self.stage3, self.stage4])
        self.newly_added = nn.ModuleList(
            [self.classifier, self.VWE, self.fc_a, self.fc_b])
예제 #3
0
    def __init__(self, trunk_name, output_stride=8, pretrained=True):
        super(get_resnet, self).__init__()

        if trunk_name == "resnet18":
            resnet = resnet18(pretrained=pretrained)
            resnet.layer0 = nn.Sequential(resnet.conv1, resnet.bn1,
                                          resnet.relu, resnet.maxpool)
        elif trunk_name == "resnet34":
            resnet = resnet34(pretrained=pretrained)
            resnet.layer0 = nn.Sequential(resnet.conv1, resnet.bn1,
                                          resnet.relu, resnet.maxpool)
        elif trunk_name == "resnet50":
            resnet = resnet50(pretrained=pretrained)
            resnet.layer0 = nn.Sequential(resnet.conv1, resnet.bn1,
                                          resnet.relu, resnet.maxpool)
        elif trunk_name == "resnet101":
            resnet = resnet101(pretrained=pretrained)
            resnet.layer0 = nn.Sequential(resnet.conv1, resnet.bn1,
                                          resnet.relu, resnet.maxpool)
        elif trunk_name == "resnet152":
            resnet = resnet152(pretrained=pretrained)
            resnet.layer0 = nn.Sequential(resnet.conv1, resnet.bn1,
                                          resnet.relu, resnet.maxpool)
        elif trunk_name == "resnext101_32x8d":
            resnet = resnext101_32x8d(pretrained=pretrained)
            resnet.layer0 = nn.Sequential(resnet.conv1, resnet.bn1,
                                          resnet.relu, resnet.maxpool)
        elif trunk_name == "se_resnet50":
            resnet = se_resnext50_32x4d(pretrained=pretrained)
        elif trunk_name == "se_resnet101":
            resnet = se_resnext101_32x4d(pretrained=pretrained)
        else:
            raise KeyError("[*] Not a valid network arch")

        self.layer0 = resnet.layer0
        self.layer1, self.layer2, self.layer3, self.layer4 = \
            resnet.layer1, resnet.layer2, resnet.layer3, resnet.layer4

        if output_stride == 8:
            for n, m in self.layer3.named_modules():
                if 'conv2' in n:
                    m.dilation, m.padding, m.stride = (2, 2), (2, 2), (1, 1)
                elif 'downsample.0' in n:
                    m.stride = (1, 1)
            for n, m in self.layer4.named_modules():
                if 'conv2' in n:
                    m.dilation, m.padding, m.stride = (4, 4), (4, 4), (1, 1)
                elif 'downsample.0' in n:
                    m.stride = (1, 1)
        elif output_stride == 16:
            for n, m in self.layer4.named_modules():
                if 'conv2' in n:
                    m.dilation, m.padding, m.stride = (2, 2), (2, 2), (1, 1)
                elif 'downsample.0' in n:
                    m.stride = (1, 1)
        else:
            raise KeyError(
                "[*] Unsupported output_stride {}".format(output_stride))
예제 #4
0
 def __init__(self, num_classes=256, input_bits=8, embed_dim=8, seq_len=100, backbone='resnet50'):
     super(CNN, self).__init__()
     self.input_bits = input_bits
     self.seq_len = seq_len
     # self.embed = nn.Embedding(256, 256)
     self.embed = nn.Embedding(2 ** input_bits, embed_dim)
     self.backbone = {
         'resnet18': resnet18(n_classes=num_classes, input_channels=embed_dim),
         'resnet34': resnet34(n_classes=num_classes, input_channels=embed_dim),
         'resnet50': resnet50(n_classes=num_classes, input_channels=embed_dim),
         'resnet101': resnet101(n_classes=num_classes, input_channels=embed_dim),
         'resnet152': resnet152(n_classes=num_classes, input_channels=embed_dim),
     }.get(backbone, 'resnet50')
예제 #5
0
    def __init__(self, args, train_transform=None, test_transform=None, val_transform=None):
        self.args = args
        self.train_transform = train_transform
        self.test_transform = test_transform
        self.val_transform = val_transform
        
        self.loss_lams = torch.zeros(args.num_classes, args.num_classes, dtype=torch.float32).cuda()
        self.loss_lams[:, :] = 1. / args.num_classes
        self.loss_lams.requires_grad = False

        if self.args.arch == 'resnet50':
            self.net = resnet.resnet50(num_classes=args.num_classes)
        elif self.args.arch == 'res2net50':
            self.net = res2net.res2net50_26w_4s(num_classes=args.num_classes)
        elif self.args.arch == 'resnet101':
            self.net = resnet.resnet101()
        elif self.args.arch == 'resnet18':
            self.net = resnet.resnet18()
        elif self.args.arch == 'resnet34':
            self.net = resnet.resnet34()
        elif self.args.arch == 'vgg16':
            self.net = vgg_cub.vgg16()
        elif self.args.arch == 'vgg16_bn':
            self.net = vgg_cub.vgg16_bn()
        elif self.args.arch == 'vgg19':
            self.net = vgg_cub.vgg19()
        elif self.args.arch == 'vgg19_bn':
            self.net = vgg_cub.vgg19_bn()
        elif self.args.arch == 'vgg16_std':
            self.net = vgg_std.vgg16()
        elif self.args.arch == 'vgg16_bn_std':
            self.net = vgg_std.vgg16_bn()
        elif self.args.arch == 'mobilenetv2':
            self.net = mobilenetv2.mobilenet_v2(num_classes=args.num_classes)

        if self.args.load_model is not None:
            self.net.load_state_dict(torch.load(self.args.load_model), strict=True)
            print('load model from %s' % self.args.load_model)
        elif self.args.pretrained_model is not None:
            self.net.load_state_dict(torch.load(self.args.pretrained_model), strict=False)
            print('load pretrained model form %s' % self.args.pretrained_model)
        else:
            print('not load any model, will train from scrach!')

        if args.expname is None:
            args.expname = 'runs/{}_{}_{}'.format(args.arch, args.dataset, args.method)
        os.makedirs(args.expname, exist_ok=True)
예제 #6
0
def create_model(Model, num_classes):
    if Model == "ResNet18":
        model = resnet18(pretrained=False)
        fc_features = model.fc.in_features
        model.fc = nn.Linear(fc_features, num_classes)
        model = model.cuda()

    elif Model == "ResNet34":
        model = resnet34(pretrained=False).cuda()
        fc_features = model.fc.in_features
        model.fc = nn.Linear(fc_features, num_classes)
        model = model.cuda()

    elif Model == "ResNet50":
        model = resnet50(pretrained=False).cuda()
        fc_features = model.fc.in_features
        model.fc = nn.Linear(fc_features, num_classes)
        model = model.cuda()

    elif Model == "ResNet101":
        model = resnet101(pretrained=False).cuda()
        fc_features = model.fc.in_features
        model.fc = nn.Linear(fc_features, num_classes)
        model = model.cuda()

    elif Model == "MobileNet_v2":
        model = mobilenet_v2(num_classes=num_classes, pretrained=False).cuda()

    elif Model == "Mobilenetv3":
        model = mobilenetv3(n_class=num_classes, pretrained=False).cuda()

    elif Model == "shufflenet_v2_x0_5":
        model = shufflenet_v2_x0_5(pretrained=False,
                                   num_classes=num_classes).cuda()

    elif Model == "shufflenet_v2_x1_0":
        model = shufflenet_v2_x1_0(pretrained=False,
                                   num_classes=num_classes).cuda()

    elif Model == "shufflenet_v2_x1_5":
        model = shufflenet_v2_x1_5(pretrained=False,
                                   num_classes=num_classes).cuda()

    elif Model == "shufflenet_v2_x1_5":
        model = shufflenet_v2_x2_0(pretrained=False,
                                   num_classes=num_classes).cuda()

    elif "efficientnet" in Model:
        model = EfficientNet.from_pretrained(Model,
                                             num_classes=num_classes,
                                             pretrained=False).cuda()

    elif Model == "inception_v3":
        model = inception_v3(pretrained=False, num_classes=num_classes).cuda()

    elif Model == "mnasnet0_5":
        model = mnasnet0_5(pretrained=False, num_classes=num_classes).cuda()

    elif Model == "vgg11":
        model = vgg11(pretrained=False, num_classes=num_classes).cuda()

    elif Model == "vgg11_bn":
        model = vgg11_bn(pretrained=False, num_classes=num_classes).cuda()

    elif Model == "vgg19":
        model = vgg19(pretrained=False, num_classes=num_classes).cuda()

    elif Model == "densenet121":
        model = densenet121(pretrained=False).cuda()

    elif Model == "ResNeXt29_32x4d":
        model = ResNeXt29_32x4d(num_classes=num_classes).cuda()

    elif Model == "ResNeXt29_2x64d":
        model = ResNeXt29_2x64d(num_classes=num_classes).cuda()

    else:
        print("model error")

    # input = torch.randn(1, 3, 32, 32).cuda()
    # flops, params = profile(model, inputs=(input,))
    # flops, params = clever_format([flops, params], "%.3f")
    # print("------------------------------------------------------------------------")
    # print("                              ",Model)
    # print( "                    flops:", flops, "    params:", params)
    # print("------------------------------------------------------------------------")

    return model