-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathModel.py
234 lines (183 loc) · 10.2 KB
/
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
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
234
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras import layers,models, regularizers
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout, BatchNormalization, Activation, GlobalAveragePooling2D, Conv3D, MaxPooling3D, GlobalAveragePooling3D, Reshape, Lambda
import time
import os
class BasicBlock(layers.Layer):
def __init__(self,filter_num,name,stride=1, **kwargs):
super(BasicBlock, self).__init__( **kwargs)
self.filter_num = filter_num
self.stride = stride
self.layers = []
self.conv1=layers.Conv2D(filter_num,3,strides=stride,padding='same', name = name+'_1')
# self.bn1=layers.BatchNormalization()
self.relu=layers.Activation('relu')
self.conv2=layers.Conv2D(filter_num,3,strides=1,padding='same', name = name+'_2')
# self.bn2 = layers.BatchNormalization()
self.layers.append(self.conv1)
self.layers.append(self.conv2)
# self.layers.append(self.bn1)
# self.layers.append(self.bn2)
if stride!=1:
self.downsample=models.Sequential()
self.downsample.add(layers.Conv2D(filter_num,1,strides=stride))
self.layers.append(self.downsample)
else:
self.downsample=lambda x:x
def get_layer(self, index):
return self.layers[index]
def get_layers(self):
return self.layers
def call(self,input,training=None):
out=self.conv1(input)
# out=self.bn1(out)
out=self.relu(out)
out=self.conv2(out)
# out=self.bn2(out)
identity=self.downsample(input)
output=layers.add([out,identity])
output=tf.nn.relu(output)
return output
def get_config(self):
config = {
'filter_num':
self.filter_num,
'stride':
self.stride
}
base_config = super(BasicBlock, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class Model:
def __init__(self, input_shape, act_dim):
self.act_dim = act_dim
self.input_shape = input_shape
self._build_model()
self.act_loss = []
self.move_loss = []
def load_model(self):
# self.shared_model = load_model("./model/shared_model.h5", custom_objects={'BasicBlock': BasicBlock})
if os.path.exists("./model/act_part.h5"):
print("load action model")
self.act_model = models.Sequential()
self.private_act_model = load_model("./model/act_part.h5", custom_objects={'BasicBlock': BasicBlock})
# self.act_model.add(self.shared_model)
self.act_model.add(self.private_act_model)
if os.path.exists("./model/move_part.h5"):
print("load move model")
self.move_model = models.Sequential()
self.private_move_model = load_model("./model/move_part.h5", custom_objects={'BasicBlock': BasicBlock})
# self.move_model.add(self.shared_model)
self.move_model.add(self.private_move_model)
def save_mode(self):
print("save model")
self.private_act_model.save("./model/act_part.h5")
self.private_move_model.save("./model/move_part.h5")
def build_resblock(self,filter_num,blocks,name="Resnet",stride=1):
res_blocks= models.Sequential()
# may down sample
res_blocks.add(BasicBlock(filter_num,name+'_1',stride))
# just down sample one time
for pre in range(1,blocks):
res_blocks.add(BasicBlock(filter_num,name+'_2',stride=1))
return res_blocks
# use two groups of net, one for action, one for move
def _build_model(self):
# ------------------ build evaluate_net ------------------
self.shared_model = models.Sequential()
self.private_act_model = models.Sequential()
self.private_move_model = models.Sequential()
# shared part
# pre-process block
# self.shared_model.add(Conv2D(64, (2,3,3),strides=(1,2,2), input_shape=self.input_shape, name='conv1'))
# # self.shared_model.add(BatchNormalization(name='b1'))
# self.shared_model.add(Activation('relu'))
# self.shared_model.add(MaxPooling3D(pool_size=(2,2,2), strides=1, padding="VALID", name='p1'))
# # resnet blocks
# self.shared_model.add(self.build_resblock(64, 2, name='Resnet_1'))
# self.shared_model.add(self.build_resblock(80, 2, name='Resnet_2', stride=2))
# self.shared_model.add(self.build_resblock(128, 2, name='Resnet_3', stride=2))
# output layer for action model
self.private_act_model.add(Conv3D(32, (2,3,3),strides=(1,2,2), input_shape=self.input_shape, name='conv1'))
self.private_act_model.add(Activation('relu'))
self.private_act_model.add(Conv3D(48, (2,3,3),strides=(1,1,1), input_shape=self.input_shape, name='conv2'))
self.private_act_model.add(Activation('relu'))
self.private_act_model.add(Conv3D(64, (2,3,3),strides=(1,1,1), input_shape=self.input_shape, name='conv3'))
self.private_act_model.add(Activation('relu'))
self.private_act_model.add(Lambda(lambda x:tf.reduce_sum(x, 1)))
# self.private_act_model.add(MaxPooling3D(pool_size=(2,2,2), strides=1, padding="VALID", name='p1'))
# resnet blocks
self.private_act_model.add(self.build_resblock(64, 2, name='Resnet_1'))
self.private_act_model.add(self.build_resblock(96, 2, name='Resnet_2', stride=2))
self.private_act_model.add(self.build_resblock(128, 2, name='Resnet_3', stride=2))
self.private_act_model.add(self.build_resblock(256, 2, name='Resnet_4', stride=2))
self.private_act_model.add(GlobalAveragePooling2D())
# self.private_act_model.add(Reshape((1, -1)))
# self.private_act_model.add(CuDNNLSTM(32))
self.private_act_model.add(Dense(self.act_dim, name="d1")) # action model
self.private_act_model.summary()
self.act_model = models.Sequential()
# self.act_model.add(self.shared_model)
self.act_model.add(self.private_act_model)
# output layer for move model
self.private_move_model.add(Conv3D(32, (2,3,3),strides=(1,2,2), input_shape=self.input_shape, name='conv1'))
self.private_move_model.add(Activation('relu'))
self.private_move_model.add(Conv3D(48, (2,3,3),strides=(1,1,1), input_shape=self.input_shape, name='conv2'))
self.private_move_model.add(Activation('relu'))
self.private_move_model.add(Conv3D(64, (2,3,3),strides=(1,1,1), input_shape=self.input_shape, name='conv3'))
self.private_move_model.add(Activation('relu'))
self.private_move_model.add(Lambda(lambda x:tf.reduce_sum(x, 1)))
# self.private_move_model.add(MaxPooling3D(pool_size=(2,2,2), strides=1, padding="VALID", name='p1'))
# resnet blocks
self.private_move_model.add(self.build_resblock(64, 2, name='Resnet_1'))
self.private_move_model.add(self.build_resblock(96, 2, name='Resnet_2', stride=2))
self.private_move_model.add(self.build_resblock(128, 2, name='Resnet_3', stride=2))
self.private_move_model.add(self.build_resblock(256, 2, name='Resnet_4', stride=2))
self.private_move_model.add(GlobalAveragePooling2D())
# self.private_move_model.add(Reshape((1, -1)))
# self.private_move_model.add(CuDNNLSTM(32))
self.private_move_model.add(Dense(4, name="d1"))
# action model
self.move_model = models.Sequential()
# self.move_model.add(self.shared_model)
self.move_model.add(self.private_move_model)
# # ------------------ build target_model ------------------
# # shared part
# self.shared_target_model = models.Sequential()
# # pre-process block
# self.shared_target_model.add(Conv3D(64, (2,3,3),strides=(1,2,2), input_shape=self.input_shape, name='conv1'))
# self.shared_target_model.add(BatchNormalization(name='b1'))
# self.shared_target_model.add(Activation('relu'))
# self.shared_target_model.add(MaxPooling3D(pool_size=(2,2,2), strides=1, padding="VALID", name='p1'))
# # resnet blocks
# self.shared_target_model.add(self.build_resblock(64, 2, name='Resnet_1'))
# self.shared_target_model.add(self.build_resblock(80, 2, name='Resnet_2', stride=2))
# self.shared_target_model.add(self.build_resblock(128, 2, name='Resnet_3', stride=2))
# # output layer for action model
# self.private_act_target_model = models.Sequential()
# self.private_act_target_model.add(self.build_resblock(200, 2, name='Resnet_4', stride=2))
# self.private_act_target_model.add(GlobalAveragePooling3D())
# # self.private_act_target_model.add(Reshape((1, -1)))
# # self.private_act_target_model.add(CuDNNLSTM(32))
# self.private_act_target_model.add(Dense(self.act_dim, name="d1", kernel_regularizer=regularizers.L2(0.001)))
# # action model
# self.act_target_model = models.Sequential()
# self.act_target_model.add(self.shared_target_model)
# self.act_target_model.add(self.private_act_target_model)
# # output layer for move model
# self.private_move_target_model = models.Sequential()
# self.private_move_target_model.add(self.build_resblock(200, 2, name='Resnet_4', stride=2))
# self.private_move_target_model.add(GlobalAveragePooling3D())
# # self.private_move_target_model.add(Reshape((1, -1)))
# # self.private_move_target_model.add(CuDNNLSTM(32))
# self.private_move_target_model.add(Dense(4, name="d1", kernel_regularizer=regularizers.L2(0.001)))
# # action model
# self.move_target_model = models.Sequential()
# self.move_target_model.add(self.shared_target_model)
# self.move_target_model.add(self.private_move_target_model)
def predict(self, input):
input = tf.expand_dims(input,axis=0)
# shard_output = self.shared_model.predict(input)
pred_move = self.private_move_model(input)
pred_act = self.private_act_model(input)
return pred_move, pred_act