Skip to content

Commit 46197cd

Browse files
author
Divam Gupta
committed
All models working
1 parent 82b88d7 commit 46197cd

File tree

8 files changed

+230
-178
lines changed

8 files changed

+230
-178
lines changed

Models/FCN32.py

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,48 @@
1111
import os
1212
file_path = os.path.dirname( os.path.abspath(__file__) )
1313

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"
1515

16+
IMAGE_ORDERING = 'channels_first'
1617

17-
# for input(360,480) output will be ( 170 , 240)
1818

19-
# input_image_size -> ( height , width )
20-
21-
22-
def FCN32( nClasses , input_height=416, input_width=608 , vgg_level=3):
19+
def FCN32( n_classes , input_height=416, input_width=608 , vgg_level=3):
2320

2421
assert input_height%32 == 0
2522
assert input_width%32 == 0
2623

2724
# https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels.h5
28-
n_classes = 3
2925
img_input = Input(shape=(3,input_height,input_width))
3026

31-
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1', data_format='channels_first' )(img_input)
32-
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2', data_format='channels_first' )(x)
33-
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool', data_format='channels_first' )(x)
27+
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1', data_format=IMAGE_ORDERING )(img_input)
28+
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2', data_format=IMAGE_ORDERING )(x)
29+
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool', data_format=IMAGE_ORDERING )(x)
3430
f1 = x
3531
# Block 2
36-
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1', data_format='channels_first' )(x)
37-
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2', data_format='channels_first' )(x)
38-
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool', data_format='channels_first' )(x)
32+
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1', data_format=IMAGE_ORDERING )(x)
33+
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2', data_format=IMAGE_ORDERING )(x)
34+
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool', data_format=IMAGE_ORDERING )(x)
3935
f2 = x
4036

4137
# Block 3
42-
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1', data_format='channels_first' )(x)
43-
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2', data_format='channels_first' )(x)
44-
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3', data_format='channels_first' )(x)
45-
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool', data_format='channels_first' )(x)
38+
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1', data_format=IMAGE_ORDERING )(x)
39+
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2', data_format=IMAGE_ORDERING )(x)
40+
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3', data_format=IMAGE_ORDERING )(x)
41+
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool', data_format=IMAGE_ORDERING )(x)
4642
f3 = x
4743

4844
# Block 4
49-
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1', data_format='channels_first' )(x)
50-
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2', data_format='channels_first' )(x)
51-
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3', data_format='channels_first' )(x)
52-
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool', data_format='channels_first' )(x)
45+
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1', data_format=IMAGE_ORDERING )(x)
46+
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2', data_format=IMAGE_ORDERING )(x)
47+
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3', data_format=IMAGE_ORDERING )(x)
48+
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool', data_format=IMAGE_ORDERING )(x)
5349
f4 = x
5450

5551
# Block 5
56-
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1', data_format='channels_first' )(x)
57-
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2', data_format='channels_first' )(x)
58-
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3', data_format='channels_first' )(x)
59-
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool', data_format='channels_first' )(x)
52+
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1', data_format=IMAGE_ORDERING )(x)
53+
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2', data_format=IMAGE_ORDERING )(x)
54+
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3', data_format=IMAGE_ORDERING )(x)
55+
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool', data_format=IMAGE_ORDERING )(x)
6056
f5 = x
6157

6258
x = Flatten(name='flatten')(x)
@@ -69,13 +65,13 @@ def FCN32( nClasses , input_height=416, input_width=608 , vgg_level=3):
6965

7066
o = f5
7167

72-
o = ( Conv2D( 4096 , ( 7 , 7 ) , activation='relu' , padding='same', data_format='channels_first'))(o)
68+
o = ( Conv2D( 4096 , ( 7 , 7 ) , activation='relu' , padding='same', data_format=IMAGE_ORDERING))(o)
7369
o = Dropout(0.5)(o)
74-
o = ( Conv2D( 4096 , ( 1 , 1 ) , activation='relu' , padding='same', data_format='channels_first'))(o)
70+
o = ( Conv2D( 4096 , ( 1 , 1 ) , activation='relu' , padding='same', data_format=IMAGE_ORDERING))(o)
7571
o = Dropout(0.5)(o)
7672

77-
o = ( Conv2D( nClasses , ( 1 , 1 ) ,kernel_initializer='he_normal' , data_format='channels_first'))(o)
78-
o = Conv2DTranspose( nClasses , kernel_size=(64,64) , strides=(32,32) , use_bias=False , data_format='channels_first' )(o)
73+
o = ( Conv2D( n_classes , ( 1 , 1 ) ,kernel_initializer='he_normal' , data_format=IMAGE_ORDERING))(o)
74+
o = Conv2DTranspose( n_classes , kernel_size=(64,64) , strides=(32,32) , use_bias=False , data_format=IMAGE_ORDERING )(o)
7975
o_shape = Model(img_input , o ).output_shape
8076

8177
outputHeight = o_shape[2]
@@ -84,8 +80,8 @@ def FCN32( nClasses , input_height=416, input_width=608 , vgg_level=3):
8480
print "koko" , o_shape
8581

8682
o = (Reshape(( -1 , outputHeight*outputWidth )))(o)
87-
# o = (Permute((2, 1)))(o)
88-
# o = (Activation('softmax'))(o)
83+
o = (Permute((2, 1)))(o)
84+
o = (Activation('softmax'))(o)
8985
model = Model( img_input , o )
9086
model.outputWidth = outputWidth
9187
model.outputHeight = outputHeight

Models/FCN8.py

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@
1111
import os
1212
file_path = os.path.dirname( os.path.abspath(__file__) )
1313

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"
1515

16-
17-
# for input(360,480) output will be ( 170 , 240)
18-
19-
# input_image_size -> ( height , width )
16+
IMAGE_ORDERING = 'channels_first'
2017

2118
# crop o1 wrt o2
2219
def crop( o1 , o2 , i ):
@@ -32,14 +29,14 @@ def crop( o1 , o2 , i ):
3229
cy = abs( outputHeight2 - outputHeight1 )
3330

3431
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)
3633
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)
3835

3936
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)
4138
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)
4340

4441
return o1 , o2
4542

@@ -49,38 +46,37 @@ def FCN8( nClasses , input_height=416, input_width=608 , vgg_level=3):
4946
# assert input_width%32 == 0
5047

5148
# https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels.h5
52-
n_classes = 3
5349
img_input = Input(shape=(3,input_height,input_width))
5450

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)
5854
f1 = x
5955
# 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)
6359
f2 = x
6460

6561
# 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)
7066
f3 = x
7167

7268
# 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)
7773
f4 = x
7874

7975
# 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)
8480
f5 = x
8581

8682
x = Flatten(name='flatten')(x)
@@ -93,29 +89,29 @@ def FCN8( nClasses , input_height=416, input_width=608 , vgg_level=3):
9389

9490
o = f5
9591

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)
9793
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)
9995
o = Dropout(0.5)(o)
10096

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)
10399

104100
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)
106102

107103
o , o2 = crop( o , o2 , img_input )
108104

109105
o = Add()([ o , o2 ])
110106

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)
112108
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)
114110
o2 , o = crop( o2 , o , img_input )
115111
o = Add()([ o2 , o ])
116112

117113

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)
119115

120116
o_shape = Model(img_input , o ).output_shape
121117

Models/VGGSegnet.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,11 @@
99

1010
import os
1111
file_path = os.path.dirname( os.path.abspath(__file__) )
12+
VGG_Weights_path = file_path+"/../data/vgg16_weights_th_dim_ordering_th_kernels.h5"
1213

13-
VGG_Weights_path = file_path+"/../../data/vgg16_weights_th_dim_ordering_th_kernels.h5"
1414

15+
def VGGSegnet( n_classes , input_height=416, input_width=608 , vgg_level=3):
1516

16-
# for input(360,480) output will be ( 170 , 240)
17-
18-
# input_image_size -> ( height , width )
19-
20-
21-
def VGGSegnet( nClasses , input_height=416, input_width=608 , vgg_level=3):
22-
23-
assert input_height%32 == 0
24-
assert input_width%32 == 0
25-
26-
# https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_th_dim_ordering_th_kernels.h5
27-
n_classes = 3
2817
img_input = Input(shape=(3,input_height,input_width))
2918

3019
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1', data_format='channels_first' )(img_input)
@@ -95,11 +84,20 @@ def VGGSegnet( nClasses , input_height=416, input_width=608 , vgg_level=3):
9584
outputHeight = o_shape[2]
9685
outputWidth = o_shape[3]
9786

98-
o = (Reshape(( n_classes , outputHeight*outputWidth )))(o)
87+
o = (Reshape(( -1 , outputHeight*outputWidth )))(o)
9988
o = (Permute((2, 1)))(o)
10089
o = (Activation('softmax'))(o)
10190
model = Model( img_input , o )
10291
model.outputWidth = outputWidth
10392
model.outputHeight = outputHeight
10493

10594
return model
95+
96+
97+
98+
99+
if __name__ == '__main__':
100+
m = VGGSegnet( 101 )
101+
from keras.utils import plot_model
102+
plot_model( m , show_shapes=True , to_file='model.png')
103+

0 commit comments

Comments
 (0)