How do I create an open class classifier

I am calling function kNN_model_add_class to trained and create one class call dogs.
How do I create an 'Open class' where there will be all the other sounds(i do not want to specify them just all the other)
I am trying to make One vs all classifier.. Dog or no Dog
Thanks
strDir = 'C:\Users\User\Desktop\3rd year\Nahravky\cats_dogs';
Statistics = {'mean', 'median', 'std', 'stdbymean', 'max', 'min'};
stWin = 0.040;
stStep = 0.040;
mtWin = 2;
mtStep = 1;
kNN_model_add_class('Mfcctrain.mat', 'dogs', [ strDir '\trainpes'], ...
Statistics, stWin, stStep, mtWin, mtStep);
function kNN_model_add_class
function kNN_model_add_class(modelName, className, classPath, ...
listOfStatistics, stWin, stStep, mtWin, mtStep)
%
% function kNN_model_add_class(modelName, className, classPath, ...
% listOfStatistics, stWin, stStep, mtWin, mtStep)
%
% This function adds an audio class to the kNN classification model
%
% ARGUMENTS;
% - modelName: the filename of the model (mat file)
% - className: the name of the audio class to be added to the model
% - classPath: the path of the directory where the audio segments of the
% new class are stored
% - listOfStatistics: list of mid-term statistics (cell array)
% - stWin, stStep: short-term window size and step
% - mtWin, mtStep: mid-term window size and step
%
% Example:
% kNN_model_add_class('modelSpeech.mat', 'speech', './Music/', ...
% {'mean','std',}, 0.050, 0.025, 2.0, 1.0);
%
if ~exist(classPath,'dir')
error('Audio sample path is not valid!');
else
classPath = [classPath filesep];
end
% check if the model elaready exists:
fp = fopen(modelName, 'r');
if fp>0 % check if file already exists
load(modelName);
end
% Feature extraction:
D = dir([classPath '*.wav']);
F = [];
for (i=1:length(D)) % for each wav file in the given path:
curFileName = [classPath D(i).name];
FileNamesTemp{i} = curFileName;
% mid-term feature extraction for each wav file:
[data, fs] = audioread(curFileName);
signal = struct('Filt_data', data, 'SampleRate', fs);
midFeatures = featureExtractionFile(signal, ...
stWin, stStep, mtWin, mtStep, listOfStatistics);
% midFeatures = featureExtractionFile(curFileName, ...
% stWin, stStep, mtWin, mtStep, listOfStatistics);
% long-term averaging:
longFeatures = mean(midFeatures,2);
F = [F longFeatures];
end
% save the model:
Statistics = listOfStatistics;
fp = fopen(modelName, 'r');
if fp<0 % model does not exist --> generate
ClassNames{1} = className;
Features{1} = F;
FileNames{1} = FileNamesTemp;
save(modelName, 'ClassNames', 'Features', ...
'Statistics', 'stWin', 'stStep', 'mtWin', 'mtStep', 'FileNames');
else
load(modelName);
ClassNames{end+1} = className;
Features{end+1} = F;
FileNames{end+1} = FileNamesTemp;
save(modelName, 'ClassNames', 'Features', ...
'Statistics', 'stWin', 'stStep', 'mtWin', 'mtStep', 'FileNames');
end

댓글 수: 6

Note in the else for if fp<0 you should fclose(fp) .
You should consider instead using
exist(modelName, 'file')
Instead of line
load(modelName);
i should use
exist(modelName, 'file')
so it creates an open class ?
function kNN_model_add_class(modelName, className, classPath, ...
listOfStatistics, stWin, stStep, mtWin, mtStep)
%
% function kNN_model_add_class(modelName, className, classPath, ...
% listOfStatistics, stWin, stStep, mtWin, mtStep)
%
% This function adds an audio class to the kNN classification model
%
% ARGUMENTS;
% - modelName: the filename of the model (mat file)
% - className: the name of the audio class to be added to the model
% - classPath: the path of the directory where the audio segments of the
% new class are stored
% - listOfStatistics: list of mid-term statistics (cell array)
% - stWin, stStep: short-term window size and step
% - mtWin, mtStep: mid-term window size and step
%
% Example:
% kNN_model_add_class('modelSpeech.mat', 'speech', './Music/', ...
% {'mean','std',}, 0.050, 0.025, 2.0, 1.0);
%
if ~exist(classPath,'dir')
error('Audio sample path is not valid!');
else
classPath = [classPath filesep];
end
% check if the model elaready exists:
fp = fopen(modelName, 'r');
if fp>0 % check if file already exists
load(modelName);
end
% Feature extraction:
D = dir([classPath '*.wav']);
F = [];
for (i=1:length(D)) % for each wav file in the given path:
curFileName = [classPath D(i).name];
FileNamesTemp{i} = curFileName;
% mid-term feature extraction for each wav file:
[data, fs] = audioread(curFileName);
signal = struct('Filt_data', data, 'SampleRate', fs);
midFeatures = featureExtractionFile(signal, ...
stWin, stStep, mtWin, mtStep, listOfStatistics);
% midFeatures = featureExtractionFile(curFileName, ...
% stWin, stStep, mtWin, mtStep, listOfStatistics);
% long-term averaging:
longFeatures = mean(midFeatures,2);
F = [F longFeatures];
end
% save the model:
Statistics = listOfStatistics;
if ~exist(modelName, 'file') % model does not exist --> generate
ClassNames{1} = className;
Features{1} = F;
FileNames{1} = FileNamesTemp;
save(modelName, 'ClassNames', 'Features', ...
'Statistics', 'stWin', 'stStep', 'mtWin', 'mtStep', 'FileNames');
else
load(modelName);
ClassNames{end+1} = className;
Features{end+1} = F;
FileNames{end+1} = FileNamesTemp;
save(modelName, 'ClassNames', 'Features', ...
'Statistics', 'stWin', 'stStep', 'mtWin', 'mtStep', 'FileNames');
end
This will not create an open class: it will just fix a bug in the code.
Kamil Kacer
Kamil Kacer 2020년 11월 18일
Thanks but how to i create an open class
You do not create an open class. A specific open class would have to be trained as having particular characteristics, and anything that did not have those characteristics would not match.
Instead you define a threshold for matching each of your defined classes, and if the image does not meet the threshold for any of the defined classes then you say that it is Other.

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

태그

질문:

2020년 11월 18일

댓글:

2020년 11월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by