의미론적 분할 신경망 훈련시키기
훈련 데이터를 불러옵니다.
dataSetDir = fullfile(toolboxdir("vision"),"visiondata","triangleImages"); imageDir = fullfile(dataSetDir,"trainingImages"); labelDir = fullfile(dataSetDir,"trainingLabels");
영상을 위한 영상 데이터저장소를 만듭니다.
imds = imageDatastore(imageDir);
ground truth 픽셀 레이블을 위한 pixelLabelDatastore
를 만듭니다.
classNames = ["triangle" "background"]; labelIDs = [255 0]; pxds = pixelLabelDatastore(labelDir,classNames,labelIDs);
훈련 영상과 ground truth 픽셀 레이블을 시각화합니다.
I = read(imds); C = read(pxds); I = imresize(I,5,"nearest"); L = imresize(uint8(C{1}),5,"nearest"); imshowpair(I,L,"montage")
훈련을 위한 영상과 픽셀 레이블 데이터저장소를 결합합니다.
trainingData = combine(imds,pxds);
의미론적 분할 신경망을 만듭니다. 이 신경망은 다운샘플링 및 업샘플링 설계를 기반으로 하는 간단한 의미론적 분할 신경망을 사용합니다.
numFilters = 64; filterSize = 3; numClasses = 2; layers = [ imageInputLayer([32 32 1]) convolution2dLayer(filterSize,numFilters,Padding=1) reluLayer() maxPooling2dLayer(2,Stride=2) convolution2dLayer(filterSize,numFilters,Padding=1) reluLayer() transposedConv2dLayer(4,numFilters,Stride=2,Cropping=1); convolution2dLayer(1,numClasses); softmaxLayer() ];
훈련 옵션을 설정합니다.
opts = trainingOptions("sgdm", ... InitialLearnRate=1e-3, ... MaxEpochs=100, ... MiniBatchSize=64);
픽셀 분류에 적합한 손실 함수를 정의합니다.
function loss = modelLoss(Y,T) mask = ~isnan(T); T(isnan(T)) = 0; loss = crossentropy(Y,T,Mask=mask,NormalizationFactor="mask-included"); end
신경망을 훈련시킵니다.
net = trainnet(trainingData,layers,@modelLoss,opts);
Iteration Epoch TimeElapsed LearnRate TrainingLoss _________ _____ ___________ _________ ____________ 1 1 00:00:13 0.001 0.65456 50 17 00:00:52 0.001 0.071766 100 34 00:01:07 0.001 0.04846 150 50 00:01:24 0.001 0.028925 200 67 00:01:54 0.001 0.028831 250 84 00:02:30 0.001 0.029716 300 100 00:03:05 0.001 0.019433 Training stopped: Max epochs completed
테스트 영상을 읽어 들이고 표시합니다.
testImage = imread("triangleTest.jpg");
imshow(testImage)
테스트 영상을 분할하고 결과를 표시합니다.
C = semanticseg(testImage,net,Classes=classNames); B = labeloverlay(testImage,C); imshow(B)
참고 항목
trainnet
(Deep Learning Toolbox)