CNN multi image classification with 4 channel

조회 수: 18(최근 30일)
Buzz_root 2021년 2월 19일
댓글: Nidhi Sawant 2021년 5월 3일
Hi guys,
My cnn should receive 4 images that represent the features of the same image.
Each image represents the vertical, horizontal, oblique details and the low pass filtered image. So I started with the original image, then extracted the image details and saved them.
From the saved data I created 4 imagaDatastore and joined them with the combine function. So now I have to pass the datastore I create to my cnn, then I create the vector of the labels and add it to the database.
Now my problem arises: the cnn does not read the input and displays the Invalid training data error. For classification networks, responses must be categorical.
I have read a lot of documentation about it but I can't solve the problem. I did not understand how to input my combinedatastore to cnn
This is a part of my code:
I use the code for create categorical vector labels from
%create the 4 imagedatastore that represent the feature of the image
LowPass_imds= imageDatastore('Low_Pass','IncludeSubfolders',true,'LabelSource','foldernames');
Vertical_imds= imageDatastore('Vertical','IncludeSubfolders',true,'LabelSource','foldernames');
Horizontal_imds= imageDatastore('Horizontal','IncludeSubfolders',true,'LabelSource','foldernames');
Diagonal_imds= imageDatastore('Diagonal','IncludeSubfolders',true,'LabelSource','foldernames');
%extract the label, in this example i use only one class
labels = LowPass_imds.Labels;
% %C = categorical for create categorical vector label from
labelStore = tabularTextDatastore('labels.txt','TextscanFormats','%C',"ReadVariableNames",false);
labelStoreCell = transform(labelStore,@setcat_and_table_to_cell);
cds = combine(LowPass_imds,Vertical_imds,Horizontal_imds,Diagonal_imds, labelStoreCell);
cds.readall %for show that all is ok
%this is only an example, infact i use only one class
layers =[
imageInputLayer([128 128 5])
convolution2dLayer(3,8, 'Padding','same')
maxPooling2dLayer(2, 'Stride',2)
convolution2dLayer(3,16, 'Padding','same')
maxPooling2dLayer(2, 'Stride',2)
convolution2dLayer(3,32, 'Padding','same')
maxPooling2dLayer(2, 'Stride',2)
options = trainingOptions('sgdm', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',4, ...
'Shuffle','every-epoch', ...
'ValidationData',cds , ...
'ValidationFrequency',30, ...
'Verbose',false, ...
net= trainNetwork(cds , layers, options);
function [dataout] = setcat_and_table_to_cell(datain)
validcats = string(0:1); % define valid labels for categorical array
datain.(1) = setcats(datain.(1),validcats);
dataout = table2cell(datain);


Shashank Gupta
Shashank Gupta 2021년 2월 22일
I think the problem is in forming the datastore. I have one more way to combine the datastore. Please refer to following piece of code for the same.
imds1 = imageDatastore('imds1 location', ...); % First datastore
imds2 = imageDatastore('imds2 location', ...); % Second datastore
imds3 = imageDatastore('imds3 location', ...); % Third datastore
% Combine datastore.
imds_combine = imageDatastore({'imds1 location', 'imds2 location', 'imds3 location'},...
The intent is to combine the location of all of the datastore and make sure rest of the argument remain same.
Now just try using this datastore to train the network.
net= trainNetwork(cds , layers, options);
I hope this helps. Do reach out if you stuck anywhere. Although this should work.
  댓글 수: 1
Nidhi Sawant
Nidhi Sawant 2021년 5월 3일
@Shashank GuptaMatlab 2020b has released multi label classification example as below.
However when the task is to peform multi-label classification on huge image dataset, then there is a need to create image datastore.
The problem is we cannot create a imagedatastore with multi-label classification. Could you please enlighten on this? Thanks.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by