-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_TraceExtraction.py
57 lines (47 loc) · 1.77 KB
/
test_TraceExtraction.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
56
57
import os
import cv2
import torch
import numpy as np
import albumentations as A
from torchvision import transforms
from model import STATE as extractor
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
class Preprocess:
def __init__(self):
self.data_size = 1536
self.albu_pre = A.Compose([
A.PadIfNeeded(min_height=self.data_size, min_width=self.data_size, p=1.0),
A.CenterCrop(height=self.data_size, width=self.data_size, p=1.0),
], p=1.0)
self.imagenet_norm = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
])
def process(self, image):
image = self.albu_pre(image=image)['image']
image = self.imagenet_norm(image)
return image
if __name__ == '__main__':
preprocess = Preprocess()
weights_path = 'weights/EffNetB0_weights.pt'
extractor = extractor()
extractor.load_state_dict(torch.load(weights_path))
extractor.eval()
extractor.cuda()
data_root = 'data/sample/'
img1 = cv2.imread(data_root + 'A01_SDR_HAND_20.jpg')
img2 = cv2.imread(data_root + 'A01_SDR_HAND_8.jpg')
img3 = cv2.imread(data_root + 'A02_SDR_HAND_006.jpg')
feat = []
for x in [img1, img2, img3]:
x = preprocess.process(x)
x = torch.stack([x]).cuda()
y = extractor(x)[0]
y = y.detach().cpu().numpy()
y = y / np.linalg.norm(y)
feat.append(y)
cos_sim1 = np.dot(feat[0], feat[1]) # same camera, cos_sim should be higher
cos_sim2 = np.dot(feat[0], feat[2]) # diff camera, cos_sim should be lower
print('Cosine Similarity:')
print('SAME Camera Model: %.4f (Expected: 0.6760)' % cos_sim1)
print('DIFF Camera Model: %.4f (Expected: 0.1139)' % cos_sim2)