How to change input values for weight classfication layer.

I am using weigth classfication fucntion which given as example in MATALAB documentaion.
But whenI use it in my network it gives error "Error using 'backwardLoss' in Layer weightedClassificationLayer. The function threw an error and could not be executed". I think the error is due to input value but i am not sure where to change these valuse. The weighted classification function works well according to input valuse assigned in example.
the code I am using for weighted classification function
%%%%%%
classdef weightedClassificationLayer < nnet.layer.ClassificationLayer
properties
% Row vector of weights corresponding to the classes in the
% training data.
ClassWeights
end
methods
function layer = weightedClassificationLayer(classWeights, name)
% layer = weightedClassificationLayer(classWeights) creates a
% weighted cross entropy loss layer. classWeights is a row
% vector of weights corresponding to the classes in the order
% that they appear in the training data.
%
% layer = weightedClassificationLayer(classWeights, name)
% additionally specifies the layer name.
% Set class weights.
layer.ClassWeights = classWeights;
% Set layer name.
if nargin == 2
layer.Name = name;
end
% Set layer description
layer.Description = 'Weighted cross entropy';
end
function loss = forwardLoss(layer, Y, T)
% loss = forwardLoss(layer, Y, T) returns the weighted cross
% entropy loss between the predictions Y and the training
% targets T.
N = size(Y,4);
Y = squeeze(Y);
T = squeeze(T);
W = layer.ClassWeights;
loss = -sum(W*(T.*log(Y)))/N;
end
function dLdY = backwardLoss(layer, Y, T)
% dLdX = backwardLoss(layer, Y, T) returns the derivatives of
% the weighted cross entropy loss with respect to the
% predictions Y.
[~,~,K,N] = size(Y);
Y = squeeze(Y);
T = squeeze(T);
W = layer.ClassWeights;
dLdY = -(W'.*T./Y)/N;
dLdY = reshape(dLdY,[1 1 K N]);
end
end
end

 採用された回答

Pujitha Narra
Pujitha Narra 2019 年 10 月 11 日

0 投票

This is a way to initialize 'classWeights'
classWeights = 1./countcats(YTrain);
classWeights = classWeights'/mean(classWeights);
and you can use it here:
Network = [
imageInputLayer([256 256 3],"Name","imageinput")
convolution2dLayer([3 3],2,"Name","conv","Padding","same")
reluLayer("Name","relu")
softmaxLayer("Name","softmax")
weightedClassificationLayer(classWeights)
];
I think this should solve the problem.

6 件のコメント

Raza Ali
Raza Ali 2019 年 10 月 12 日
編集済み: Raza Ali 2019 年 10 月 12 日
can you explain here two terms: YTrain and Classweight.
what does these term means here?
what should be the size of class weights?
i am getting same error "Error using 'backwardLoss' in Layer weightedClassificationLayer. The function threw an error and could not be executed."
Raza Ali
Raza Ali 2019 年 10 月 12 日
Layer Name Activation
imageInputLayer([256 256 3] 256 256 3
convolution2dLayer([3 3],2,"Name","conv","Padding","same") 256 256 2
reluLayer("Name","relu") 256 256 2
softmaxLayer("Name","softmax") 256 256 2
weightedClassificationLayer(classWeights)
Now what should I set class weights....to avoid this error "Error using 'backwardLoss' in Layer weightedClassificationLayer. The function threw an error and could not be executed."
Pujitha Narra
Pujitha Narra 2019 年 10 月 14 日
'classWeights' are the weights you want to set the layer(weightedClassificationLayer) with. The number of weights(the size of 'classWeights') is defined by the training data.
'YTrain' is the set of all the labels/classes in your training data set.
You can go through this example for further clarification.
Raza Ali
Raza Ali 2019 年 10 月 14 日
Thank you for your help and sharing this link. I have been to this link, but my problem is still un solved.
I tried different valuse of Class weigths but getting same error again and again,
Let me explain you through example.
I have two classes A and B. both folders has 2500 images of 256 by 256 by 3 (in total 5000 images in two different classes).
I have tried following classweights.
  1. classWeights = rand(1,2)
  2. classWeights = rand(1,2500)
  3. classWeights = rand(1,5000)
  4. classWeights = rand(1,196608) % 256*256*3=196608
but no vlaues of classweight is working... Error using 'backwardLoss' in Layer weightedClassificationLayer. The function threw an error and could not be executed."
Pujitha Narra
Pujitha Narra 2019 年 10 月 14 日
Can you share the code your are using?
Raza Ali
Raza Ali 2019 年 10 月 14 日
I am using two different image types( two classes A and B). Each Image has size: 256 by 256 by 3
%%%Start
imds = imageDatastore('Images','IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
YTrain=imdsTrain.Labels;
YTrain = removecats(YTrain);
classWeights = 1./countcats(YTrain)
classWeights = classWeights'/mean(classWeights)
Network = [
imageInputLayer([256 256 3],"Name","data")
convolution2dLayer([3 3],16,"Name","conv1","BiasLearnRateFactor",2,"Stride",[4 4])
reluLayer("Name","relu1")
crossChannelNormalizationLayer(5,"Name","norm1","K",1)
maxPooling2dLayer([3 3],"Name","pool1","Stride",[2 2])
convolution2dLayer([3 3],32,"Name","conv","Padding","same")
reluLayer("Name","relu5")
maxPooling2dLayer([3 3],"Name","pool5","Stride",[2 2])
fullyConnectedLayer(2,"Name","fc8","BiasLearnRateFactor",2)
softmaxLayer("Name","prob")
weightedClassificationLayer("classWeights")
];
Options = trainingOptions('sgdm', ...
'MiniBatchSize',5, ...
'MaxEpochs',3, ...
'Shuffle','every-epoch', ...
'InitialLearnRate',1e-4, ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',2100, ...
'Verbose',true, ...
'Plots','training-progress');
TrainedNetwork = trainNetwork(imdsTrain,Network,Options);

サインインしてコメントする。

その他の回答 (2 件)

Pujitha Narra
Pujitha Narra 2019 年 10 月 10 日

0 投票

Hi Raza Ali,
Can you mention how are you using 'weightedClassificationLayer' in your network? Assuming you want to know the inputs to the constructor of this class:
'classWeights' and the layer's 'name' are the only inputs.
'classWeights'-. classWeights is a row vector of weights corresponding to the classes in the order that they appear in the training data.
'name' -additionally specifies the layer name.
Also this example might be of help
Hope this helps!

8 件のコメント

Raza Ali
Raza Ali 2019 年 10 月 10 日
Thank you.
I am using this after softmax layer:
Network= [
imageInputLayer([256 256 3],"Name","imageinput")
convolution2dLayer([3 3],64,"Name","conv_1","BiasLearnRateFactor",2,"Padding","same")
reluLayer("Name","relu_1")
transposedConv2dLayer([3 3],2,"Name","transposed-conv","Cropping","same")
softmaxLayer("Name","softmax")
weightedClassificationLayer("ClassificationLayer") ];
my input size is 256 by 256 by 3.
Pujitha Narra
Pujitha Narra 2019 年 10 月 10 日
In the last line, when calling the constructor 'weightedClassificationLayer()', 'classWeights' are missing in the inputs. 'classWeights' are those which customize the classification layer according to your requirements.
Raza Ali
Raza Ali 2019 年 10 月 10 日
Here is the problem, these classweights has to be taken from softmax layer.
Raza Ali
Raza Ali 2019 年 10 月 10 日
I recive this Error
Error using trainNetwork (line 170)
Error using 'backwardLoss' in Layer weightedClassificationLayer. The function threw an error and could not be executed.
Pujitha Narra
Pujitha Narra 2019 年 10 月 11 日
Hi Raza,
What do you mean when you say, you want to take classWeights from softmax layer?
Raza Ali
Raza Ali 2019 年 10 月 11 日
I just want to use weightClassfication layer in simple CNN layer as output layer. and my image size is 256 x 256 x 3.
The nnetwork configurations i have mentioned in question. but when i use it gives error.
Error using 'backwardLoss' in Layer weightedClassificationLayer. The function threw an error and could not be executed.
Raza Ali
Raza Ali 2019 年 10 月 11 日
Network = [
imageInputLayer([256 256 3],"Name","imageinput")
convolution2dLayer([3 3],2,"Name","conv","Padding","same")
reluLayer("Name","relu")
softmaxLayer("Name","softmax")
weightedClassificationLayer('classWeights')
];
evelyn
evelyn 2024 年 4 月 29 日
'ClassWeights', classWeights is a row vector of weights corresponding to the classes in the order that they appear in the training data.
how about the train data is shuffle? how to do that?

サインインしてコメントする。

Ashwin
Ashwin 2022 年 7 月 13 日

0 投票

Try to use classWeights' instead of classWeights
And check if it works

カテゴリ

ヘルプ センター および File ExchangeDeep Learning Toolbox についてさらに検索

質問済み:

2019 年 10 月 7 日

コメント済み:

2024 年 4 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by