Exemplo n.º 1
0
def make_model():
    resnet50_backbone = get_backbone()
    loss_fn = RetinaNetLoss(num_classes)
    model = RetinaNet(num_classes, resnet50_backbone)

    optimizer = tf.optimizers.SGD(learning_rate=learning_rate_fn, momentum=0.9)
    model.compile(loss=loss_fn, optimizer=optimizer)
    return model
Exemplo n.º 2
0
 def __init__(self, backbone=None, **kwargs):
     super(FeaturePyramid, self).__init__(name="FeaturePyramid", **kwargs)
     self.backbone = backbone if backbone else get_backbone()
     self.conv_c3_1x1 = tf.keras.layers.Conv2D(256, 1, 1, "same")
     self.conv_c4_1x1 = tf.keras.layers.Conv2D(256, 1, 1, "same")
     self.conv_c5_1x1 = tf.keras.layers.Conv2D(256, 1, 1, "same")
     self.conv_c3_3x3 = tf.keras.layers.Conv2D(256, 3, 1, "same")
     self.conv_c4_3x3 = tf.keras.layers.Conv2D(256, 3, 1, "same")
     self.conv_c5_3x3 = tf.keras.layers.Conv2D(256, 3, 1, "same")
     self.conv_c6_3x3 = tf.keras.layers.Conv2D(256, 3, 2, "same")
     self.conv_c7_3x3 = tf.keras.layers.Conv2D(256, 3, 2, "same")
     self.upsample_2x = tf.keras.layers.UpSampling2D(2)
Exemplo n.º 3
0
    def __init__(self, cfg):
        super(FineTuneNet, self).__init__()
        trunk = cfg.MODEL.TRUNK
        num_classes = cfg.MODEL.FINETUNING.NUM_CLASSES
        assert num_classes > 0 and isinstance(
            num_classes, int
        ), 'Please give a positive integer for the number of classes in the finetuning stage'
        p_dropout = cfg.MODEL.FINETUNING.DROPOUT

        self.backbone, backbone_channels = get_backbone(trunk)
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.dropout = nn.Dropout(p=p_dropout)
        self.fc = nn.Linear(backbone_channels, num_classes)
Exemplo n.º 4
0
    def __init__(self, cfg):
        super(PreTrainNet, self).__init__()
        trunk = cfg.MODEL.TRUNK
        fc_dim = cfg.MODEL.PRETRAINING.FC_DIM

        self.backbone, backbone_channels = get_backbone(trunk)
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc_contrastive = nn.Linear(backbone_channels, fc_dim)
        self.l2norm = Normalise(2)

        self.fc_rotation_1 = nn.Linear(backbone_channels, 200)
        self.bn_rotation_1 = nn.BatchNorm1d(num_features=200)
        self.fc_rotation_2 = nn.Linear(200, 200)
        self.bn_rotation_2 = nn.BatchNorm1d(num_features=200)
        self.fc_rotation_3 = nn.Linear(200, 4)
Exemplo n.º 5
0
                                           shuffle=True,
                                           num_workers=config.num_workers,
                                           pin_memory=True,
                                           drop_last=True)
test_loader = torch.utils.data.DataLoader(dataset=test_set,
                                          batch_size=config.batch_size,
                                          shuffle=False,
                                          num_workers=config.num_workers,
                                          pin_memory=True,
                                          drop_last=True)

print("Batches train set: {}".format(len(train_loader)))
print("Barches test set:  {}".format(len(test_loader)))

# model
backbone = get_backbone(train_config.backbone)
model = SimSiam(backbone, train_config.projector_args,
                train_config.predictor_args)
msg = model.load_state_dict(ckpt["state_dict"], strict=True)
print("Loading weights: {}".format(msg))
model = model.encoder
model = model.to(config.device)

# classifier
classifier = nn.Linear(in_features=train_config.projector_args["out_dim"],
                       out_features=len(test_set.classes),
                       bias=True)
classifier = classifier.to(config.device)

optimizer = get_optimizer(config.optimizer, classifier, config.optimizer_args)
Exemplo n.º 6
0
def main():
    # data normalization
    input_size = 224
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    # data loaders
    kwargs = {'num_workers': 8, 'pin_memory': True} if args.cuda else {}

    if args.da:
        train_transforms = transforms.Compose([
            random_transform,
            transforms.ToPILImage(),
            transforms.Resize((input_size, input_size)),
            transforms.ToTensor(), normalize
        ])
    else:
        train_transforms = transforms.Compose([
            transforms.ToPILImage(),
            transforms.Resize((input_size, input_size)),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(), normalize
        ])

    test_transforms = transforms.Compose([
        transforms.ToPILImage(),
        transforms.Resize((input_size, input_size)),
        transforms.ToTensor(), normalize
    ])

    train_loader = torch.utils.data.DataLoader(DataLoader(df_train,
                                                          train_transforms,
                                                          root=args.data_dir,
                                                          mode=args.mode),
                                               batch_size=args.batch_size,
                                               shuffle=True,
                                               **kwargs)

    test_loader = torch.utils.data.DataLoader(DataLoader(df_gal,
                                                         test_transforms,
                                                         root=args.data_dir,
                                                         mode=args.mode),
                                              batch_size=args.batch_size,
                                              shuffle=False,
                                              **kwargs)

    # instanciate the models
    output_shape, backbone = get_backbone(args)
    embed = LinearProjection(output_shape, args.dim_embed)
    model = ConvNet(backbone, embed)

    # instanciate the proxies
    fsem = get_semantic_fname(args.word)
    path_semantic = os.path.join('aux', 'Semantic', args.dataset, fsem)
    train_proxies = get_proxies(path_semantic, df_train['cat'].cat.categories)
    test_proxies = get_proxies(path_semantic, df_gal['cat'].cat.categories)

    train_proxynet = ProxyNet(args.n_classes,
                              args.dim_embed,
                              proxies=torch.from_numpy(train_proxies))
    test_proxynet = ProxyNet(args.n_classes_gal,
                             args.dim_embed,
                             proxies=torch.from_numpy(test_proxies))

    # criterion
    criterion = ProxyLoss(args.temperature)

    if args.multi_gpu:
        model = nn.DataParallel(model)

    if args.cuda:
        backbone.cuda()
        embed.cuda()
        model.cuda()
        train_proxynet.cuda()
        test_proxynet.cuda()

    parameters_set = []

    low_layers = []
    upper_layers = []

    for c in backbone.children():
        low_layers.extend(list(c.parameters()))
    for c in embed.children():
        upper_layers.extend(list(c.parameters()))

    parameters_set.append({
        'params': low_layers,
        'lr': args.lr * args.factor_lower
    })
    parameters_set.append({'params': upper_layers, 'lr': args.lr * 1.})

    optimizer = optim.SGD(parameters_set,
                          lr=args.lr,
                          momentum=0.9,
                          nesterov=True,
                          weight_decay=args.wd)

    n_parameters = sum([p.data.nelement() for p in model.parameters()])
    print('  + Number of params: {}'.format(n_parameters))

    scheduler = CosineAnnealingLR(optimizer,
                                  args.epochs * len(train_loader),
                                  eta_min=3e-6)

    print('Starting training...')
    for epoch in range(args.start_epoch, args.epochs + 1):
        # update learning rate
        scheduler.step()

        # train for one epoch
        train(train_loader, model, train_proxynet.proxies.weight, criterion,
              optimizer, epoch, scheduler)

        val_acc = evaluate(test_loader, model, test_proxynet.proxies.weight,
                           criterion)

        # saving
        if epoch == args.epochs:
            save_checkpoint({'epoch': epoch, 'state_dict': model.state_dict()})

    print('\nResults on test set (end of training)')
    write_logs('\nResults on test set (end of training)')
    test_acc = evaluate(test_loader, model, test_proxynet.proxies.weight,
                        criterion)
Exemplo n.º 7
0
def main():
    # data normalization
    input_size = 224
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    # data loaders
    kwargs = {'num_workers': 8, 'pin_memory': True} if args.cuda else {}

    test_transforms = transforms.Compose([
        transforms.ToPILImage(),
        transforms.Resize((input_size, input_size)),
        transforms.ToTensor(), normalize
    ])

    feats = {}
    labels = {}

    for domain in ['im', 'sk']:
        key = '_'.join([domain, 'model_path'])
        dirname = os.path.dirname(args.__dict__[key])
        fpath = os.path.join(dirname, 'features.npz')

        results_path = os.path.join(dirname, 'results.txt')

        if os.path.isfile(fpath) and args.rewrite is False:
            data = np.load(fpath)
            feats[domain] = data['features']
            labels[domain] = data['labels']

            txt = ('Domain (%s): Acc %.2f' % (domain, data['acc'] * 100.))
            print(txt)
            write_logs(txt, results_path)

            df_gal = splits[domain]['gal']
            fsem = get_semantic_fname(args.word)
            path_semantic = os.path.join('aux', 'Semantic', args.dataset, fsem)
            test_proxies = get_proxies(path_semantic,
                                       df_gal['cat'].cat.categories)
        else:
            df_gal = splits[domain]['gal']

            test_loader = torch.utils.data.DataLoader(
                DataLoader(df_gal,
                           test_transforms,
                           root=args.data_dir,
                           mode=domain),
                batch_size=args.batch_size * 10,
                shuffle=False,
                **kwargs)

            # instanciate the models
            output_shape, backbone = get_backbone(args)
            embed = LinearProjection(output_shape, args.dim_embed)
            model = ConvNet(backbone, embed)

            # instanciate the proxies
            fsem = get_semantic_fname(args.word)
            path_semantic = os.path.join('aux', 'Semantic', args.dataset, fsem)
            test_proxies = get_proxies(path_semantic,
                                       df_gal['cat'].cat.categories)

            test_proxynet = ProxyNet(args.n_classes_gal,
                                     args.dim_embed,
                                     proxies=torch.from_numpy(test_proxies))

            # criterion
            criterion = ProxyLoss(args.temperature)

            if args.multi_gpu:
                model = nn.DataParallel(model)

            # loading
            checkpoint = torch.load(args.__dict__[key])
            model.load_state_dict(checkpoint['state_dict'])
            txt = ("\n=> loaded checkpoint '{}' (epoch {})".format(
                args.__dict__[key], checkpoint['epoch']))
            print(txt)
            write_logs(txt, results_path)

            if args.cuda:
                backbone.cuda()
                embed.cuda()
                model.cuda()
                test_proxynet.cuda()

            txt = 'Extracting testing set (%s)...' % (domain)
            print(txt)
            x, y, acc = extract_predict(test_loader, model,
                                        test_proxynet.proxies.weight,
                                        criterion)

            feats[domain] = x
            labels[domain] = y

            np.savez(fpath,
                     features=feats[domain],
                     labels=labels[domain],
                     acc=acc)

            fpath_train = os.path.join(dirname, 'features_train.npz')
            if args.train and not os.path.isfile(fpath_train):
                df_train = splits[domain]['train']

                train_loader = torch.utils.data.DataLoader(
                    DataLoader(df_train,
                               test_transforms,
                               root=args.data_dir,
                               mode=domain),
                    batch_size=args.batch_size * 10,
                    shuffle=False,
                    **kwargs)

                train_proxies = get_proxies(path_semantic,
                                            df_train['cat'].cat.categories)

                train_proxynet = ProxyNet(
                    args.n_classes_gal,
                    args.dim_embed,
                    proxies=torch.from_numpy(train_proxies))
                train_proxynet.cuda()
                txt = 'Extracting training set (%s)...' % (domain)
                print(txt)

                x, y, _ = extract_predict(train_loader, model,
                                          train_proxynet.proxies.weight,
                                          criterion)

                fpath = os.path.join(dirname, 'features_train.npz')

                np.savez(fpath,
                         features=feats[domain],
                         features_train=x,
                         labels=labels[domain],
                         labels_train=y,
                         acc=acc)

            txt = ('Domain (%s): Acc %.2f' % (domain, acc * 100.))
            print(txt)
            write_logs(txt, results_path)

    if args.shape:
        print('\nRetrieval per model')
        new_feat_im, new_labels_im = average_views(splits['im']['test'],
                                                   feats['im'], labels['im'])

        idx = retrieve(feats['sk'], new_feat_im)

        metrics = score_shape(labels['sk'], new_labels_im, idx)
        names = ['NN', 'FT', 'ST', 'E', 'nDCG', 'mAP']
        txt = [('%s %.3f' % (name, value))
               for name, value in zip(names, metrics)]
        txt = '\t'.join(txt)
        print(txt)
        write_logs(txt, results_path)

        print('\nRetrieval per model with refinement')

        alpha = 0.4

        g_sk_x = KNN(feats['sk'], new_feat_im, K=1, mode='ones')
        new_sk_x = slerp(alpha, L2norm(feats['sk']), L2norm(g_sk_x))
        idx = retrieve(new_sk_x, new_feat_im)
        metrics = score_shape(labels['sk'], new_labels_im, idx)
        names = ['NN', 'FT', 'ST', 'E', 'nDCG', 'mAP']
        txt = [('%s %.3f' % (name, value))
               for name, value in zip(names, metrics)]
        txt = '\t'.join(txt)
        print(txt)
        write_logs(txt, results_path)

    else:
        print('\nRetrieval')
        txt = evaluate(feats['im'], labels['im'], feats['sk'], labels['sk'])
        print(txt)
        write_logs(txt, results_path)

        print('\nRetrieval with refinement')
        if args.overwrite:
            alpha = 0.7
        else:
            alpha = 0.4

        g_sk_x = KNN(feats['sk'], feats['im'], K=1, mode='ones')

        new_sk_x = slerp(alpha, L2norm(feats['sk']), L2norm(g_sk_x))
        txt = evaluate(feats['im'], labels['im'], new_sk_x, labels['sk'])
        print(txt)
        write_logs(txt, results_path)
Exemplo n.º 8
0
def main():
    # data normalization
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    # data loaders
    kwargs = {'num_workers': 8, 'pin_memory': True} if args.cuda else {}

    test_transforms = transforms.Compose([
        transforms.ToPILImage(),
        transforms.Resize((224, 224)),
        transforms.ToTensor(), normalize
    ])

    feats = {}
    labels = {}

    for domain in ['im', 'sk']:
        key = '_'.join([domain, 'model_path'])
        dirname = os.path.dirname(args.__dict__[key])
        fpath = os.path.join(dirname, 'features.npz')

        results_path = os.path.join(dirname, 'results.txt')

        if os.path.isfile(fpath) and args.rewrite is False:
            data = np.load(fpath)
            feats[domain] = data['features']
            labels[domain] = data['labels']

            txt = ('Domain (%s): Acc %.2f' % (domain, data['acc'] * 100.))
            print(txt)
            write_logs(txt, results_path)

            df_gal = splits[domain]['test']
            fsem = get_semantic_fname(args.word)
            path_semantic = os.path.join('aux', 'Semantic', args.dataset, fsem)
            test_proxies = get_proxies(path_semantic,
                                       df_gal['cat'].cat.categories)
        else:
            df_gal = splits[domain]['test']

            test_loader = torch.utils.data.DataLoader(
                DataLoader(df_gal,
                           test_transforms,
                           root=args.data_dir,
                           mode=domain),
                batch_size=args.batch_size * 1,
                shuffle=False,
                **kwargs)

            # instanciate the models
            output_shape, backbone = get_backbone(args)
            embed = LinearProjection(output_shape, args.dim_embed)
            model = ConvNet(backbone, embed)

            # instanciate the proxies
            fsem = get_semantic_fname(args.word)
            path_semantic = os.path.join('aux', 'Semantic', args.dataset, fsem)
            test_proxies = get_proxies(path_semantic,
                                       df_gal['cat'].cat.categories)

            test_proxynet = ProxyNet(args.n_classes_gal,
                                     args.dim_embed,
                                     proxies=torch.from_numpy(test_proxies))

            # criterion
            criterion = ProxyLoss(args.temperature)

            if args.multi_gpu:
                model = nn.DataParallel(model)

            # loading
            checkpoint = torch.load(args.__dict__[key])
            model.load_state_dict(checkpoint['state_dict'])
            txt = ("\n=> loaded checkpoint '{}' (epoch {})".format(
                args.__dict__[key], checkpoint['epoch']))
            print(txt)

            if args.cuda:
                backbone.cuda()
                embed.cuda()
                model.cuda()
                test_proxynet.cuda()

            txt = 'Extracting testing set (%s)...' % (domain)
            print(txt)
            x, y, acc = extract_predict(test_loader, model,
                                        test_proxynet.proxies.weight,
                                        criterion)

            feats[domain] = x
            labels[domain] = y

            np.savez(fpath,
                     features=feats[domain],
                     labels=labels[domain],
                     acc=acc)

            txt = ('Domain (%s): Acc %.2f' % (domain, acc * 100.))
            print(txt)

    print('\nFew-Shot')
    fs(feats, labels, test_proxies)