
function [ net ] = get_test_ms(varargin)
warning off;
addpath(genpath('src\'));
addpath(genpath('Simulate_Data\'));
% cpu and gpu settings 
opts.idx_gpus = 1; % 0: cpu
opts.num_images = 32; % images to be generated
opts.matconvnet_path = 'matconvnet-1.0-beta24/matlab/vl_setupnn.m';

opts.net_path = 'net/net-epoch-100.mat'; 
% opts.net_path = strcat('net/net-epoch-',num2str(j),'.mat') ;
Category = 1;
sigma     =  0;
stripe_min     = -50;        %%% min value
stripe_max     = 50;         %%% max value
% stripe_min     = 0.8;        %%% min value
% stripe_max     = 1.2;         %%% max value
showResult  = 0;
pauseTime = 1;
opts = vl_argparse(opts, varargin);

run(opts.matconvnet_path);

%% load network
net = load(opts.net_path);
net = net.net(1); % idx 1: Generator, 2: Discriminator
net = dagnn.DagNN.loadobj(net);
net.mode = 'test';

if opts.idx_gpus >0
%     gpuDevice()
    net.move('gpu');
end

folderTest  = 'testsets\Set12'; %%% test dataset
% folderTest  =  'testsets\DOTA';
ext         =  {'*.jpg','*.png','*.bmp','*.tif','*.tiff'};
filePaths   =  [];
for i = 1 : length(ext)
    filePaths = cat(1,filePaths, dir(fullfile(folderTest,ext{i})));
end

%%% PSNR and SSIM
PSNRs = zeros(1,length(filePaths));
SSIMs = zeros(1,length(filePaths));

for i = 1:length(filePaths)
    %%% read images
    label = imread(fullfile(folderTest,filePaths(i).name));
    row = floor(size(label,1)/8)*8;
    col = floor(size(label,2)/8)*8;
    label = label(1:row,1:col,:) ; 

    if size(label,3) == 3
        label = rgb2gray(label);
    end
    
    label = im2double(label);

    if Category == 1  
        stripe     =  repmat((stripe_max - stripe_min).*rand(1,size(label,2)) + stripe_min,size(label,1),1);
        stripe(:,randperm(size(label,2),fix(size(label,2)*0))) = 0;
    elseif Category == 2
        stripe = zeros(size(label));
        k = randperm(size(label,2),fix(size(label,2)*0.15));
        for c = 1:numel(k)
            leng = randperm(size(label,1),1);
            temp   = repmat((stripe_max - stripe_min).*rand + stripe_min,leng,1);
            begin  = randperm(size(label,1)-leng+1,1);
            stripe(begin:begin+leng-1,k(c))      = temp;
        end
    elseif Category == 3
        perio = 10;
        stripe      = zeros(size(label));
        stripe(:,1:perio*floor(size(label,2)/perio))      = single(repmat((stripe_max - stripe_min).*rand(1,perio) + stripe_min,size(label,1),floor(size(label,2)/perio)));
    elseif Category == 4
        if randn(1)>0
            stripe_value      = diag((stripe_max - stripe_min).*rand(1,size(label,2)) + stripe_min);
            k = randperm(size(label,2),fix(size(label,2)/2));
            stripe_value(k,k)     = 0;
            aa = [30:60]; theta = aa(randperm(30,1));
            kernel = fspecial('motion', 1024, theta);
            stripe = 255*10*imfilter(stripe_value,kernel);
        else
            stripe_value      = fliplr(diag((stripe_max - stripe_min).*rand(size(label,1),1) + stripe_min));
            k = randperm(size(label,2),fix(size(label,2)/2));
            stripe_value(k,size(label,2)+1-k)     = 0;
            aa = [120:150]; theta = aa(randperm(30,1));
            kernel = fspecial('motion', 1024, theta);
            stripe = 255*10*imfilter(stripe_value,kernel);
        end
    elseif Category == 5
        coe      = single(repmat((stripe_max - stripe_min).*rand(1,size(label,2)) + stripe_min,size(label,1),1));
        k = randperm(size(label,2),fix(size(label,2)*0.75));
        coe(:,k) = 1;
        noise   = label .* coe;
        stripe = 255*(noise - label);
    elseif Category == 6
        k = randperm(size(label,2),fix(size(label,2)*0.5));
        stripe      = ones(size(label));
        stripe(:,k) = 0;
        mask = stripe;
    end

%    noise1   = single(label .* stripe);
%    noise = cat(3,noise1,mask);
   noise   = label +   stripe/255 + single(sigma/255*randn(size(label)));
    if opts.idx_gpus >0,   noise = gpuArray(noise);    end
    net.eval({'input',noise});
    im_out = gather(net.vars(net.getVarIndex('conv62')).value);
%     output = noise - im_out;
    output = im_out;
    if opts.idx_gpus 
        output = gather(output);
        noise  = gather(noise);
    end  

    %%% calculate PSNR and SSIM
    [PSNRCur, SSIMCur] = Cal_PSNRSSIM(im2uint8(label),im2uint8(output),0,0);
    fprintf( sprintf('%s : PSNR_TSWEU = %2.2f  SSIM_TSWEU = %2.4f \n', filePaths(i).name, PSNRCur, SSIMCur) );
    if showResult
        figure, imshow(cat(2,im2uint8(label),im2uint8(noise),im2uint8(output)));
        title([filePaths(i).name,'    ',num2str(PSNRCur,'%2.2f'),'dB','    ',num2str(SSIMCur,'%2.4f')])
        drawnow;
        pause(pauseTime)
    end
    PSNRs(i) = PSNRCur;
    SSIMs(i) = SSIMCur;
end

fprintf( sprintf('%s : Mean_PSNR_TSWEU = %2.2f  Mean_SSIM_TSWEU = %2.4f \n', 'Mean_TSWEU', mean(PSNRs), mean(SSIMs)) );
