forked from fregu856/segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
98 lines (74 loc) · 2.97 KB
/
utilities.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
import tensorflow as tf
import cv2
import numpy as np
def PReLU(x, scope):
# PReLU(x) = x if x > 0, alpha*x otherwise
alpha = tf.get_variable(scope + "/alpha", shape=[1],
initializer=tf.constant_initializer(0), dtype=tf.float32)
output = tf.nn.relu(x) + alpha*(x - abs(x))*0.5
return output
# function for 2D spatial dropout:
def spatial_dropout(x, drop_prob):
# x is a tensor of shape [batch_size, height, width, channels]
keep_prob = 1.0 - drop_prob
input_shape = x.get_shape().as_list()
batch_size = input_shape[0]
channels = input_shape[3]
# drop each channel with probability drop_prob:
noise_shape = tf.constant(value=[batch_size, 1, 1, channels])
x_drop = tf.nn.dropout(x, keep_prob, noise_shape=noise_shape)
output = x_drop
return output
# function for unpooling max_pool:
def max_unpool(inputs, pooling_indices, output_shape=None, k_size=[1, 2, 2, 1]):
# NOTE! this function is based on the implementation by kwotsin in
# https://github.com/kwotsin/TensorFlow-ENet
# inputs has shape [batch_size, height, width, channels]
# pooling_indices: pooling indices of the previously max_pooled layer
# output_shape: what shape the returned tensor should have
pooling_indices = tf.cast(pooling_indices, tf.int32)
input_shape = tf.shape(inputs, out_type=tf.int32)
one_like_pooling_indices = tf.ones_like(pooling_indices, dtype=tf.int32)
batch_shape = tf.concat([[input_shape[0]], [1], [1], [1]], 0)
batch_range = tf.reshape(tf.range(input_shape[0], dtype=tf.int32), shape=batch_shape)
b = one_like_pooling_indices*batch_range
y = pooling_indices//(output_shape[2]*output_shape[3])
x = (pooling_indices//output_shape[3]) % output_shape[2]
feature_range = tf.range(output_shape[3], dtype=tf.int32)
f = one_like_pooling_indices*feature_range
inputs_size = tf.size(inputs)
indices = tf.transpose(tf.reshape(tf.stack([b, y, x, f]), [4, inputs_size]))
values = tf.reshape(inputs, [inputs_size])
ret = tf.scatter_nd(indices, values, output_shape)
return ret
# function for colorizing a label image:
def label_img_to_color(img):
label_to_color = {
0: [128, 64,128],
1: [244, 35,232],
2: [ 70, 70, 70],
3: [102,102,156],
4: [190,153,153],
5: [153,153,153],
6: [250,170, 30],
7: [220,220, 0],
8: [107,142, 35],
9: [152,251,152],
10: [ 70,130,180],
11: [220, 20, 60],
12: [255, 0, 0],
13: [ 0, 0,142],
14: [ 0, 0, 70],
15: [ 0, 60,100],
16: [ 0, 80,100],
17: [ 0, 0,230],
18: [119, 11, 32],
19: [81, 0, 81]
}
img_height, img_width = img.shape
img_color = np.zeros((img_height, img_width, 3))
for row in range(img_height):
for col in range(img_width):
label = img[row, col]
img_color[row, col] = np.array(label_to_color[label])
return img_color