function [Acc, model] = svm_gender(X_train, Y_train, X_val, Y_val, X_test, Y_test)


%% Extract LBP features

feat_train = extract_LBP(X_train);
feat_val = extract_LBP(X_val);
feat_test = extract_LBP(X_test);

%% Train SVM

% set validation data
V{1} = feat_val;
V{2} = Y_val;

% train the model using training data, validate on validation data
disp('Training the model');
model = fitclinear(feat_train, Y_train, 'ValidationData', V);

%% Testing

% predict the label of testing data
disp('Testing the model');
pred = predict(model, feat_test);

% calculate the accuracy on testing set
Acc = sum(pred == Y_test) / length(pred);
disp(['Acc = ', num2str(Acc)]);


function feat = extract_LBP(X)

disp('Extracting Features');

feat = zeros(size(X, 1), 59);
mapping = getmapping(8, 'u2');
for i = 1 : size(X, 1)
    I = squeeze(X(i, :, :));
    feat(i, :) = LBP(I, 1, 8, mapping, 'nh');
end