Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
99 lines (74 sloc) 2.97 KB
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