res = PrunedImgNetDetectorSet;
models_folder = 'models/';
addpath('/afs/cs/group/vision/aifiles/yongwhan/CURIS/08_08_10/OBFeatCode/');
classes = {'badminton', 'rowing', 'sailing', 'snowboarding'};

num_models = 50;
input_folder = 'images/';
output_folder = 'response_maps/';
input_files = cell(0, 0);
output_files = cell(0, 0);

for i=1:length(classes)
    files = arrayfun(@(x) x.name, dir([input_folder classes{i} '/train/*.jpg']), 'UniformOutput', false);
    input_files(end+1:end+length(files), 1) = cellfun(@(x) [input_folder classes{i} '/train/' x], files, 'UniformOutput', false);
    output_files(end+1:end+length(files), 1) = cellfun(@(x) [output_folder classes{i} '/train/' x(1:end-4)], files, 'UniformOutput', false);
    
    files = arrayfun(@(x) x.name, dir([input_folder classes{i} '/test/*.jpg']), 'UniformOutput', false);
    input_files(end+1:end+length(files), 1) = cellfun(@(x) [input_folder classes{i} '/test/' x], files, 'UniformOutput', false);
    output_files(end+1:end+length(files), 1) = cellfun(@(x) [output_folder classes{i} '/test/' x(1:end-4)], files, 'UniformOutput', false);
end

for i=1:num_models
    disp([num2str(i) ' of ' num2str(num_models)]);
    load([models_folder num2str(res{i}) '_final.mat']);
    thresh = model.thresh; model_name = num2str(res{i});
    parfor j=1:length(input_files)
        output_file = [output_files{j} '_' model_name '.mat'];
        if(~exist(output_file, 'file'))
            input_file = input_files{j};
            img = imread(input_file);
            [~, response_maps] = detect_with_responsemap(img, model, thresh);
            make_dir(output_file);
            parsaveResponseMaps(output_file, response_maps);
        end
    end
end