-
Notifications
You must be signed in to change notification settings - Fork 3
/
cnn_sampling_training.m
81 lines (73 loc) · 2.46 KB
/
cnn_sampling_training.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
% This script is used to prepare the a sample from the the total dataset
% for the CNN training
% Not used any more because a smaller sample set is not good compared to
% large dataset
% please discard it.
%% random sample data
folder = 'test_loops_new';
images = dir([folder,'\*.jpg']);
%%
numImgs = size(images,1);
sample_ratio = 0.01;
testingset=zeros(64,64,3,numImgs);
parfor fileID = 1:numImgs
imagename = images(fileID).name;
filename = fullfile(images(fileID).folder,imagename);
I = imread(filename);
% Some images may be grayscale. Replicate the image 3 times to
% create an RGB image.
if ~ismatrix(I)
I = rgb2gray(I);
end
I2 = imadjust(I);
I3 = imgaussfilt(I,2);
I = cat(3,I,I2,I3);
% Resize the image as required for the CNN.
Iout = imresize(I, [64 64]);
testingset(:,:,:,fileID) = Iout;
end
%%
[sampleSet,idx] = datasample(testingset,round(sample_ratio*numImgs),4,'Replace',false);
for i = 1:size(sampleSet,4)
imagename = ['test_loops_resize\',num2str(i),'.jpg'];
imwrite(uint8(sampleSet(:,:,:,i)), imagename);
end
%%
%% random sample data
negfolder = 'test_nonloop_new';
negimages = dir([negfolder,'\*.jpg']);
%%
numImgs = size(negimages,1);
sample_ratio = 0.01;
negtestingset=zeros(64,64,3,numImgs);
parfor fileID = 1:numImgs
imagename = negimages(fileID).name;
filename = fullfile(negimages(fileID).folder,imagename);
I = imread(filename);
% Some images may be grayscale. Replicate the image 3 times to
% create an RGB image.
if ~ismatrix(I)
I = rgb2gray(I);
end
I2 = imadjust(I);
I3 = imgaussfilt(I,2);
I = cat(3,I,I2,I3);
% Resize the image as required for the CNN.
Iout = imresize(I, [64 64]);
negtestingset(:,:,:,fileID) = Iout;
end
%%
[negsampleSet,negidx] = datasample(negtestingset,round(sample_ratio*numImgs),4,'Replace',false);
for i = 1:size(negsampleSet,4)
imagename = ['nonloop_resize\',num2str(i),'.jpg'];
imwrite(uint8(negsampleSet(:,:,:,i)), imagename);
end
%%
cropimages_new = cat(4,testingset,negtestingset);
cropimglabels_new = cell(size(cropimages_new,4),1);
cropimglabels_new(1:size(testingset,4))={'loop'};
cropimglabels_new(size(testingset,4)+1:end)={'nonloop'};
cropimglabels_new = categorical(cropimglabels_new);
randidx = randperm(size(cropimglabels_new,1));
cropimages_new = cropimages_new(:,:,:,randidx);
cropimglabels_new = cropimglabels_new(randidx);