MATLAB: How to change color model of all pictures in image datastore

color modelimage datastoreimage processingMATLABneural networkstransform datastore

Hello everybody, I need help with a changing color model of pictures in a Image Datastore.
I've tried to change the color model with a transform database.
clc; clear; close all;
net = resnet101();
%% IMDS
rootFolder = 'cifar100Train';
categories = {'Dolphin','Rocket','Bed','Can','Pear'};
imds.train = imageDatastore(fullfile(rootFolder, categories), 'LabelSource', 'foldernames');
imds.train = splitEachLabel(imds.train, 2 , 'randomize');
auimds.train = augmentedImageDatastore(net.Layers(1).InputSize(1:2), imds.train);
Here comes the problem with transformFcn (function is defined below).
dsnew.train = transform(auimds.train, @transformFcn);
%% Changing layer of a CNN
lgraph = layerGraph(net);
newFCLayer = fullyConnectedLayer(5, 'Name', 'fc5');
lgraph = replaceLayer(lgraph,'fc1000',newFCLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,'ClassificationLayer_predictions',newClassLayer);
%% Training options
opts = trainingOptions('sgdm', ...
'InitialLearnRate',.001,...
'MaxEpochs',10, ...
'MiniBatchSize',64, ...
'Shuffle','every-epoch', ...
'Plots','training-progress');
CNN = trainNetwork(dsnew.train, lgraph, opts);
%% Changing color model via the transformFcn
function dataOut = transformFcn(dataIn)
dataOut = rgb2hsv(dataIn);
end
For color model change I've used rgb2hsv function. Matlab displays this error message:
Error using trainNetwork (line 170)
Invalid transform function defined on datastore.
Error in transferlearning (line 37)
CNN = trainNetwork(dsnew.train, lgraph, opts);
Caused by:
Error using nnet.internal.cnn.util.NetworkDataValidator/assertDatastoreHasResponses (line 140)
Invalid transform function defined on datastore.
Error using rgb2hsv>parseInputs (line 95)
MAP must be a Mx3 array.
I would love to find any effective solution for changing color model.
Thank you for any answer!
Martin

Best Answer

I hope I've figured this problem with Transform datastore:
First error
Error using trainNetwork (line 170)
Invalid transform function defined on datastore.
Error in transferlearning (line 37)
CNN = trainNetwork(dsnew.train, lgraph, opts);
Caused by:
Error using nnet.internal.cnn.util.NetworkDataValidator/assertDatastoreHasResponses (line 140)
Invalid transform function defined on datastore.
Error using rgb2hsv>parseInputs (line 95)
MAP must be a Mx3 array.
was figured out by using im2uint8 function.
im2uint8(rgb2hsv(dataIn));
Second error
Error using trainNetwork (line 170)
Invalid training data. Responses must be nonempty.
was figured out thanks to this script in transformFcn file:
function dataOut = transformFcn(dataIn)
B = table2cell(dataIn);
NoImg = size(B,1);
for i=1:NoImg
B{i,1} = im2uint8(rgb2hsv(B{i,1}));
end
dataOut = cell2table(B);
end
In main file:
imds.train = imageDatastore(fullfile(trainDirectoryName, categories), 'LabelSource', 'foldernames');
imds.train = splitEachLabel(imds.train,5, 'randomize');
auimds.train = augmentedImageDatastore(sizeImg, imds.train);
dsnew.train = transform(auimds.train, @transformFcn);
dsnew.train.UnderlyingDatastore.MiniBatchSize = 10;
ReadFcn takes only one image, instead Transform datastore takes whole batch of images (size of this batch is defined in the last line: dsnew.train.UnderlyingDatastore.MiniBatchSize; default is 128).
Hope this helps :-)
Related Question