11
11
import os
12
12
file_path = os .path .dirname ( os .path .abspath (__file__ ) )
13
13
14
- VGG_Weights_path = file_path + "/../../ data/vgg16_weights_th_dim_ordering_th_kernels.h5"
14
+ VGG_Weights_path = file_path + "/../data/vgg16_weights_th_dim_ordering_th_kernels.h5"
15
15
16
-
17
- # for input(360,480) output will be ( 170 , 240)
18
-
19
- # input_image_size -> ( height , width )
16
+ IMAGE_ORDERING = 'channels_first'
20
17
21
18
# crop o1 wrt o2
22
19
def crop ( o1 , o2 , i ):
@@ -32,14 +29,14 @@ def crop( o1 , o2 , i ):
32
29
cy = abs ( outputHeight2 - outputHeight1 )
33
30
34
31
if outputWidth1 > outputWidth2 :
35
- o1 = Cropping2D ( cropping = ((0 ,0 ) , ( 0 , cx )), data_format = 'channels_first' )(o1 )
32
+ o1 = Cropping2D ( cropping = ((0 ,0 ) , ( 0 , cx )), data_format = IMAGE_ORDERING )(o1 )
36
33
else :
37
- o2 = Cropping2D ( cropping = ((0 ,0 ) , ( 0 , cx )), data_format = 'channels_first' )(o2 )
34
+ o2 = Cropping2D ( cropping = ((0 ,0 ) , ( 0 , cx )), data_format = IMAGE_ORDERING )(o2 )
38
35
39
36
if outputHeight1 > outputHeight2 :
40
- o1 = Cropping2D ( cropping = ((0 ,cy ) , ( 0 , 0 )), data_format = 'channels_first' )(o1 )
37
+ o1 = Cropping2D ( cropping = ((0 ,cy ) , ( 0 , 0 )), data_format = IMAGE_ORDERING )(o1 )
41
38
else :
42
- o2 = Cropping2D ( cropping = ((0 , cy ) , ( 0 , 0 )), data_format = 'channels_first' )(o2 )
39
+ o2 = Cropping2D ( cropping = ((0 , cy ) , ( 0 , 0 )), data_format = IMAGE_ORDERING )(o2 )
43
40
44
41
return o1 , o2
45
42
@@ -49,38 +46,37 @@ def FCN8( nClasses , input_height=416, input_width=608 , vgg_level=3):
49
46
# assert input_width%32 == 0
50
47
51
48
# https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels.h5
52
- n_classes = 3
53
49
img_input = Input (shape = (3 ,input_height ,input_width ))
54
50
55
- x = Conv2D (64 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block1_conv1' , data_format = 'channels_first' )(img_input )
56
- x = Conv2D (64 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block1_conv2' , data_format = 'channels_first' )(x )
57
- x = MaxPooling2D ((2 , 2 ), strides = (2 , 2 ), name = 'block1_pool' , data_format = 'channels_first' )(x )
51
+ x = Conv2D (64 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block1_conv1' , data_format = IMAGE_ORDERING )(img_input )
52
+ x = Conv2D (64 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block1_conv2' , data_format = IMAGE_ORDERING )(x )
53
+ x = MaxPooling2D ((2 , 2 ), strides = (2 , 2 ), name = 'block1_pool' , data_format = IMAGE_ORDERING )(x )
58
54
f1 = x
59
55
# Block 2
60
- x = Conv2D (128 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block2_conv1' , data_format = 'channels_first' )(x )
61
- x = Conv2D (128 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block2_conv2' , data_format = 'channels_first' )(x )
62
- x = MaxPooling2D ((2 , 2 ), strides = (2 , 2 ), name = 'block2_pool' , data_format = 'channels_first' )(x )
56
+ x = Conv2D (128 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block2_conv1' , data_format = IMAGE_ORDERING )(x )
57
+ x = Conv2D (128 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block2_conv2' , data_format = IMAGE_ORDERING )(x )
58
+ x = MaxPooling2D ((2 , 2 ), strides = (2 , 2 ), name = 'block2_pool' , data_format = IMAGE_ORDERING )(x )
63
59
f2 = x
64
60
65
61
# Block 3
66
- x = Conv2D (256 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block3_conv1' , data_format = 'channels_first' )(x )
67
- x = Conv2D (256 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block3_conv2' , data_format = 'channels_first' )(x )
68
- x = Conv2D (256 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block3_conv3' , data_format = 'channels_first' )(x )
69
- x = MaxPooling2D ((2 , 2 ), strides = (2 , 2 ), name = 'block3_pool' , data_format = 'channels_first' )(x )
62
+ x = Conv2D (256 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block3_conv1' , data_format = IMAGE_ORDERING )(x )
63
+ x = Conv2D (256 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block3_conv2' , data_format = IMAGE_ORDERING )(x )
64
+ x = Conv2D (256 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block3_conv3' , data_format = IMAGE_ORDERING )(x )
65
+ x = MaxPooling2D ((2 , 2 ), strides = (2 , 2 ), name = 'block3_pool' , data_format = IMAGE_ORDERING )(x )
70
66
f3 = x
71
67
72
68
# Block 4
73
- x = Conv2D (512 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block4_conv1' , data_format = 'channels_first' )(x )
74
- x = Conv2D (512 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block4_conv2' , data_format = 'channels_first' )(x )
75
- x = Conv2D (512 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block4_conv3' , data_format = 'channels_first' )(x )
76
- x = MaxPooling2D ((2 , 2 ), strides = (2 , 2 ), name = 'block4_pool' , data_format = 'channels_first' )(x )
69
+ x = Conv2D (512 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block4_conv1' , data_format = IMAGE_ORDERING )(x )
70
+ x = Conv2D (512 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block4_conv2' , data_format = IMAGE_ORDERING )(x )
71
+ x = Conv2D (512 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block4_conv3' , data_format = IMAGE_ORDERING )(x )
72
+ x = MaxPooling2D ((2 , 2 ), strides = (2 , 2 ), name = 'block4_pool' , data_format = IMAGE_ORDERING )(x )
77
73
f4 = x
78
74
79
75
# Block 5
80
- x = Conv2D (512 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block5_conv1' , data_format = 'channels_first' )(x )
81
- x = Conv2D (512 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block5_conv2' , data_format = 'channels_first' )(x )
82
- x = Conv2D (512 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block5_conv3' , data_format = 'channels_first' )(x )
83
- x = MaxPooling2D ((2 , 2 ), strides = (2 , 2 ), name = 'block5_pool' , data_format = 'channels_first' )(x )
76
+ x = Conv2D (512 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block5_conv1' , data_format = IMAGE_ORDERING )(x )
77
+ x = Conv2D (512 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block5_conv2' , data_format = IMAGE_ORDERING )(x )
78
+ x = Conv2D (512 , (3 , 3 ), activation = 'relu' , padding = 'same' , name = 'block5_conv3' , data_format = IMAGE_ORDERING )(x )
79
+ x = MaxPooling2D ((2 , 2 ), strides = (2 , 2 ), name = 'block5_pool' , data_format = IMAGE_ORDERING )(x )
84
80
f5 = x
85
81
86
82
x = Flatten (name = 'flatten' )(x )
@@ -93,29 +89,29 @@ def FCN8( nClasses , input_height=416, input_width=608 , vgg_level=3):
93
89
94
90
o = f5
95
91
96
- o = ( Conv2D ( 4096 , ( 7 , 7 ) , activation = 'relu' , padding = 'same' , data_format = 'channels_first' ))(o )
92
+ o = ( Conv2D ( 4096 , ( 7 , 7 ) , activation = 'relu' , padding = 'same' , data_format = IMAGE_ORDERING ))(o )
97
93
o = Dropout (0.5 )(o )
98
- o = ( Conv2D ( 4096 , ( 1 , 1 ) , activation = 'relu' , padding = 'same' , data_format = 'channels_first' ))(o )
94
+ o = ( Conv2D ( 4096 , ( 1 , 1 ) , activation = 'relu' , padding = 'same' , data_format = IMAGE_ORDERING ))(o )
99
95
o = Dropout (0.5 )(o )
100
96
101
- o = ( Conv2D ( nClasses , ( 1 , 1 ) ,kernel_initializer = 'he_normal' , data_format = 'channels_first' ))(o )
102
- o = Conv2DTranspose ( nClasses , kernel_size = (4 ,4 ) , strides = (2 ,2 ) , use_bias = False , data_format = 'channels_first' )(o )
97
+ o = ( Conv2D ( nClasses , ( 1 , 1 ) ,kernel_initializer = 'he_normal' , data_format = IMAGE_ORDERING ))(o )
98
+ o = Conv2DTranspose ( nClasses , kernel_size = (4 ,4 ) , strides = (2 ,2 ) , use_bias = False , data_format = IMAGE_ORDERING )(o )
103
99
104
100
o2 = f4
105
- o2 = ( Conv2D ( nClasses , ( 1 , 1 ) ,kernel_initializer = 'he_normal' , data_format = 'channels_first' ))(o2 )
101
+ o2 = ( Conv2D ( nClasses , ( 1 , 1 ) ,kernel_initializer = 'he_normal' , data_format = IMAGE_ORDERING ))(o2 )
106
102
107
103
o , o2 = crop ( o , o2 , img_input )
108
104
109
105
o = Add ()([ o , o2 ])
110
106
111
- o = Conv2DTranspose ( nClasses , kernel_size = (4 ,4 ) , strides = (2 ,2 ) , use_bias = False , data_format = 'channels_first' )(o )
107
+ o = Conv2DTranspose ( nClasses , kernel_size = (4 ,4 ) , strides = (2 ,2 ) , use_bias = False , data_format = IMAGE_ORDERING )(o )
112
108
o2 = f3
113
- o2 = ( Conv2D ( nClasses , ( 1 , 1 ) ,kernel_initializer = 'he_normal' , data_format = 'channels_first' ))(o2 )
109
+ o2 = ( Conv2D ( nClasses , ( 1 , 1 ) ,kernel_initializer = 'he_normal' , data_format = IMAGE_ORDERING ))(o2 )
114
110
o2 , o = crop ( o2 , o , img_input )
115
111
o = Add ()([ o2 , o ])
116
112
117
113
118
- o = Conv2DTranspose ( nClasses , kernel_size = (16 ,16 ) , strides = (8 ,8 ) , use_bias = False , data_format = 'channels_first' )(o )
114
+ o = Conv2DTranspose ( nClasses , kernel_size = (16 ,16 ) , strides = (8 ,8 ) , use_bias = False , data_format = IMAGE_ORDERING )(o )
119
115
120
116
o_shape = Model (img_input , o ).output_shape
121
117
0 commit comments