<jupyter_start><jupyter_text>data import<jupyter_code>lib_train = phototour.PhotoTour('.','liberty', download=True, train=True, mode = 'Quadruwithglobal', augment = True, nsamples=409600)
yos_train = phototour.PhotoTour('.','yosemite', download=True, train=True, mode = 'Quadruwithglobal', augment = True)
nd_train = phototour.PhotoTour('.','notredame', download=True, train=True, mode = 'Quadruwithglobal', augment = True)<jupyter_output># Found cached data ./liberty.pt
# Found cached data ./yosemite.pt
# Found cached data ./notredame.pt<jupyter_text>train  1. train : notredam & test : yosemite<jupyter_code>eval_db = phototour.PhotoTour('.','yosemite', download=True, train=False)

train_db = nd_train
train_name = 'notredame'
eval_name = 'yosemite'

tfeat = tfeat_model.TNet()
tfeat = tfeat.cuda()

# this kind of works
optimizer = optim.Adam(tfeat.parameters(), lr=1e-3, betas=(0., 0.999))
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.8)

seed=42
torch.manual_seed(seed)
np.random.seed(seed)
# cv2.setRNGSeed(seed)

train_loader = torch.utils.data.DataLoader(train_db,
                                             batch_size=300, shuffle=False,
                                             num_workers=30)

eval_loader = torch.utils.data.DataLoader(eval_db,batch_size=1024, shuffle=False,num_workers=32)

fpr_per_epoch = []

for e in range(100):
    tfeat.train()
    for batch_idx, (data_a, data_p, data_n1, data_n2) in tqdm(enumerate(train_loader)):
        data_a = data_a.unsqueeze(1).float().cuda()
        data_p = data_p.unsqueeze(1).float().cuda()
        data_n1 = data_n1.unsqueeze(1).float().cuda()
        data_n2 = data_n2.unsqueeze(1).float().cuda()
        out_a, out_p, out_n1,out_n2 = tfeat(data_a), tfeat(data_p), tfeat(data_n1), tfeat(data_n2)

        hyperparameter = tfeat_model.newQuadruplet(margin1=2.0, margin2=1.0,gamma=0.8,ramda=0.8,t1=0.1,t2=0.05)
        loss=hyperparameter.loss(out_a,out_p,out_n1,out_n2,swap=False)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

#     print(loss)
    tfeat.eval()
    l = np.empty((0,))
    d = np.empty((0,))
    # evaluate the network after each epoch
    for batch_idx, (data_l, data_r, lbls) in enumerate(eval_loader):
        data_l = data_l.unsqueeze(1).float().cuda()
        data_r = data_r.unsqueeze(1).float().cuda()
        out_l, out_r = tfeat(data_l), tfeat(data_r)
        dists = torch.norm(out_l - out_r, 2, 1).detach().cpu().numpy()
        l = np.hstack((l, lbls.numpy()))
        d = np.hstack((d, dists))

    # FPR95 code from <NAME>
    d = torch.from_numpy(d)
    l = torch.from_numpy(l)
    dist_pos = d[l == 1]
    dist_neg = d[l != 1]
    dist_pos, indice = torch.sort(dist_pos)
    loc_thr = int(np.ceil(dist_pos.numel() * 0.95))
    thr = dist_pos[loc_thr]
    fpr95 = float(dist_neg.le(thr).sum()) / dist_neg.numel()
    print(e, fpr95)
    fpr_per_epoch.append([e, fpr95])
    scheduler.step()
    np.savetxt(train_name+ eval_name+'_quadru_fpr_without_swap.txt', np.array(fpr_per_epoch), delimiter=',')
    #torch.save(tfeat.state_dict(), train_name + '-quadru.params')<jupyter_output># Found cached data ./yosemite.pt<jupyter_text>2. train : notredam & test : liberty<jupyter_code>eval_db = phototour.PhotoTour('.','liberty', download=True, train=False)

train_db = nd_train
train_name = 'notredame'
eval_name = 'liberty'

tfeat = tfeat_model.TNet()
tfeat = tfeat.cuda()

# this kind of works
optimizer = optim.Adam(tfeat.parameters(), lr=1e-3, betas=(0., 0.999))
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.8)

seed=42
torch.manual_seed(seed)
np.random.seed(seed)
# cv2.setRNGSeed(seed)

train_loader = torch.utils.data.DataLoader(train_db,
                                             batch_size=300, shuffle=False,
                                             num_workers=30)

eval_loader = torch.utils.data.DataLoader(eval_db,batch_size=1024, shuffle=False,num_workers=32)

fpr_per_epoch = []

for e in range(100):
    tfeat.train()
    for batch_idx, (data_a, data_p, data_n1, data_n2) in tqdm(enumerate(train_loader)):
        data_a = data_a.unsqueeze(1).float().cuda()
        data_p = data_p.unsqueeze(1).float().cuda()
        data_n1 = data_n1.unsqueeze(1).float().cuda()
        data_n2 = data_n2.unsqueeze(1).float().cuda()
        out_a, out_p, out_n1,out_n2 = tfeat(data_a), tfeat(data_p), tfeat(data_n1), tfeat(data_n2)

        hyperparameter = tfeat_model.newQuadruplet(margin1=2.0, margin2=1.0,gamma=0.8,ramda=0.8,t1=0.4,t2=0.2)
        loss=hyperparameter.loss(out_a,out_p,out_n1,out_n2,swap=False)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

#     print(loss)
    tfeat.eval()
    l = np.empty((0,))
    d = np.empty((0,))
    # evaluate the network after each epoch
    for batch_idx, (data_l, data_r, lbls) in enumerate(eval_loader):
        data_l = data_l.unsqueeze(1).float().cuda()
        data_r = data_r.unsqueeze(1).float().cuda()
        out_l, out_r = tfeat(data_l), tfeat(data_r)
        dists = torch.norm(out_l - out_r, 2, 1).detach().cpu().numpy()
        l = np.hstack((l, lbls.numpy()))
        d = np.hstack((d, dists))

    # FPR95 code from <NAME>
    d = torch.from_numpy(d)
    l = torch.from_numpy(l)
    dist_pos = d[l == 1]
    dist_neg = d[l != 1]
    dist_pos, indice = torch.sort(dist_pos)
    loc_thr = int(np.ceil(dist_pos.numel() * 0.95))
    thr = dist_pos[loc_thr]
    fpr95 = float(dist_neg.le(thr).sum()) / dist_neg.numel()
    print(e, fpr95)
    fpr_per_epoch.append([e, fpr95])
    scheduler.step()
    np.savetxt(train_name+ eval_name+'_quadru_fpr_without_swap.txt', np.array(fpr_per_epoch), delimiter=',')
    #torch.save(tfeat.state_dict(), train_name+'-'+eval_name + '-quadru.params')<jupyter_output># Found cached data ./liberty.pt<jupyter_text>3. train : yosemite & test : notredam<jupyter_code>eval_db = phototour.PhotoTour('.','notredame', download=True, train=False)

train_db = yos_train
eval_name = 'notredame'
train_name = 'yosemite'

tfeat = tfeat_model.TNet()
tfeat = tfeat.cuda()

# this kind of works
optimizer = optim.Adam(tfeat.parameters(), lr=1e-3, betas=(0., 0.999))
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.8)

seed=42
torch.manual_seed(seed)
np.random.seed(seed)
# cv2.setRNGSeed(seed)

train_loader = torch.utils.data.DataLoader(train_db,
                                             batch_size=300, shuffle=False,
                                             num_workers=30)

eval_loader = torch.utils.data.DataLoader(eval_db,
                                             batch_size=1024, shuffle=False,

                                             num_workers=32)

fpr_per_epoch = []

for e in range(100):
    tfeat.train()
    for batch_idx, (data_a, data_p, data_n1, data_n2) in tqdm(enumerate(train_loader)):
        data_a = data_a.unsqueeze(1).float().cuda()
        data_p = data_p.unsqueeze(1).float().cuda()
        data_n1 = data_n1.unsqueeze(1).float().cuda()
        data_n2 = data_n2.unsqueeze(1).float().cuda()
        out_a, out_p, out_n1,out_n2 = tfeat(data_a), tfeat(data_p), tfeat(data_n1), tfeat(data_n2)

        hyperparameter = tfeat_model.newQuadruplet(margin1=2.0, margin2=1.0,gamma=0.8,ramda=0.8,t1=0.1)
        loss=hyperparameter.loss(out_a,out_p,out_n1,out_n2,swap=False)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()


    tfeat.eval()
    l = np.empty((0,))
    d = np.empty((0,))
    # evaluate the network after each epoch
    for batch_idx, (data_l, data_r, lbls) in enumerate(eval_loader):
        data_l = data_l.unsqueeze(1).float().cuda()
        data_r = data_r.unsqueeze(1).float().cuda()
        out_l, out_r = tfeat(data_l), tfeat(data_r)
        dists = torch.norm(out_l - out_r, 2, 1).detach().cpu().numpy()
        l = np.hstack((l, lbls.numpy()))
        d = np.hstack((d, dists))

    # FPR95 code from <NAME>
    d = torch.from_numpy(d)
    l = torch.from_numpy(l)
    dist_pos = d[l == 1]
    dist_neg = d[l != 1]
    dist_pos, indice = torch.sort(dist_pos)
    loc_thr = int(np.ceil(dist_pos.numel() * 0.95))
    thr = dist_pos[loc_thr]
    fpr95 = float(dist_neg.le(thr).sum()) / dist_neg.numel()
    print(e, fpr95)
    fpr_per_epoch.append([e, fpr95])
    scheduler.step()
    np.savetxt(train_name+ eval_name+'_quadru_fpr_without_swap.txt', np.array(fpr_per_epoch), delimiter=',')
    #torch.save(tfeat.state_dict(), train_name + '-quadru.params')<jupyter_output># Found cached data ./notredame.pt<jupyter_text>4. train : yosemite & test : liberty<jupyter_code>eval_db = phototour.PhotoTour('.','liberty', download=True, train=False)

train_db = yos_train
eval_name = 'notredame'
train_name = 'liberty'

tfeat = tfeat_model.TNet()
tfeat = tfeat.cuda()

# this kind of works
optimizer = optim.Adam(tfeat.parameters(), lr=1e-3, betas=(0., 0.999))
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.8)

seed=42
torch.manual_seed(seed)
np.random.seed(seed)
# cv2.setRNGSeed(seed)

train_loader = torch.utils.data.DataLoader(train_db,
                                             batch_size=300, shuffle=False,
                                             num_workers=30)

eval_loader = torch.utils.data.DataLoader(eval_db,
                                             batch_size=1024, shuffle=False,

                                             num_workers=32)

fpr_per_epoch = []

for e in range(100):
    tfeat.train()
    for batch_idx, (data_a, data_p, data_n1, data_n2) in tqdm(enumerate(train_loader)):
        data_a = data_a.unsqueeze(1).float().cuda()
        data_p = data_p.unsqueeze(1).float().cuda()
        data_n1 = data_n1.unsqueeze(1).float().cuda()
        data_n2 = data_n2.unsqueeze(1).float().cuda()
        out_a, out_p, out_n1,out_n2 = tfeat(data_a), tfeat(data_p), tfeat(data_n1), tfeat(data_n2)

        hyperparameter = tfeat_model.newQuadruplet(margin1=2.0, margin2=1.0,gamma=0.8,ramda=0.8,t1=0.4,t2=0.2)
        loss=hyperparameter.loss(out_a,out_p,out_n1,out_n2,swap=False)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()


    tfeat.eval()
    l = np.empty((0,))
    d = np.empty((0,))
    # evaluate the network after each epoch
    for batch_idx, (data_l, data_r, lbls) in enumerate(eval_loader):
        data_l = data_l.unsqueeze(1).float().cuda()
        data_r = data_r.unsqueeze(1).float().cuda()
        out_l, out_r = tfeat(data_l), tfeat(data_r)
        dists = torch.norm(out_l - out_r, 2, 1).detach().cpu().numpy()
        l = np.hstack((l, lbls.numpy()))
        d = np.hstack((d, dists))

    # FPR95 code from <NAME>
    d = torch.from_numpy(d)
    l = torch.from_numpy(l)
    dist_pos = d[l == 1]
    dist_neg = d[l != 1]
    dist_pos, indice = torch.sort(dist_pos)
    loc_thr = int(np.ceil(dist_pos.numel() * 0.95))
    thr = dist_pos[loc_thr]
    fpr95 = float(dist_neg.le(thr).sum()) / dist_neg.numel()
    print(e, fpr95)
    fpr_per_epoch.append([e, fpr95])
    scheduler.step()
    np.savetxt(train_name+'-'+ eval_name+'_quadru_fpr_without_swap.txt', np.array(fpr_per_epoch), delimiter=',')
    #torch.save(tfeat.state_dict(), train_name+'-'+eval_name + '-quadru.params')<jupyter_output># Found cached data ./liberty.pt<jupyter_text>5. train : liberty & test : yosemite<jupyter_code>eval_db = phototour.PhotoTour('.','yosemite', download=True, train=False)

train_db = lib_train
eval_name = 'yosemite'
train_name = 'liberty'

tfeat = tfeat_model.TNet()
tfeat = tfeat.cuda()

# this kind of works
optimizer = optim.Adam(tfeat.parameters(), lr=1e-3, betas=(0., 0.999))
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.8)

seed=42
torch.manual_seed(seed)
np.random.seed(seed)
# cv2.setRNGSeed(seed)

train_loader = torch.utils.data.DataLoader(train_db,
                                             batch_size=300, shuffle=False,
                                             num_workers=30)

eval_loader = torch.utils.data.DataLoader(eval_db,
                                             batch_size=1024, shuffle=False,

                                             num_workers=32)

fpr_per_epoch = []

for e in range(100):
    tfeat.train()
    for batch_idx, (data_a, data_p, data_n1, data_n2) in tqdm(enumerate(train_loader)):
        data_a = data_a.unsqueeze(1).float().cuda()
        data_p = data_p.unsqueeze(1).float().cuda()
        data_n1 = data_n1.unsqueeze(1).float().cuda()
        data_n2 = data_n2.unsqueeze(1).float().cuda()
        out_a, out_p, out_n1,out_n2 = tfeat(data_a), tfeat(data_p), tfeat(data_n1), tfeat(data_n2)

        hyperparameter = tfeat_model.newQuadruplet(margin1=2.0, margin2=1.0,gamma=0.8,ramda=0.8,t1=0.1)
        loss=hyperparameter.loss(out_a,out_p,out_n1,out_n2,swap=False)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    tfeat.eval()
    l = np.empty((0,))
    d = np.empty((0,))
    # evaluate the network after each epoch
    for batch_idx, (data_l, data_r, lbls) in enumerate(eval_loader):
        data_l = data_l.unsqueeze(1).float().cuda()
        data_r = data_r.unsqueeze(1).float().cuda()
        out_l, out_r = tfeat(data_l), tfeat(data_r)
        dists = torch.norm(out_l - out_r, 2, 1).detach().cpu().numpy()
        l = np.hstack((l, lbls.numpy()))
        d = np.hstack((d, dists))

    # FPR95 code from <NAME>
    d = torch.from_numpy(d)
    l = torch.from_numpy(l)
    dist_pos = d[l == 1]
    dist_neg = d[l != 1]
    dist_pos, indice = torch.sort(dist_pos)
    loc_thr = int(np.ceil(dist_pos.numel() * 0.95))
    thr = dist_pos[loc_thr]
    fpr95 = float(dist_neg.le(thr).sum()) / dist_neg.numel()
    print(e, fpr95)
    fpr_per_epoch.append([e, fpr95])
    scheduler.step()
    np.savetxt(train_name+ eval_name+'_quadru_fpr_without_swap.txt', np.array(fpr_per_epoch), delimiter=',')
    #torch.save(tfeat.state_dict(), train_name + '-quadru.params')<jupyter_output># Found cached data ./yosemite.pt<jupyter_text>6. train : liberty & test : notredame<jupyter_code>eval_db = phototour.PhotoTour('.','notredame', download=True, train=False)

train_db = lib_train
eval_name = 'notredame'
train_name = 'liberty'

tfeat = tfeat_model.TNet()
tfeat = tfeat.cuda()

# this kind of works
optimizer = optim.Adam(tfeat.parameters(), lr=1e-3, betas=(0., 0.999))
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.8)

seed=42
torch.manual_seed(seed)
np.random.seed(seed)
# cv2.setRNGSeed(seed)

train_loader = torch.utils.data.DataLoader(train_db,
                                             batch_size=300, shuffle=False,
                                             num_workers=30)

eval_loader = torch.utils.data.DataLoader(eval_db,
                                             batch_size=1024, shuffle=False,

                                             num_workers=32)

fpr_per_epoch = []

for e in range(100):
    tfeat.train()
    for batch_idx, (data_a, data_p, data_n1, data_n2) in tqdm(enumerate(train_loader)):
        data_a = data_a.unsqueeze(1).float().cuda()
        data_p = data_p.unsqueeze(1).float().cuda()
        data_n1 = data_n1.unsqueeze(1).float().cuda()
        data_n2 = data_n2.unsqueeze(1).float().cuda()
        out_a, out_p, out_n1,out_n2 = tfeat(data_a), tfeat(data_p), tfeat(data_n1), tfeat(data_n2)

        hyperparameter = tfeat_model.newQuadruplet(margin1=2.0, margin2=1.0,gamma=0.8,ramda=0.8,t1=0.4,t2=0.2)
        loss=hyperparameter.loss(out_a,out_p,out_n1,out_n2,swap=False)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

    tfeat.eval()
    l = np.empty((0,))
    d = np.empty((0,))
    # evaluate the network after each epoch
    for batch_idx, (data_l, data_r, lbls) in enumerate(eval_loader):
        data_l = data_l.unsqueeze(1).float().cuda()
        data_r = data_r.unsqueeze(1).float().cuda()
        out_l, out_r = tfeat(data_l), tfeat(data_r)
        dists = torch.norm(out_l - out_r, 2, 1).detach().cpu().numpy()
        l = np.hstack((l, lbls.numpy()))
        d = np.hstack((d, dists))

    # FPR95 code from <NAME>
    d = torch.from_numpy(d)
    l = torch.from_numpy(l)
    dist_pos = d[l == 1]
    dist_neg = d[l != 1]
    dist_pos, indice = torch.sort(dist_pos)
    loc_thr = int(np.ceil(dist_pos.numel() * 0.95))
    thr = dist_pos[loc_thr]
    fpr95 = float(dist_neg.le(thr).sum()) / dist_neg.numel()
    print(e, fpr95)
    fpr_per_epoch.append([e, fpr95])
    scheduler.step()
    np.savetxt(train_name+'-'+ eval_name+'_quadru_fpr_without_swap.txt', np.array(fpr_per_epoch), delimiter=',')
    #torch.save(tfeat.state_dict(), train_name+'-'+eval_name + '-quadru.params')<jupyter_output># Found cached data ./notredame.pt
{'fraction_non_alphanumeric': 0.15098743267504489, 'fraction_numerical': 0.02878515858767205, 'mean_word_length': 3.0968374601618045, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 49, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '95345', 'license': 'MIT', 'n_tokens_mistral': 6755, 'n_tokens_neox': 6562, 'n_words': 985, 'path': 'train(without swap).ipynb', 'repo_name': 'eugene6923/local-descriptor-learning-with-quadruplet-with-global-loss', 'size': '16734'}