-
Notifications
You must be signed in to change notification settings - Fork 0
/
myhandwrittentrain_for2sets2_0.m
77 lines (74 loc) · 3.54 KB
/
myhandwrittentrain_for2sets2_0.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
clear; clc; close all;
% data from 2 people/二人分のデータを
% Create mypic2 folder and copy the contents of mypic1 and mypic/mypic2フォルダを作りmypicとmypic1のファイルの中身をコピーする
mkdir mypic2
copyfile mypic1 mypic2
copyfile mypic mypic2
%% Loading layers and setting images/層の読み込みと画像のセット
load mylayers
imds = imageDatastore('mypic2\train', 'IncludeSubfolders',true,'LabelSource','foldernames');
imdshandTrain = imds;
%[imdshandTrain,~] = splitEachLabel(imds,5,'randomized')
imdshandValidation = imageDatastore('mypic2\test', 'IncludeSubfolders',true,'LabelSource','foldernames');
%% Set training option/学習オプションの設定
options = trainingOptions('sgdm', ...
'MaxEpochs',1, ...
'ValidationData',imdshandValidation, ...
'ValidationFrequency',10, ...
'Verbose',false);
%% Visualization of training and results, plot creation/学習と結果の可視化、プロット作成
k=30; % number of epochs//学習の繰り返し回数(Epoch)
f = figure; %create plot object/ プロットの作成
f.Units = 'normalized';
f.Position = [0.05 0 0.5 0.8];shg
% Set initial values of variables/変数の初期値設定
flag25 =1;
flag50 =1;
flag75 =1;
m =zeros(4,1);
for nn = 1:k
if nn == 1
net = trainNetwork(imdshandTrain,layers,options);
else
net = trainNetwork(imdshandTrain,net.Layers,options);
end
YPred = classify(net,imdshandValidation);
for ii = 1:numel(imdshandValidation.Labels)
Itest = read(imdshandValidation); % Read images/画像の読み込み
YPred1 = classify(net,Itest); % Classify each data/画像ごとに予測
Itest3 = cat(3,Itest,Itest,Itest); % covert the image into RGB/画像をカラーに変換
if ~(YPred1 == imdshandValidation.Labels(ii))
Color = 'red'; % show red when prediction is wrong/予測結果が間違っていたら赤
else
Color = 'green'; % show green when prediction is correct/予測が正しければ緑
end
Itest3 = insertShape(Itest3,'Rectangle',[1 1 28 28],...
'Color', Color,'LineWidth',3); % Put a specified frame around the image/画像の周囲に指定した枠をつける
data{ii} = Itest3; % Store the image in the data the frame is placed/枠が付いたあとの画像をdataに格納
end
reset(imdshandValidation); %reset datastore to initial state/ imagedatastoreの読み出しをリセット
subplot(1,2,1), montage(data,'Size',[10 5]); % The result of each image is displayed on the left side of the figure/各画像の判定結果をfigureの左側に表示
YValidation = imdshandValidation.Labels;
accuracy(nn) = sum(YPred == YValidation)/numel(YValidation);% Calculate accuracy at this moment/現時点での精度を計算
% Save file as variable when accuracy exceeds 25%, 50%, 75%, respectively/精度が25%,50%,75%を超えた直後のファイルのみ変数として保持
if accuracy(nn) < 0.25
elseif accuracy(nn) >0.25 & flag25 ==1
net25 = net;
m(1) =nn;
flag25=0;
elseif accuracy(nn) > 0.5 & flag50 ==1
net50 = net;
m(2) =nn;
flag50=0;
elseif accuracy(nn) > 0.75 & flag75 ==1
net75 = net;
m(3) =nn;
flag75 =0;
end
subplot(1,2,2), plot(accuracy),ylim([0 1]) % Display accuracy at right side of figure/精度をfigureの右側に表示
shg;
end
accuracy(end) % Dispaly accuracy/精度の表示
m(4) = nn(end);
save('netresult2.mat','net*','accuracy','m')
% Copyright 2019 The MathWorks, Inc.