-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.py
233 lines (166 loc) · 5.97 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
# -*- utf-8: python -*-
"""
Software Design
Authoer: Chris Cui
Time: 2019-09-03
"""
import math
import torch
import enum
import tqdm
import numpy as np
from PIL import Image
import sys
import io
import matplotlib
import matplotlib.pyplot as plt
from pathlib import Path
# https://matplotlib.org/faq/usage_faq.html#what-is-a-backend
matplotlib.use("Agg")
# INPUT_IMAGE_PATH = "./images/case_01/RGB.png"
# LABEL_IMAGE_PATH = "./images/case_01/GT.png"
# WEIGHTS_FILE_PATH = "./weights/Adam.model.weights.pt"
# INPUT_IMAGE_PATH = "./images/case_02/RGB.png"
# LABEL_IMAGE_PATH = "./images/case_02/GT.png"
# WEIGHTS_FILE_PATH = "./weights/Boxhill.model.weights.pt"
# INPUT_IMAGE_PATH = "./images/case_03/RGB.png"
# LABEL_IMAGE_PATH = "./images/case_03/GT.png"
# WEIGHTS_FILE_PATH = "./weights/CapeTown.model.weights.pt"
@enum.unique
class ClassLabel(enum.Enum):
background = 0
house = 1
class Stats:
def __init__(self, float_fmt=".6f"):
super(Stats, self).__init__()
self.__losses = np.array([], dtype=np.float64)
self.__float_fmt = float_fmt
def append_loss(self, loss):
self.__losses = np.append(self.__losses, [loss])
@property
def loss_mean(self):
return np.mean(self.__losses)
def save_loss_plot(self, fpath):
err_records = self.save_loss_data(fpath)
plt.plot(err_records)
plt.title("Cross Entropy Loss2d")
plt.ylabel("negative log likelihood")
plt.xlabel("iter")
plt.ylim(ymin=0, ymax=int(math.ceil(np.max(err_records))))
# over write the error records figure
figPath = fpath + "/loss_plot.png"
plt.savefig(figPath)
plt.close()
print("(ii) Loss plot saved at {}".format(figPath))
def save_loss_data(self, fpath):
# read last time records
# append the new error records
# save
fpath_npy = fpath + "/loss_data.npy"
if Path(fpath_npy).is_file():
last_error_records = np.load(fpath_npy)
else:
last_error_records = np.array([])
err_records = np.append(last_error_records, self.__losses)
# over write the error records
np.save(fpath_npy, err_records)
return err_records
def fmt_dict(self):
return {"loss": format(self.loss_mean, self.__float_fmt)}
def device(use_gpu=True):
if use_gpu and torch.cuda.is_available():
return torch.device("cuda")
else:
print("GPU is not available and using CPU instead.")
return torch.device("cpu")
def x_dtype():
return torch.float
def y_dtype():
return torch.long
def input_image(input_image_path):
return Image.open(input_image_path)
def label_image(input_label_image_path):
return Image.open(input_label_image_path)
def save_weights_to_disk(model, path):
weights = model.state_dict()
torch.save(weights, path)
return path
def save_entire_model(model, path):
torch.save(model, path)
print("(i) Model saved at {}".format(path))
return path
def load_weights_from_disk(model, path):
if torch.cuda.is_available():
def map_location(storage, loc):
return storage.cuda()
else:
map_location = "cpu"
weights = torch.load(path, map_location=map_location)
model.load_state_dict(weights)
return model
def load_entire_model(model, path, use_gpu=False):
if torch.cuda.is_available() and use_gpu:
def map_location(storage, loc):
return storage.cuda()
else:
map_location = "cpu"
model = torch.load(path, map_location=map_location)
return model
def loader_with_progress(
loader, epoch_n=None, epoch_total=None, stats=None, leave=True
):
if epoch_n is not None and epoch_total is not None:
total_str = str(epoch_total)
n_str = str(epoch_n + 1).rjust(len(total_str))
desc = "Epoch {}/{}".format(n_str, total_str)
else:
desc = None
return tqdm.tqdm(
iterable=loader, desc=desc, leave=leave,
dynamic_ncols=True, postfix=stats,
file=sys.stdout
)
def tiled_image_size(image_size, tile_size, tile_stride_ratio=1.0):
assert type(image_size) == type(tile_size) == tuple
assert len(image_size) == len(tile_size) == 2
assert tile_size[0] <= image_size[0]
assert tile_size[1] <= image_size[1]
w = math.ceil(image_size[0] / tile_size[0]) * tile_size[0]
h = math.ceil(image_size[1] / tile_size[1]) * tile_size[1]
cols = w / tile_size[0] * 1 / tile_stride_ratio
rows = h / tile_size[1] * 1 / tile_stride_ratio
n = int(cols * rows)
return n, (w, h)
def extend_image(image, new_size, color=0):
assert type(new_size) == tuple
assert len(new_size) == len(image.size) == 2
assert image.size[0] <= new_size[0]
assert image.size[1] <= new_size[1]
new_image = Image.new(image.mode, size=new_size, color=0)
new_image.paste(image, image.getbbox())
return new_image
def overlay_class_prediction(image, prediction, color=(88, 226, 212)): # color in red
input_image = image
prediction = prediction.detach().numpy()
assert len(prediction.shape) == 3
N = prediction.shape[0]
W = prediction.shape[1]
H = prediction.shape[2]
original_size = input_image.size
tile_count, extended_size = tiled_image_size(original_size, (W, H))
input_image = extend_image(input_image, extended_size)
mask = np.zeros((extended_size[1], extended_size[0]))
def tile_generator():
for x in range(0, extended_size[0], W):
for y in range(0, extended_size[1], H):
yield (x, y)
tiles = tile_generator()
for n in range(N):
(x, y) = next(tiles)
tile = prediction[n, :, :]
mask[y: y + H, x: x + W] = tile * 255
color_image = Image.new("RGB", extended_size, color=color)
mask_image = Image.fromarray(mask.astype("uint8"), mode="L")
input_image.paste(color_image, mask=mask_image)
input_image = input_image.crop((0, 0, original_size[0], original_size[1]))
return input_image, mask_image