lr = 1e-3
optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=1e-4)
scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1)
n_epochs = 20
log_interval = 50

fit(online_train_loader,
    online_test_loader,
    model,
    loss_fn,
    optimizer,
    scheduler,
    n_epochs,
    cuda,
    log_interval,
    metrics=[AverageNonzeroTripletsMetric()])

train_embeddings_otl, train_labels_otl = extract_embeddings(
    train_loader, model)
plot_embeddings(train_embeddings_otl, train_labels_otl)
val_embeddings_otl, val_labels_otl = extract_embeddings(test_loader, model)
plot_embeddings(val_embeddings_otl, val_labels_otl)

# display_emb_online, display_emb, display_label_online, display_label = train_embeddings_otl, train_embeddings_tl, train_labels_otl, train_labels_tl
display_emb_online, display_emb, display_label_online, display_label = val_embeddings_otl, val_embeddings_tl, val_labels_otl, val_labels_tl
x_lim = (np.min(display_emb_online[:, 0]), np.max(display_emb_online[:, 0]))
y_lim = (np.min(display_emb_online[:, 1]), np.max(display_emb_online[:, 1]))
plot_embeddings(display_emb, display_label, x_lim, y_lim)
plot_embeddings(display_emb_online, display_label_online, x_lim, y_lim)

x_lim = (np.min(train_embeddings_ocl[:, 0]), np.max(train_embeddings_ocl[:,
Example #2
0
    def train_net(self,
                  num_epochs,
                  lr,
                  triplet_selector,
                  gmp_lambda,
                  margin=1.,
                  scheduler_step=config.SCHEDULER_STEP,
                  lr_decay=config.LR_DECAY,
                  weight_decay=config.WEIGHT_DECAY,
                  log_interval=50):

        # TODO: different LR for different layers
        # parameter_options = [
        #     {'params': encoder.model.layer1.parameters(), 'lr': lr / 1000},
        #     {'params': encoder.model.layer2.parameters(), 'lr': lr / 100},
        #     {'params': encoder.model.layer3.parameters(), 'lr': lr / 10},
        #     {'params': encoder.model.layer4.parameters(), 'lr': lr / 10},
        #     {'params': encoder.model.avgpool.parameters(), 'lr': lr},
        #     {'params': encoder.model.fc.parameters(), 'lr': lr}
        # ]
        parameters = [{
            'params': self.encoder.feature_extractor.parameters()
        }, {
            'params': self.encoder.pool.parameters(),
            'lr': config.POOL_LR_MULTIPLIER * lr,
            'weight_decay': 0
        }]

        if self.optimizer == 'adam' or self.optimizer == 'Adam':
            optimizer = optim.Adam(parameters,
                                   lr,
                                   weight_decay=weight_decay,
                                   amsgrad=True)
        elif self.optimizer in ('sgd', 'SGD'):
            optimizer = optim.SGD(parameters,
                                  lr,
                                  momentum=0.9,
                                  weight_decay=weight_decay,
                                  nesterov=True)

        scheduler = None
        plateau_scheduler = None
        if self.scheduler == 'step-lr':
            scheduler = lr_scheduler.StepLR(optimizer, scheduler_step,
                                            lr_decay)
            plateau_scheduler = None
        elif self.scheduler == 'plateau':
            scheduler = None
            plateau_scheduler = lr_scheduler.ReduceLROnPlateau(
                optimizer, 'max', 0.1, patience=scheduler_step, verbose=True)

        train_sampler = PerClassBatchSampler(self.data.train_data,
                                             num_classes=self.num_classes,
                                             num_samples=self.num_samples)
        val_sampler = PerClassBatchSampler(self.data.val_data,
                                           num_classes=self.num_classes,
                                           num_samples=self.num_samples)
        train_data_loader = self.data.get_train_data_loader(
            batch_sampler=train_sampler)
        val_loader = self.data.get_val_data_loader(batch_sampler=val_sampler)

        cuda = torch.cuda.is_available()
        if cuda:
            self.encoder.cuda()

        loss_fn = OnlineTripletLoss(margin, triplet_selector)

        triplet_net = TripletNetwork(self.encoder, triplet_loss=loss_fn)

        if config.USE_PATCHES:
            trans = torchvision.transforms.Compose([
                PatchCrop(400, resize_to=256),
                # torchvision.transforms.Resize(256),
                torchvision.transforms.Lambda(lambda crops: [
                    torchvision.transforms.ToTensor()(crop) for crop in crops
                ]),
            ])
            seq_loader = self.data.get_val_data_loader2(transform=trans)
            test_loader = self.data.get_test_data_loader(transform=trans)
        else:
            seq_loader = self.data.get_sequential_data_loader(batch_size=15)
            test_loader = self.data.get_competition_data_loader()

        triplet_net.train(train_data_loader,
                          val_loader,
                          num_epochs,
                          optimizer,
                          scheduler,
                          cuda,
                          log_interval,
                          gmp_lambda,
                          lr,
                          seq_loader,
                          margin,
                          test_loader=test_loader,
                          plateau_scheduler=plateau_scheduler,
                          metrics=[
                              AverageNonzeroTripletsMetric(),
                          ])
Example #3
0
def main():
    # print('fsafsdaf:', args.training_dataset, args.arch)
    print(">> Creating directory if it does not exist:\n>> '{}'".format(
        args.directory))
    if not os.path.exists(args.directory):
        os.makedirs(args.directory)

    log_dir = os.path.join(args.directory, 'log')
    if not os.path.exists(log_dir):
        os.mkdir(log_dir)

    params = {'architecture': args.arch, 'pooling': args.pool}
    n_classes = args.n_classes
    n_samples = args.n_samples
    cuda = args.cuda
    input_size = args.image_size
    transform, transform_te, transform_label = init_transform(input_size)
    kwargs = {'num_workers': 1, 'pin_memory': True} if cuda else {}
    online_train_loader, online_test_loader = init_data_loader(
        args.root, n_classes, n_samples, transform, transform_te,
        transform_label, kwargs)

    # Set up the network and training parameters
    model = init_network(params)
    parameters = []
    # add feature parameters
    parameters.append({'params': model.features.parameters()})
    if cuda:
        # print('model cuda:', cuda)
        model.cuda()
    pos_margin = 1.0
    neg_margin = 0.3
    # define optimizer
    if args.optimizer == 'sgd':
        optimizer = torch.optim.SGD(parameters,
                                    args.lr,
                                    momentum=args.momentum,
                                    weight_decay=args.weight_decay)
    elif args.optimizer == 'adam':
        optimizer = torch.optim.Adam(parameters,
                                     args.lr,
                                     weight_decay=args.weight_decay)

    metrics = [AverageNonzeroTripletsMetric()]
    if args.loss.startswith('OnlineContrastiveEucLoss'):
        loss_fn = OnlineContrastiveEucLoss(pos_margin, neg_margin,
                                           HardNegativePairSelector())
    elif args.loss.startswith('OnlineContrastiveCosLoss'):
        loss_fn = OnlineContrastiveCosLoss(args.loss_margin)
    elif args.loss.startswith('OnlineTriplet'):
        loss_fn = OnlineTripletLoss(
            args.loss_margin, HardestNegativeTripletSelector(args.loss_margin))

    exp_decay = math.exp(-0.01)
    scheduler = torch.optim.lr_scheduler.ExponentialLR(optimizer,
                                                       gamma=exp_decay)
    writer = SummaryWriter(log_dir=log_dir)
    writer.add_graph(model.features, torch.ones([1, 3, 224, 224]).cuda())
    fit(online_train_loader,
        online_test_loader,
        model,
        loss_fn,
        optimizer,
        scheduler,
        writer,
        metrics=metrics,
        args=args)
Example #4
0
def main():
    print()
    print('***************By Manyeo***************')

    # load data
    train_triplets = np.load(config.dir_train)
    val_triplets = np.load(config.dir_val)
    test_all = np.loadtxt(config.dir_test)
    print("train size:{}".format(train_triplets.shape[0]))
    print("val size:{}".format(val_triplets.shape[0]))
    print("test size:{}".format(test_all.shape[0]))

    # detect device
    cuda = torch.cuda.is_available()
    device = torch.device("cuda:0" if cuda else "cpu")
    print('device: ' + str(device) + '\n')

    # init dataset
    train_set = FoodDataset(config.dir_images,
                            train_triplets,
                            train=True,
                            val=False,
                            test=False)
    val_set = FoodDataset(config.dir_images,
                          val_triplets,
                          train=False,
                          val=True,
                          test=False)
    test_set = FoodDataset(config.dir_images,
                           test_all,
                           train=False,
                           val=False,
                           test=True)

    # load data in dataloader
    train_loader = DataLoader(train_set,
                              batch_size=32,
                              shuffle=True,
                              num_workers=16)
    val_loader = DataLoader(val_set,
                            batch_size=32,
                            shuffle=False,
                            num_workers=16)
    test_loader = DataLoader(test_set,
                             batch_size=32,
                             shuffle=False,
                             num_workers=16)
    print("Data Loaded.")

    # set up the model
    # embedding_net = ImageRetrievalModel(config.NUM_CLUSTERS, config.NUM_ENCODER_DIM)
    # embedding_net = EmbeddingNet()
    embedding_net = vgg_back()
    model = TripletNet(embedding_net)
    model = model.to(device)
    if (config.RESUME):
        model.load_state_dict(
            torch.load(config.resume_ckp_location,
                       map_location=torch.device(device)))
    # set up loss
    # loss_fn = TripletLoss(config.MARGIN)
    print("lr:{}".format(config.lr))
    loss_fn = SoftmaxTripletLoss()
    my_params = list(model.parameters())
    print("using adamW")
    optimizer = optim.AdamW(params=my_params,
                            lr=config.lr,
                            weight_decay=config.weight_decay)
    scheduler = optim.lr_scheduler.StepLR(optimizer,
                                          10,
                                          gamma=0.1,
                                          last_epoch=-1)
    n_epochs = config.num_epochs
    log_interval = config.log_interval
    save_root = config.save_root
    # fit the model
    fit(train_loader,
        val_loader,
        test_loader,
        model,
        loss_fn,
        optimizer,
        scheduler,
        n_epochs,
        cuda,
        log_interval,
        save_root,
        metrics=[AverageNonzeroTripletsMetric()])
        print("Let's use", torch.cuda.device_count(), "GPUs!")
        # dim = 0 [30, xxx] -> [10, ...], [10, ...], [10, ...] on 3 GPUs
        model = nn.DataParallel(model)
    model.cuda()

loss_fn = OnlineTripletLoss(margin, RandomNegativeTripletSelector(margin))
lr = 1e-3
optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=1e-4)
scheduler = lr_scheduler.StepLR(optimizer, 8, gamma=0.1, last_epoch=-1)
n_epochs = 20
log_interval = 10

if not resume_from_pth:
    print("begin fit")
    record_history = fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, n_epochs,
                         cuda, log_interval, metrics=[AverageNonzeroTripletsMetric()])
    with open('./{}/record_history.pkl'.format(experiment_folder), 'wb') as pkl_file:
        pickle.dump(record_history, pkl_file)
        pkl_file.close()

    torch.save(model.state_dict(), trained_weight_file)
else:
    with open('./{}/record_history.pkl'.format(experiment_folder), 'rb') as pkl_file:
        record_history = pickle.load(pkl_file)
        pkl_file.close()

    model.load_state_dict(torch.load(trained_weight_file))

plot_history(experiment_folder, record_history)

train_embeddings_baseline, train_labels_baseline = extract_embeddings(train_loader, model)
Example #6
0
def main(args):
    if os.path.exists('models') is False:
        os.makedirs('models')

    # img_list, base_path, item_dict = read_data(args.dataset, args.bbox)
    img_list, base_path, item_dict = read_data("DeepFashion2", bbox_gt=False)
    model_save_path = args.model_path  # 'models/siames_triplet_df2.pth'

    # writer = SummaryWriter('runs/fashion_mnist_experiment_1')
    model = ResNetbasedNet()
    if os.path.exists(model_save_path):
        model.load_state_dict(torch.load(model_save_path))

    cuda = torch.cuda.is_available()
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    if cuda:
        if torch.cuda.device_count() > 1:
            model = nn.DataParallel(model)
        model.to(device)

    kwargs = {'num_workers': 8, 'pin_memory': True} if cuda else {}

    if not args.phase:
        train_dataset = DeepFashionDataset(img_list['train'], root=base_path)
        train_batch_sampler = BalancedBatchSampler(train_dataset.labels,
                                                   train_dataset.source,
                                                   n_classes=32,
                                                   n_samples=4)
        online_train_loader = torch.utils.data.DataLoader(
            train_dataset, batch_sampler=train_batch_sampler, **kwargs)

        test_dataset = DeepFashionDataset(img_list['validation'],
                                          root=base_path)
        test_batch_sampler = BalancedBatchSampler(test_dataset.labels,
                                                  test_dataset.source,
                                                  n_classes=32,
                                                  n_samples=4)
        online_test_loader = torch.utils.data.DataLoader(
            test_dataset, batch_sampler=test_batch_sampler, **kwargs)

        margin = 1.
        loss_fn = OnlineTripletLoss(margin,
                                    RandomNegativeTripletSelector(margin))
        lr = 1e-3
        optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=1e-4)
        scheduler = lr_scheduler.StepLR(optimizer, 5, gamma=0.1, last_epoch=-1)
        n_epochs = 20
        log_interval = 200

        fit(online_train_loader,
            online_test_loader,
            model,
            loss_fn,
            optimizer,
            scheduler,
            n_epochs,
            cuda,
            log_interval,
            model_save_path,
            metrics=[AverageNonzeroTripletsMetric()])

    else:
        model.eval()
        gallery_dataset = DeepFashionDataset(img_list['validation'],
                                             root=base_path)

        acc, query_cnt = get_topK_acc(gallery_dataset, item_dict['validation'],
                                      model, cuda,
                                      open('retrieval_result.txt', 'a'), 100)
        np.savetxt('TopK_accuracy.txt',
                   np.concatenate((acc, np.asarray([query_cnt]))),
                   fmt='%1.5f',
                   delimiter=',')
Example #7
0
embedding_net = Net(
#model = TripletNet(embedding_net)
model = embedding_net
if cuda:
    model.cuda()
loss_fn = OnlineTripletLoss(margin, RandomNegativeTripletSelector(margin))
lr = 1e-4
optimizer = optim.Adam(model.parameters(), lr=lr,betas=(0.9, 0.999))
scheduler = lr_scheduler.StepLR(optimizer,2, gamma=0.9, last_epoch=-1)
n_epochs = 100
log_interval = 1

if os.path.isfile('./model/VeRi_checkpoint'):
    print('*load Data ㅋㅋ*')
    checkpoint = torch.load('./model/VeRi_checkpoint')
    model.load_state_dict(checkpoint['model_state_dict'])
    model.train()

# %%
fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, n_epochs, cuda, log_interval, metrics=[AverageNonzeroTripletsMetric()])


train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=32, shuffle=False, **kwargs)

# %%
#train_embeddings_tl, train_labels_tl = extract_embeddings(train_loader, model)
#print(len(train_labels_tl))
#plot_embeddings(train_embeddings_tl, train_labels_tl)
#val_embeddings_tl, val_labels_tl = extract_embeddings(test_loader, model)
#plot_embeddings(val_embeddings_tl, val_labels_tl)
Example #8
0
def main(args):
    model_path = args.model_path
    save_dir = args.save_dir
    vec_dim = 128

    data_type = ['validation'
                 ] if args.phase == 'test' else ['train', 'validation']
    img_list, base_path, item_dict = read_data("DeepFashion2",
                                               bbox_gt=True,
                                               type_list=data_type)

    # model = ResNetbasedNet(vec_dim=vec_dim, max_pool=True, load_path=model_path, clf2_num=2, adv_eta=1e-4)
    model = ResNetbasedNet(vec_dim=vec_dim,
                           max_pool=True,
                           load_path=model_path,
                           clf2_num=2)

    domain_adap = args.domain_adap
    adv_train = args.adv_train
    is_cud = torch.cuda.is_available()
    device = torch.device("cuda" if is_cud else "cpu")
    if is_cud:
        if torch.cuda.device_count() > 1:
            model = nn.DataParallel(model)
        model.to(device)
    kwargs = {'num_workers': 8, 'pin_memory': True} if is_cud else {}

    if args.phase == 'train':
        train_dataset = DeepFashionDataset(img_list['train'],
                                           root=base_path,
                                           augment=True)
        train_batch_sampler = BalancedBatchSampler(train_dataset.labels,
                                                   train_dataset.source,
                                                   n_classes=64,
                                                   n_samples=4)
        online_train_loader = torch.utils.data.DataLoader(
            train_dataset, batch_sampler=train_batch_sampler, **kwargs)

        test_dataset = DeepFashionDataset(img_list['validation'],
                                          root=base_path)
        test_batch_sampler = BalancedBatchSampler(test_dataset.labels,
                                                  test_dataset.source,
                                                  n_classes=64,
                                                  n_samples=4)
        online_test_loader = torch.utils.data.DataLoader(
            test_dataset, batch_sampler=test_batch_sampler, **kwargs)

        margin = 0.2
        loss_fn = OnlineTripletLoss(margin,
                                    HardestNegativeTripletSelector(margin),
                                    domain_adap)
        # loss_fn = AllTripletLoss(margin)
        criterion = nn.CrossEntropyLoss()
        optimizer = optim.Adam(model.parameters(), lr=1e-5, weight_decay=5e-4)
        # scheduler = lr_scheduler.ReduceLROnPlateau(optimizer, mode="max", patience=4, threshold=0.001, cooldown=2, min_lr=1e-4 / (10 * 2),)
        scheduler = lr_scheduler.ReduceLROnPlateau(
            optimizer,
            mode="max",
            patience=4,
            threshold=1,
            cooldown=2,
            min_lr=1e-5 / (10 * 2),
        )
        n_epochs = 300
        log_interval = 200

        fit(online_train_loader,
            online_test_loader,
            model,
            loss_fn,
            optimizer,
            scheduler,
            n_epochs,
            is_cud,
            log_interval,
            save_dir,
            metrics=[AverageNonzeroTripletsMetric()],
            start_epoch=200,
            criterion=criterion,
            domain_adap=domain_adap,
            adv_train=adv_train)
        # fit(online_train_loader, online_test_loader, model, loss_fn, optimizer, scheduler, n_epochs, is_cud, log_interval,
        #     save_dir, metrics=[AverageNonzeroTripletsMetric()], start_epoch=0, criterion=criterion,
        #     adv_train=True, adv_epsilon=0.01, adv_alph=0.007, adv_iter=1)

    else:
        with torch.no_grad():
            model.eval()
            test_dataset = DeepFashionDataset(img_list['validation'],
                                              root=base_path)
            test_loader = torch.utils.data.DataLoader(test_dataset,
                                                      batch_size=256,
                                                      shuffle=False,
                                                      num_workers=4)
            embedding_mtx = torch.zeros((len(test_dataset), vec_dim))
            labels = np.zeros(len(test_dataset))
            top_k = 500
            idx_ = 0
            start_time = time.time()
            cf_mtx = np.zeros(
                4, dtype=float
            )  # predict_user_real_user / predict_user_real_shop / predict_shop_real_user / predict_shop_real_shop

            for idx, (data, target, _, source) in enumerate(test_loader):
                emb_vecs = model(data.cuda())
                embedding_mtx[idx_:idx_ + len(data)] = emb_vecs[0]
                predict = torch.argmax(emb_vecs[1], dim=1).cpu().numpy()
                real = source.cpu().numpy()
                cf_mtx[0] += np.sum((predict == 0) & (real == 0))
                cf_mtx[1] += np.sum((predict == 0) & (real == 1))
                cf_mtx[2] += np.sum((predict == 1) & (real == 0))
                cf_mtx[3] += np.sum((predict == 1) & (real == 1))
                labels[idx_:idx_ + len(data)] = np.asarray(target)
                idx_ += len(data)
                if idx % 20 == 0:
                    print('processing {}/{}... elapsed time {}s'.format(
                        idx + 1, len(test_loader),
                        time.time() - start_time))

        print('Total: {}, Domain Classification Acc: {:.5f}'.format(
            np.sum(cf_mtx), (cf_mtx[0] + cf_mtx[3]) / np.sum(cf_mtx)))
        print('Recall User Photo: {:.5f}'.format(cf_mtx[0] /
                                                 (cf_mtx[0] + cf_mtx[2])))
        print('Recall Shop Photo: {:.5f}'.format(cf_mtx[3] /
                                                 (cf_mtx[1] + cf_mtx[3])))

        np.save(os.path.join(save_dir, 'emb_mtx.npy'), embedding_mtx)
        with open(os.path.join(save_dir, 'file_info.txt'), 'w') as f:
            for i in range(len(test_dataset)):
                f.write('{},{},{},{}\n'.format(img_list['validation'][i][0],
                                               test_dataset[i][1],
                                               test_dataset[i][2],
                                               test_dataset[i][3]))
        print('save files!')

        distance_mtx = pdist(embedding_mtx)
        sorted_idx = torch.argsort(distance_mtx, dim=1).cpu().numpy()
        result_arr = np.zeros((sorted_idx.shape[0], top_k))
        for idx in range(sorted_idx.shape[0]):
            result_arr[idx] = sorted_idx[idx][sorted_idx[idx] != idx][:top_k]
            result_arr[idx] = labels[result_arr[idx].astype(
                np.int)] == labels[idx]
            if idx % 1000 == 0:
                print(idx)

        for k in [1, 5, 10, 20, 100, 200, 500]:
            topk_accuracy = np.sum(
                np.sum(result_arr[:, :k], axis=1) > 0) / result_arr.shape[0]
            print('Top-{} Accuracy: {:.5f}'.format(k, topk_accuracy))