res = PrunedImgNetDetectorSet;
classes = {'badminton', 'rowing', 'sailing', 'snowboarding'};

num_models = 50;
models = res(1:num_models);
input_folder = 'images/';
response_maps_folder = 'response_maps/';
train_files = cell(0, 0);
train_labels = [];

test_files = cell(0, 0);
test_labels = [];

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

pyramidLevels = 3;

Train_OBFeatures = CompileOB(train_files, models, pyramidLevels);
Test_OBFeatures = CompileOB(test_files, models, pyramidLevels);

%% Train a linear SVM using liblinear
addpath('liblinear-1.7/matlab/');
% Liblinear options to use:
% -s 2
% Set bias to 1
% Set C value to 10
svm_options = '-s 2 -B 1 -c 0.1';
model = train(train_labels, sparse(Train_OBFeatures), svm_options);
[predicted_labels, accuracy] = predict(test_labels, sparse(Test_OBFeatures), model);

expected_accuracy = 85;
disp(['Expected Accuracy: ' num2str(expected_accuracy) '%']);

if(accuracy<expected_accuracy)
    disp('Try again! Did you normalize each histogram to sum to 1? Did you use the correct SVM parameters?');
else
    disp('Good job! Move on to the next part!')
end