-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcombine_nets.py
91 lines (63 loc) · 3.17 KB
/
combine_nets.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
import mahotas
import scipy.ndimage
import scipy.misc
import numpy as np
import gzip
import cPickle
import glob
import os
import h5py
import partition_comparison
from datetime import date
#param_path = 'D:/dev/Rhoana/membrane_cnn/results/good3/'
param_path = 'D:/dev/Rhoana/membrane_cnn/results/stump_combo/'
param_files = glob.glob(param_path + "*.h5")
param_files = [x for x in param_files if x.find('.ot.h5') == -1]
date_string = date.today().strftime('%d%B%Y')
combo_file = 'D:/dev/Rhoana/membrane_cnn/results/combo/deep_net_combo{0}_{1}.h5'.format(len(param_files), date_string)
combo_output_h5 = h5py.File(combo_file, 'w')
combo_output_h5['/nets'] = len(param_files)
for i, param_file in enumerate(param_files):
net_string = '/net{0}'.format(i)
print param_file
offset_file = param_file.replace('.h5', '.sm.ot.h5')
offset_h5 = h5py.File(offset_file, 'r')
best_offset = offset_h5['/best_offset'][...]
best_sigma = offset_h5['/best_sigma'][...]
offset_h5.close()
combo_output_h5[net_string + '/best_offset'] = best_offset
combo_output_h5[net_string + '/best_sigma'] = best_sigma
network_h5 = h5py.File(param_file, 'r')
nlayers = network_h5['/layers'][...]
combo_output_h5[net_string + '/layers'] = nlayers
combo_output_h5[net_string + '/iterations'] = network_h5['/iterations'][...]
combo_output_h5[net_string + '/verror'] = network_h5['/verror'][...]
combo_output_h5[net_string + '/terror'] = network_h5['/terror'][...]
downsample = 1
if param_file.find('_ds2') != -1:
downsample = 2
elif param_file.find('_ds4') != -1:
downsample = 4
combo_output_h5[net_string + '/downsample_factor'] = downsample
if param_file.find('Stump') != -1:
combo_output_h5[net_string + '/stumpin'] = True
print 'Network {0} has {1} layers.'.format(i, nlayers)
for layer in range(nlayers):
layer_string = '/layer{0}/'.format(layer)
layer_type = network_h5[layer_string + 'type'][...]
combo_output_h5[net_string + layer_string + 'type'] = str(layer_type)
if layer_type == 'Convolution':
combo_output_h5[net_string + layer_string + 'weights'] = network_h5[layer_string + 'weights'][...]
combo_output_h5[net_string + layer_string + 'bias'] = network_h5[layer_string + 'bias'][...]
combo_output_h5[net_string + layer_string + 'maxpoolsize'] = network_h5[layer_string + 'maxpoolsize'][...]
elif layer_type == 'FullyConnected':
combo_output_h5[net_string + layer_string + 'weights'] = network_h5[layer_string + 'weights'][...]
combo_output_h5[net_string + layer_string + 'bias'] = network_h5[layer_string + 'bias'][...]
combo_output_h5[net_string + layer_string + 'ksize'] = network_h5[layer_string + 'ksize'][...]
elif layer_type == 'LogisticRegression':
combo_output_h5[net_string + layer_string + 'weights'] = network_h5[layer_string + 'weights'][...]
combo_output_h5[net_string + layer_string + 'bias'] = network_h5[layer_string + 'bias'][...]
else:
raise Exception("Unknown layer type: {0}".format(layer_type))
network_h5.close()
combo_output_h5.close()