-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_model.py
151 lines (116 loc) · 5.25 KB
/
build_model.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
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 9 17:17:56 2017
@author: mducoffe
"""
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Reshape, Activation
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D
def build_model_AlexNet(img_size, nb_classes):
nb_pool = 2
model = Sequential()
nb_channel, img_rows, img_cols = img_size
#layer 1
model.add(Conv2D(96, (11, 11), padding='same', input_shape = (nb_channel, img_rows, img_cols), data_format='channels_first'))
model.add(Activation('relu'))
model.add(MaxPooling2D((nb_pool,nb_pool), strides=(2,2)))
model.add(Dropout(0.25))
#layer 2
model.add(Conv2D(256, (5, 5), padding='same', data_format='channels_first'))
model.add(Activation('relu'))
model.add(MaxPooling2D((nb_pool,nb_pool), strides=(2,2)))
model.add(Dropout(0.25))
#layer 3
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(512, (3, 3), padding='same', data_format='channels_first'))
model.add(Activation('relu'))
#layer 4
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(1024, (3, 3), padding='same', data_format='channels_first'))
model.add(Activation('relu'))
#layer 5
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(1024, (3, 3), padding='same', data_format='channels_first'))
model.add(Activation('relu'))
model.add(MaxPooling2D((nb_pool,nb_pool), strides=(2,2)))
model.add(Dropout(0.25))
#layer 6
model.add(Flatten())
model.add(Dense(3072, kernel_initializer="glorot_normal"))
model.add(Activation('relu'))
model.add(Dropout(0.5))
#layer 7
model.add(Dense(4096, kernel_initializer="glorot_normal"))
model.add(Activation('relu'))
model.add(Dropout(0.5))
#layer 8
model.add(Dense(nb_classes, kernel_initializer="glorot_normal"))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["acc"])
return model;
def build_model_VGG8(img_size, nb_classes):
nb_conv = 3
nb_pool = 2
nb_channel, img_rows, img_cols = img_size
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(nb_channel,img_rows,img_cols)))
model.add(Conv2D(64, (nb_conv, nb_conv), activation='relu', data_format='channels_first'))
model.add(MaxPooling2D((nb_pool,nb_pool), strides=(2,2)))
model.add(Dropout(0.25))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(128, (nb_conv, nb_conv), activation='relu', data_format='channels_first'))
model.add(MaxPooling2D((nb_pool,nb_pool), strides=(2,2)))
model.add(Dropout(0.25))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(256, (nb_conv, nb_conv), activation='relu', data_format='channels_first'))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(256, (nb_conv, nb_conv), activation='relu', data_format='channels_first'))
model.add(MaxPooling2D((nb_pool,nb_pool), strides=(2,2)))
model.add(Dropout(0.25))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(512, (nb_conv, nb_conv), activation='relu', data_format='channels_first'))
model.add(ZeroPadding2D((1,1)))
model.add(Conv2D(512, (nb_conv, nb_conv), activation='relu', data_format='channels_first'))
model.add(MaxPooling2D((nb_pool,nb_pool), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["acc"])
return model
def build_model_LeNet5(img_size, nb_classes):
nb_pool = 2
nb_channel, img_rows, img_cols = img_size
model = Sequential()
model.add(Conv2D(6, (5, 5), padding='valid', input_shape = (nb_channel, img_rows, img_cols), data_format='channels_first'))
model.add(Activation('relu'))
model.add(MaxPooling2D((nb_pool,nb_pool), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(16, (5, 5), padding='valid', data_format='channels_first'))
model.add(Activation('relu'))
model.add(MaxPooling2D((nb_pool,nb_pool), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(120, (1, 1), padding='valid', data_format='channels_first'))
model.add(Flatten())
model.add(Dense(84, activation='relu'))
model.add(Dense(nb_classes))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["acc"])
return model
def build_model_func(network_archi, img_size=(1,28,28, 10)):
network_archi = network_archi.lower()
num_classes = img_size[3]
img_size = (img_size[0], img_size[1], img_size[2])
model = None
assert (network_archi in ['vgg8', 'lenet5', 'alexnet']), ('unknown architecture', network_archi)
if network_archi == 'vgg8':
model = build_model_VGG8(img_size, nb_classes=num_classes)
if network_archi == 'lenet5':
model = build_model_LeNet5(img_size,nb_classes=num_classes)
if network_archi == 'alexnet':
model = build_model_AlexNet(img_size, nb_classes=num_classes)
return model