Perturbing an image datastore for bag of words
    10 views (last 30 days)
  
       Show older comments
    
I am trying to apply pertubations (eg increasing brightness) to every image in a datastore. I am using transform( imds, @(x) function(x)) and this works but outputs a TransformedDatastore. When using the bagOfFeatures encode function I get an error saying that the function was expecting an imageDatastore. Is there a way to convert TransformedDatastore to imageDatastore for use in encode() function, or should I be using a different function to creat my bag of words? 
function [class_av] = model_eval(class_type,model,test_set,perturb_ft, bag)
% Take a model and evaluate it on a perturbed test set 
% Return an array of classification averages for the perturbed set on that
% model:
% Type
% eg enter model_eval('R',net1,imds_C,@inc_brightness, @Anything random@)
% eg model_eval('S', SVM1, imds_C , @inc_brightness, bag1)
%
class_av = zeros(1,10);
p = test_set;
test_labels = test_set.Labels;
test_files = numel(test_set.Files);
for i=1:10
    if class_type == 'R'
        [YPred,probs] = classify(model,p);
        accuracy = mean(YPred == test_labels);
        disp(accuracy);
        class_av(i) = accuracy;
        p = transform(p,@(x) perturb_ft(x));
    elseif class_type == 'S'
        %Encode the test set with BOVW
        [featureVector, words_val] = encode(bag, p);
        %Make predictions
        label = predict(model, featureVector);
        %Calculate classification accuracy
        score = sum(label == test_labels) / test_files;
        %Append score
        class_av(i) = score;
        disp(score);
        p = transform(p,@(x) perturb_ft(x));     
    else 
        warning('Please choose R or S')
    end
end
end
0 Comments
Answers (1)
  Jayanti
 on 15 May 2025
        Hi Megan,
You can manually apply perturbations and save them to a temporary folder, then create a new "imageDatastore" from that folder for use with encode().
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
