How to change color model of all pictures in image datastore?
Show older comments
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
Accepted Answer
More Answers (1)
Roshni Garnayak
on 7 Feb 2020
0 votes
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 Comment
Martin Simunsky
on 13 Feb 2020
Categories
Find more on Waveform Generation 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!