Invalid Transformation in trainNetwork (on pixelLabelImageDatastore)
Show older comments
Hi
I am trying to do transfer learning on semantic segmentation with the Deep Learning Toolbox, however I am not sure if I am doing things right.
As I don't have a lot of training data, I am using vgg19 as basis, using 224x224 images. However the images and labels I created with ImageLabeler are of size 1200x1600. Thus I try to transform both the image and pixel label datastores to 224x224, but it doesn't seem to work.
The error I am getting is:
Error using trainNetwork (line 165)
Invalid transform function defined on datastore.
Caused by:
Error using nnet.internal.cnn.util.NetworkDataValidator/assertDatastoreHasResponses (line 120)
Invalid transform function defined on datastore.
Error using imresize
Expected input number 1, A, to be one of these types:
single, double, int8, int16, int32, uint8, uint16, uint32, logical
Instead its type was table.
Here is the code:
%prepare training data from ground truth
[imdsTrain,pxds] = pixelLabelTrainingData(gTruth);
%segnetLayers with e.g. vgg19
imageSize = [224 224 3];
numClasses = 2;
lgraph = segnetLayers(imageSize,numClasses,'vgg19');
%Create a pixel label image datastore for training a semantic segmentation network.
pximds = pixelLabelImageDatastore(imdsTrain,pxds);
%resize the labels and images
targetSize = [224,224];
imdsReSz = transform(pximds,@(x) imresize(x,targetSize));
%Set up training options.
options = trainingOptions('sgdm','InitialLearnRate',1e-3, ...
'MaxEpochs',20,'VerboseFrequency',10);
%Train the network.
net = trainNetwork(imdsReSz,lgraph,options);
What am I doing wrong?
Answers (1)
Sourav Bairagya
on 23 Aug 2019
1 vote
The error you are getting is coming from the use of “imresize” function over pixel-label image datastore object i.e. “pximds”. This “pximds” is of datatype ‘table’and resizing a table cannot be done using “imresize” function. The “imresize”function can only resize objects of numerical (‘single’, ‘double’, ‘int8’, ‘int16’, ‘int32’, ‘uint8’, ‘uint16’, ‘uint32’) or “logical” datatypes.
Now, one option here is to do this resize individually “imdsTrain” and “pxds”. But this is possible only for “imdsTrain” which is an image datastore object. In this case, each path stored in “imdsTrain” leads to an image file (where pixels are either ‘double’ or ‘uint8’ type) and hence, it is possible to resize it.
But, “pxds” is the pixel label datastore object, where each saved path leads to a label image where each pixel is saved as a category. Now, logically you cannot resize this categorical array as here the original pixel values of the corresponding categories are not retained anymore. Hence, resizing of label images are not possible.
Here, you can resize your training dataset to the desired dimension (224,224) before labelling is done. After resizing you can generate the pixel labels using “ImageLabeler”. Then, there will be no need of resizing label images in any intermediate step.
4 Comments
Markus Ess
on 24 Aug 2019
drummer
on 22 Oct 2020
Sourav Bairagya, I'm applying a transformation in a imdsTrain set, but I'm having the same problem Marjus Ess had previously.
I adapted a preprocessVolumetricIMDS function as in this link, but I'd rather apply geometric transformation to my dataset of tri-dimensional images (i.e., randomAffine3D) to augment my training dataset.
What would be the problem, if I'm doing exactly what you mentioned?
augImdsTest = transform(imdsTrain, @(x) preprocessVolumetricIMDS(x),...
'IncludeInfo', true)
function dataOut = preprocessVolumetricIMDS(data)
numRows = size(data, 1);
dataOut = cell(numRows, 1);
for idx = 1 : numRows
dataTransf = randomAffine3d( 'Rotation', @selectRotation, ...
'Scale', @selectScale, ...
'Shear', @selectShear, ...
'XTranslation', @selectXTranslation, ...
'YTranslation', @selectYTranslation, ...
'ZTranslation', @selectZTranslation);
outputView = affineOutputView(size(data), testTransf);
dataWarp = imwarp(data, dataTransf, 'OutputView', outputView);
dataWarp = imresize3(dataWarp, [256, 256, 89]); % resize the image.
dataOut(idx) = dataWarp;
end
% my handle functions are here.
end
Error message is:
Invalid trasform function defined on datastore
Caused by:
Error using matlab.io.datastore.CombinedDatastore/read
Invalid transform function defined on datastore
Too many input arguments.
Matthew J
on 12 Apr 2021
Thank you Sourav
Shanmugasundari Mariappan
on 21 Nov 2022
Categories
Find more on Deep Learning for Image Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!