This repository has been archived by the owner on Oct 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
resnet_model.py
73 lines (52 loc) · 2.27 KB
/
resnet_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
from keras.layers.merge import add,concatenate
from keras.layers.convolutional import Conv2D,MaxPooling2D,ZeroPadding2D,AveragePooling2D,Conv1D,MaxPooling1D
from keras.layers.core import Dense,Activation,Flatten,Dropout,Masking
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras.layers import Input,TimeDistributed
from keras_contrib.layers.normalization import GroupNormalization
def first_block(tensor_input,filters,kernel_size=3,pooling_size=1,dropout=0.5):
k1,k2 = filters
out = Conv1D(k1,1,padding='same')(tensor_input)
out = GroupNormalization()(out)
out = Activation('relu')(out)
out = Dropout(dropout)(out)
out = Conv1D(k2,kernel_size,strides=2,padding='same')(out)
pooling = MaxPooling1D(pooling_size,strides=2,padding='same')(tensor_input)
# out = merge([out,pooling],mode='sum')
out = add([out,pooling])
return out
def repeated_block(x,filters,kernel_size=3,pooling_size=1,dropout=0.5):
k1,k2 = filters
out = GroupNormalization()(x)
out = Activation('relu')(out)
out = Conv1D(k1,kernel_size,padding='same')(out)
out = GroupNormalization()(out)
out = Activation('relu')(out)
out = Dropout(dropout)(out)
out = Conv1D(k2,kernel_size,strides=2,padding='same')(out)
pooling = MaxPooling1D(pooling_size,strides=2,padding='same')(x)
out = add([out, pooling])
#out = merge([out,pooling])
return out
def build_multi_input_main_residual_network(batch_size,
time_length,
input_dim,
output_dim,
loop_depth=15,
dropout=0.5):
inp = Input(shape=(time_length,input_dim),name='a2')
out = Conv1D(128,5)(inp)
out = GroupNormalization()(out)
out = Activation('relu')(out)
out = first_block(out,(64,128),dropout=dropout)
for _ in range(loop_depth):
out = repeated_block(out,(64,128),dropout=dropout)
# add flatten
out = Flatten()(out)
out = GroupNormalization()(out)
out = Activation('relu')(out)
out = Dense(output_dim)(out)
model = Model(inputs=[inp],outputs=[out])
model.compile(loss='logcosh',optimizer='adam',metrics=['mse','mae'])
return model