Matlab neural network. Problem with RGB.

1 Ansicht (letzte 30 Tage)
Kirill Karpov
Kirill Karpov am 1 Mai 2019
Kommentiert: Kirill Karpov am 1 Mai 2019
Hi all.
Recently began to study neural networks in Matlab. Did to guide semantic segmentation via model VGG-16. Downloaded the camvid dataset. Produced resize from 720х960 to 360х480. After that, the initial piece of code.
imgDir = fullfile('C:\Users\ras19\AppData\Local\Temp\CamVid\images\701_StillsRaw_full\image\imagesResized');
imds = imageDatastore(imgDir);
I = readimage(imds,1);
I = histeq(I);
classes = [
"Sky"
"Road"
"RoadSide"
"Car"
"SideWalk"
"Signs"
"Post"
"Buildings"
"Backgrounds"
"fence"
"Person"
];
%Создание базы данных меток
labelIDs = camvidPixelLabelIDs();
% labelDir = fullfile('C:\Users\ras19\Desktop\Учёбка\ВКР\MY\Matlab\Test\PixelLabelData_7');
labelDir = fullfile('C:\Users\ras19\AppData\Local\Temp\CamVid\labels\label\labelsResized');
pxds = pixelLabelDatastore(labelDir,classes,labelIDs);
C = readimage(pxds,1);
cmap = camvidColorMap;
B = labeloverlay(I,C,'ColorMap',cmap);
imshow(B)
Gives this error:
Error using matlab.io.datastore.PixelLabelDatastore.label2cat (line 1033)
Pixel label image has scalar pixel label IDs instead of RGB-triplet pixel label IDs.
Error in matlab.io.datastore.PixelLabelDatastore/label2categorical (line 937)
L = matlab.io.datastore.PixelLabelDatastore.label2cat(...
Error in matlab.io.datastore.PixelLabelDatastore/readimage (line 544)
C = this.label2categorical(C, info);
Error in gaid (line 28)
C = readimage(pxds,1);
I noticed that after resizing the labels instead of the steel color in different shades of grey can this problem?
Functions that were used:
function labelIDs = camvidPixelLabelIDs()
labelIDs = { ...
% "Sky"
[
128 128 128; ... % "Sky"
]
% Road
[
128 064 128; ... % "Road"
128 000 192; ... % "LaneMkgsDriv"
192 000 064; ... % "LaneMkgsNonDriv"
]
% "RoadSide"
[
128 128 192; ... % "RoadShoulder"
064 192 128; ... % "ParkingBlock
]
% "Car"
[
064 000 128; ... % "Car"
064 128 192; ... % "SUVPickupTruck"
192 128 192; ... % "Truck_Bus"
192 064 128; ... % "Train"
128 064 064; ... % "OtherMoving"
000 128 192; ... % "Bicyclist"
192 000 192; ... % "MotorcycleScooter"
]
% "Sidewalk"
[
000 000 192; ... % "Sidewalk"
]
% "Signs"
[
192 128 128; ... % "SignSymbol"
128 128 064; ... % "Misc_Text"
000 064 064; ... % "TrafficLight"
]
% "Post"
[
192 192 128; ... % "Column_Pole"
000 000 064; ... % "TrafficCone"
]
% "Buildings"
[
000 128 064; ... % "Bridge"
128 000 000; ... % "Building"
064 192 000; ... % "Wall"
% 064 000 064; ... % "Tunnel"
192 000 128; ... % "Archway"
]
% "Backgrounds"
[
128 128 000; ... % "Tree"
192 192 000; ... % "VegetationMisc"
]
% "fence"
[
064 064 128; ... % "Fence"
064 000 064; ... % "Tunnel"
]
% "Person"
[
064 064 000; ... % "Pedestrian"
192 128 064; ... % "Child"
064 000 192; ... % "CartLuggagePram"
064 128 064; ... % "Animal"
]
% % "Bicyclist"
% [
% 000 128 192; ... % "Bicyclist"
% 192 000 192; ... % "MotorcycleScooter"
% ]
};
end
function pixelLabelColorbar(cmap, classNames)
% Add a colorbar to the current axis. The colorbar is formatted
% to display the class names with the color.
colormap(gca,cmap)
% Add colorbar to current figure.
c = colorbar('peer', gca);
% Use class names for tick marks.
c.TickLabels = classNames;
numClasses = size(cmap,1);
% Center tick labels.
c.Ticks = 1/(numClasses*2):1/numClasses:1;
% Remove tick mark.
c.TickLength = 0;
end
% Создание карты цветов для каждого класса
function cmap = camvidColorMap()
% Define the colormap used by CamVid dataset.
cmap = [
0 114 189; %Sky
217 83 25; %Road
237 177 32; %RoadSide
126 47 142; %Car
119 172 48; %SideWalk
77 190 238; %Signs
162 20 47; %Post
255 0 0; %Buildings
0 255 0; %Backgrounds
255 26 185; %fence
255 211 0 %Person
];
% Normalize between [0 1].
cmap = cmap ./ 255;
end
function imds = resizeCamVidImages(imds, imageFolder)
% Resize images to [360 480].
if ~exist(imageFolder,'dir')
mkdir(imageFolder)
else
imds = imageDatastore(imageFolder);
return; % Skip if images already resized
end
reset(imds)
while hasdata(imds)
% Read an image.
[I,info] = read(imds);
% Resize image.
I = imresize(I,[360 480]);
% Write to disk.
[~, filename, ext] = fileparts(info.Filename);
imwrite(I,[imageFolder filename ext])
end
imds = imageDatastore(imageFolder);
end
function pxds = resizeCamVidPixelLabels(pxds, labelFolder)
% Resize pixel label data to [360 480].
classes = pxds.ClassNames;
labelIDs = 1:numel(classes);
if ~exist(labelFolder,'dir')
mkdir(labelFolder)
else
pxds = pixelLabelDatastore(labelFolder,classes,labelIDs);
return; % Skip if images already resized
end
reset(pxds)
while hasdata(pxds)
% Read the pixel data.
[C,info] = read(pxds);
% Convert from categorical to uint8.
L = uint8(C);
% Resize the data. Use 'nearest' interpolation to
% preserve label IDs.
L = imresize(L,[360 480],'nearest');
% Write the data to disk.
[~, filename, ext] = fileparts(info.Filename);
imwrite(L,[labelFolder filename ext])
end
labelIDs = 1:numel(classes);
pxds = pixelLabelDatastore(labelFolder,classes,labelIDs);
end
  1 Kommentar
Kirill Karpov
Kirill Karpov am 1 Mai 2019
if you do not make a resize, then everything works.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by