77
88from __future__ import division , print_function , absolute_import
99
10- < << << << HEAD
1110from tensorgraph .layers import Conv2D , RELU , MaxPooling , LRN , Tanh , Dropout , \
1211 Softmax , Flatten , Linear , AvgPooling , \
1312 Lambda , BatchNormalization , IdentityBlock , \
1716import tensorflow as tf
1817from tensorgraph .cost import entropy , accuracy , mse
1918from tensorgraph .dataset import Mnist , Cifar10
20- == == == =
21- from tensorgraphx .layers import Conv2D , RELU , MaxPooling , LRN , Tanh , Dropout , \
22- Softmax , Flatten , Linear , AvgPooling , \
23- Lambda , BatchNormalization , IdentityBlock , \
24- TransitionLayer , DenseNet
25- from tensorgraphx .utils import same , valid , same_nd , valid_nd
26- import tensorgraphx as tg
27- import tensorflow as tf
28- from tensorgraphx .cost import entropy , accuracy , mse
29- from tensorgraphx .dataset import Mnist , Cifar10
30- > >> >> >> e55a706e1467da7b7c54b6d04055aba847f5a2b5
3119from tensorflow .python .framework import ops
3220import numpy as np
3321
3422
3523def model (nclass , h , w , c ):
3624 with tf .name_scope ('Cifar10AllCNN' ):
3725 seq = tg .Sequential ()
38- < << << << HEAD
3926 seq .add (Conv2D (num_filters = 96 , kernel_size = (3 , 3 ), stride = (1 , 1 ), padding = 'SAME' ))
4027 seq .add (RELU ())
4128 seq .add (BatchNormalization ())
@@ -73,54 +60,6 @@ def model(nclass, h, w, c):
7360 seq .add (BatchNormalization ())
7461
7562 seq .add (AvgPooling (poolsize = (8 , 8 ), stride = (1 ,1 ), padding = 'VALID' ))
76- == == == =
77- seq .add (Conv2D (input_channels = c , num_filters = 96 , kernel_size = (3 , 3 ), stride = (1 , 1 ), padding = 'SAME' ))
78- seq .add (RELU ())
79- h , w = same (in_height = h , in_width = w , stride = (1 ,1 ), kernel_size = (3 ,3 ))
80- seq .add (BatchNormalization (input_shape = [h ,w ,96 ]))
81-
82- seq .add (Conv2D (input_channels = 96 , num_filters = 96 , kernel_size = (3 , 3 ), stride = (1 , 1 ), padding = 'SAME' ))
83- seq .add (RELU ())
84- h , w = same (in_height = h , in_width = w , stride = (1 ,1 ), kernel_size = (3 ,3 ))
85- seq .add (Dropout (0.5 ))
86-
87- seq .add (Conv2D (input_channels = 96 , num_filters = 96 , kernel_size = (3 , 3 ), stride = (2 , 2 ), padding = 'SAME' ))
88- seq .add (RELU ())
89- h , w = same (in_height = h , in_width = w , stride = (2 ,2 ), kernel_size = (3 ,3 ))
90- seq .add (BatchNormalization (input_shape = [h ,w ,96 ]))
91-
92- seq .add (Conv2D (input_channels = 96 , num_filters = 192 , kernel_size = (3 , 3 ), stride = (1 , 1 ), padding = 'SAME' ))
93- seq .add (RELU ())
94- h , w = same (in_height = h , in_width = w , stride = (1 ,1 ), kernel_size = (3 ,3 ))
95- seq .add (Dropout (0.5 ))
96-
97- seq .add (Conv2D (input_channels = 192 , num_filters = 192 , kernel_size = (3 , 3 ), stride = (1 , 1 ), padding = 'SAME' ))
98- seq .add (RELU ())
99- h , w = same (in_height = h , in_width = w , stride = (1 ,1 ), kernel_size = (3 ,3 ))
100- seq .add (BatchNormalization (input_shape = [h ,w ,192 ]))
101-
102- seq .add (Conv2D (input_channels = 192 , num_filters = 192 , kernel_size = (3 , 3 ), stride = (2 , 2 ), padding = 'SAME' ))
103- seq .add (RELU ())
104- h , w = same (in_height = h , in_width = w , stride = (2 ,2 ), kernel_size = (3 ,3 ))
105- seq .add (Dropout (0.5 ))
106-
107- seq .add (Conv2D (input_channels = 192 , num_filters = 192 , kernel_size = (3 , 3 ), stride = (1 , 1 ), padding = 'SAME' ))
108- seq .add (RELU ())
109- h , w = same (in_height = h , in_width = w , stride = (1 ,1 ), kernel_size = (3 ,3 ))
110- seq .add (BatchNormalization (input_shape = [h ,w ,192 ]))
111-
112- seq .add (Conv2D (input_channels = 192 , num_filters = 192 , kernel_size = (1 , 1 ), stride = (1 , 1 ), padding = 'SAME' ))
113- seq .add (RELU ())
114- h , w = same (in_height = h , in_width = w , stride = (1 ,1 ), kernel_size = (1 ,1 ))
115- seq .add (Dropout (0.5 ))
116-
117- seq .add (Conv2D (input_channels = 192 , num_filters = nclass , kernel_size = (1 , 1 ), stride = (1 , 1 ), padding = 'SAME' ))
118- seq .add (RELU ())
119- h , w = same (in_height = h , in_width = w , stride = (1 ,1 ), kernel_size = (1 ,1 ))
120- seq .add (BatchNormalization (input_shape = [h ,w ,nclass ]))
121-
122- seq .add (AvgPooling (poolsize = (h , w ), stride = (1 ,1 ), padding = 'VALID' ))
123- >> >> >> > e55a706e1467da7b7c54b6d04055aba847f5a2b5
12463 seq .add (Flatten ())
12564 seq .add (Softmax ())
12665 return seq
@@ -210,11 +149,7 @@ def train():
210149
211150
212151def train_with_trainobject ():
213- < << << << HEAD
214152 from tensorgraph .trainobject import train as mytrain
215- == == == =
216- from tensorgraphx .trainobject import train as mytrain
217- >> >> >> > e55a706e1467da7b7c54b6d04055aba847f5a2b5
218153 config = tf .ConfigProto ()
219154 config .gpu_options .allow_growth = True
220155 with tf .Session (config = config ) as sess :
@@ -243,11 +178,7 @@ def train_with_trainobject():
243178
244179
245180def train_with_VGG ():
246- < << << << HEAD
247181 from tensorgraph .trainobject import train as mytrain
248- == == == =
249- from tensorgraphx .trainobject import train as mytrain
250- >> >> >> > e55a706e1467da7b7c54b6d04055aba847f5a2b5
251182 config = tf .ConfigProto ()
252183 config .gpu_options .allow_growth = True
253184 with tf .Session (config = config ) as sess :
@@ -256,21 +187,12 @@ def train_with_VGG():
256187 _ , nclass = y_train .shape
257188 print ('X max' , np .max (X_train ))
258189 print ('X min' , np .min (X_train ))
259- < << << << HEAD
260190 from tensorgraph .layers import VGG19
261191 seq = tg .Sequential ()
262192 layer = VGG19 ()
263193 seq .add (layer )
264194 seq .add (Flatten ())
265195 seq .add (Linear (nclass ))
266- == == == =
267- from tensorgraphx .layers import VGG19
268- seq = tg .Sequential ()
269- layer = VGG19 (input_channels = c , input_shape = (h ,w ))
270- seq .add (layer )
271- seq .add (Flatten ())
272- seq .add (Linear (512 ,nclass ))
273- >> >> >> > e55a706e1467da7b7c54b6d04055aba847f5a2b5
274196 seq .add (Softmax ())
275197 X_ph = tf .placeholder ('float32' , [None , h , w , c ])
276198 y_ph = tf .placeholder ('float32' , [None , nclass ])
@@ -292,11 +214,7 @@ def train_with_VGG():
292214
293215
294216def train_with_Resnet ():
295- < << << << HEAD
296217 from tensorgraph .trainobject import train as mytrain
297- == == == =
298- from tensorgraphx .trainobject import train as mytrain
299- >> >> >> > e55a706e1467da7b7c54b6d04055aba847f5a2b5
300218 config = tf .ConfigProto ()
301219 config .gpu_options .allow_growth = True
302220 with tf .Session (config = config ) as sess :
@@ -306,28 +224,12 @@ def train_with_Resnet():
306224 print ('X max' , np .max (X_train ))
307225 print ('X min' , np .min (X_train ))
308226 seq = tg .Sequential ()
309- < << << << HEAD
310227 seq .add (IdentityBlock (nlayers = 4 , filters = [32 , 64 ]))
311228 seq .add (TransitionLayer (16 ))
312229 seq .add (IdentityBlock (nlayers = 4 , filters = [64 , 128 ]))
313230 seq .add (TransitionLayer (16 ))
314231 seq .add (Flatten ())
315232 seq .add (Linear (nclass ))
316- == == == =
317- id1 = IdentityBlock (input_channels = c , input_shape = (h ,w ), nlayers = 4 , filters = [32 , 64 ])
318- seq .add (id1 )
319- trans1 = TransitionLayer (input_channels = id1 .output_channels , input_shape = id1 .output_shape )
320- seq .add (trans1 )
321-
322- id2 = IdentityBlock (input_channels = trans1 .output_channels , input_shape = trans1 .output_shape ,
323- nlayers = 4 , filters = [64 , 128 ])
324- seq .add (id2 )
325- trans2 = TransitionLayer (input_channels = id2 .output_channels , input_shape = id2 .output_shape )
326- seq .add (trans2 )
327- seq .add (Flatten ())
328- ldim = trans2 .output_channels * np .prod (trans2 .output_shape )
329- seq .add (Linear (ldim ,nclass ))
330- >> >> >> > e55a706e1467da7b7c54b6d04055aba847f5a2b5
331233 seq .add (Softmax ())
332234
333235 X_ph = tf .placeholder ('float32' , [None , h , w , c ])
@@ -350,11 +252,7 @@ def train_with_Resnet():
350252
351253
352254def train_with_Densenet ():
353- < << << << HEAD
354255 from tensorgraph .trainobject import train as mytrain
355- == == == =
356- from tensorgraphx .trainobject import train as mytrain
357- >> >> >> > e55a706e1467da7b7c54b6d04055aba847f5a2b5
358256 config = tf .ConfigProto ()
359257 config .gpu_options .allow_growth = True
360258 with tf .Session (config = config ) as sess :
@@ -364,17 +262,9 @@ def train_with_Densenet():
364262 print ('X max' , np .max (X_train ))
365263 print ('X min' , np .min (X_train ))
366264 seq = tg .Sequential ()
367- < << << << HEAD
368265 seq .add (DenseNet (ndense = 3 , growth_rate = 4 , nlayer1blk = 4 ))
369266 seq .add (Flatten ())
370267 seq .add (Linear (nclass ))
371- == == == =
372- dense = DenseNet (input_channels = c , input_shape = (h ,w ), ndense = 3 , growth_rate = 4 , nlayer1blk = 4 )
373- seq .add (dense )
374- seq .add (Flatten ())
375- ldim = dense .output_channels
376- seq .add (Linear (ldim ,nclass ))
377- >> >> >> > e55a706e1467da7b7c54b6d04055aba847f5a2b5
378268 seq .add (Softmax ())
379269
380270 X_ph = tf .placeholder ('float32' , [None , h , w , c ])
0 commit comments