本文使用matlab环境,测试了自动驾驶场景的图像分割任务。分割网络使用Unet。
一千张标注图像,最终训练精度达到 90%。
场景预标注数据下载地址:Semantic Segmentation for Self Driving Cars | Kaggle
数据下载可能需要,如果无法下载可移步 自动驾驶场景图像-分割标注数据.zip
数据标注了13个类别,每个类别具体名字未知,随便给个类名测试。
imagedir="E:\DLDatasets\kaggle\SelfDriving\dataA\dataA\CameraRGB";
labeldir="E:\DLDatasets\kaggle\SelfDriving\dataA\dataA\CameraSeg";
classNames=["c0","c1","c2","c3","c4","c5","c6","c7","c8","c9","c10","c11","c12"];
labelIds=0:12;
imds=imageDatastore(imagedir, "ReadFcn",@readImage);
pxds=pixelLabelDatastore(labeldir,classNames,labelIds,"ReadFcn",@readLabel);
ds=combine(imds,pxds);
图像原始大小为 600x800,首先自定义数据读取函数将其缩放到 96x128,另外标注图像是三通道,需要将其转成单通道图像。
function [data] = readImage(path)
data=imread(path);
data=imresize(data,[96 128]);
end
function [data] = readLabel(path)
data=imread(path);
data=data(:,:,1);
data=imresize(data,[96 128]);
end
% 定义网络
imageSize = [96 128 3];
numClasses = 13;
lgraph = unetLayers(imageSize, numClasses);
% 训练参数
options = trainingOptions('adam', ...
'InitialLearnRate',1e-3, ... % 1e-4不收敛,改为1e-3
'MaxEpochs',100, ...
"MiniBatchSize", 8,...
'VerboseFrequency',50, ...
'Plots','training-progress');
net = trainNetwork(ds,lgraph,options)
最终训练情况
pic = imread("E:\DLDatasets\kaggle\SelfDriving\dataA\dataA\CameraRgb\02_00_120.png");
msk = imread("E:\DLDatasets\kaggle\SelfDriving\dataA\dataA\CameraSeg\02_00_120.png");
pic = imresize(pic, [96,128]);
msk = imresize(msk, [96,128]);
msk=msk(:,:,1);
out2 = predict(net,pic);
[h,w,~] = size(out2);
result = uint8(zeros(h,w));
for y = 1:h
for x = 1:w
[~,i]=max(out2(y,x,:));
result(y,x)=i-1;
end
end
result = result*20;
msk = msk*20;
subplot(1,3,1)
imshow(pic)
subplot(1,3,2)
imshow(result,'Colormap',jet(255))
subplot(1,3,3)
imshow(msk,'Colormap',jet(255))