-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeatureExtractor.py
More file actions
239 lines (185 loc) · 8.09 KB
/
Copy pathFeatureExtractor.py
File metadata and controls
239 lines (185 loc) · 8.09 KB
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
235
236
237
238
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import tables
import skimage.transform as st
chunk_number = 400
chunk_length = 16
C3D_input_size = 112
sliding_window_size = 16
sliding_window_stride = 1
number_of_windows = 107985
num_windows_test = 8100 - 15
original_size = 128
number_of_frame_to_load = number_of_windows - sliding_window_size + 1
resized_stimulus_path = "/resized/resized_stimulus.npy"
extracted_features_path = "resized/extracted_features_train.npy"
extracted_features_path_test = "resized/extracted_features_test.npy"
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = 'x'):
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end = '\r')
# Print New Line on Complete
if iteration == total:
print()
'''
printProgressBar(0, 10, prefix = 'Prefix:', suffix = 'Complete', length = 20)
for i in range(10):
# Do something
printProgressBar(i + 1, 10, prefix = 'Prefix:', suffix = 'Complete', length = 20)
'''
######################
# Load Dataset #
######################
def roi_index(region, file):
roi = file.get_node('/roi/v1lh')[:].flatten()
return np.nonzero(roi==region)[0]
def load_train_stimulus():
stimuli = tables.open_file('Stimuli.mat')
return stimuli.get_node('/st')[:]
def load_train_response(subject, roi):
path = "VoxelResponses_subject" + subject + ".mat"
response = tables.open_file(path)
data = response.get_node('/rt')[:]
return data[roi_index(roi, response)]
def load_train_response_all(subject):
path = "VoxelResponses_subject" + subject + ".mat"
response = tables.open_file(path)
data = response.get_node('/rt')[:]
return data
def load_validation_stimulus():
stimuli = tables.open_file('Stimuli.mat')
return stimuli.get_node('/sv')[:]
def load_validation_response(subject, roi):
path = "VoxelResponses_subject" + subject + ".mat"
response = tables.open_file(path)
data = response.get_node('/rv')[:]
return data[roi_index(roi, response)]
def load_validation_response_all(subject):
path = "VoxelResponses_subject" + subject + ".mat"
response = tables.open_file(path)
data = response.get_node('/rv')[:]
return data
stimulus_train = load_train_stimulus()
np.save("stimulus_train.npy", np.asarray(stimulus_train))
print("Stimulus Loaded. Shape:" + str(stimulus_train.shape))
######################
# Load Model #
######################
import h5py
import tensorflow as tf
from keras.models import Model
from keras.models import model_from_json
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Convolution3D, MaxPooling3D, ZeroPadding3D
from keras.optimizers import SGD
from keras import regularizers
from keras.models import Sequential, Model
from keras.layers import Input, Dense, Dropout, Flatten
from keras.layers import Conv3D, MaxPooling3D, ZeroPadding3D
def create_model():
""" Creates model object with the sequential API:
https://keras.io/models/sequential/
"""
model = Sequential()
input_shape = (16, 112, 112, 3)
model.add(Conv3D(64, (3, 3, 3), activation='relu',
padding='same', name='conv1',
input_shape=input_shape))
model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2),
padding='valid', name='pool1'))
# 2nd layer group
model.add(Conv3D(128, (3, 3, 3), activation='relu',
padding='same', name='conv2'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2),
padding='valid', name='pool2'))
# 3rd layer group
model.add(Conv3D(256, (3, 3, 3), activation='relu',
padding='same', name='conv3a'))
model.add(Conv3D(256, (3, 3, 3), activation='relu',
padding='same', name='conv3b'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2),
padding='valid', name='pool3'))
# 4th layer group
model.add(Conv3D(512, (3, 3, 3), activation='relu',
padding='same', name='conv4a'))
model.add(Conv3D(512, (3, 3, 3), activation='relu',
padding='same', name='conv4b'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2),
padding='valid', name='pool4'))
# 5th layer group
model.add(Conv3D(512, (3, 3, 3), activation='relu',
padding='same', name='conv5a'))
model.add(Conv3D(512, (3, 3, 3), activation='relu',
padding='same', name='conv5b'))
model.add(ZeroPadding3D(padding=((0, 0), (0, 1), (0, 1)), name='zeropad5'))
model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2),
padding='valid', name='pool5'))
model.add(Flatten())
# FC layers group
model.add(Dense(4096, activation='relu', name='fc6'))
model.add(Dropout(.5))
model.add(Dense(4096, activation='relu', name='fc7'))
model.add(Dropout(.5))
model.add(Dense(487, activation='softmax', name='fc8'))
return model
def intermediate_layer_model(layer, model):
return Model(inputs=model.input, outputs=model.get_layer(layer).output)
def feature_hook(layer, model, data):
model = intermediate_layer_model(layer, model)
return model(data)
def create_features_extractor(model, layer_name):
extractor = Model(inputs= model.input,
outputs= model.get_layer(layer_name).output)
return extractor
model = create_model()
model.summary()
model.load_weights('c3d-sports1M_weights.h5' , by_name = True)
print("Weights Loaded")
#####################################
# Pick a Layer to Create the Extractor #
#####################################
output_layer_name = 'flatten'
extractor = create_features_extractor(model,output_layer_name)
extractor.summary()
################################
# Extract TrainSet Features #
################################
#extracted_feature = np.zeros((number_of_windows,extractor.output.shape[1]))
extracted_feature = np.zeros((number_of_windows,8192))
print("Starting to extract features. Expected output:" + str(extracted_feature.shape))
printProgressBar(0, number_of_windows, prefix = 'Progress:', suffix = '', length = 100)
for i in range(number_of_windows):
chunk = stimulus_train[i:i+sliding_window_size, :,:,:]
chunk_transposed = np.transpose(chunk,(0,2,3,1))
chunk_resized = st.resize(chunk_transposed, (sliding_window_size,C3D_input_size, C3D_input_size,3))
to_be_fed = np.zeros((1,sliding_window_size, C3D_input_size, C3D_input_size, 3))
to_be_fed[0,:,:,:,:] = chunk_resized
extracted_feature[i,:] = extractor.predict(to_be_fed)
printProgressBar(i + 1, number_of_windows, prefix = 'Progress:', suffix = '', length = 100)
#save to file
np.save("extracted_features.npy", extracted_features)
checking = np.load("extracted_features.npy")
print(str(checking.shape))
print(str(checking.mean()))
stimulus_test = load_validation_stimulus()
#############################
# Extract Test Features #
#############################
#extracted_feature = np.zeros((number_of_windows,extractor.output.shape[1]))
extracted_feature = np.zeros((num_windows_test, 8192))
print("Starting to extract features. Expected output:" + str(extracted_feature.shape))
printProgressBar(0, num_windows_test, prefix = 'Progress:', suffix = '', length = 100)
for i in range(num_windows_test):
chunk = stimulus_test[i:i+sliding_window_size, :,:,:]
chunk_transposed = np.transpose(chunk,(0,2,3,1))
chunk_resized = st.resize(chunk_transposed, (sliding_window_size, C3D_input_size, C3D_input_size,3))
to_be_fed = np.zeros((1,sliding_window_size, C3D_input_size, C3D_input_size, 3))
to_be_fed[0,:,:,:,:] = chunk_resized
extracted_feature[i,:] = extractor.predict(to_be_fed)
printProgressBar(i + 1, num_windows_test, prefix = 'Progress:', suffix = '', length = 100)
#save to file
np.save("extracted_features_test.npy", extracted_feature)
print(str(extracted_feature.shape))