-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (41 loc) · 1.62 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from pathlib import Path
import sys
import torch
from skimage.io import imread
root_path = Path(__file__).resolve().parent
if str(root_path) not in sys.path:
sys.path.insert(0, str(root_path))
from models.dejavu import DejaVu
from losses.contextual import ContextualLoss
from utils import ops
if __name__ == '__main__':
device = ops.get_device()
ckpt_file = root_path / 'ckpts' / 'alpha_04.pt'
model = DejaVu.from_ckpt(ckpt_file).to(device)
model.eval()
loss = ContextualLoss(offset=1.0, bandwidth=0.5, is_similarity=True).to(device)
loss.eval()
imfiles = [
'image_anchor_0.png', 'image_anchor_1.png',
'image_positive_0.png', 'image_positive_1.png',
'image_negative_0.png', 'image_negative_1.png'
]
batch_size = len(imfiles)
def load_image(file):
image = imread(root_path / 'images' / file)
return ops.img2torch(image)
images = torch.stack([load_image(file) for file in imfiles])
# Compute features and downsample
with torch.no_grad():
images = images.to(device)
images = ops.downsample(images, 2)
dense_features = model(images)
dense_features = ops.downsample(dense_features, 4) # Downsample for faster processing
# Compute all pairs of similarities
# We expect all anchor/positive pairs to be higher than the negatives
# NOTE: Similarities depend on each model, which might be higher or lower
sims = torch.zeros(batch_size, batch_size)
for i, fmap in enumerate(dense_features):
fmap = fmap[None].repeat(batch_size, 1, 1, 1)
sims[i] = loss(fmap, dense_features)
print(sims)