How to change color model of all pictures in image datastore?

8 vues (au cours des 30 derniers jours)
Martin Simunsky
Martin Simunsky le 3 Fév 2020
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

Réponse acceptée

Martin Simunsky
Martin Simunsky le 20 Fév 2020
Modifié(e) : Martin Simunsky le 20 Fév 2020
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 :-)

Plus de réponses (1)

Roshni Garnayak
Roshni Garnayak le 7 Fév 2020
Instead of using ‘transformFcn’, you can use the ‘ReadFcn’ parameter in the ImageDatastore object. You can define the colour space transformation in the @customreader.
For more information on how to do it, please refer the Properties section in the following link:
  1 commentaire
Martin Simunsky
Martin Simunsky le 13 Fév 2020
ReadFcn parameter works
I've tried this option as well. But they say this:
"Using ReadFcn to transform or pre-process 2-D images is not recommended. For file formats recognized by imformats, specifying ReadFcn slows down the performance of imageDatastore. For more efficient ways to transform and pre-process images, see Preprocess Images for Deep Learning (Deep Learning Toolbox)." (1)
Howewer, after few attempts it works:
...
imds.train = imageDatastore(fullfile(trainDirectoryName, categories), 'LabelSource', 'foldernames');
imds.train = splitEachLabel(imds.train,5, 'randomize');
imds.train.ReadFcn = @transformFcn;
...
transformFcn file:
function dataOut = transformFcn(dataIn)
dataOut = imread(dataIn);
dataOut = im2uint8(rgb2hsv(dataOut));
end
transformFcn displays error
If I used transformFcn instead of ReadFcn in this way:
...
imds.train = imageDatastore(fullfile(trainDirectoryName, categories), 'LabelSource', 'foldernames');
imds.train = splitEachLabel(imds.train,5, 'randomize');
% imds.train.ReadFcn = @transformFcn;
dsnew.train = transform(imds.train, @transformFcn);
...
transformFcn file:
function dataOut = transformFcn(dataIn)
% dataOut = imread(dataIn);
dataOut = im2uint8(rgb2hsv(dataIn));
end
Matlab displays this error message in the beginning of training:
Error using trainNetwork (line 170)
Invalid training data. Responses must be nonempty.
Can anyone help me, please?

Connectez-vous pour commenter.

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by