forked from dandxy89/ImageModels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVGG19.py
145 lines (121 loc) · 5.05 KB
/
VGG19.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
138
139
140
141
142
143
144
145
"""
Model Name:
VGG-19 - using the Functional Keras API
A model of the 19-layer network used by the VGG team in the ILSVRC-2014 competition.
Paper:
Very Deep Convolutional Networks for Large-Scale Image Recognition - K. Simonyan, A. Zisserman
arXiv:1409.1556
Alternative Example:
Available at (Caffe) : http://caffe.berkeleyvision.org/model_zoo.html
Original Dataset:
ILSVRC 2012
"""
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.layers import Flatten, Dense, Dropout
from keras.layers import Input
from keras.models import Model
from keras.utils.visualize_util import plot
def create_model():
# Global Constants
NB_CLASS = 1000 # number of classes
# 'th' (channels, width, height) or 'tf' (width, height, channels)
DIM_ORDERING = 'th'
# Define image input layer
if DIM_ORDERING == 'th':
INP_SHAPE = (3, 224, 224) # 3 - Number of RGB Colours
img_input = Input(shape=INP_SHAPE)
CONCAT_AXIS = 1
elif DIM_ORDERING == 'tf':
INP_SHAPE = (224, 224, 3) # 3 - Number of RGB Colours
img_input = Input(shape=INP_SHAPE)
CONCAT_AXIS = 3
else:
raise Exception('Invalid dim ordering: ' + str(DIM_ORDERING))
# Layer Cluster - 1
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(img_input)
x = Convolution2D(64, 3, 3,
activation='relu',
border_mode='same',
dim_ordering=DIM_ORDERING)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
x = Convolution2D(64, 3, 3,
activation='relu',
border_mode='same',
dim_ordering=DIM_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
# Layer Cluster - 2
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
x = Convolution2D(128, 3, 3,
activation='relu',
border_mode='same',
dim_ordering=DIM_ORDERING)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
x = Convolution2D(128, 3, 3,
activation='relu',
border_mode='same',
dim_ordering=DIM_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
# Layer Cluster - 3
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
x = Convolution2D(256, 3, 3,
activation='relu',
border_mode='same',
dim_ordering=DIM_ORDERING)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
x = Convolution2D(256, 3, 3,
activation='relu',
border_mode='same',
dim_ordering=DIM_ORDERING)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
x = Convolution2D(256, 3, 3,
activation='relu',
border_mode='same',
dim_ordering=DIM_ORDERING)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
x = Convolution2D(256, 3, 3,
activation='relu',
border_mode='same',
dim_ordering=DIM_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
# Layer Cluster - 4
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
x = Convolution2D(512, 3, 3,
activation='relu',
border_mode='same',
dim_ordering=DIM_ORDERING)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
x = Convolution2D(512, 3, 3,
activation='relu',
border_mode='same',
dim_ordering=DIM_ORDERING)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
x = Convolution2D(512, 3, 3,
activation='relu',
border_mode='same',
dim_ordering=DIM_ORDERING)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
x = Convolution2D(512, 3, 3,
activation='relu',
border_mode='same',
dim_ordering=DIM_ORDERING)(x)
x = MaxPooling2D((2, 2), strides=(2, 2))(x)
# Layer Cluster - 5 - Output Layer
x = Flatten()(x)
x = Dense(4096, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(4096, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(1000, activation='softmax')(x)
return x, img_input, CONCAT_AXIS, INP_SHAPE, DIM_ORDERING
def check_print():
# Create the Model
x, img_input, CONCAT_AXIS, INP_SHAPE, DIM_ORDERING = create_model()
# Create a Keras Model - Functional API
model = Model(input=img_input,
output=[x])
model.summary()
# Save a PNG of the Model Build
plot(model, to_file='./Model/VGG19.png')
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy')
print('Model Compiled')