When using a trained classifier for object detection in images using a Bag-of-Features approach, an error occurs stating "Unable to use a value of type cell as an index"

3 views (last 30 days)
I have a script for object detection in images using a trained classifier and a Bag-of-Features method. When attempting to apply the trained classifier to a test image, I receive the error "Unable to use a value of type cell as an index". How can I resolve this error and successfully apply the classifier to the test image? I already exported from the classificationLearner, therefore there is a % in the script.
Thank you.

Answers (1)

Drew
Drew on 12 Apr 2024
You have not really provided all the details, but it looks like perhaps there is some mismatch between the format of the features that are being passed to trainedClassifier.predictFcn(imagefeatures), and the format of the features used to train the model in Classification Learner. Were the features in a table, or a matrix, in Classification Learner? On the doc page https://www.mathworks.com/help/stats/export-classification-model-for-use-with-new-data.html , regarding using trainedModel.predictFcn with a model from Classification Learner:
Supply the data T with the same format and data type as the training data used in the app (table or matrix).
  • If you supply a table, ensure it contains the same predictor names as your training data. The predictFcn function ignores additional variables in tables. Variable formats and types must match the original training data.
  • If you supply a matrix, it must contain the same predictor columns or rows as your training data, in the same order and format. Do not include a response variable, any variables that you did not import in the app, or other unused variables.
If this answer helps you, please remember to accept the answer.
  1 Comment
Mirsad
Mirsad on 13 Apr 2024
Thank you for the response. I trained the model using Classification Learner and then exported it to the workspace (see attachment).
This is the command window:
>> ObjectIdentification
Creating Bag-Of-Features.
-------------------------
* Image category 1: Airpods
* Image category 2: Keys
* Image category 3: Marker
* Image category 4: Pencil
* Selecting feature point locations using the Detector method.
* Extracting SURF features from the selected feature point locations.
** detectSURFFeatures is used to detect key points for feature extraction.
* Extracting features from 110 images in image set 1...done. Extracted 6250 features.
* Extracting features from 105 images in image set 2...done. Extracted 5170 features.
* Extracting features from 104 images in image set 3...done. Extracted 5030 features.
* Extracting features from 116 images in image set 4...done. Extracted 6184 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: 4024.
** Using the strongest 4024 features from each of the other image categories.
* Creating a 200 word visual vocabulary.
* Number of levels: 1
* Branching factor: 200
* Number of clustering steps: 1
* [Step 1/1] Clustering vocabulary level 1.
* Number of features : 16096
* Number of clusters : 200
* Initializing cluster centers...100.00%.
* Clustering...completed 39/100 iterations (~0.08 seconds/iteration)...converged in 39 iterations.
* Finished creating Bag-Of-Features
Encoding images using Bag-Of-Features.
--------------------------------------
* Image category 1: Airpods
* Image category 2: Keys
* Image category 3: Marker
* Image category 4: Pencil
* Encoding 110 images from image set 1...done.
* Encoding 105 images from image set 2...done.
* Encoding 104 images from image set 3...done.
* Encoding 116 images from image set 4...done.
* Finished encoding images.
Encoding images using Bag-Of-Features.
--------------------------------------
* Encoding an image...done.
Unable to use a value of type cell as an index.
Error in mlearnapp.internal.model.DatasetSpecification>@(t)t(:,predictorNames) (line 173)
extractPredictorsFromTableFcn = @(t) t(:,predictorNames);
Error in mlearnapp.internal.model.DatasetSpecification>@(x)extractPredictorsFromTableFcn(x) (line 178)
predictorExtractionFcn = @(x) extractPredictorsFromTableFcn(x);
Error in mlearnapp.internal.model.DatasetSpecification>@(x)exportableModel.predictFcn(predictorExtractionFcn(x)) (line 182)
newExportableModel.predictFcn = @(x) exportableModel.predictFcn(predictorExtractionFcn(x));
Error in ObjectFinderOnImage (line 21)
labels = trainedClassifier.predictFcn(imagefeatures);
Error in ObjectIdentification (line 44)
ObjectFinderOnImage(trainedClassifier, bag, testImagePath);
>>
However, I'm unsure if this is correct. As a beginner in this field, any help or guidance is greatly appreciated. If there's anything I might have done wrong or misunderstood, please let me know.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!