Skip to content

Commit

Permalink
changes to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ariel415el committed Mar 9, 2022
1 parent 6e75552 commit 768cf49
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 64 deletions.
2 changes: 1 addition & 1 deletion scripts/duplicate_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def edit_images(input_and_target_images, out_dir):
criteria = distribution_metrics.PatchSWDLoss(patch_size=7, stride=1, num_proj=256, mask_patches_factor=3)

for (target_image_path, mask_image_path, coarse_dim) in input_and_target_images:
model = GPDM(coarse_dim=coarse_dim, pyr_factor=0.85, lr=0.03, num_steps=300, init='noise', noise_sigma=0, resize=256)
model = GPDM(coarse_dim=coarse_dim, pyr_factor=0.85, lr=0.03, num_steps=300, init='noise', noise_sigma=0, resize=512)

result = model.run(target_image_path, criteria, mask_img_path=mask_image_path, debug_dir=None)
output_path = os.path.join(out_dir, f'{get_file_name(target_image_path)}.png')
Expand Down
109 changes: 64 additions & 45 deletions tests/compare_speeds.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
from collections import defaultdict

import cv2
import torch
from time import time
import numpy as np
import faiss
import pandas as pd


def get_NN_indices_faiss(X, Y, dim, index='flat', n_first=1):
def get_NN_indices_faiss(X, Y, dim, index='flat', n_first=1, on_gpu=False):
X = np.ascontiguousarray(X.cpu().numpy(), dtype='float32')
Y = np.ascontiguousarray(Y.cpu().numpy(), dtype='float32')

if index == 'flat':
index = faiss.IndexFlat(dim)
elif index == 'ivf':
index = faiss.IndexIVFFlat(faiss.IndexFlat(dim), dim, 100)
index = faiss.IndexIVFFlat(faiss.IndexFlat(dim), dim, int(np.sqrt(len(X))))
index.nprobe = 2
index.train(Y)
elif index == 'lsh':
index = faiss.IndexLSH(dim)
else:
raise ValueError

# index = faiss.index_cpu_to_all_gpus(index)
res = faiss.StandardGpuResources()
index = faiss.index_cpu_to_gpu(res, 0, index)
if on_gpu:
# index = faiss.index_cpu_to_all_gpus(index)
res = faiss.StandardGpuResources()
index = faiss.index_cpu_to_gpu(res, 0, index)

index.add(Y) # add vectors to the index

_, I = index.search(X, n_first) # actual search
print(4)

NNs = I[:, 0]

Expand Down Expand Up @@ -100,47 +103,63 @@ def time_call(func, X, Y, *args):

return np.mean(times), np.std(times)

def compute_ann_accuracy(s, p):
n = s ** 2
d = 3 * p ** 2
X = torch.randn((n, d))
Y = torch.randn((n, d))

nn_fais, I = get_NN_indices_faiss(X, Y, d, 'ivf', n_first=10)
nn = get_NN_indices_low_memory(X, Y, 1, b=256).numpy()

recall = np.sum(nn_fais == nn) / nn.shape[0]
n_recall = np.sum([nn[i] in I[i] for i in range(n)]) / nn.shape[0]
dist_faiss = ((X - Y[nn_fais])**2).sum(1).mean()
dist_true = ((X - Y[nn])**2).sum(1).mean()

print(recall)
print(n_recall)
print(dist_faiss)
print(dist_true)

x =1
def get_vectors_from_img(path, resize):
img = cv2.imread(path)
img = cv2.resize(img, (resize, resize))[None, :]
unfold = torch.nn.Unfold(kernel_size=p, stride=1)
vecs = unfold(torch.from_numpy(img).float().permute(0,3,1,2))[0].T
return vecs

def compute_ann_accuracy():
# n = s ** 2
# d = 3 * p ** 2
# X = torch.randn((n, d))
# Y = torch.randn((n, d))
table = pd.DataFrame()
for s in [64, 128]:
X = get_vectors_from_img('/home/ariel/university/GPDM/images/Places50/50.jpg', resize=s)
Y = get_vectors_from_img('/home/ariel/university/GPDM/images/Places50/37.jpg', resize=s)
n, d = X.shape

nn_fais, I = get_NN_indices_faiss(X, Y, d, 'ivf', n_first=10)
nn = get_NN_indices_faiss(X, Y, d, 'flat', n_first=1)
# nn = get_NN_indices_low_memory(X, Y, 1, b=256).numpy()

column = {
'Recall-1': np.sum(nn_fais == nn) / nn.shape[0],
'Recall-10': np.sum([nn[i] in I[i] for i in range(n)]) / nn.shape[0],
'faiss-IVF-dists': ((X - Y[nn_fais])**2).sum(1).mean().item(),
'True-dists':((X - Y[nn])**2).sum(1).mean().item()
}
table[s] = pd.Series(column)
print(column)
table.to_csv("accuracy_table.csv")


def compute_runtime():
table = pd.DataFrame()
for s in range(64, 512 + 1, 64):
n = s ** 2
d = 3 * p ** 2
swd_loss = swd(p, n_proj=64)
X = torch.randn((n, d)).to(device)
Y = torch.randn((n, d)).to(device)

column = {
'Pytorch-NN': time_call(get_NN_indices_low_memory, X, Y, 1, 256)[0],
'SWD': time_call(swd_loss, X, Y)[0],
'faiss-PureNN': time_call(get_NN_indices_faiss, X, Y, d, 'flat')[0],
'faiss-IVF':time_call(get_NN_indices_faiss, X, Y, d, 'ivf')[0]
}
table[s] = pd.Series(column)
print(column)
table.to_csv("timing_table.csv")

if __name__ == '__main__':
# compute_ann_accuracy(128, 7)
# exit()

p = 7
n_reps = 50
device = torch.device('cuda:0')
with torch.no_grad():
full_report = pd.DataFrame()
for s in [16]: # 64, 128, 256, 512, 1024]:
for p in [7]:
n = s ** 2
d = 3 * p ** 2
swd_loss = swd(p, n_proj=256)
X = torch.randn((n, d)).to(device)
Y = torch.randn((n, d)).to(device)

print(s, p)
# print(f"Pytorch-NN ", time_call(get_NN_indices_low_memory, X, Y, 1, 256))
# print(f"SWD ", time_call(swd_loss, X, Y))
print(f"faiss-PureNN", time_call(get_NN_indices_faiss, X, Y, d, 'flat'))
# print(f"faiss-IVF ", time_call(get_NN_indices_faiss, X, Y, d, 'ivf'))

# device = torch.device('cpu')

# compute_ann_accuracy()
compute_runtime()
50 changes: 32 additions & 18 deletions tests/test_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,43 @@ def test(reference_dir, synthetic_root):
sfid = calculate_sifid_given_paths(reference_dir, synthetic_dir, 1, False, 64)
scores.append(sfid)

print(f"SFID: {np.mean(scores)} +- {np.std(scores)}, Diversity: {diversity}")
# print(f"{os.path.basename}: SFID: {np.mean(scores):.3f} +- {np.std(scores):.3f}, Diversity: {diversity:.3f}")
print(f"{os.path.basename(synthetic_root).ljust(30)}: SFID: {np.mean(scores):.3f}, Diversity: {diversity:.3f}")


if __name__ == '__main__':
np.set_printoptions(suppress=True)
reference_dir = '/home/ariel/university/GPDM/images/SIGD16'
synthetic_root = '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/SIGD16_alpha=1'
test(reference_dir, synthetic_root)
synthetic_root = '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/SIGD16_alpha=0.005'
test(reference_dir, synthetic_root)
synthetic_root = '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/SIGD16_faissIVF-50'
test(reference_dir, synthetic_root)

synthetic_root = '/home/ariel/university/GPDM/scripts/outputs/reshuffle/SIGD16_28-noise'
test(reference_dir, synthetic_root)
test(reference_dir, '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/SIGD16_alpha=1')
test(reference_dir, '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/SIGD16_alpha=0.005')
test(reference_dir, '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/SIGD16_faissIVF-50')

test(reference_dir, '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/SIGD16_target_alpha=1')
test(reference_dir, '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/SIGD16_target_alpha=0.005')
test(reference_dir, '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/SIGD16_target_faissIVF-50')

test(reference_dir, '/home/ariel/university/GPDM/scripts/outputs/reshuffle/SIGD16_28-noise')
test(reference_dir, '/home/ariel/university/GPDM/scripts/outputs/reshuffle/SIGD16_64_300')

sfid = calculate_sifid_given_paths(reference_dir, '/home/ariel/university/GPDM/tests/downloaded_results/jpeg_100/SIGD16_GPNN', 1, False, 64)
print(sfid)
sfid = calculate_sifid_given_paths(reference_dir, '/home/ariel/university/GPDM/tests/downloaded_results/jpeg_100/SIGD16_SINGAN', 1, False, 64)
print(sfid)

reference_dir = '/home/ariel/university/GPDM/images/Places50'
synthetic_root = '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/Places50_alpha=1'
test(reference_dir, synthetic_root)
synthetic_root = '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/Places50_alpha=0.005'
test(reference_dir, synthetic_root)
synthetic_root = '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/Places50_faissIVF-50'
test(reference_dir, synthetic_root)

synthetic_root = '/home/ariel/university/GPDM/scripts/outputs/reshuffle/Places50_28-noise'
test(reference_dir, synthetic_root)
test(reference_dir, '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/Places50_alpha=1')
test(reference_dir, '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/Places50_alpha=0.005')
test(reference_dir, '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/Places50_faissIVF-50')

test(reference_dir, '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/Places50_target_alpha=1')
test(reference_dir, '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/Places50_target_alpha=0.005')
test(reference_dir, '/home/ariel/university/Efficient-GPNN/scripts/outputs/reshuffle/Places50_target_faissIVF-50')

test(reference_dir, '/home/ariel/university/GPDM/scripts/outputs/reshuffle/Places50_28-noise')
test(reference_dir, '/home/ariel/university/GPDM/scripts/outputs/reshuffle/Places50_64_300')

sfid = calculate_sifid_given_paths(reference_dir, '/home/ariel/university/GPDM/tests/downloaded_results/jpeg_100/Places50_GPNN_high_var', 1, False, 64)
print(sfid)
sfid = calculate_sifid_given_paths(reference_dir, '/home/ariel/university/GPDM/tests/downloaded_results/jpeg_100/Places50_SINGAN_high_var', 1, False, 64)
print(sfid)

0 comments on commit 768cf49

Please sign in to comment.