-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
277 lines (240 loc) · 9.77 KB
/
Copy pathutils.py
File metadata and controls
277 lines (240 loc) · 9.77 KB
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
import gc
import numpy as np
import torch
import random
import platform
import os
import sys
from shutil import rmtree
import matplotlib
import logging
import cv2
# To be able to save figure using screen with matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as AA
from mpl_toolkits.axes_grid1 import host_subplot
from sklearn.metrics import confusion_matrix
#########################################################################
###################### Reset Pytorch Session ############################
#########################################################################
def reset_training(seed):
gc.collect()
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
##########################################################################
############################## Print and log #############################
##########################################################################
def print_and_log(message, log=None):
print(message)
if log is not None:
log.info(message)
def setup_logger(logger_name, log_file, level=logging.INFO):
l = logging.getLogger(logger_name)
formatter = logging.Formatter('%(message)s')
fileHandler = logging.FileHandler(log_file, mode='w')
fileHandler.setFormatter(formatter)
l.setLevel(level)
l.addHandler(fileHandler)
return l
def close_log(log):
if log is not None:
x = list(log.handlers)
for i in x:
log.removeHandler(i)
i.flush()
i.close()
###############################################################################
######################### Files processing functions ##########################
###############################################################################
def make_new_path(path):
if os.path.exists(path):
rmtree(path)
os.mkdir(path)
else:
os.mkdir(path)
def make_path(path) :
if not os.path.exists(path):
os.mkdir(path)
#################################################################
############### Pool to launch several process ##################
#################################################################
class ActivePool(object):
def __init__(self):
super(ActivePool, self).__init__()
self.active=[]
self.running_time=[]
self.lock=threading.Lock()
def makeActive(self, name):
with self.lock:
self.active.append(name)
def makeInactive(self, name):
with self.lock:
self.active.remove(name)
def numActive(self):
with self.lock:
return len(self.active)
def __str__(self):
with self.lock:
return str(self.active)
#######################################################################
################ Progression bar in the terminal ######################
#######################################################################
def progress_bar(count, total, title, completed=0, log=None):
terminal_size = get_terminal_size()
percentage = int(100.0 * count / total)
length_bar = min([max([3, terminal_size[0] - len(title) - len(str(total)) - len(str(count)) - len(str(percentage)) - 10]),20])
filled_len = int(length_bar * count / total)
bar = '█' * filled_len + ' ' * (length_bar - filled_len)
sys.stdout.write('%s [%s] %s %% (%d/%d)\r' % (title, bar, percentage, count, total))
sys.stdout.flush()
if completed:
sys.stdout.write("\n")
if log is not None:
log.info('%s [%s] %s %% (%d/%d)' % (title, bar, percentage, count, total))
###############################################################################
#################### Terminal size for different platform #####################
###############################################################################
def get_terminal_size():
current_os = platform.system()
tuple_xy = None
if current_os == 'Windows':
tuple_xy = _get_terminal_size_windows()
if tuple_xy is None:
tuple_xy = _get_terminal_size_tput()
# needed for window's python in cygwin's xterm!
if current_os in ['Linux', 'Darwin'] or current_os.beginswith('CYGWIN'):
tuple_xy = _get_terminal_size_linux()
if tuple_xy is None:
tuple_xy = (80, 25) # default value
return tuple_xy
def _get_terminal_size_windows():
try:
from ctypes import windll, create_string_buffer
h = windll.kernel32.GetStdHandle(-12)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
if res:
(bufx, bufy, curx, cury, wattr,
left, top, right, bottom,
maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
sizex = right - left + 1
sizey = bottom - top + 1
return sizex, sizey
except:
pass
def _get_terminal_size_tput():
try:
cols = int(subprocess.check_call(shlex.split('tput cols')))
rows = int(subprocess.check_call(shlex.split('tput lines')))
return (cols, rows)
except:
pass
def _get_terminal_size_linux():
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
return cr
except:
pass
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
try:
cr = (os.environ['LINES'], os.environ['COLUMNS'])
except:
return None
return int(cr[1]), int(cr[0])
###############################################################
######################### Figures #############################
###############################################################
def make_train_figure(loss_train, loss_val, acc_val, acc_train, path_to_save):
host = host_subplot(111, axes_class=AA.Axes)
par = host.twinx()
host.set_xlabel("Epochs")
host.set_ylabel("Loss")
par.set_ylabel("Accuracy")
par.axis["right"].toggle(all=True)
epochs = [i for i in range(1, len(loss_val)+1)]
host.set_xlim(1, len(epochs))
host.set_ylim(0, np.max([np.max(loss_train), np.max(loss_val)]))
par.set_ylim(0, 1)
max_acc = max(acc_val)
max_acc_idx = epochs[acc_val.index(max_acc)]
host.set_title("Max Validation Accuracy: %.1f%% at iteration %d" % (max_acc*100, max_acc_idx))
host.plot(epochs, loss_train, label="Train loss", linewidth=1.5)
host.plot(epochs, loss_val, label="Validation loss", linewidth=1.5)
par.plot(epochs, acc_val, label="Validation Accuracy", linewidth=1.5)
par.plot(epochs, acc_train, label="Train Accuracy", linewidth=1.5)
host.legend(loc='lower right', ncol=1, fancybox=False, shadow=True)
plt.savefig(path_to_save)
plt.close('all')
return True
##########################################################################
######################### Confusion Matrix ###############################
##########################################################################
def plot_confusion_matrix(cm, classes, path, normalize=False, cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting 'normalize=True'.
"""
acc = np.mean(np.array([cm[i,i] for i in range(len(cm))]).sum()/cm.sum()) * 100
if normalize:
cm_txt = cm.astype('float') / np.array([max(tmp,1) for tmp in cm.sum(axis=1)[:, np.newaxis]]).astype('float')
else:
cm_txt = cm
cm = cm.astype('float') / np.array([max(tmp,1) for tmp in cm.sum(axis=1)[:, np.newaxis]]).astype('float')
acc_2 = np.array([cm[i,i] for i in range(len(cm))])
title = 'Accuracy of %.1f%% ($\\mu$ = %.1f with $\\sigma$ = %.1f)' % (acc, np.mean(acc_2)*100, np.std(acc_2)*100)
plt.subplots(figsize=(12,12))
plt.imshow(cm.astype('float'), interpolation='nearest', cmap=cmap, vmin=0, vmax=1)
plt.title(title, fontsize=18)
plt.colorbar(fraction=0.046, pad=0.04)
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=90, fontsize=14)
plt.yticks(tick_marks, classes, fontsize=14)
fmt = '.2g' if normalize else 'd'
thresh = .5
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
if normalize:
plt.text(j, i, format(round(cm_txt[i, j]*100,2), fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
else:
plt.text(j, i, format(round(cm_txt[i, j],2), fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.ylabel('True label', fontsize=14)
plt.xlabel('Predicted label', fontsize=14)
plt.tight_layout()
plt.savefig(path)
plt.close('all')
#########################################################################
######################### Frame Extractor ###############################
#########################################################################
def frame_extractor(video_path, width, save_path):
# Load Video
cap = cv2.VideoCapture(video_path)
length_video = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frame_number = 0
# Check if video uploaded
if not cap.isOpened():
sys.exit("Unable to open the video, check the path.\n")
while frame_number < length_video:
# Load video
_, rgb = cap.read()
# Check if load Properly
if _ == 1:
# Resizing and Save
rgb = cv2.resize(rgb, (width, rgb.shape[0]*width//rgb.shape[1]))
cv2.imwrite(os.path.join(save_path, '%08d.png' % frame_number), rgb)
frame_number+=1
cap.release()