-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.py
247 lines (203 loc) · 8.63 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import torch
import numpy as np
import pandas as pd
import random
from pathlib import Path
import glob
import re
import os
from matplotlib.patches import Patch
import webcolors
import matplotlib.pyplot as plt
import json
from importlib import import_module
def increment_path(path, exist_ok=False):
""" Automatically increment path, i.e. runs/exp --> runs/exp0, runs/exp1 etc.
Args:
path (str or pathlib.Path): f"{model_dir}/{args.name}".
exist_ok (bool): whether increment path (increment if False).
"""
path = Path(path)
if (path.exists() and exist_ok) or (not path.exists()):
return str(path)
else:
dirs = glob.glob(f"{path}*")
matches = [re.search(rf"%s(\d+)" % path.stem, d) for d in dirs]
i = [int(m.groups()[0]) for m in matches if m]
n = max(i) + 1 if i else 2
return f"{path}{n}"
def set_seed(random_seed=42):
torch.manual_seed(random_seed)
torch.cuda.manual_seed(random_seed)
torch.cuda.manual_seed_all(random_seed) # if use multi-GPU
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(random_seed)
random.seed(random_seed)
def save_model(model, save_dir, file_name):
os.makedirs(save_dir, exist_ok=True)
check_point = {'net': model.state_dict()}
output_path = os.path.join(save_dir, file_name)
torch.save(model, output_path)
def collate_fn(batch):
return tuple(zip(*batch))
def get_lr(optimizer):
""" Returns learning rate initialized at optimizer
Args:
opimizer: The optimizer included with nn.optim
"""
for param_group in optimizer.param_groups:
return param_group['lr']
# Metric Function
def _fast_hist(label_true, label_pred, n_class):
mask = (label_true >= 0) & (label_true < n_class)
hist = np.bincount(n_class * label_true[mask].astype(int) + label_pred[mask],
minlength=n_class ** 2).reshape(n_class, n_class)
return hist
def write_json(dir, log_file, data):
os.makedirs(dir, exist_ok=True)
with open(os.path.join(dir, log_file), 'a', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=1)
def logging(dir, log_file, data):
with open(os.path.join(dir, log_file), "a") as f:
f.write(data + "\n")
def label_accuracy_score(hist):
"""
Returns accuracy score evaluation result.
- [acc]: overall accuracy
- [acc_cls]: mean accuracy
- [mean_iu]: mean IU
- [fwavacc]: fwavacc
"""
acc = np.diag(hist).sum() / hist.sum()
with np.errstate(divide='ignore', invalid='ignore'):
acc_cls = np.diag(hist) / hist.sum(axis=1)
acc_cls = np.nanmean(acc_cls)
with np.errstate(divide='ignore', invalid='ignore'):
iu = np.diag(hist) / (hist.sum(axis=1) + hist.sum(axis=0) - np.diag(hist))
mean_iu = np.nanmean(iu)
freq = hist.sum(axis=1) / hist.sum()
fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
return acc, acc_cls, mean_iu, fwavacc, iu
def add_hist(hist, label_trues, label_preds, n_class):
"""
stack hist(confusion matrix)
"""
for lt, lp in zip(label_trues, label_preds):
hist += _fast_hist(lt.flatten(), lp.flatten(), n_class)
return hist
def _fast_hist(label_true, label_pred, n_class):
mask = (label_true >= 0) & (label_true < n_class)
hist = np.bincount(
n_class * label_true[mask].astype(int) +
label_pred[mask], minlength=n_class ** 2).reshape(n_class, n_class)
return hist
# Data Sample Viz
def create_trash_label_colormap():
"""Creates a label colormap used in Trash segmentation.
Returns:
A colormap for visualizing segmentation results.
"""
class_colormap = pd.read_csv("class_dict.csv")
colormap = np.zeros((11, 3), dtype=np.uint8)
for inex, (_, r, g, b) in enumerate(class_colormap.values):
colormap[inex] = [r, g, b]
return colormap
def label_to_color_image(label):
"""Adds color defined by the dataset colormap to the label.
Args:
label: A 2D array with integer type, storing the segmentation label.
Returns:
result: A 2D array with floating type. The element of the array
is the color indexed by the corresponding element in the input label
to the trash color map.
Raises:
ValueError: If label is not of rank 2 or its value is larger than color
map maximum entry.
"""
if label.ndim != 2:
raise ValueError('Expect 2-D input label')
colormap = create_trash_label_colormap()
if np.max(label) >= len(colormap):
raise ValueError('label value too large.')
return colormap[label]
# visualization function
def plot_examples(model_path, model_name, mode, batch_id, num_examples, dataloaer):
"""Visualization of images and masks according to batch size
Args:
mode: train/val/test (str)
batch_id : 0 (int)
num_examples : 1 ~ batch_size (e.g. 8) (int)
dataloaer : data_loader (dataloader)
Returns:
None
"""
model = torch.load(os.path.join(model_path, model_name))
class_colormap = pd.read_csv("class_dict.csv")
device = "cuda" if torch.cuda.is_available() else "cpu"
# variable for legend
category_and_rgb = [[category, (r,g,b)] for idx, (category, r, g, b) in enumerate(class_colormap.values)]
legend_elements = [Patch(facecolor=webcolors.rgb_to_hex(rgb),
edgecolor=webcolors.rgb_to_hex(rgb),
label=category) for category, rgb in category_and_rgb]
# test / validation set에 대한 시각화
if (mode in ('train', 'val')):
with torch.no_grad():
for index, (imgs, masks, image_infos) in enumerate(dataloaer):
if index == batch_id:
image_infos = image_infos
temp_images = imgs
temp_masks = masks
model.eval()
# inference
outs = model(torch.stack(temp_images).to(device))
oms = torch.argmax(outs, dim=1).detach().cpu().numpy()
break
else:
continue
fig, ax = plt.subplots(nrows=num_examples, ncols=3, figsize=(12, 4*num_examples), constrained_layout=True)
fig.tight_layout()
for row_num in range(num_examples):
# Original Image
ax[row_num][0].imshow(temp_images[row_num].permute([1,2,0]))
ax[row_num][0].set_title(f"Orignal Image : {image_infos[row_num]['file_name']}")
# Groud Truth
ax[row_num][1].imshow(label_to_color_image(masks[row_num].detach().cpu().numpy()))
ax[row_num][1].set_title(f"Groud Truth : {image_infos[row_num]['file_name']}")
# Pred Mask
ax[row_num][2].imshow(label_to_color_image(oms[row_num]))
ax[row_num][2].set_title(f"Pred Mask : {image_infos[row_num]['file_name']}")
ax[row_num][2].legend(handles=legend_elements, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)
plt.show()
# test set에 대한 시각화
else :
with torch.no_grad():
for index, (imgs, image_infos) in enumerate(dataloaer):
if index == batch_id:
image_infos = image_infos
temp_images = imgs
model.eval()
# inference
outs = model(torch.stack(temp_images).to(device))
oms = torch.argmax(outs, dim=1).detach().cpu().numpy()
break
else:
continue
fig, ax = plt.subplots(nrows=num_examples, ncols=2, figsize=(10, 4*num_examples), constrained_layout=True)
for row_num in range(num_examples):
# Original Image
ax[row_num][0].imshow(temp_images[row_num].permute([1,2,0]))
ax[row_num][0].set_title(f"Orignal Image : {image_infos[row_num]['file_name']}")
# Pred Mask
ax[row_num][1].imshow(label_to_color_image(oms[row_num]))
ax[row_num][1].set_title(f"Pred Mask : {image_infos[row_num]['file_name']}")
ax[row_num][1].legend(handles=legend_elements, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0)
plt.show()
def model_test(args):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_module = getattr(import_module("models"), args.model)
model = model_module(num_classes=11)
input = torch.randn([8, 3, 512, 512])
print("input shape:", input.shape)
output = model(input).to(device)
print("output shape: ", output.shape)