-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathextractFeatures_5_25.m
126 lines (100 loc) · 3.35 KB
/
extractFeatures_5_25.m
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
function extractFeatures_5_25(task)
% extract raw CNN features per frame and save to feat_cache
%%
addpath('/home/user/kaixu/myGitHub/caffe/matlab/');
net_model_5 = ['/home/user/kaixu/myGitHub/caffe/examples/videoNet/training/' ...
'videoNetCNN_cr5_model_deploy_feature.prototxt'];
net_weights_5 = ['/home/user/kaixu/myGitHub/caffe/examples/videoNet/training/'...
'Snapshots/cr_5_CNN_10072016/videoNetCNN_5_iter_390000.caffemodel'];
net_model_25 = ['/home/user/kaixu/myGitHub/caffe/examples/videoNet/training/' ...
'videoNetCNN_cr25_model_deploy_feature.prototxt'];
net_weights_25 = ['/home/user/kaixu/myGitHub/caffe/examples/videoNet/training/'...
'Snapshots/cr_25_CNN_10072016/videoNetCNN_25_iter_145000.caffemodel'];
phase = 'test';
% task = 'Train';
% task = 'Val';
% testFile = '/home/user/kaixu/myGitHub/caffe/examples/videoNet/training/ValData_5_25.h5';
% savepath = 'feature_conv5_5_25_test_80_test.h5';
if strcmp(task, 'Train') == 1
testFile = './data/TrainData_5_25_10072016.h5';
savepath = './data/Feature_conv5_5_25_train.h5';
elseif strcmp(task, 'Val') == 1
testFile = './data/ValData_5_25_10072016.h5';
savepath = './data/Feature_conv5_5_25_val.h5';
end
seqLength = 10;
numChannels = 16;
imHeight = 32;
imWidth = 32;
use_gpu = 1;
%%
if exist('use_gpu', 'var')
% try
% id = obtain_gpu_lock_id;
% catch e
% disp(e)
% end
id = 1;
caffe.set_mode_gpu();
caffe.set_device(id);
net_5 = caffe.Net(net_model_5, net_weights_5, phase);
net_25 = caffe.Net(net_model_25, net_weights_25, phase);
% weight = net_5.blobs('conv5').get_data();
% weights_5 = net_5.copy_from(net_weights_5);
else
caffe.set_mode_cpu();
end
%%
testData = h5read(testFile, '/data');
testLabel = h5read(testFile, '/label');
numData = size(testData, 5)/10;
if strcmp(task, 'Train') == 1
numData = 256*80; %300 - 40G 100 - 17G
end
chunksz = 128;
totalct = 0;
created_flag = false;
outputFeature = [];
for i = 1:numData
data = testData(:,:,:,:,i);
data_5 = data(:,:,:,1);
data_25 = data(:,:,:,2:end);
data_25 = data_25(1:floor(imHeight*imWidth/25),:,:,:);
feature = net_5.forward({data_5});
feature = feature{1};
feature = permute(feature, [4 3 2 1]);
outputFeature_5 = feature;
feature = net_25.forward({data_25});
feature = feature{1};
feature = permute(feature, [4 3 2 1]);
outputFeature_25 = feature;
outputFeature_5=reshape(outputFeature_5,[1,numChannels,imHeight,imWidth]);
outputFeature_25=reshape(outputFeature_25,[9,numChannels,imHeight,imWidth]);
tmp = [outputFeature_5;outputFeature_25];
tmp = reshape(tmp, [1, size(tmp)]);
outputFeature = [outputFeature;tmp];
disp(['Completed',num2str(i/numData),'\n'])
if mod(i, chunksz) == 0
batchno = i / chunksz;
last_read=(batchno-1) * chunksz;
outputFeature = permute(outputFeature, [5 4 3 2 1]);
batchdata = outputFeature;
batchlabs = testLabel(:,:,:,:,last_read+1:last_read+chunksz);
startloc = struct('dat',[1,1,1,1,totalct+1], 'lab', [1,1,1,1,totalct+1]);
curr_dat_sz = store2hdf5(savepath, batchdata, batchlabs, ~created_flag, startloc, chunksz);
created_flag = true;
totalct = curr_dat_sz(end);
outputFeature = [];
end
end
%% writing to HDF5
h5disp(savepath);
%%
caffe.reset_all();
% save('./data/outputFeature_5_25_conv5.mat', 'outputFeature');
function [feature] = caffe_forward(net, imseq)
feature = net.forward({imseq});
feature = feature{1};
feature = permute(feature, [4 3 2 1]);
end
end