forked from Vladkryvoruchko/PSPNet-Keras-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
91 lines (74 loc) · 3.33 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
from __future__ import print_function
import colorsys
import numpy as np
from keras.models import Model
from cityscapes_labels import trainId2label
from ade20k_labels import ade20k_id2label
from pascal_voc_labels import voc_id2label
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 color_class_image(class_image, model_name):
"""Color classed depending on the model used."""
if 'cityscapes' in model_name:
colored_image = class_image_to_image(class_image, trainId2label)
elif 'voc' in model_name:
colored_image = class_image_to_image(class_image, voc_id2label)
elif 'ade20k' in model_name:
colored_image = class_image_to_image(class_image, ade20k_id2label)
else:
colored_image = add_color(class_image)
return colored_image
def color_class_image_slow(class_image, model_name):
"""Color classed depending on the model used."""
if 'cityscapes' in model_name:
colored_image = class_image_to_image_slow(class_image, trainId2label)
elif 'voc' in model_name:
colored_image = class_image_to_image_slow(class_image, voc_id2label)
elif 'ade20k' in model_name:
colored_image = class_image_to_image_slow(class_image, ade20k_id2label)
else:
colored_image = add_color(class_image)
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))