-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnnUtils.py
More file actions
82 lines (62 loc) · 2.29 KB
/
nnUtils.py
File metadata and controls
82 lines (62 loc) · 2.29 KB
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
import tensorflow as tf
STRIDE = 2
def weight_variable(shape, name):
return tf.get_variable(name, shape=shape,
initializer=tf.contrib.layers.xavier_initializer())#tf.initializers.orthogonal())
def bias_variable(shape, name):
initial = tf.constant(0.1, shape=shape, name=name)
return tf.Variable(initial)
def conv(input, filter, name, pad="SAME", dilation=0, dropR=0.3):
f = weight_variable(filter, name+"f1")
if not dilation > 0:
conv = tf.nn.conv2d(input, f, strides=[1,1,1,1], padding=pad, name=name)
else:
conv = tf.nn.atrous_conv2d(input, f, dilation, padding=pad)
#conv_bias = tf.nn.bias_add(conv, bias_variable([filter[3]], name=name+"b1"))
batch_norm = tf.contrib.layers.batch_norm(conv)
relu = tf.nn.relu(batch_norm)
#if leakyR:
#relu = tf.nn.leaky_relu(batch_norm,alpha=0.2,name=None)
#drop = tf.nn.dropout(relu, dropR)
return relu
def pool(input, window, stride, poolIndices=False, name="POOL"):
if poolIndices:
pool = tf.nn.max_pool_with_argmax(
input,
ksize=[1, window, window,1],
strides=[1, stride, stride, 1],
padding="SAME",
name=name
)
else:
pool = tf.nn.avg_pool(
input,
ksize=[1, window, window,1],
strides=[1, stride, stride, 1],
padding="SAME",
name=name
)
return pool
def deconv_filter(shape, name):
# filter = tf.zeros((
# shape[0], # height
# shape[1], # width
# shape[2], # out channels
# shape[3] # in channels
# ), name=name)
#return tf.Variable(filter)
return tf.get_variable(name, shape=shape,
initializer=tf.contrib.layers.xavier_initializer())
def deconv(layer, outputShape, filterShape, name, stride=2):
filter = deconv_filter(filterShape, "deconvF"+layer.name[:len(layer.name)-2])
deconv = tf.nn.conv2d_transpose(
layer,
filter,
outputShape,
strides=[1,stride,stride,1],
padding="SAME",
name=name
)
#deconv = tf.image.resize_bilinear(
# layer, outputShape, align_corners=True)
return deconv