@@ -9,97 +9,149 @@ def __init__(self,args):
9
9
self .args = args
10
10
self .variables_deepem ()
11
11
self .build_model ()
12
+ #self.one_layer()
12
13
14
+
15
+ def one_layer (self ):
16
+ w1 = tf .Variable (tf .truncated_normal ([self .args .FL_kernelsize , self .args .FL_kernelsize , 1 , self .args .FL_feature_map ], stddev = 4 ))
17
+ b1 = tf .Variable (tf .truncated_normal ([self .args .FL_feature_map ],stddev = 0.5 ))
18
+
19
+ w2 = [1 , self .args .SL_poolingsize , self .args .SL_poolingsize , 1 ]
20
+
21
+ input_map_size = self .args .boxsize
22
+ C1_map_size = input_map_size - self .args .FL_kernelsize + 1
23
+ S2_map_size = C1_map_size // self .args .SL_poolingsize
24
+
25
+ fully_para_num = int (self .args .FL_feature_map * S2_map_size * S2_map_size )
26
+ hidden_neurons = 2
27
+ print ("type(fully_para_num):" ,type (fully_para_num ))
28
+ print ("type(hidden_neurons):" ,type (hidden_neurons ))
29
+ # output layer
30
+ # todo: decide the second patameters of fully connected layer , now is 1
31
+ w3 = tf .Variable (tf .truncated_normal ([fully_para_num , hidden_neurons ], stddev = 4 ))
32
+ b3 = tf .Variable (tf .truncated_normal ([hidden_neurons ],stddev = 0.5 ))
33
+
34
+ self .X = tf .placeholder (tf .float32 , shape = [None , self .args .boxsize , self .args .boxsize , 1 ])
35
+ self .Y = tf .placeholder (tf .float32 , shape = [None ,2 ])
36
+ self .global_step = tf .Variable (0 , name = 'global_step' ,trainable = False )
37
+
38
+ layer1_conv = tf .nn .conv2d (self .X , w1 , [1 , 1 , 1 , 1 ], padding = 'VALID' )
39
+ layer1_actv = tf .sigmoid (layer1_conv + b1 )
40
+ #layer1_actv = tf.nn.relu(layer1_conv + self.variables['b1'])
41
+ print ("layer 1 shape is: " ,layer1_conv .shape )
42
+
43
+ print ("w2 = " , w2 )
44
+ layer2_pool = tf .nn .avg_pool (layer1_actv , w2 ,w2 , padding = 'VALID' )
45
+ print ("layer 2 shape is: " ,layer2_pool .shape )
46
+
47
+ layer3_input = tf .reshape (layer2_pool ,[- 1 ,fully_para_num ])
48
+ self .l3_input = layer3_input
49
+
50
+ print ("layer 3 shape is: " ,layer3_input .shape )
51
+ print ("w3 shape is: " ,w3 .shape )
52
+
53
+ self .logits = tf .matmul (layer3_input , w3 ) + b3
54
+
55
+ if not self .args .is_training :
56
+ return
57
+ #self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.logits, labels = self.Y))
58
+ self .loss = tf .reduce_mean (tf .nn .softmax_cross_entropy_with_logits (logits = self .logits , labels = self .Y ))
59
+ #self.loss = tf.sqrt(tf.reduce_mean(tf.square(self.Y - self.logits)))
60
+ #self.lr = tf.maximum(1e-5,tf.train.exponential_decay(self.args.alpha, self.global_step, self.args.decay_step, self.args.decay_rate, staircase=True))
61
+ self .lr = self .args .alpha
62
+ self .optimizer = tf .train .AdamOptimizer (self .lr ).minimize (self .loss )
63
+
64
+
13
65
def variables_deepem (self ):
14
66
# first convolutional layer
15
- w1 = tf .Variable (tf .truncated_normal ([self .args .FL_kernelsize , self .args .FL_kernelsize , 1 , self .args .FL_feature_map ], stddev = 0.1 ))
16
- b1 = tf .Variable (tf .zeros ([self .args .FL_feature_map ]))
67
+ w1 = tf .Variable (tf .truncated_normal ([self .args .FL_kernelsize , self .args .FL_kernelsize , 1 , self .args .FL_feature_map ], stddev = 4 ))
68
+ b1 = tf .Variable (tf .truncated_normal ([self .args .FL_feature_map ],stddev = 0.5 ))
69
+ #b1 = tf.Variable(tf.zeros([self.args.FL_feature_map]))
17
70
18
71
# second pooling layer
19
72
w2 = [1 , self .args .SL_poolingsize , self .args .SL_poolingsize , 1 ]
20
73
21
74
# third convolutional layer
22
- w3 = tf .Variable (tf .truncated_normal ([self .args .TL_kernelsize , self .args .TL_kernelsize , self .args .FL_feature_map , self .args .TL_feature_map ], stddev = 0.1 ))
23
- b3 = tf .Variable (tf .zeros ([self .args .TL_feature_map ]))
75
+ w3 = tf .Variable (tf .truncated_normal ([self .args .TL_kernelsize , self .args .TL_kernelsize , self .args .FL_feature_map , self .args .TL_feature_map ], stddev = 4 ))
76
+ b3 = tf .Variable (tf .truncated_normal ([self .args .TL_feature_map ],stddev = 0.5 ))
77
+ #b3 = tf.Variable(tf.zeros([self.args.TL_feature_map]))
24
78
25
79
# forth pooling layer
26
80
w4 = [1 , self .args .FOL_poolingsize , self .args .FOL_poolingsize , 1 ]
27
81
28
82
# fifth convolutional layer
29
- w5 = tf .Variable (tf .truncated_normal ([self .args .FIL_kernelsize , self .args .FIL_kernelsize , self .args .TL_feature_map , self .args .FIL_feature_map ], stddev = 0.1 ))
30
- b5 = tf .Variable (tf .zeros ([self .args .FIL_feature_map ]))
83
+ w5 = tf .Variable (tf .truncated_normal ([self .args .FIL_kernelsize , self .args .FIL_kernelsize , self .args .TL_feature_map , self .args .FIL_feature_map ], stddev = 4 ))
84
+ b5 = tf .Variable (tf .truncated_normal ([self .args .FIL_feature_map ],stddev = 0.5 ))
85
+ #b5 = tf.Variable(tf.zeros([self.args.FIL_feature_map]))
31
86
32
87
# sixth pooling layer
33
88
w6 = [1 , self .args .SIL_poolingsize , self .args .SIL_poolingsize , 1 ]
34
89
35
90
input_map_size = self .args .boxsize
36
91
C1_map_size = input_map_size - self .args .FL_kernelsize + 1
37
- S2_map_size = C1_map_size / self .args .SL_poolingsize
92
+ S2_map_size = C1_map_size // self .args .SL_poolingsize
38
93
C3_map_size = S2_map_size - self .args .TL_kernelsize + 1
39
- S4_map_size = C3_map_size / self .args .FOL_poolingsize
94
+ S4_map_size = C3_map_size // self .args .FOL_poolingsize
40
95
C5_map_size = S4_map_size - self .args .FIL_kernelsize + 1
41
- S6_map_size = C5_map_size / self .args .SIL_poolingsize
42
- fully_para_num = self .args .FIL_feature_map * S6_map_size * S6_map_size
43
- hidden_neurons = 1
44
-
96
+ S6_map_size = C5_map_size // self .args .SIL_poolingsize
97
+ fully_para_num = int (self .args .FIL_feature_map * S6_map_size * S6_map_size )
98
+ hidden_neurons = 2
99
+ print ("type(fully_para_num):" ,type (fully_para_num ))
100
+ print ("type(hidden_neurons):" ,type (hidden_neurons ))
45
101
# output layer
46
102
# todo: decide the second patameters of fully connected layer , now is 1
47
- w7 = tf .Variable (tf .truncated_normal ([fully_para_num , hidden_neurons ], stddev = 0.1 ))
48
- b7 = tf .Variable (tf .zeros ([hidden_neurons ]))
103
+ w7 = tf .Variable (tf .truncated_normal ([fully_para_num , hidden_neurons ], stddev = 4 ))
104
+ b7 = tf .Variable (tf .truncated_normal ([hidden_neurons ],stddev = 0.5 ))
105
+ #b7 = tf.Variable(tf.zeros([hidden_neurons]))
49
106
50
107
self .variables = {
51
108
'w1' : w1 , 'w2' : w2 , 'w3' : w3 , 'w4' : w4 , 'w5' : w5 , 'w6' : w6 , 'w7' : w7 ,
52
109
'b1' : b1 , 'b3' : b3 , 'b5' : b5 , 'b7' : b7 , 'fully_para_num' : fully_para_num
53
110
}
54
111
55
- def build_model (self ):
56
- # if self.args.is_training:
57
- # self.X = tf.placeholder(tf.float32, shape = [self.args.batch_size, self.args.boxsize, self.args.boxsize, 1])
58
- # self.Y = tf.placeholder(tf.float32, shape = [self.args.batch_size,1])
59
- # else:
60
- # self.X = tf.placeholder(tf.float32, shape = [self.args.rotation_n, self.args.boxsize, self.args.boxsize, 1])
61
- # self.Y = tf.placeholder(tf.float32, shape = [self.args.rotation_n,1])
62
-
112
+ def build_model (self ):
63
113
self .X = tf .placeholder (tf .float32 , shape = [None , self .args .boxsize , self .args .boxsize , 1 ])
64
- self .Y = tf .placeholder (tf .float32 , shape = [None ,1 ])
114
+ self .Y = tf .placeholder (tf .float32 , shape = [None ,2 ])
65
115
self .global_step = tf .Variable (0 , name = 'global_step' ,trainable = False )
66
116
67
117
layer1_conv = tf .nn .conv2d (self .X , self .variables ['w1' ], [1 , 1 , 1 , 1 ], padding = 'VALID' )
68
- layer1_actv = tf .sigmoid (layer1_conv + self .variables ['b1' ])
69
- print "layer 1 shape is: " ,layer1_conv .shape
118
+ layer1_actv = tf .nn .relu (layer1_conv + self .variables ['b1' ])
119
+ #layer1_actv = tf.nn.relu(layer1_conv + self.variables['b1'])
120
+ print ("layer 1 shape is: " ,layer1_conv .shape )
70
121
71
- print "w2 = " , self .variables ['w2' ]
72
- layer2_pool = tf .nn .avg_pool (layer1_actv , self .variables ['w2' ],self .variables ['w2' ], padding = 'VALID' )
73
- print "layer 2 shape is: " ,layer2_pool .shape
122
+ print ( "w2 = " , self .variables ['w2' ])
123
+ layer2_pool = tf .nn .max_pool (layer1_actv , self .variables ['w2' ],self .variables ['w2' ], padding = 'VALID' )
124
+ print ( "layer 2 shape is: " ,layer2_pool .shape )
74
125
75
126
layer3_conv = tf .nn .conv2d (layer2_pool , self .variables ['w3' ], [1 , 1 , 1 , 1 ], padding = 'VALID' )
76
- layer3_actv = tf .sigmoid (layer3_conv + self .variables ['b3' ])
77
- print "layer 3 shape is: " ,layer3_conv .shape
127
+ layer3_actv = tf .nn . relu (layer3_conv + self .variables ['b3' ])
128
+ print ( "layer 3 shape is: " ,layer3_conv .shape )
78
129
79
- layer4_pool = tf .nn .avg_pool (layer3_actv , self .variables ['w4' ], self .variables ['w4' ], padding = 'VALID' )
80
- print "layer 4 shape is: " ,layer4_pool .shape
130
+ layer4_pool = tf .nn .max_pool (layer3_actv , self .variables ['w4' ], self .variables ['w4' ], padding = 'VALID' )
131
+ print ( "layer 4 shape is: " ,layer4_pool .shape )
81
132
82
133
layer5_conv = tf .nn .conv2d (layer4_pool , self .variables ['w5' ], [1 , 1 , 1 , 1 ], padding = 'VALID' )
83
- layer5_actv = tf .sigmoid (layer5_conv + self .variables ['b5' ])
84
- print "layer 5 shape is: " ,layer5_conv .shape
134
+ layer5_actv = tf .nn . relu (layer5_conv + self .variables ['b5' ])
135
+ print ( "layer 5 shape is: " ,layer5_conv .shape )
85
136
86
- layer6_pool = tf .nn .avg_pool (layer5_actv , self .variables ['w6' ], self .variables ['w6' ], padding = 'VALID' )
137
+ layer6_pool = tf .nn .max_pool (layer5_actv , self .variables ['w6' ], self .variables ['w6' ], padding = 'VALID' )
87
138
88
139
# layer6_flatten = tf.contrib.layer.flaten(layer6_pool)
89
140
# flatten the output of layer6
90
- print "layer 6 shape is: " ,layer6_pool .shape
141
+ print ( "layer 6 shape is: " ,layer6_pool .shape )
91
142
layer7_input = tf .reshape (layer6_pool ,[- 1 ,self .variables ['fully_para_num' ]])
92
143
self .l7_input = layer7_input
93
144
94
- print "layer 7 shape is: " ,layer7_input .shape
95
- print "w7 shape is: " ,self .variables ['w7' ].shape
145
+ print ( "layer 7 shape is: " ,layer7_input .shape )
146
+ print ( "w7 shape is: " ,self .variables ['w7' ].shape )
96
147
97
148
self .logits = tf .matmul (layer7_input , self .variables ['w7' ]) + self .variables ['b7' ]
98
149
99
150
if not self .args .is_training :
100
151
return
101
- #self.cost_func = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.logits, labels = self.Y))
102
- self .loss = tf .sqrt (tf .reduce_mean (tf .square (self .Y - self .logits )))
152
+ #self.loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.logits, labels = self.Y))
153
+ self .loss = tf .reduce_mean (tf .nn .softmax_cross_entropy_with_logits (logits = self .logits , labels = self .Y ))
154
+ #self.loss = tf.sqrt(tf.reduce_mean(tf.square(self.Y - self.logits)))
103
155
#self.lr = tf.maximum(1e-5,tf.train.exponential_decay(self.args.alpha, self.global_step, self.args.decay_step, self.args.decay_rate, staircase=True))
104
156
self .lr = self .args .alpha
105
157
self .optimizer = tf .train .AdamOptimizer (self .lr ).minimize (self .loss )
0 commit comments