-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.py
More file actions
182 lines (142 loc) · 6.69 KB
/
Copy pathnet.py
File metadata and controls
182 lines (142 loc) · 6.69 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
########################################################################################
# Guan Heng, 2017.5.10 #
# trainable VGG16 implementation in TensorFlow #
# Details: #
# http://www.cs.toronto.edu/~frossard/post/vgg16/ #
# #
# Model from https://github.com/machrisaa/tensorflow-vgg #
# Weights from https://mega.nz/#!YU1FWJrA!O1ywiCS2IiOlUCtCpI6HTJOMrneN-Qdv3ywQP5poecM #
########################################################################################
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
import re
__author__ = "GuanHeng"
class Net(object):
"""Base Net class
"""
def __init__(self, model_npy_path=None, trainable=True, dropout=0.5):
if model_npy_path is not None:
self.data_dict = np.load(model_npy_path, encoding='latin1').item()
else:
self.data_dict = None
self.var_dict = {}
self.trainable = trainable
self.dropout = dropout
def avg_pooling(self, bottom, kernel_size, strides=2, name=None):
"""subsample a convolution layer using average pool rule
:param bottom:
:param kernel_size:
:param strides:
:param name:
:return:
"""
return tf.nn.avg_pool(bottom,
ksize=[1, kernel_size[0], kernel_size[1], 1],
strides=[1, strides, strides, 1],
padding='SAME', name=name)
def max_pooling(self, bottom, kernel_size, strides=2, name=None):
return tf.nn.max_pool(bottom,
ksize=[1, kernel_size[0], kernel_size[1], 1],
strides=[1, strides, strides, 1],
padding='SAME', name=name)
def conv2d(self, bottom, kernel_shape, name, strides=1):
with tf.variable_scope(name):
filt, conv_biases = self.get_conv_var(kernel_shape, name)
conv = tf.nn.conv2d(bottom, filt, [1, strides, strides, 1], padding='SAME')
bias = tf.nn.bias_add(conv, conv_biases)
relu = tf.nn.relu(bias)
return relu
def conv2d_basic(self, bottom, kernel_shape, name, strides=1):
"""a basic 2-dimentional convolution layer
:param bottom:
:param kernel_shape:
:param name:
:param strides:
:return:
"""
with tf.variable_scope(name):
filt, conv_biases = self.get_conv_var(kernel_shape, name)
conv = tf.nn.conv2d(bottom, filt, [1, strides, strides, 1], padding='SAME')
return tf.nn.bias_add(conv, conv_biases)
def fc_layer(self, bottom, in_size, out_size, name):
""" full connected layer
:param bottom:
:param in_size:
:param out_size:
:param name:
:return:
"""
with tf.variable_scope(name):
weights, biases = self.get_fc_var(in_size, out_size, name)
x = tf.reshape(bottom, [-1, in_size])
fc = tf.nn.bias_add(tf.matmul(x, weights), biases)
return fc
def conv2d_transpose_strided(self, bottom, kernel_shape, output_shape=None, stride=2, name=None):
""" deconvolution layer
:param bottom:
:param kernel_shape:
:param output_shape:
:param stride:
:param name:
:return:
"""
with tf.variable_scope(name):
filt, conv_biases = self.get_conv_trans_var(kernel_shape, name)
if output_shape is None:
output_shape = x.get_shape().as_list()
output_shape[1] *= 2
output_shape[2] *= 2
output_shape[3] = W.get_shape().as_list()[2]
# print output_shape
conv = tf.nn.conv2d_transpose(bottom, filt, output_shape, strides=[1, stride, stride, 1], padding="SAME")
return tf.nn.bias_add(conv, conv_biases)
def get_conv_var(self, kernel_shape, name):
initial_value = tf.truncated_normal(kernel_shape, 0.0, 0.001)
filters = self.get_var(initial_value, name, 0, name + "_filters")
initial_value = tf.truncated_normal([kernel_shape[3]], 0.0, 0.001)
biases = self.get_var(initial_value, name, 1, name + "_biases")
return filters, biases
def get_conv_trans_var(self, kernel_shape, name):
initial_value = tf.truncated_normal(kernel_shape, 0.0, 0.001)
filters = self.get_var(initial_value, name, 0, name + "_filters")
initial_value = tf.truncated_normal([kernel_shape[2]], 0.0, 0.001)
biases = self.get_var(initial_value, name, 1, name + "_biases")
return filters, biases
def get_fc_var(self, in_size, out_size, name):
initial_value = tf.truncated_normal([in_size, out_size], 0.0, 0.001)
weights = self.get_var(initial_value, name, 0, name + "_weights")
initial_value = tf.truncated_normal([out_size], 0.0, 0.001)
biases = self.get_var(initial_value, name, 1, name + "_biases")
return weights, biases
def get_var(self, initial_value, name, idx, var_name):
if self.data_dict is not None and name in self.data_dict:
value = self.data_dict[name][idx]
else:
value = initial_value
if self.trainable:
var = tf.Variable(value, name=var_name)
else:
var = tf.constant(value, dtype=tf.float32, name=var_name)
self.var_dict[(name, idx)] = var
# print var_name, var.get_shape().as_list()
assert var.get_shape() == initial_value.get_shape()
return var
def save_npy(self, sess, npy_path="./model-save.npy"):
assert isinstance(sess, tf.Session)
data_dict = {}
for (name, idx), var in list(self.var_dict.items()):
var_out = sess.run(var)
if name not in data_dict:
data_dict[name] = {}
data_dict[name][idx] = var_out
np.save(npy_path, data_dict)
print(("file saved", npy_path))
return npy_path
def get_var_count(self):
count = 0
for v in list(self.var_dict.values()):
count += reduce(lambda x, y: x * y, v.get_shape().as_list())
return count