-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensemble.py
41 lines (33 loc) · 1.25 KB
/
ensemble.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
import os
from process_result_problem_1 import CapsNet as CapsNet_1
from process_result_problem_2 import CapsNet as CapsNet_2
from keras import layers, models
from keras.models import Model
def load_trained_models_1(input_shape, directory):
n_folds = 5
models = list()
for i in range(n_folds):
# define model
model = CapsNet_1(input_shape=input_shape, n_class=2, routings=3)
weight_file = directory + '/fold_%d' % (i) + '/best_model.h5'
model.load_weights(weight_file)
print('load weight from ', weight_file)
models.append(model)
return models
def load_trained_models_2(input_shape, directory):
n_folds = 10
models = list()
for i in range(n_folds):
# define model
model = CapsNet_2(input_shape=input_shape, n_class=2, routings=3)
weight_file = directory + '/fold_%d' % (i) + '/best_model.h5'
model.load_weights(weight_file)
print('load weight from ', weight_file)
models.append(model)
return models
def ensemble_folds(models, input_shape):
model_input = layers.Input(shape=input_shape)
outputs = [model(model_input) for model in models]
y = layers.Average()(outputs)
model = Model(model_input, y, name='ensemble')
return model