% Initialize algorithm.
% Copyright (2017): Zhenwei Shi, Zhengxia Zou, Beihang University.
% Date: Mar., 2017
% Version: 1.0

clear all; close all; clc;

display('initializing networks...')

% set gpu mode: true/false
Opts.gpuMode = true;

% set image resolution (m/pixel)
Opts.resolution = 1.0;

% set detection params
Opts.dete.minWSize = 171;
Opts.dete.maxWSize = 400;
Opts.dete.sStep = 1.3;
Opts.dete.oilpotThre = 0;
Opts.dete.shipThre = 0;
Opts.dete.planeThre = 0;

% set envi info extraction params
Opts.envExtr.pH = 360;
Opts.envExtr.pW = 480;
Opts.envExtr.stride = 120;

% set scene type evaluation params
Opts.scnEval.pH = 360;
Opts.scnEval.pW = 480;
Opts.scnEval.stride = 120;

% load imagenet-vgg-f and layer8weight
load('model\layer8weight.mat');
if(exist('model\imagenet-vgg-f.mat'));
    load('imagenet-vgg-f.mat');
else
    % Download a pre-trained CNN from the web (needed once).
    display('Downloading imagenet-vgg-f model from the web ');
    urlwrite(...
        'http://www.vlfeat.org/matconvnet/models/imagenet-vgg-f.mat', ...
        'imagenet-vgg-f.mat') ;
end

run  ../matlab/vl_setupnn;
net = vl_simplenn_tidy(net);

% set conv-layer's padding and stride
net.layers{1,8}.stride = [1,1];
net.layers{1,15}.stride = [1,1];
net.layers{1,1}.pad = [5,5,5,5];
net.layers{1,4}.pad = [1,1,1,1];
net.layers{1,8}.pad = [1,1,1,1];
net.layers{1,15}.pad = [1,1,1,1];
net.layers{1,16}.pad = [2,2,2,2];
net.layers = net.layers(1:end-2);

% if Opts.gpuMode==true, convert conv-filters to gpuArray
if(Opts.gpuMode)
    for i=1:length(net.layers)
        if(strcmp(net.layers{1,i}.type,'conv'))
            net.layers{1,i}.weights{1,1} = ...
                gpuArray(net.layers{1,i}.weights{1,1});
            net.layers{1,i}.weights{1,2} = ...
                gpuArray(net.layers{1,i}.weights{1,2});
        end
    end
end

display('initializing done.')