Error using vision.int​ernal.Kdtr​ee/index Invalid input class.

Error using bagOfFeatures. It was earlier working fine but suddenly it stopped. I am using custom extractor function.
extractorFcn= @calcFeatures;
bag = bagOfFeatures(trainingSet,'CustomExtractor',extractorFcn,'VocabularySize',500);
% function [T, TMetric] = calcFeatures(pic) Custom Extractor looks like this
The Error I get is:
Clustering...completed 0/100 iterations Error using vision.internal.Kdtree/index
Invalid input class.
Error in vision.internal.approximateKMeans>assignDataToClustersSerial (line 172)
searcher.index(centers);
Error in vision.internal.approximateKMeans>assignDataToClusters (line 153)
[assignments, dists, varargout{1:nargout-2}] = assignDataToClustersSerial(features, centers, randState);
Error in vision.internal.approximateKMeans (line 73)
[assignments, dists, isValid] = assignDataToClusters(features, centers, params);
Error in bagOfFeatures/createVocabulary (line 640)
clusterCenters = vision.internal.approximateKMeans(descriptors, K, ...
Error in bagOfFeatures (line 197)
this.Vocabulary = this.createVocabulary(descriptorSet, ...

11 Commenti

Hi, I am having the same issue. Did you manage to solve this problem?
Could you post code the reproduces the problem?
I am using CNN for custom feature extraction and using BoF for feature selection.
imageDir=fullfile(oldfolder,dataset,newfold,strip(list2(subfold,:)));
imds=imageDatastore(imageDir);
imds=imageDatastore(imageDir,'Labels',labelIds);
imds.ReadFcn= @(loc)imresize3(imread(loc),[227 227 3]);
extractorFcn = @exampleBagofFeaturesExtractor;
FeatureBag=bagOfFeatures(imds,'CustomExtractor',extractorFcn,'vocabularySize',100);
function [features,featureMetric] = exampleBagofFeaturesExtractor(patchimds)
%patchimds.ReadFcn = @(loc)imresize3(imread(loc),[227 227 3]);
net = alexnet;
layer = 'pool5';
trainFeatures = activations(net,patchimds,layer,'OutputAs','rows','ExecutionEnvironment','gpu');
features=reshape(trainFeatures,[size(trainFeatures,1)*size(trainFeatures,2)*size(trainFeatures,3),size(trainFeatures,4)]);
% features=features';
featureMetric=var(features,[],2);
end
Creating Bag-Of-Features.
-------------------------
* Image category 1: 1
* Image category 2: 2
* Image category 3: 3
* Image category 4: 4
* Extracting features using a custom feature extraction function: exampleBagofFeaturesExtractor.
* Extracting features from 43735 images...done. Extracted 403061760 features.
* Keeping 80 percent of the strongest features from each category.
* Balancing the number of features across all image categories to improve clustering.
** Image category 3 has the least number of strongest features: 35278848.
** Using the strongest 35278848 features from each of the other image categories.
* Using K-Means clustering to create a 100 word visual vocabulary.
Starting parallel pool (parpool) using the 'local' profile ...
IdleTimeout has been reached.
Parallel pool using the 'local' profile is shutting down.
connected to 8 workers.
* Number of features : 141115392
* Number of clusters (K) : 100
* Initializing cluster centers...100.00%.
* Clustering...completed 0/100 iterations Error using vision.internal.approximateKMeans>assignDataToClustersSerial (line
172)
Invalid input class.
Error in vision.internal.approximateKMeans>assignDataToClustersParallel (line
214)
parfor n = 1:size(featuresCube,3)
Error in vision.internal.approximateKMeans>assignDataToClusters (line 151)
[assignments, dists, varargout{1:nargout-2}] =
assignDataToClustersParallel(features, centers, randState);
Error in vision.internal.approximateKMeans (line 73)
[assignments, dists, isValid] = assignDataToClusters(features, centers,
params);
Error in bagOfFeatures/createVocabulary (line 639)
clusterCenters = vision.internal.approximateKMeans(descriptors, K,
...
Error in bagOfFeatures (line 197)
this.Vocabulary = this.createVocabulary(descriptorSet, ...
Is there a set of example folders to run this on?
the dataset is not publicly available, so I cannot share it. You can run this on any dataset like caltech101 for testing.
I must also mention that this same code was working fine few days ago, but unusually, this error started to occur now. Also, if I transpose the features matrix _ features_ (like I used to do earlier), then It is reducing the number of samples instead of trimming the feature vector. And consequently encoding feature vectors is giving me only ones and zeros. I am perplexed because the same methodology had been working well few days back. I am desperate for help.
Unfortunately current versions of MATLAB require a GPU driver that is later than any compatible with my operating system.
I was able to pull up a version of MATLAB that was new enough to have alexnet and imresize3() but also still supported GPU on my OS.
I loaded down caltech101 and modified your code to use it. I encountered a problem in that some of the images in it are grayscale, including only the second grayscale .jpg image I have ever seen that was not just a proof of concept to show that such images were possible. But I encountered a problem / limitation with imresize3() that I had to work around.
Now my GPU is busy analayzing 9144 images, and will need to get through those before the kdtrees problem can possibly show up. And if I need to retest, it will need to re-analyze 9144 images again...
This is not exactly convenient for a volunteer running on a single 5 year old desktop.
I chopped out a lot of images and was able to trigger the input class problem you mention. Unfortunately it also triggered a MATLAB kernel crash, so I am trying again.
I have faced the problem in knn search when using alexnet features
* Clustering...completed 0/100 iterations Error using vision.internal.Kdtree/index
Invalid input class.
Error in vision.internal.approximateKMeans>assignDataToClustersSerial (line 172)
searcher.index(centers);
Error in vision.internal.approximateKMeans>assignDataToClusters (line 153)
[assignments, dists, varargout{1:nargout-2}] = assignDataToClustersSerial(features, centers, randState);
Error in vision.internal.approximateKMeans (line 73)
[assignments, dists, isValid] = assignDataToClusters(features, centers, params);

Accedi per commentare.

Risposte (2)

Digging into this: that error is being triggered by the fact that the features array that vision.internal.approximateKMeans is being asked to work on is a single column; it is expecting a minimum of two columns.
I am still tracing through to figure out what the one column is at that point, and why it is not multiple columns.

2 Commenti

Your exampleBagofFeaturesExtractor uses activations('OutputAs','rows'), which is going to return either a vector or a 2D array. But the next line does
features=reshape(trainFeatures,[size(trainFeatures,1)*size(trainFeatures,2)*size(trainFeatures,3),size(trainFeatures,4)]);
which will reduce anything of less than 4 dimensions into a column vector.
There is a transpose in the next line, but it is commented out, so the output is a single column.
If the output were a row instead of a column, then that would have succeeded in the approximateKMeans -- just watch out for which dimension you are taking the feature metric relative to.
Yes, You are right. I ran the example code by MATLAB, and figured out the error yesterday. I corrected it by reshaping my array like this
features=reshape(trainFeatures,[size(trainFeatures,1)*size(trainFeatures,2) ,size(trainFeatures,3),size(trainFeatures,4)]);
This worked for me. Thank you for your time.

Accedi per commentare.

I got the same error when my features matrix was of size M-by-1 in the custom feature extractor function. I changed it to size 1-by-M then the error did not appear.

Richiesto:

il 16 Mar 2017

Commentato:

il 23 Nov 2019

Community Treasure Hunt

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

Start Hunting!

Translated by