MATLAB (12)

제어문

조건문

if-elseif-else

1
2
3
4
5
6
7
8
9
10
if 조건1
실행 문장
elseif 조건2
실행 문장
elseif 조건3
실행 문장
...
else
실행 문장
end

switch

1
2
3
4
5
6
7
8
9
switch(변수)
case(값1)
실행 문장
case(값2)
실행 문장
...
otherwise
실행 문장
end

반복문

while

1
2
3
while(조건)
실행 문장
end

for

1
2
3
for 변수 = 시작:끝
실행 문장
end
1
2
3
for 변수 = 시작:delta:끝
실행 문장
end
1
2
3
for 변수 = [num1, num2, ...]
실행 문장
end

함수

1
2
3
4
5
% 주석 작성 시 help 명령어에 출력
function 출력 = 함수명(param1, param2, ...)
실행 문장
...
end
1
함수명 = @(param1, param2, ...) 공식;

DNN

MNIST.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
>> digitDatasetPath=fullfile(matlabroot,'toolbox','nnet','nndemos','nndatasets','DigitDataset')

digitDatasetPath =

'/Applications/MATLAB_R2020a.app/toolbox/nnet/nndemos/nndatasets/DigitDataset'

>> imds=imageDatastore(digitDatasetPath,'IncludeSubfolders',true,'LabelSource','foldernames')

imds =

ImageDatastore - 속성 있음:

Files: {
' .../toolbox/nnet/nndemos/nndatasets/DigitDataset/0/image10000.png';
' .../toolbox/nnet/nndemos/nndatasets/DigitDataset/0/image9001.png';
' .../toolbox/nnet/nndemos/nndatasets/DigitDataset/0/image9002.png'
... and 9997 more
}
Folders: {
' .../MATLAB_R2020a.app/toolbox/nnet/nndemos/nndatasets/DigitDataset'
}
Labels: [0; 0; 0 ... and 9997 more categorical]
AlternateFileSystemRoots: {}
ReadSize: 1
SupportedOutputFormats: ["png" "jpg" "jpeg" "tif" "tiff"]
DefaultOutputFormat: "png"
ReadFcn: @readDatastoreImage

>> figure;
perm=randperm(10000,20);
for i=1:20
subplot(4,5,i);
imshow(imds.Files{perm(i)});
end
>> layers=[imageInputLayer([28 28 1])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
fullyConnectedLayer(10)
softmaxLayer
classificationLayer];

>> options=trainingOptions('sgdm','InitialLearnRate',0.01,'MaxEpochs',4,'Shuffle','every-epoch','ExecutionEnvironment','cpu','Plots','training-progress') % gpu 가능

options =

TrainingOptionsSGDM - 속성 있음:

Momentum: 0.9000
InitialLearnRate: 0.0100
LearnRateSchedule: 'none'
LearnRateDropFactor: 0.1000
LearnRateDropPeriod: 10
L2Regularization: 1.0000e-04
GradientThresholdMethod: 'l2norm'
GradientThreshold: Inf
MaxEpochs: 4
MiniBatchSize: 128
Verbose: 1
VerboseFrequency: 50
ValidationData: []
ValidationFrequency: 50
ValidationPatience: Inf
Shuffle: 'every-epoch'
CheckpointPath: ''
ExecutionEnvironment: 'cpu'
WorkerLoad: []
OutputFcn: []
Plots: 'training-progress'
SequenceLength: 'longest'
SequencePaddingValue: 0
SequencePaddingDirection: 'right'
DispatchInBackground: 0
ResetInputNormalization: 1

>> net=trainNetwork(imds,layers,options)
입력 데이터의 정규화를 초기화하는 중입니다.
|========================================================================|
|  Epoch  |  반복 횟수  |    경과 시간     |  미니 배치 정확도  |  미니 배치 손실  |  기본 학습률  |
|         |         |  (hh:mm:ss)  |             |            |          |
|========================================================================|
|       1 |       1 |     00:00:00 |      10.94% |     2.9609 |   0.0100 |
|       1 |      50 |     00:00:03 |      81.25% |     0.5866 |   0.0100 |
|       2 |     100 |     00:00:05 |      94.53% |     0.1719 |   0.0100 |
|       2 |     150 |     00:00:07 |      98.44% |     0.1784 |   0.0100 |
|       3 |     200 |     00:00:10 |     100.00% |     0.0615 |   0.0100 |
|       4 |     250 |     00:00:12 |      98.44% |     0.0713 |   0.0100 |
|       4 |     300 |     00:00:14 |     100.00% |     0.0326 |   0.0100 |
|       4 |     312 |     00:00:15 |     100.00% |     0.0291 |   0.0100 |
|========================================================================|

net =

SeriesNetwork - 속성 있음:

Layers: [15×1 nnet.cnn.layer.Layer]
InputNames: {'imageinput'}
OutputNames: {'classoutput'}

>> Out=classify(net,imds)

trainnetwork

alex.m
1
2
3
4
5
6
7
8
9
10
camera=webcam;
nnet=alexnet;
while true
picture=camera.snapshot;
picture=imresize(picture,[227,227]);
label=classify(nnet,picture);
image(picture);
title(char(label));
drawnow;
end