-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
141 lines (113 loc) · 4.44 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
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
import tensorflow as tf
import cv2
import numpy as np
from tensorflow.python.framework import ops
from tensorflow.python.ops import gen_nn_ops
from tensorflow.python.ops import array_ops
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)
#(1)
#inputs= [4, 64, 128, 64]
#pooling_indices= [4, 64, 128, 64]
#indices= [2097152, 4]
#values= [2097152]
#output_shape= [4, 128, 256, 64]
#ret= [4, 128, 256, 64] retname= ScatterNd:0
#(2)
#inputs= [4, 128, 256, 16]
#pooling_indices= [4, 128, 256, 16]
#indices= [2097152, 4]
#values= [2097152]
#output_shape= [4, 256, 512, 16]
#ret= [4, 256, 512, 16] retname= ScatterNd_1:0
print("inputs=", inputs.get_shape().as_list())
print("pooling_indices=", pooling_indices.get_shape().as_list())
print("indices=", indices.get_shape().as_list())
print("values=", values.get_shape().as_list())
print("output_shape=", output_shape)
print("ret=", ret.get_shape().as_list(), "retname=", ret.name)
return ret
@ops.RegisterGradient("MaxPoolGradWithArgmax")
def _MaxPoolGradGradWithArgmax(op, grad):
#print(len(op.outputs))
#print(len(op.inputs))
#print(op.name)
return (array_ops.zeros(
shape=array_ops.shape(op.inputs[0]),
dtype=op.inputs[0].dtype), array_ops.zeros(
shape=array_ops.shape(op.inputs[1]), dtype=op.inputs[1].dtype),
gen_nn_ops._max_pool_grad_grad_with_argmax(
op.inputs[0],
grad,
op.inputs[2],
op.get_attr("ksize"),
op.get_attr("strides"),
padding=op.get_attr("padding")))
# 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