forked from xiaobai1217/MBMD
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vggm.py
137 lines (120 loc) · 7.95 KB
/
vggm.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
import tensorflow as tf
class vggM:
def __init__(self):
self.learningRates = {}
def extractFeature(self, inputs):
with tf.variable_scope('VGGM') as scope:
scope.reuse_variables()
with tf.variable_scope('layer1'):
weights = tf.get_variable("weights")
biases = tf.get_variable("biases")
outputs = tf.nn.conv2d(inputs, weights, strides=[1, 2, 2, 1], padding='VALID') + biases
outputs = tf.nn.relu(outputs)
outputs = tf.nn.lrn(outputs, depth_radius=2, bias=1.0, alpha=0.0001, beta=0.75)
outputs = tf.nn.max_pool(outputs, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID')
with tf.variable_scope('layer2'):
weights = tf.get_variable("weights")
biases = tf.get_variable("biases")
outputs = tf.nn.conv2d(outputs, weights, strides=[1, 2, 2, 1], padding='VALID') + biases
outputs = tf.nn.relu(outputs)
outputs = tf.nn.lrn(outputs, depth_radius=2, bias=1.0, alpha=0.0001, beta=0.75)
outputs = tf.nn.max_pool(outputs, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID')
with tf.variable_scope('layer3'):
weights = tf.get_variable("weights")
biases = tf.get_variable("biases")
outputs3 = tf.nn.conv2d(outputs, weights, strides=[1, 1, 1, 1], padding='VALID') + biases
outputs = tf.nn.relu(outputs3)
return outputs
def classification(self,inputs):
with tf.variable_scope('VGGM') as scope:
scope.reuse_variables()
with tf.variable_scope('layer4'):
weights = tf.get_variable("weights")
biases = tf.get_variable("biases")
outputs = tf.nn.dropout(inputs, keep_prob=0.5)
outputs = tf.nn.conv2d(outputs, weights, strides=[1, 1, 1, 1], padding='VALID') + biases
outputs = tf.nn.relu(outputs)
with tf.variable_scope('layer5'):
outputs = tf.contrib.layers.flatten(outputs)
weights = tf.get_variable("weights")
biases = tf.get_variable("biases")
outputs = tf.nn.dropout(outputs, keep_prob=0.5)
outputs = tf.matmul(outputs, weights) + biases
outputs = tf.nn.relu(outputs)
with tf.variable_scope('layer6'):
weights = tf.get_variable("weights")
biases = tf.get_variable("biases")
outputs = tf.nn.dropout(outputs, keep_prob=0.5)
outputs = tf.matmul(outputs, weights) + biases
score = tf.nn.softmax(outputs,dim = 1)
return outputs,score
def loss(self,inputs,label):
loss1 = tf.losses.softmax_cross_entropy(onehot_labels=label,logits=inputs)
loss = tf.reduce_sum(loss1)
#with tf.variable_scope('VGGM'):
# regularization = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
# loss = tf.add_n([loss]+regularization)
with tf.variable_scope("VGGM") as scope:
scope.reuse_variables()
with tf.variable_scope("layer4"):
weights1 = tf.get_variable("weights")
with tf.variable_scope("layer5"):
weights2 = tf.get_variable("weights")
with tf.variable_scope("layer6"):
weights3 = tf.get_variable("weights")
loss += (tf.nn.l2_loss(weights1)+ tf.nn.l2_loss(weights2) + tf.nn.l2_loss(weights3)) * 5e-4
return loss,loss1
def vggM(self,inputs,reuse=False):
with tf.variable_scope('VGGM') as scope:
if reuse is True:
scope.reuse_variables()
with tf.variable_scope('layer1'):
weights = tf.get_variable("weights", shape=(7, 7, 3, 96), dtype=tf.float32,
initializer=tf.truncated_normal_initializer(0.01), trainable=False)
biases = tf.get_variable("biases", shape=(96,), dtype=tf.float32, initializer=tf.constant_initializer(0.0),trainable=False)
outputs = tf.nn.conv2d(inputs, weights, strides=[1, 2, 2, 1], padding='VALID') + biases
outputs = tf.nn.relu(outputs)
outputs = tf.nn.lrn(outputs, depth_radius=2, bias=1.0, alpha=0.0001, beta=0.75)
outputs = tf.nn.max_pool(outputs, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID')
with tf.variable_scope('layer2'):
weights = tf.get_variable("weights", shape=(5, 5, 96, 256), dtype=tf.float32,
initializer=tf.truncated_normal_initializer(0.01), trainable=False)
biases = tf.get_variable("biases", shape=(256,), dtype=tf.float32, initializer=tf.constant_initializer(0.0), trainable=False)
outputs = tf.nn.conv2d(outputs, weights, strides=[1, 2, 2, 1], padding='VALID') + biases
outputs = tf.nn.relu(outputs)
outputs = tf.nn.lrn(outputs, depth_radius=2, bias=1.0, alpha=0.0001, beta=0.75)
outputs = tf.nn.max_pool(outputs, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='VALID')
with tf.variable_scope('layer3'):
weights = tf.get_variable("weights", shape=(3, 3, 256, 512), dtype=tf.float32,
initializer=tf.truncated_normal_initializer(0.01), trainable=False)
biases = tf.get_variable("biases", shape=(512,), dtype=tf.float32, initializer=tf.constant_initializer(0.0),trainable=False)
outputs3 = tf.nn.conv2d(outputs, weights, strides=[1, 1, 1, 1], padding='VALID') + biases
outputs = tf.nn.relu(outputs3)
with tf.variable_scope('layer4'):
weights = tf.get_variable("weights", shape=(3, 3, 512, 512), dtype=tf.float32,
initializer=tf.truncated_normal_initializer(0.01),
regularizer=tf.contrib.layers.l2_regularizer(5e-4), trainable=True)
biases = tf.get_variable("biases", shape=(512,), dtype=tf.float32, initializer=tf.constant_initializer(0.0),
regularizer=tf.contrib.layers.l2_regularizer(5e-4), trainable=True)
#outputs = tf.nn.dropout(outputs,keep_prob=0.5)
outputs = tf.nn.conv2d(outputs, weights, strides=[1, 1, 1, 1], padding='VALID') + biases
outputs = tf.nn.relu(outputs)
with tf.variable_scope('layer5'):
outputs = tf.contrib.layers.flatten(outputs)
weights = tf.get_variable("weights", shape=(512, 512), dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer(),
regularizer=tf.contrib.layers.l2_regularizer(5e-4), trainable=True)
biases = tf.get_variable("biases", shape=(512,), dtype=tf.float32, initializer=tf.constant_initializer(0.0),
regularizer=tf.contrib.layers.l2_regularizer(5e-4), trainable=True)
#outputs = tf.nn.dropout(outputs,keep_prob=0.5)
outputs = tf.matmul(outputs,weights) + biases
outputs = tf.nn.relu(outputs)
with tf.variable_scope('layer6'):
weights = tf.get_variable("weights", shape=(512, 2), dtype=tf.float32,
initializer=tf.contrib.layers.xavier_initializer(),
regularizer=tf.contrib.layers.l2_regularizer(5e-4), trainable=True)
biases = tf.get_variable("biases", shape=(2,), dtype=tf.float32, initializer=tf.constant_initializer(0.0),
regularizer=tf.contrib.layers.l2_regularizer(5e-4), trainable=True)
#outputs = tf.nn.dropout(outputs,keep_prob=0.5)
outputs = tf.matmul(outputs,weights)+ biases
return outputs