-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilis.py
315 lines (254 loc) · 9.9 KB
/
utilis.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import csv
import pandas as pd
# import dataframe_image as dfi
import numpy as np
from matplotlib import pyplot as plt
import tensorflow as tf
from IPython.display import display
import os
import time
import random
import numpy as np
import cv2
import torch
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import json
import pandas as pd
from collections import defaultdict
import seaborn as sn
from pathlib import Path
from PIL import Image
class utilitis:
def compareTwoList(self, a, b):
matches = [idx for idx, item in enumerate(zip(a, b)) if item[0] == item[1]]
matchesNum = len(matches)
return matches, matchesNum
def save2csv(self, fileName, data, cols, header = False):
with open(fileName, 'w+', newline='', encoding='utf-8') as f:
write = csv.writer(f, delimiter=',')
if header:
write.writerow(cols)
write.writerows(data)
def find_between(self, s, first, last ):
try:
start = s.index( first ) + len( first )
end = s.index( last, start )
return s[start:end]
except ValueError:
return ""
def find_between_r(self, s, first, last ):
try:
start = s.rindex( first ) + len( first )
end = s.rindex( last, start )
return s[start:end]
except ValueError:
return ""
def isContain(self, fileName, fileTypes):
for ext in fileTypes:
if ext not in fileName:
return False
return True
def show(self, df, nr):
with pd.option_context('display.max_rows', nr,
'display.max_columns', None,
'display.width', 800,
'display.precision', 3,
'display.colheader_justify', 'left'):
display(df)
def tensor(self, *x):
tmp = []
for i in x:
tmp.append(np.array(i))
# tmp.append(tf.convert_to_tensor(i))
return tmp
def dataframeAsImage(self, d, path, rowNames, save, colsNames =None):
df = pd.DataFrame(data=d, index = rowNames, columns = colsNames)
if save:
dfi.export(df, path)
return df
def showRow(self, display_list, title, size = None):
# plt.figure()
fig, ax = plt.subplots(1, len(display_list), figsize = size)
for i in range(len(display_list)):
if display_list[i] is not None:
ax[i].set_title(title[i])
ax[i].imshow(tf.keras.utils.array_to_img(display_list[i]));
ax[i].axis('off')
plt.close()
# plt.show()
return fig
df = pd.DataFrame(data=d, index = rowNames)
if save:
dfi.export(df, path)
def display(self, display_list, idx = None, num = None, title = None, size =(10, 10), show = True):
if len(display_list[0].shape) in [2,3] :
f = self.showRow(display_list, title, size = size)
return f
else:
if idx is None and num is not None or num == 1:
idx = np.random.randint(0, len(display_list[0]), num)
fig, ax = plt.subplots(num, len(display_list), figsize = size)
plt.subplots_adjust(wspace=0.1, hspace=0.1)
for j, i in enumerate(idx):
if j ==0:
titles__ = title
else:
titles__ = [""] * len(display_list)
tmp = []
for img in display_list:
if img is not None and i < len(img):
x = img[i]
else:
x = None
tmp.append(x)
for i in range(len(display_list)):
if tmp[i] is not None:
ax[j][i].set_title(titles__[i])
if i in [1,2]:
ax[j][i].imshow(tf.keras.utils.array_to_img(tmp[i]), cmap = 'jet');
else:
ax[j][i].imshow(tf.keras.utils.array_to_img(tmp[i]));
ax[j][i].axis('off')
ax[j][i].set_aspect('equal')
plt.subplots_adjust(wspace=0.1, hspace=0.1)
if show == False:
plt.close()
return fig
def plot_img(img, title="", cmap= "gray", figsize = (5,5)):
plt.figure()
plt.imshow(img, cmap=cmap)
plt.axis('off')
plt.title(title)
def to3D(img, SIZE = None):
# if SIZE is not None:
# img = cv2.resize(img, (64,64))
if img.ndim == 2:
# img = np.expand_dims(img, axis=2)
img = np.dstack((img, img, img))
return img
def concat_images(*images):
"""Generate composite of all supplied images."""
# Get the widest width.
width = max(image.width for image in images)
# Add up all the heights.
padding = 10
height = sum(image.height+padding for image in images) - padding
composite = Image.new('RGB', (width, height))
# Paste each image below the one before it.
y = 0
for image in images:
composite.paste(image, (0, y))
y += image.height + padding
return composite
def add_margin(pil_img, top, right, bottom, left, color):
width, height = pil_img.size
new_width = width + right + left
new_height = height + top + bottom
result = Image.new(pil_img.mode, (new_width, new_height), color)
result.paste(pil_img, (left, top))
return result
def display_image_grid(A, B = None, C = None, num = 5, color = (255, 255, 255), size = (256, 256), direction = [0,1]):
m = min(num, len(A))
wanted = np.random.randint(0,len(A) , m)
col1 = [add_margin(Image.fromarray(cv2.resize(A[i], size)), 10, 10, 10, 10, color = color) for i in wanted]
if B is not None:
col2 = [add_margin(Image.fromarray(cv2.resize(B[i], size)), 10, 10, 10, 10, color = color) for i in wanted]
col3 = [add_margin(Image.fromarray(cv2.resize(C[i], size)), 10, 10, 10, 10, color = color) for i in wanted]
col1 = np.concatenate(col1, axis = direction[0])
col3 = np.concatenate(col3, axis = direction[0])
if B is not None:
col2 = np.concatenate(col2, axis = direction[0])
res = np.concatenate([col1, col2, col3], axis=direction[1])
else:
res = np.concatenate([col1, col3], axis=direction[1])
return res
""" Seeding the randomness. """
def load_json(path):
with open(path, 'r') as f:
return json.load(f)
def append2csv(filename, df):
df.to_csv(filename, mode='a', index=False, header = not Path(filename).exists())
def seeding(seed):
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
""" Create a directory. """
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
""" Calculate the time taken """
def epoch_time(start_time, end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins, elapsed_secs
class epochs_logger:
def __init__(self, metrics_names, float_precision = 3):
self.metrics_names = metrics_names
self.history = pd.DataFrame(columns = self.metrics_names)
self.float_precision = float_precision
self.reset()
def reset(self):
self.metrics = defaultdict(lambda: {"val": 0, "count": 0, "avg": 0})
self.history = pd.DataFrame(columns = self.metrics_names)
def update(self, metric_name, val):
metric = self.metrics[metric_name]
metric["val"] = val
metric["count"] += 1
metric["avg"] = metric["val"] / metric["count"]
def __str__(self):
return " | ".join(
[
"{metric_name}: {avg:.{float_precision}f}".format(
metric_name=metric_name, avg=metric["val"], float_precision=self.float_precision
)
for (metric_name, metric) in self.metrics.items()])
def update_history(self, values):
self.history.loc[len(self.history.index)] = values
def update_history_in(self):
# print(self.metrics_names)
to_add = []
for metr in self.metrics_names:
if metr in self.metrics.keys():
val = self.metrics[metr]["val"]
else:
val = None
to_add.append(val)
self.update_history(to_add)
def get_history(self):
return self.history
# epochs_log = epochs_logger(metrics_names = ['loss', 'iou', 'dice_score', 'f1_score', 'val_loss', 'val_iou', 'val_dice', 'val_f1_score'])
# # epochs_log.update_history([1,2,3,4,5, 6, 7, 448])
# # epochs_log.update_history([1,2,3,4,5, 6, 7, 8])
# epochs_log.update("loss", 22)
# epochs_log.update("iou", 221)
# #
# epochs_log.update_history_in()
# epochs_log.update("iou", 99)
# epochs_log.update_history_in()
# epochs_log.update("val_iou", 11)
# epochs_log.update_history_in()
# print(epochs_log)
# epochs_log.get_history()
# # print(epochs_log)
# concat_images(img, img)
# img = add_margin(Image.fromarray(img), 10, 10, 10, 10, color = (255, 255, 255))
def plot_to_image(figure):
"""Converts the matplotlib plot specified by 'figure' to a PNG image and
returns it. The supplied figure is closed and inaccessible after this call."""
# Save the plot to a PNG in memory.
buf = io.BytesIO()
plt.savefig(buf, format='png')
# Closing the figure prevents it from being displayed directly inside
# the notebook.
plt.close(figure)
buf.seek(0)
# Convert PNG buffer to TF image
image = tf.image.decode_png(buf.getvalue(), channels=4)
# Add the batch dimension
image = tf.expand_dims(image, 0)
return image