forked from vlimant/mpi_learn
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathModels.py
233 lines (199 loc) · 7.01 KB
/
Models.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
### Predefined Keras models
#import setGPU
import numpy as np
try:
from keras.models import Sequential, Model
from keras.layers import Dense, Activation, Dropout, Flatten, Input, Permute
from keras.layers import Convolution2D, MaxPooling2D, Conv2D
import keras.backend as K
except:
print ("no keras support")
try:
import torch
import torch.nn as nn
import torch.nn.functional as F
except:
print ("no torch support")
def make_model(model_name):
"""Constructs the Keras model indicated by model_name"""
model_maker_dict = {
'example':make_example_model,
'mnist':make_mnist_model,
'cifar10':make_cifar10_model,
'mnist_torch':make_mnist_torch_model,
'topclass': make_topclass_model,
'topclass_torch':make_topclass_torch_model
}
return model_maker_dict[model_name]()
def make_example_model():
"""Example model from keras documentation"""
model = Sequential()
model.add(Dense(output_dim=64, input_dim=100))
model.add(Activation("relu"))
model.add(Dense(output_dim=10))
model.add(Activation("softmax"))
return model
def make_topclass_model(**args):
conv_layers=2
dense_layers=2
dropout=0.5
classes=3
in_channels=5
in_ch = in_channels
## the trace in the input file is 750, 150, 94, 5
input = Input( (150,94,in_ch))
## convs
c = input
for i in range(conv_layers):
channel_in = in_ch*((i+1)%5)
channel_out = in_ch*((i+2)%5)
if channel_in == 0: channel_in += 1
if channel_out == 0: channel_out += 1
c = Conv2D( filters=channel_out, kernel_size=(3,3) , strides=1, padding="same", activation = 'relu') (c)
c = Conv2D(1, (3,3), activation = 'relu',strides=2, padding="same")(c)
## pooling
m = MaxPooling2D((10,10))(c)
f = Flatten()(m)
d = f
for i in range(dense_layers):
N = int(10000//(2**(i+1)))
print (N)
d = Dense( N, activation='relu')(d)
if dropout:
d = Dropout(dropout)(d)
o = Dense(classes, activation='softmax')(d)
model = Model(inputs=input, outputs=o)
model.summary()
return model
def make_cifar10_model(**args):
nb_classes = 10
img_rows, img_cols = 32, 32
# use 1 kernel size for all convolutional layers
ks = args.get('kernel_size', 3)
# tune the number of filters for each convolution layer
nb_filters1 = args.get('nb_filters1', 48)
nb_filters2 = args.get('nb_filters2', 96)
nb_filters3 = args.get('nb_filters3', 192)
# tune the pool size once
ps = args.get('pool_size', 2)
pool_size = (ps,ps)
# tune the dropout rates independently
#do1 = args.get('dropout1', 0.25)
#do2 = args.get('dropout2', 0.25)
#do3 = args.get('dropout3', 0.25)
do4 = args.get('dropout1', 0.25)
do5 = args.get('dropout2', 0.5)
#do1 = args.get('dropout1', 0.)
#do2 = args.get('dropout2', 0.)
#do3 = args.get('dropout3', 0.)
#do4 = args.get('dropout4', 0.)
#do5 = args.get('dropout5', 0.)
# tune the dense layers independently
dense1 = args.get('dense1', 512)
dense2 = args.get('dense2', 256)
if K.image_dim_ordering() == 'th':
input_shape = (3, img_rows, img_cols)
else:
input_shape = (img_rows, img_cols, 3)
#act = 'sigmoid'
act = 'relu'
i = Input( input_shape)
l = Conv2D(nb_filters1,( ks, ks), padding='same', activation = act)(i)
#l = Conv2D(nb_filters1, (ks, ks), activation=act)(l)
l = MaxPooling2D(pool_size=pool_size)(l)
#l = Dropout(do1)(l)
l = Conv2D(nb_filters2, (ks, ks), padding='same',activation=act)(l)
#l = Conv2D(nb_filters2, (ks, ks))(l)
l = MaxPooling2D(pool_size=pool_size)(l)
#l = Dropout(do2)(l)
l = Conv2D(nb_filters3, (ks, ks), padding='same',activation=act)(l)
#l = Conv2D(nb_filters3, (ks, ks))(l)
l = MaxPooling2D(pool_size=pool_size)(l)
#l = Dropout(do3)(l)
l = Flatten()(l)
l = Dense(dense1,activation=act)(l)
l = Dropout(do4)(l)
l = Dense(dense2,activation=act)(l)
l =Dropout(do5)(l)
o = Dense(nb_classes, activation='softmax')(l)
model = Model(inputs=i, outputs=o)
model.summary()
return model
model = Sequential()
model.add(Convolution2D(nb_filters1, ks, ks,
border_mode='same',
input_shape=input_shape))
model.add(Activation(act))
model.add(Convolution2D(nb_filters1, ks, ks))
model.add(Activation(act))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(do1))
model.add(Convolution2D(nb_filters2, ks, ks, border_mode='same'))
model.add(Activation(act))
model.add(Convolution2D(nb_filters2, ks, ks))
model.add(Activation(act))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(do2))
model.add(Convolution2D(nb_filters3, ks, ks, border_mode='same'))
model.add(Activation(act))
model.add(Convolution2D(nb_filters3, ks, ks))
model.add(Activation(act))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(do3))
model.add(Flatten())
model.add(Dense(dense1))
model.add(Activation(act))
model.add(Dropout(do4))
model.add(Dense(dense2))
model.add(Activation(act))
model.add(Dropout(do5))
model.add(Dense(nb_classes, activation='softmax'))
return model
def make_mnist_model(**args):
"""MNIST ConvNet from keras/examples/mnist_cnn.py"""
np.random.seed(1337) # for reproducibility
nb_classes = 10
# input image dimensions
img_rows, img_cols = 28, 28
# number of convolutional filters to use
nb_filters = args.get('nb_filters',32)
# size of pooling area for max pooling
ps = args.get('pool_size',2)
# convolution kernel size
ks = args.get('kernel_size',3)
do = args.get('dropout', 0.25)
dense = args.get('dense', 128)
pool_size = (ps,ps)
if K.image_dim_ordering() == 'th':
input_shape = (1, img_rows, img_cols)
else:
input_shape = (img_rows, img_cols, 1)
model = Sequential()
model.add(Convolution2D(nb_filters, ks, ks,
border_mode='valid',
input_shape=input_shape))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, ks, ks))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(do))
model.add(Flatten())
model.add(Dense(dense))
model.add(Activation('relu'))
model.add(Dropout(do))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
return model
def make_mnist_torch_model(**args):
from PytorchCNN import MNistNet
model = MNistNet()
return model
def make_topclass_torch_model(**args):
conv_layers=2
dense_layers=2
dropout=0.5
classes=3
in_channels=5
from PytorchCNN import CNN
model = CNN(conv_layers=conv_layers, dense_layers=dense_layers, dropout=dropout, classes=classes, in_channels=in_channels)
return model