-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
163 lines (133 loc) · 5.68 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import skimage
import skimage.io
import skimage.transform
import numpy as np
import tensorflow as tf
# returns image of shape [224, 224, 3]
# [height, width, depth]
def load_image(path):
# load image
img = skimage.io.imread(path)
#print "Original Image Shape: ", img.shape
# we crop image from center
short_edge = min(img.shape[:2])
yy = int((img.shape[0] - short_edge) / 2)
xx = int((img.shape[1] - short_edge) / 2)
crop_img = img[yy : yy + short_edge, xx : xx + short_edge]
# resize to 224, 224
resized_img = skimage.transform.resize(crop_img, (224, 224))
return resized_img
conv_counter = 0
def _conv(inpOp, kH, kW, nOut, dH=1, dW=1, relu=True):
global conv_counter
global parameters
name = 'conv' + str(conv_counter)
conv_counter += 1
with tf.name_scope(name) as scope:
nIn = int(inpOp.get_shape()[-1])
stddev = 5e-3
kernel = tf.Variable(tf.truncated_normal([kH, kW, nIn, nOut],
dtype=tf.float32,
stddev=(kH*kW*nIn)**0.5*stddev), name='weights')
conv = tf.nn.conv2d(inpOp, kernel, [1, 1, 1, 1],
padding="SAME")
biases = tf.Variable(tf.constant(0.0, shape=[nOut], dtype=tf.float32),
trainable=True, name='biases')
bias = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())
if relu:
bias = tf.nn.relu(bias, name=scope)
#parameters += [kernel, biases]
#bias = tf.Print(bias, [tf.sqrt(tf.reduce_mean(tf.square(inpOp - tf.reduce_mean(inpOp))))], message=kernel.name)
tf.histogram_summary(scope+"/output", bias)
tf.image_summary(scope+"/output", bias[:,:,:,0:3])
tf.image_summary(scope+"/kernel_weight", tf.expand_dims(kernel[:,:,0:3,0], 0))
# tf.image_summary(scope+"/point_weight", pointwise_filter)
return bias
deconv_counter = 0
def _deconv(inpOp, kH, kW, nOut, dH=1, dW=1, relu=True, name=None):
global deconv_counter
global parameters
if not name:
name = 'deconv' + str(deconv_counter)
deconv_counter += 1
with tf.variable_scope(name) as scope:
nIn = int(inpOp.get_shape()[-1])
in_shape = inpOp.get_shape()
stddev = 1e-3
kernel = tf.get_variable('weights',[kH, kW, nOut, nIn], initializer=tf.random_normal_initializer(stddev=(kH*kW*nIn)**0.5*stddev))
conv = tf.nn.deconv2d(inpOp, kernel, [int(in_shape[0]),int(in_shape[1]),int(in_shape[2]),nOut], [1, 1, 1, 1],
padding="SAME")
biases = tf.get_variable('biases', [nOut], initializer=tf.constant_initializer(value=0.0))
bias = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())
if relu:
bias = tf.nn.relu(bias, name='relu')
#parameters += [kernel, biases]
#bias = tf.Print(bias, [tf.sqrt(tf.reduce_mean(tf.square(inpOp - tf.reduce_mean(inpOp))))], message=kernel.name)
tf.histogram_summary(bias.name+"/output", bias)
tf.image_summary(bias.name+"/output", bias[:,:,:,0:3])
#tf.image_summary(scope+"/depth_weight", depthwise_filter)
# tf.image_summary(scope+"/point_weight", pointwise_filter)
return bias
fc_counter = 0
def _fc(inpOp, nOut):
global fc_counter
name = 'fc' + str(fc_counter)
fc_counter += 1
with tf.name_scope(name) as scope:
shape = inpOp.get_shape()
reshaped = tf.reshape(inpOp, [int(shape[0]), shape.num_elements()/int(shape[0])])
nIn = int(reshaped.get_shape()[-1])
kernel = tf.Variable(tf.truncated_normal([nIn, nOut],
dtype=tf.float32,
stddev=nIn**0.5 * 4e-3), name='weights', trainable=True)
b = tf.Variable(tf.constant(0.0, shape=[nOut], dtype=tf.float32),
trainable=True, name='biases')
res = tf.matmul(reshaped, kernel) + b
return res
def _doubledim(inp, dim):
#a, b = tf.split(3, 2, inp)
#return tf.concat(dim, [a, b])
b = tf.expand_dims(inp,dim+1)
c = tf.concat(dim+1, [b, tf.zeros(b.get_shape())])
#blur_kernel_size = [1,1,1,1]
#blur_kernel_size[dim-1]=2
shape = inp.get_shape()
new_shape = []
for i in range(len(shape)):
if i==dim:
new_shape.append(int(shape[i])*2)
else:
new_shape.append(int(shape[i]))
c = tf.reshape(c, new_shape)
return c
def _doublesh(inp):
with tf.name_scope("doublesh"):
a = _double(inp)
ndims = int(a.get_shape()[3])
kernel = tf.constant([[0.25, -0.5, 0.25], [-0.5, 1.0, -0.5], [0.25, -0.5, 0.25]])
kernel = tf.expand_dims(kernel,2)
kernel = tf.expand_dims(kernel,3)
kernel = tf.tile(kernel,[1,1,ndims,1])
#a = tf.nn.depthwise_conv2d(a, kernel, [1, 1, 1, 1],
# padding="SAME", name="blur")
return a
def _doublelp(inp):
with tf.name_scope("doublelp"):
a = _double(inp)
ndims = int(a.get_shape()[3])
kernel = tf.constant([[0.25, 0.5, 0.25], [0.5, 1.0, 0.5], [0.25, 0.5, 0.25]])
kernel = tf.expand_dims(kernel,2)
kernel = tf.expand_dims(kernel,3)
kernel = tf.tile(kernel,[1,1,ndims,1])
a = tf.nn.depthwise_conv2d(a, kernel, [1, 1, 1, 1],
padding="SAME", name="blur")
return a
def _double(inp):
with tf.name_scope("double"):
a = _doubledim(inp, 1)
a = _doubledim(a, 2)
return a
def _half(inpOp):
shape = inpOp.get_shape()
#inpOp = tf.nn.dropout(inpOp, 0.5);
return tf.nn.avg_pool(inpOp, [1,2,2,1], [1,2,2,1], 'SAME')