This repository has been archived by the owner on Dec 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.py
87 lines (70 loc) · 2.72 KB
/
utils.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import time
from functools import wraps
import numpy as np
import torch
import torch.backends.cudnn as cudnn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from torchvision.datasets.cifar import CIFAR10
import torchvision.transforms as transforms
def init_cifar_dataloader(root, batchSize):
"""load dataset"""
normalize = transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
transform_train = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize
])
transform_test = transforms.Compose([
transforms.ToTensor(),
normalize
])
train_loader = DataLoader(CIFAR10(root, train=True, download=True, transform=transform_train),
batch_size=batchSize, shuffle=True, num_workers=4, pin_memory=True)
print(f'train set: {len(train_loader.dataset)}')
test_loader = DataLoader(CIFAR10(root, train=False, download=True, transform=transform_test),
batch_size=batchSize * 8, shuffle=False, num_workers=4, pin_memory=True)
print(f'val set: {len(test_loader.dataset)}')
return train_loader, test_loader
def timing(f):
"""print time used for function f"""
@wraps(f)
def wrapper(*args, **kwargs):
time_start = time.time()
ret = f(*args, **kwargs)
print(f'total time = {time.time() - time_start:.4f}')
return ret
return wrapper
def compute_result(dataloader, net):
bs, clses = [], []
net.eval()
for img, cls in dataloader:
clses.append(cls)
bs.append(net(Variable(img.cuda(), volatile=True)).data.cpu())
return torch.sign(torch.cat(bs)), torch.cat(clses)
@timing
def compute_mAP(trn_binary, tst_binary, trn_label, tst_label):
"""
compute mAP by searching testset from trainset
https://github.com/flyingpot/pytorch_deephash
"""
for x in trn_binary, tst_binary, trn_label, tst_label: x.long()
AP = []
Ns = torch.arange(1, trn_binary.size(0) + 1)
for i in range(tst_binary.size(0)):
query_label, query_binary = tst_label[i], tst_binary[i]
_, query_result = torch.sum((query_binary != trn_binary).long(), dim=1).sort()
correct = (query_label == trn_label[query_result]).float()
P = torch.cumsum(correct, dim=0) / Ns
AP.append(torch.sum(P * correct) / torch.sum(correct))
mAP = torch.mean(torch.Tensor(AP))
return mAP
def choose_gpu(i_gpu):
"""choose current CUDA device"""
torch.cuda.device(i_gpu).__enter__()
cudnn.benchmark = True
def feed_random_seed(seed=np.random.randint(1, 10000)):
"""feed random seed"""
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)