-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
128 lines (92 loc) · 2.99 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from torchvision import models
from torch.autograd import Variable
from torch._thnn import type2backend
from torch.nn import Upsample
import numpy as np
def load_model(arch):
'''
Args:
arch: (string) valid torchvision model name,
recommendations 'vgg16' | 'googlenet' | 'resnet50'
'''
if arch == 'googlenet':
from googlenet import get_googlenet
model = get_googlenet(pretrain=True)
elif arch == 'resnext':
from resnext import get_resnext
model = get_resnext(pretrain=True)
else:
model = models.__dict__[arch](pretrained=True)
model.eval()
return model
def cuda_var(tensor, requires_grad=False):
return Variable(tensor.cuda(),requires_grad=requires_grad)
def upsample(inp, size):
'''
Args:
inp: (Tensor) input
size: (Tuple [int, int]) height x width
'''
#backend = type2backend[inp.type()]
#f = getattr(backend, 'SpatialUpSamplingBilinear_updateOutput')
#upsample_inp = inp.new()
#f(backend.library_state, inp, upsample_inp, size[0], size[1])
m=Upsample(scale_factor=size, mode='trilinear')
#print np.min(inp.squeeze().cpu().numpy())
upsample_inp=m(inp)
attr=upsample_inp
return attr
import csv
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
class Logger(object):
def __init__(self, path, header):
self.log_file = open(path, 'w')
self.logger = csv.writer(self.log_file, delimiter='\t')
self.logger.writerow(header)
self.header = header
def __del(self):
self.log_file.close()
def log(self, values):
write_values = []
for col in self.header:
assert col in values
write_values.append(values[col])
self.logger.writerow(write_values)
self.log_file.flush()
def load_value_file(file_path):
with open(file_path, 'r') as input_file:
value = float(input_file.read().rstrip('\n\r'))
return value
def calculate_accuracy(outputs, targets):
batch_size = targets.size(0)
_, pred = outputs.topk(1, 1, True)
pred = pred.t()
correct = pred.eq(targets.view(1, -1))
n_correct_elems = correct.float().sum().data[0]
return n_correct_elems / batch_size
class Logger(object):
def __init__(self, path, header):
self.log_file = open(path, 'w')
self.logger = csv.writer(self.log_file, delimiter='\t')
self.logger.writerow(header)
self.header = header
def __del(self):
self.log_file.close()
def log(self, values):
write_values = []
for col in self.header:
assert col in values
write_values.append(values[col])