-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract_feat_all.m
56 lines (48 loc) · 1.97 KB
/
extract_feat_all.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
% Code to extract cNN Features from images and distribute them randomly into
% training and testing set
clear all; close all;
rng('default');
net = load('imagenet-vgg-f.mat');
cnnModel.net = vl_simplenn_tidy(net);
cnnModel.net.normalization = cnnModel.net.meta.normalization;
cnnModel.net.classes = cnnModel.net.meta.classes;
save('cnnModel.mat', 'cnnModel');
load('cnnModel.mat');
cnnModel.info.opts.batchSize = 200;
imset = dir('../train/*.jpg'); names = {imset.name};
index = randperm(numel(names));tr_size = 5000;
index_train = index(1:tr_size);
imageSize = cnnModel.net.normalization.imageSize(1:3);
trainingImages = zeros([imageSize tr_size],'single');
trainingLabels = zeros(tr_size,1);
for jj = 1:tr_size
trainingImages(:,:,:,jj) = imresize(im2single(imread(strcat('../train/',...
names{1,index_train(jj)}))),imageSize(1:2));
if strfind(names{1,index_train(jj)}, 'cat')
trainingLabels(jj) = 1;
else
trainingLabels(jj) = 2;
end
end
% % save('trainingImages.mat', 'trainingImages', '-v7.3');
save('trainingLabels.mat', 'trainingLabels');
[~, cnnFeat_train, timeCPU] = cnnPredict(cnnModel,trainingImages,'UseGPU',false);
save('cnnFeat_train.mat', 'cnnFeat_train', '-v7.3');
size(cnnFeat_train)
size(trainingLabels)
test_size=numel(index)-tr_size; index_test = index(tr_size+1:numel(index));
testingImages = zeros([imageSize test_size],'single');
testingLabels = zeros(test_size,1);
for jj1 = 1:test_size
testingImages(:,:,:,jj1) = imresize(im2single(imread(strcat('../train/',...
names{1,index_test(jj1)}))),imageSize(1:2));
if strfind(names{1,index_test(jj1)}, 'cat')
testingLabels(jj1) = 1;
else
testingLabels(jj1) = 2;
end
end
save('testingImages.mat', 'testingImages', '-v7.3');
save('testingLabels.mat', 'testingLabels');
[~, cnnFeat_test, timeCPU] = cnnPredict(cnnModel,testingImages,'UseGPU',false);
save('cnnFeat_test.mat', 'cnnFeat_test', '-v7.3');