%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Released under the MIT License.
% If you use this code, please cite the following paper:
% Mahmoud Afifi, Abdelrahman Abdelhamed, Abdullah Abuolaim, Abhijith 
% Punnappurath, and Michael S Brown. CIE XYZ Net: Unprocessing Images for 
% Low-Level Computer Vision Tasks. arXiv preprint, 2020.
%
% Author: Mahmoud Afifi | Email: mafifi@eecs.yorku.ca, m.3afifi@gmail.com
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function options = get_training_options(epochs,miniBatch,lR,...
    checkpoint_dir,validation_data, validation_frequency, ...
    GPUDevice, L2Reg, checkpoint_period)

if nargin == 6
    L2Reg = 1.000e-7;
    checkpoint_period = 5;
elseif nargin == 7
    checkpoint_period = 5;
end
gpuDevice(GPUDevice);

if exist(checkpoint_dir,'dir') == 0
    mkdir(checkpoint_dir);
end

options = trainingOptions('adam', ...
    'InitialLearnRate',lR, ...
    'L2Regularization',L2Reg, ...
    'MaxEpochs',epochs, ...
    'MiniBatchSize',miniBatch, ...
    'CheckpointPath',checkpoint_dir, ...
    'Shuffle','once',...%'every-epoch', ...
    'VerboseFrequency',1,...
    'LearnRateSchedule','piecewise',...
    'LearnRateDropPeriod',75,... %drop learning rate every 250 epochs
    'LearnRateDropFactor',0.5,... %drop by 0.5
    'Plots','none',...%'training-progress',... 
    'ExecutionEnvironment','gpu',...
    'GradientDecayFactor',0.9,... 
    'SquaredGradientDecayFactor' ,0.999,... 
    'ValidationData',validation_data,...
    'ValidationFrequency',validation_frequency, ... 
    'OutputFcn',@(info)plotTrainingAccuracy(info,checkpoint_dir,...
    checkpoint_period));
