Create a multiclass SVM classification with templateSVM and a custom kernel

5 Ansichten (letzte 30 Tage)
hi to everybody,
I would like to build a multiclass SVM classificator (20 different classes) using templateSVM() and chi_squared kernel, but I don't know how to define the custom kernel: I tryin the folowing way:
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function gram = compute_gram_matrix(U,V)
[sample_U,~] = size(U);
[sample_V,~] = size(V);
gram = zeros(sample_U,sample_V);
for r=1:sample_U
for t =r:sample_V
temp = chi_squared_kernel(U(r,:),V(t,:));
gram(r,t) = temp;
gram(t,r) = temp;
end
end
% calcolo la media
mean = 0;
for i=1:size(gram,1)
for j=1:size(gram,2)
if i <= j
mean = mean + gram(i,j);
else
continue
end
end
end
mean = mean / ((sample_U^2 + sample_U)/2);
gram = exp(-gram/mean);
end
function value = chi_squared_kernel(hist_1,hist_2)
value = 0;
k = size(hist_1,1);
for i=1:k
if hist_1(i)==0 && hist_2(i)==0
continue
else
value = value + (hist_1(i) - hist_2(i))^2 / (hist_1(i) + hist_2(i));
end
end
value = 0.5 * value;
end
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
but it says that the kernel is not of the correct type: How can I do to solve this problem?

Antworten (1)

Shashank Gupta
Shashank Gupta am 18 Nov. 2020
Hi Alberto,
I just took your gram matrix and able to define it properly, can you elaborate what you all did? Just for the reference I will attach a piece of code which I used and it worked.
% take some data.
load fisheriris
% define SVM
t = templateSVM('KernelFunction','compute_gram_matrix');
% Specify template t to binary leaner.
Mdl = fitcecoc(meas,species,'Learners',t);
I took your "compute_gram_matrix" function and able to execute it properly without fail. Check out this and let me know if it make sense.
Cheers.
  1 Kommentar
Alberto Presta
Alberto Presta am 18 Nov. 2020
Bearbeitet: Alberto Presta am 18 Nov. 2020
thanks for your time.
I am very new with Matlab. I want to buid a multiclass svm classificator with custom kernel (I have 20 different species to classify).
1-I extract dense descriptors (Dense sift descriptors) foe each image and group all toghether with bag of visual words tech (300 words).
2. I extract histograms of bag of visual words and I want to use them to fit my chi-sqaured kernel svm model.
I attached main code here:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
extractor = @phowFeatures; % function which exracts dense descriptors
bag = bagOfFeatures(trainingSet, 'CustomExtractor',extractor,'VocabularySize',300);
[trainingSet, validationSet] = splitEachLabel(imds, 0.70, 'randomize');
opts = templateSVM('KernelFunction', 'compute_gram_matrix', 'BoxConstraint', SVM_C, 'kernelScale', SVM_RBF_Gamma);
classifier = trainImageCategoryClassifier(trainingSet, bag, 'LearnerOptions', opts);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
My question is : can I use fitcecoc also for multiclass svm?

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by