Automate Ground Truth Labeling for Semantic Segmentation

2 views (last 30 days)
Kirill Karpov
Kirill Karpov on 2 May 2019
Answered: Zhe Wang on 1 Dec 2021
Hi all.
I'm trying to write code to automate markup and it gives such an error.
Matrix dimensions must agree.
Error in vision.internal.labeler.tool.LabelerTool/checkUserLabels
Error in vision.internal.imageLabeler.tool.ImageLabelerTool/runAlgorithm
Error in vision.internal.labeler.tool.AlgorithmTab/setAlgorithmModeAndExecute
Error in vision.internal.labeler.tool.AlgorithmTab
Error in internal.Callback.execute (line 128)
feval(callback, src, event);
Error in matlab.ui.internal.toolstrip.base.Action/PeerEventCallback (line 832)
internal.Callback.execute(this.PushPerformedFcn, this, eventdata);
Error in matlab.ui.internal.toolstrip.base.PeerInterface>@(event,data)PeerEventCallback(this,event,data)
Error in hgfeval (line 62)
feval(fcn{1},varargin{:},fcn{2:end});
Error in javaaddlistener>cbBridge (line 52)
hgfeval(response, java(o), e.JavaEvent)
Error in javaaddlistener>@(o,e)cbBridge(o,e,response) (line 47)
@(o,e) cbBridge(o,e,response));
My code looks like this:
% Properties
properties
% Network saves the SeriesNetwork object that does the semantic
% segmentation.
PretrainedNetwork
% Categories holds the default
% categorical types.
AllCategories = {'Background'};
% Store names for 11 classes.
Sky
Road
RoadSide
Car
SideWalk
Signs
Buildings
Post
Backgrounds
fence
Person
end
%----------------------------------------------------------------------
% Setup
methods
function isValid = checkLabelDefinition(algObj, labelDef)
% Allow any labels that are of type 'PixelLabel', and are named
isValid = false;
if (strcmpi(labelDef.Name, 'Sky') && labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.Sky = labelDef.Name;
algObj.AllCategories{end+1} = labelDef.Name;
elseif (strcmpi(labelDef.Name, 'Road') && labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.Road = labelDef.Name;
algObj.AllCategories{end+1} = labelDef.Name;
elseif (strcmpi(labelDef.Name, 'RoadSide') && labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.RoadSide = labelDef.Name;
algObj.AllCategories{end+1} = labelDef.Name;
elseif (strcmpi(labelDef.Name, 'Car') && labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.Car = labelDef.Name;
algObj.AllCategories{end+1} = labelDef.Name;
elseif (strcmpi(labelDef.Name, 'SideWalk') && labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.SideWalk = labelDef.Name;
algObj.AllCategories{end+1} = labelDef.Name;
elseif (strcmpi(labelDef.Name, 'Signs') && labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.Signs = labelDef.Name;
algObj.AllCategories{end+1} = labelDef.Name;
elseif (strcmpi(labelDef.Name, 'Buildings') && labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.Buildings = labelDef.Name;
algObj.AllCategories{end+1} = labelDef.Name;
elseif (strcmpi(labelDef.Name, 'Post') && labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.Post = labelDef.Name;
algObj.AllCategories{end+1} = labelDef.Name;
elseif (strcmpi(labelDef.Name, 'Backgrounds') && labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.Backgrounds = labelDef.Name;
algObj.AllCategories{end+1} = labelDef.Name;
elseif (strcmpi(labelDef.Name, 'fence') && labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.fence = labelDef.Name;
algObj.AllCategories{end+1} = labelDef.Name;
elseif (strcmpi(labelDef.Name, 'Person') && labelDef.Type == labelType.PixelLabel)
isValid = true;
algObj.Person = labelDef.Name;
algObj.AllCategories{end+1} = labelDef.Name;
elseif(labelDef.Type == labelType.PixelLabel)
isValid = true;
end
end
end
%----------------------------------------------------------------------
% Execution
methods
function initialize(algObj, ~)
% Point to tempdir where pretrainedSegNet was downloaded.
pretrainedFolder = fullfile('C:\VKR\Workspace');
pretrainedSegNet = fullfile(pretrainedFolder,'SegNet3.mat');
data = load(pretrainedSegNet);
% Store the network in the 'Network' property of this object.
algObj.PretrainedNetwork = data.net;
end
function autoLabels = run(algObj, I)
% Setup categorical matrix with categories including road and
% sky
autoLabels = categorical(zeros(size(I,1), size(I,2), size(I,3), size(I,4), size(I,5), size(I,6), size(I,7), size(I,8), size(I,9), size(I,10)),0:10,algObj.AllCategories,'Ordinal',true);
pixelCat = semanticseg(I, algObj.PretrainedNetwork);
if ~isempty(pixelCat)
% Add the selected label at the bounding box position(s)
autoLabels(pixelCat == "Road") = algObj.Road;
autoLabels(pixelCat == "Sky") = algObj.Sky;
autoLabels(pixelCat == "RoadSide") = algObj.RoadSide;
autoLabels(pixelCat == "Car") = algObj.Car;
autoLabels(pixelCat == "SideWalk") = algObj.SideWalk;
autoLabels(pixelCat == "Signs") = algObj.Signs;
autoLabels(pixelCat == "Buildings") = algObj.Buildings;
autoLabels(pixelCat == "Post") = algObj.Post;
autoLabels(pixelCat == "Backgrounds") = algObj.Backgrounds;
autoLabels(pixelCat == "fence") = algObj.fence;
autoLabels(pixelCat == "Person") = algObj.Person;
end
end
end
end
  1 Comment
Image Analyst
Image Analyst on 2 May 2019
Somewhere in the callback, it should give the line of your code that was the source of this error. What in the error message is your code? What line of which of your methods? How are you instantiating and calling your class? What are you passing in for algObj?

Sign in to comment.

Answers (1)

Zhe Wang
Zhe Wang on 1 Dec 2021
You should to learn more about size() functional
autoLabels = categorical(zeros(size(I,1),size(I,2)),0:11,algObj.AllCategories,'Ordinal',true);

Community Treasure Hunt

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

Start Hunting!

Translated by