-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathutils.py
177 lines (136 loc) · 6.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
166
167
168
169
170
171
172
173
174
175
176
177
import os
import torch
from torchvision import datasets, transforms
import config as c
from torchvision.datasets import ImageFolder
from torch.utils.data import Dataset
import numpy as np
def t2np(tensor):
'''pytorch tensor -> numpy array'''
return tensor.cpu().data.numpy() if tensor is not None else None
def flat(tensor):
return tensor.reshape(tensor.shape[0], -1)
def concat_maps(maps):
flat_maps = list()
for m in maps:
flat_maps.append(flat(m))
return torch.cat(flat_maps, dim=1)[..., None]
def get_loss(z, jac):
z = torch.cat([z[i].reshape(z[i].shape[0], -1) for i in range(len(z))], dim=1)
jac = sum(jac)
return torch.mean(0.5 * torch.sum(z ** 2, dim=(1,)) - jac) / z.shape[1]
def cat_maps(z):
return torch.cat([z[i].reshape(z[i].shape[0], -1) for i in range(len(z))], dim=1)
def load_datasets(dataset_path, class_name):
'''
Expected folder/file format to find anomalies of class <class_name> from dataset location <dataset_path>:
train data:
dataset_path/class_name/train/good/any_filename.png
dataset_path/class_name/train/good/another_filename.tif
dataset_path/class_name/train/good/xyz.png
[...]
test data:
'normal data' = non-anomalies
dataset_path/class_name/test/good/name_the_file_as_you_like_as_long_as_there_is_an_image_extension.webp
dataset_path/class_name/test/good/did_you_know_the_image_extension_webp?.png
dataset_path/class_name/test/good/did_you_know_that_filenames_may_contain_question_marks????.png
dataset_path/class_name/test/good/dont_know_how_it_is_with_windows.png
dataset_path/class_name/test/good/just_dont_use_windows_for_this.png
[...]
anomalies - assume there are anomaly classes 'crack' and 'curved'
dataset_path/class_name/test/crack/dat_crack_damn.png
dataset_path/class_name/test/crack/let_it_crack.png
dataset_path/class_name/test/crack/writing_docs_is_fun.png
[...]
dataset_path/class_name/test/curved/wont_make_a_difference_if_you_put_all_anomalies_in_one_class.png
dataset_path/class_name/test/curved/but_this_code_is_practicable_for_the_mvtec_dataset.png
[...]
'''
def target_transform(target):
return class_perm[target]
if c.pre_extracted:
trainset = FeatureDataset(train=True)
testset = FeatureDataset(train=False)
else:
data_dir_train = os.path.join(dataset_path, class_name, 'train')
data_dir_test = os.path.join(dataset_path, class_name, 'test')
classes = os.listdir(data_dir_test)
if 'good' not in classes:
print('There should exist a subdirectory "good". Read the doc of this function for further information.')
exit()
classes.sort()
class_perm = list()
class_idx = 1
for cl in classes:
if cl == 'good':
class_perm.append(0)
else:
class_perm.append(class_idx)
class_idx += 1
tfs = [transforms.Resize(c.img_size), transforms.ToTensor(), transforms.Normalize(c.norm_mean, c.norm_std)]
transform_train = transforms.Compose(tfs)
trainset = ImageFolder(data_dir_train, transform=transform_train)
testset = ImageFolder(data_dir_test, transform=transform_train, target_transform=target_transform)
return trainset, testset
class FeatureDataset(Dataset):
def __init__(self, root="data/features/" + c.class_name + '/', n_scales=c.n_scales, train=False):
super(FeatureDataset, self).__init__()
self.data = list()
self.n_scales = n_scales
self.train = train
suffix = 'train' if train else 'test'
for s in range(c.n_scales):
self.data.append(np.load(root + c.class_name + '_scale_' + str(s) + '_' + suffix + '.npy'))
self.labels = np.load(os.path.join(root, c.class_name + '_labels.npy')) if not train else np.zeros(
[len(self.data[0])])
self.paths = np.load(os.path.join(root, c.class_name + '_image_paths.npy'))
self.class_names = [img_path.split('/')[-2] for img_path in self.paths]
def __len__(self):
return len(self.data[0])
def __getitem__(self, index):
out = list()
for d in self.data:
sample = d[index]
sample = torch.FloatTensor(sample)
out.append(sample)
return out, self.labels[index]
def make_dataloaders(trainset, testset):
trainloader = torch.utils.data.DataLoader(trainset, pin_memory=True, batch_size=c.batch_size, shuffle=True,
drop_last=False)
testloader = torch.utils.data.DataLoader(testset, pin_memory=True, batch_size=c.batch_size, shuffle=False,
drop_last=False)
return trainloader, testloader
def preprocess_batch(data):
'''move data to device and reshape image'''
if c.pre_extracted:
inputs, labels = data
for i in range(len(inputs)):
inputs[i] = inputs[i].to(c.device)
labels = labels.to(c.device)
else:
inputs, labels = data
inputs, labels = inputs.to(c.device), labels.to(c.device)
inputs = inputs.view(-1, *inputs.shape[-3:])
return inputs, labels
class Score_Observer:
'''Keeps an eye on the current and highest score so far'''
def __init__(self, name):
self.name = name
self.max_epoch = 0
self.max_score = None
self.min_loss_epoch = 0
self.min_loss_score = 0
self.min_loss = None
self.last = None
def update(self, score, epoch, print_score=False):
self.last = score
if self.max_score == None or score > self.max_score:
self.max_score = score
self.max_epoch = epoch
if print_score:
self.print_score()
def print_score(self):
print('{:s}: \t last: {:.4f} \t max: {:.4f} \t epoch_max: {:d} \t epoch_loss: {:d}'.format(self.name, self.last,
self.max_score,
self.max_epoch,
self.min_loss_epoch))