forked from dandxy89/ImageModels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaffeNet.py
167 lines (130 loc) · 5.17 KB
/
CaffeNet.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
Model Name:
AlexNet - using the Functional Keras API
Replicated from the Caffe Zoo Model Version.
Paper:
ImageNet classification with deep convolutional neural networks by Krizhevsky et al. in NIPS 2012
Alternative Example:
Available at: http://caffe.berkeleyvision.org/model_zoo.html
https://github.com/uoguelph-mlrg/theano_alexnet/tree/master/pretrained/alexnet
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 import regularizers
from keras.utils.visualize_util import plot
from KerasLayers.Custom_layers import LRN2D
# global constants
NB_CLASS = 1000 # number of classes
LEARNING_RATE = 0.01
MOMENTUM = 0.9
ALPHA = 0.0001
BETA = 0.75
GAMMA = 0.1
DROPOUT = 0.5
WEIGHT_DECAY = 0.0005
LRN2D_norm = True # whether to use batch normalization
# Theano - 'th' (channels, width, height)
# Tensorflow - 'tf' (width, height, channels)
DIM_ORDERING = 'th'
def conv2D_lrn2d(x, nb_filter, nb_row, nb_col,
border_mode='same', subsample=(1, 1),
activation='relu', LRN2D_norm=False,
weight_decay=WEIGHT_DECAY, dim_ordering=DIM_ORDERING):
'''
Info:
Function taken from the Inceptionv3.py script keras github
Utility function to apply to a tensor a module Convolution + lrn2d
with optional weight decay (L2 weight regularization).
'''
if weight_decay:
W_regularizer = regularizers.l2(weight_decay)
b_regularizer = regularizers.l2(weight_decay)
else:
W_regularizer = None
b_regularizer = None
x = Convolution2D(nb_filter, nb_row, nb_col,
subsample=subsample,
activation=activation,
border_mode=border_mode,
W_regularizer=W_regularizer,
b_regularizer=b_regularizer,
bias=False,
dim_ordering=dim_ordering)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
if LRN2D_norm:
x = LRN2D(alpha=ALPHA, beta=BETA)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
return x
def create_model():
# 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))
# Channel 1 - Convolution Net Input Layer
x = conv2D_lrn2d(
img_input, 3, 11, 11, subsample=(
1, 1), border_mode='same')
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
# Channel 1 - Convolution Net Layer 1
x = conv2D_lrn2d(x, 96, 55, 55, subsample=(1, 1), border_mode='same')
x = MaxPooling2D(
strides=(
2, 2), pool_size=(
2, 2), dim_ordering=DIM_ORDERING)(x)
x = LRN2D(alpha=ALPHA, beta=BETA)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
# Channel 1 - Convolution Net Layer 2
x = conv2D_lrn2d(x, 192, 27, 27, subsample=(1, 1), border_mode='same')
x = MaxPooling2D(
strides=(
2, 2), pool_size=(
2, 2), dim_ordering=DIM_ORDERING)(x)
x = LRN2D(alpha=ALPHA, beta=BETA)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
# Channel 1 - Convolution Net Layer 3
x = conv2D_lrn2d(x, 288, 13, 13, subsample=(1, 1), border_mode='same')
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
# Channel 1 - Convolution Net Layer 4
x = conv2D_lrn2d(x, 288, 13, 13, subsample=(1, 1), border_mode='same')
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
# Channel 1 - Convolution Net Layer 5
x = conv2D_lrn2d(x, 256, 13, 13, subsample=(1, 1), border_mode='same')
x = MaxPooling2D(
strides=(
2, 2), pool_size=(
2, 2), dim_ordering=DIM_ORDERING)(x)
x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x)
# Channel 1 - Cov Net Layer 7
x = Flatten()(x)
x = Dense(4096, activation='relu')(x)
x = Dropout(DROPOUT)(x)
# Channel 1 - Cov Net Layer 8
x = Dense(4096, activation='relu')(x)
x = Dropout(DROPOUT)(x)
# Final Channel - Cov Net 9
x = Dense(output_dim=NB_CLASS,
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/CaffeNet.png')
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy')
print('Model Compiled')