forked from eastbrother87/FAPM_official
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
182 lines (150 loc) · 5.8 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
178
179
180
181
182
#from this import d
from sklearn.random_projection import SparseRandomProjection
from torch.utils.data import Dataset, DataLoader
from sklearn.metrics import confusion_matrix
from scipy.ndimage import gaussian_filter
from sklearn.metrics import roc_auc_score
from torch.nn import functional as F
from torchvision import transforms
from PIL import Image
import numpy as np
import argparse
import shutil
import torch
import glob
import cv2
import os
import time
from tqdm import tqdm
from einops import rearrange, reduce, repeat
from PIL import Image
from sklearn.metrics import roc_auc_score
from torch import nn
from sklearn.metrics import confusion_matrix
import pickle
from sklearn.random_projection import SparseRandomProjection
from sklearn.neighbors import NearestNeighbors
from scipy.ndimage import gaussian_filter
def distance_matrix(x, y=None, p=2): # pairwise distance of vectors
y = x if type(y) == type(None) else y
n = x.size(0)
m = y.size(0)
d = x.size(1)
x = x.unsqueeze(1).expand(n, m, d)
y = y.unsqueeze(0).expand(n, m, d)
dist = torch.pow(x - y, p).sum(2)
return dist
class NN():
def __init__(self, X=None, Y=None, p=2):
self.p = p
self.train(X, Y)
def train(self, X, Y):
self.train_pts = X
self.train_label = Y
def __call__(self, x):
return self.predict(x)
def predict(self, x):
if type(self.train_pts) == type(None) or type(self.train_label) == type(None):
name = self.__class__.__name__
raise RuntimeError(f"{name} wasn't trained. Need to execute {name}.train() first")
dist = distance_matrix(x, self.train_pts, self.p) ** (1 / self.p)
labels = torch.argmin(dist, dim=1)
return self.train_label[labels]
class KNN(NN):
def __init__(self, X=None, Y=None, k=3, p=2):
self.k = k
super().__init__(X, Y, p)
def train(self, X, Y):
super().train(X, Y)
if type(Y) != type(None):
self.unique_labels = self.train_label.unique()
def predict(self, x):
dist = torch.cdist(x, self.train_pts, self.p)
knn = dist.topk(self.k, largest=False)
return knn
def copy_files(src, dst, ignores=[]):
src_files = os.listdir(src)
for file_name in src_files:
ignore_check = [True for i in ignores if i in file_name]
if ignore_check:
continue
full_file_name = os.path.join(src, file_name)
if os.path.isfile(full_file_name):
shutil.copy(full_file_name, os.path.join(dst,file_name))
if os.path.isdir(full_file_name):
os.makedirs(os.path.join(dst, file_name), exist_ok=True)
copy_files(full_file_name, os.path.join(dst, file_name), ignores)
def prep_dirs(root):
# make embeddings dir
embeddings_path = os.path.join(root)
os.makedirs(embeddings_path, exist_ok=True)
# make sample dir
sample_path = os.path.join(root, 'sample')
os.makedirs(sample_path, exist_ok=True)
# make source code record dir & copy
source_code_save_path = os.path.join(root, 'src')
return embeddings_path, sample_path, source_code_save_path
def embedding_concat(x, y):
# from https://github.com/xiahaifeng1995/PaDiM-Anomaly-Detection-Localization-master
B, C1, H1, W1 = x.size()
_, C2, H2, W2 = y.size()
s = int(H1 / H2)
x = F.unfold(x, kernel_size=s, dilation=1, stride=s)
x = x.view(B, C1, -1, H2, W2)
z = torch.zeros(B, C1 + C2, x.size(2), H2, W2)
for i in range(x.size(2)):
z[:, :, i, :, :] = torch.cat((x[:, :, i, :, :], y), 1)
z = z.view(B, -1, H2 * W2)
z = F.fold(z, kernel_size=s, output_size=(H1, W1), stride=s)
return z
def reshape_embedding(embedding):
embedding_list = []
for k in range(embedding.shape[0]):
for i in range(embedding.shape[2]):
for j in range(embedding.shape[3]):
embedding_list.append(embedding[k, :, i, j])
return embedding_list
def cvt2heatmap(gray):
heatmap = cv2.applyColorMap(np.uint8(gray), cv2.COLORMAP_JET)
return heatmap
def heatmap_on_image(heatmap, image):
if heatmap.shape != image.shape:
heatmap = cv2.resize(heatmap, (image.shape[0], image.shape[1]))
out = np.float32(heatmap)/255 + np.float32(image)/255
out = out / np.max(out)
return np.uint8(255 * out)
def min_max_norm(image):
a_min, a_max = image.min(), image.max()
return (image-a_min)/(a_max - a_min)
def cal_confusion_matrix(y_true, y_pred_no_thresh, thresh, img_path_list):
pred_thresh = []
false_n = []
false_p = []
for i in range(len(y_pred_no_thresh)):
if y_pred_no_thresh[i] > thresh:
pred_thresh.append(1)
if y_true[i] == 0:
false_p.append(img_path_list[i])
else:
pred_thresh.append(0)
if y_true[i] == 1:
false_n.append(img_path_list[i])
cm = confusion_matrix(y_true, pred_thresh)
print(cm)
print('false positive')
print(false_p)
print('false negative')
print(false_n)
def save_anomaly_map(result_path, anomaly_map, input_img, gt_img, file_name, x_type):
if anomaly_map.shape != input_img.shape:
anomaly_map = cv2.resize(anomaly_map, (input_img.shape[0], input_img.shape[1]))
anomaly_map_norm = min_max_norm(anomaly_map)
anomaly_map_norm_hm = cvt2heatmap(anomaly_map_norm*255)
# anomaly map on image
heatmap = cvt2heatmap(anomaly_map_norm*255)
hm_on_img = heatmap_on_image(heatmap, input_img)
# save images
cv2.imwrite(os.path.join(result_path, f'{x_type}_{file_name}.jpg'), input_img)
cv2.imwrite(os.path.join(result_path, f'{x_type}_{file_name}_amap.jpg'), anomaly_map_norm_hm)
cv2.imwrite(os.path.join(result_path, f'{x_type}_{file_name}_amap_on_img.jpg'), hm_on_img)
cv2.imwrite(os.path.join(result_path, f'{x_type}_{file_name}_gt.jpg'), gt_img)