-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.py
123 lines (89 loc) · 2.85 KB
/
config.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
import numpy as np
import math
import torch
from torchvision import transforms
mean=np.array([0.4914, 0.4822, 0.4465])
std=np.array([0.2023, 0.1994, 0.2010])
max_val = np.array([ (1. - mean[0]) / std[0],
(1. - mean[1]) / std[1],
(1. - mean[2]) / std[2],
])
min_val = np.array([ (0. - mean[0]) / std[0],
(0. - mean[1]) / std[1],
(0. - mean[2]) / std[2],
])
eps_size=np.array([ abs( (1. - mean[0]) / std[0] ) + abs( (0. - mean[0]) / std[0] ),
abs( (1. - mean[1]) / std[1] ) + abs( (0. - mean[1]) / std[1] ),
abs( (1. - mean[2]) / std[2] ) + abs( (0. - mean[2]) / std[2] ),
])
def train_scale():
return transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
])
def train_zero_norm():
return transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean, std)
])
def test_scale():
return transforms.Compose([
transforms.ToTensor(),
])
def test_zero_norm():
return transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean, std)
])
def unnormalize():
return transforms.Normalize( (- mean / std).tolist(), (1.0 / std ).tolist() )
def inverse_normalize():
u = [-0.4914/0.2023, -0.4822/0.1994, -0.4465/0.2010]
sigma = [1./0.2023, 1./0.1994, 1./0.2010]
return transforms.Normalize( u, sigma )
def compute_lr(lr, itr):
if itr < 75:
return lr
else:
return 0.01
return lr * math.pow(0.2, optim_factor)
# def compute_lr(lr, itr):
# optim_factor = 0
# if itr > 80:
# optim_factor = 3
# elif itr > 60:
# optim_factor = 2
# elif itr > 30:
# optim_factor = 1
# return lr * math.pow(0.2, optim_factor)
def accuracy(output, y, k=1):
"""Computes the precision@k for the specified values of k"""
# Rehape to [N, 1]
target = y.view(-1, 1)
_, pred = torch.topk(output, k, dim=1, largest=True, sorted=True)
correct = torch.eq(pred, target)
return torch.sum(correct).float() / y.size(0)
class AverageMeter():
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
# mean = {
# 'cifar10': (0.4914, 0.4822, 0.4465),
# 'cifar100': (0.5071, 0.4867, 0.4408),
# }
# std = {
# 'cifar10': (0.2023, 0.1994, 0.2010),
# 'cifar100': (0.2675, 0.2565, 0.2761),
# }