-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
102 lines (78 loc) · 3.42 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
# -*- coding: utf-8 -*-
from __future__ import print_function
import colorsys
import os
import sys
import numpy as np
from keras.models import Model
def mkdir_recursive(path):
sub_path = os.path.dirname(path)
if not os.path.exists(sub_path):
mkdir_recursive(sub_path)
if not os.path.exists(path):
os.mkdir(path)
def class_image_to_image(class_id_image, class_id_to_rgb_map):
"""Map the class image to a rgb-color image."""
colored_image = np.zeros((class_id_image.shape[0], class_id_image.shape[1], 3), np.uint8)
for i in range(-1, 256):
try:
cl = class_id_to_rgb_map[i]
colored_image[class_id_image[:, :] == i] = cl.color
except KeyError as key_error:
pass
return colored_image
def class_image_to_image_slow(class_id_image, class_id_to_rgb_map):
"""Map the class image to a rgb-color image."""
colored_image = np.zeros((class_id_image.shape[0], class_id_image.shape[1], 3), np.uint8)
for row in range(class_id_image.shape[0]):
for col in range(class_id_image.shape[1]):
try:
colored_image[row, col, :] = class_id_to_rgb_map[int(class_id_image[row, col])].color
except KeyError as key_error:
print("Warning: could not resolve classid %s" % key_error)
return colored_image
def add_color(img):
"""Color classes a good distance away from each other."""
h, w = img.shape
img_color = np.zeros((h, w, 3))
for i in xrange(1, 151):
img_color[img == i] = to_color(i)
return img_color * 255 # is [0.0-1.0] should be [0-255]
def to_color(category):
"""Map each category color a good distance away from each other on the HSV color space."""
v = (category - 1) * (137.5 / 360)
return colorsys.hsv_to_rgb(v, 1, 1)
def debug(model, data):
"""Debug model by printing the activations in each layer."""
names = [layer.name for layer in model.layers]
for name in names[:]:
print_activation(model, name, data)
def print_activation(model, layer_name, data):
"""Print the activations in each layer."""
intermediate_layer_model = Model(inputs=model.input,
outputs=model.get_layer(layer_name).output)
io = intermediate_layer_model.predict(data)
print(layer_name, array_to_str(io))
def array_to_str(a):
return "{} {} {} {} {}".format(a.dtype, a.shape, np.min(a),
np.max(a), np.mean(a))
# Print iterations progress
def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
bar_length - Optional : character length of bar (Int)
"""
str_format = "{0:." + str(decimals) + "f}"
percents = str_format.format(100 * (iteration / float(total)))
filled_length = int(round(bar_length * iteration / float(total)))
bar = '█' * filled_length + '-' * (bar_length - filled_length)
sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
if iteration == total:
sys.stdout.write('\n')
sys.stdout.flush()