-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
165 lines (130 loc) · 5.54 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import math
import os
import numpy as np
import torch
from torchvision.datasets import VisionDataset
from PIL import Image
class OODDataset(torch.utils.data.Dataset):
def __init__(self, in_dataset, ood_dataset):
self.in_dataset = in_dataset
self.n_in = len(in_dataset)
self.ood_dataset = ood_dataset
self.n_ood = len(ood_dataset)
def __getitem__(self, i):
if i < self.n_in:
# Fetch in-distribution data
input, target = self.in_dataset[i]
return input, 0
else:
# Fetch out-of-distribution data
input, target = self.ood_dataset[i - self.n_in]
return input, 1
def __len__(self):
return self.n_in + self.n_ood
class CorruptedCIFAR10(VisionDataset):
corruption_types = [
'brightness',
'contrast',
'defocus_blur',
'elastic_transform',
'fog',
'frost',
'gaussian_blur',
'gaussian_noise',
'glass_blur',
'impulse_noise',
'jpeg_compression',
'motion_blur',
'pixelate',
'saturate',
'shot_noise',
'snow',
'spatter',
'speckle_noise',
'zoom_blur']
max_intensity = 5
data_dir = 'CIFAR-10-C'
def __init__(self, root, skew_intensity, transform=None, target_transform=None):
super().__init__(root, transform=transform, target_transform=target_transform)
assert skew_intensity in [1, 2, 3, 4, 5]
self.targets = np.tile(np.load(os.path.join(root, self.data_dir, 'labels.npy'))[:10000], len(self.corruption_types))
self.data = []
for corruption_type in self.corruption_types:
self.data.append(np.load(os.path.join(root, self.data_dir, corruption_type + '.npy'))[10000*(skew_intensity-1):10000*skew_intensity])
self.data = np.concatenate(self.data, axis=0)
def __getitem__(self, index: int):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
img, target = self.data[index], self.targets[index]
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self) -> int:
return len(self.data)
class CorruptedCIFAR100(CorruptedCIFAR10):
data_dir = 'CIFAR-100-C'
def compute_ece(confs, targets, bins=10):
device = confs.device
bin_boundaries = torch.linspace(0, 1, bins + 1, dtype=torch.float, device=device)
confs, preds = confs.max(dim=1)
confs = confs.float()
accs = preds.eq(targets).float()
# computes bins
acc_bin = torch.zeros(bins, device=device)
prob_bin = torch.zeros(bins, device=device)
count_bin = torch.zeros(bins, device=device)
indices = torch.bucketize(confs, bin_boundaries) - 1
count_bin.scatter_add_(dim=0, index=indices, src=torch.ones_like(confs))
prob_bin.scatter_add_(dim=0, index=indices, src=confs)
prob_bin = torch.nan_to_num(prob_bin / count_bin)
acc_bin.scatter_add_(dim=0, index=indices, src=accs)
acc_bin = torch.nan_to_num(acc_bin / count_bin)
prop_bin = count_bin / count_bin.sum()
ece = torch.sum(torch.abs(acc_bin - prob_bin) * prop_bin)
return ece
class ExpectedCalibrationError():
def __init__(self, device, bins=10):
self.bins = bins
self.bin_boundaries = torch.linspace(0, 1, bins + 1, dtype=torch.float, device=device)
self.acc_bin = torch.zeros(bins, device=device)
self.prob_bin = torch.zeros(bins, device=device)
self.count_bin = torch.zeros(bins, device=device)
def update(self, confs, targets):
confs, preds = confs.max(dim=1)
confs = confs.float()
accs = preds.eq(targets).float()
# if confs is 1, causes index errors in the following scatter_add
indices = torch.bucketize(confs, self.bin_boundaries).clamp(min=1, max=self.bins) - 1
self.count_bin.scatter_add_(dim=0, index=indices, src=torch.ones_like(confs))
self.prob_bin.scatter_add_(dim=0, index=indices, src=confs)
self.acc_bin.scatter_add_(dim=0, index=indices, src=accs)
_prob_bin = torch.nan_to_num(self.prob_bin / self.count_bin)
_acc_bin = torch.nan_to_num(self.acc_bin / self.count_bin)
prop_bin = self.count_bin / self.count_bin.sum()
ece = torch.sum(torch.abs(_acc_bin - _prob_bin) * prop_bin)
return ece
def causal_mask(width, height, starting_point):
row_grid, col_grid = np.meshgrid(np.arange(width), np.arange(height), indexing='ij')
mask = np.logical_or(
row_grid < starting_point[0],
np.logical_and(row_grid == starting_point[0], col_grid <= starting_point[1]))
return torch.tensor(mask)
def conv_mask(width, height, include_center=False):
return 1.0 * causal_mask(width, height, starting_point=(width//2, height//2 + include_center - 1))
def weight_mask(in_channels, kernel_size):
conv_mask_with_center = conv_mask(kernel_size, kernel_size, include_center=True)
conv_mask_no_center = conv_mask(kernel_size, kernel_size, include_center=False)
mask = torch.zeros(in_channels, in_channels, kernel_size, kernel_size)
for i in range(in_channels):
for j in range(in_channels):
mask[i][j] = conv_mask_no_center if j >= i else conv_mask_with_center
return mask