% The demo.m is from the following paper:

% L. Zhang and D. Zhang,"SVM and ELM: Who Wins? Object Recognition with Deep Convolutional Features from ImageNet," arXiv:1506.02509, 2015.

% Please contact us by the following email: 

% leizhang@cqu.edu.cn

clc, clear all, close all;
% load data
% randomly select 20 samples per class from source domain (amazon and caltech data)
% randomly select 8 samples per class from source domain (webcam and dslr data)
% randomly select 3 labeled training samples per class from target domain

addpath([cd '/DataSplitsOfficeCaltech']);
addpath([cd '/LS-SVMLab-1.7']);
addpath([cd '/libsvm-3.12/matlab']);

Class=10; % classes

norm_type=2;

fprintf('-------------Start generating data------------- \n');  
load('DeCAF_Data.mat');
% normalization
%
amazon_data{1}=amazon_data{1}./repmat(sum(amazon_data{1},2),1,size(amazon_data{1},2)); % layer 6
amazon_data{2}=amazon_data{2}./repmat(sum(amazon_data{2},2),1,size(amazon_data{2},2)); % layer 7
dslr_data{1}=dslr_data{1}./repmat(sum(dslr_data{1},2),1,size(dslr_data{1},2));
dslr_data{2}=dslr_data{2}./repmat(sum(dslr_data{2},2),1,size(dslr_data{2},2));
caltech_data{1}=caltech_data{1}./repmat(sum(caltech_data{1},2),1,size(caltech_data{1},2));
caltech_data{2}=caltech_data{2}./repmat(sum(caltech_data{2},2),1,size(caltech_data{2},2));
webcam_data{1}=webcam_data{1}./repmat(sum(webcam_data{1},2),1,size(webcam_data{1},2));
webcam_data{2}=webcam_data{2}./repmat(sum(webcam_data{2},2),1,size(webcam_data{2},2));
%}
SD=amazon_data;SD_label=amazon_label;
TD=dslr_data;TD_label=dslr_label;

load('SameCategory_amazon-dslr_20RandomTrials_10Categories.mat')

layer_num=7;% 7 or 6
setting=2; % 2 or 3

for i=1:20 % 20 random splits
    Xs=SD{layer_num-5}(train.source{i},:)';source_label=SD_label(train.source{i});
    Xs_test=SD{layer_num-5}(test.source{i},:)';source_label_test=SD_label(test.source{i});
    Xt=TD{layer_num-5}(train.target{i},:)';target_label_train=TD_label(train.target{i});
    Xt_test=TD{layer_num-5}(test.target{i},:)';target_label_test=TD_label(test.target{i});

    % classifier
    if setting==2 % source_only
       Yat=source_label;%
       Xat=[Xs(:,1:length(source_label))]'; 
    elseif setting ==3 % source and target
          Yat=[source_label;target_label_train];%
          Xat=[Xs(:,1:length(source_label)) Xt(:,1:length(target_label_train))]'; 
    end

    Y=-1*ones(length(Yat),Class);
    for j=1:length(Yat)
        Y(j,Yat(j))=1;
    end

    %% nearest neighbor
    for p1=1:size(Xt_test,2)
        xt1=Xt_test(:,p1)';
        for p2=1:size(Xat,1)
            dista(p2)=(xt1-Xat(p2,:))*(xt1-Xat(p2,:))';
        end   
        [p,index]=min(dista);
        yte3(p1)=Yat(index);
    end

    rate1(i)=length(find(yte3'==target_label_test))/length(yte3)*100;
     
   %% SVM: the useage refers to the svmtrain.c file in libsvm toolbox
    model1 = svmtrain(Yat, Xat, '-c 1000 -g 1 -b 1 -t 0'); % -c:penalty coefficient;-t:kernel type;-g:gamma kernel parameter
    [ytest1, test_accuracy, prob_estimates] = svmpredict(target_label_test,Xt_test', model1, '-b 1');
    rate2(i)=test_accuracy(1);

   %% LSSVM:
    model=initlssvm(Xat,Yat,'classifier',[],[],'RBF_kernel','orginal');% 'RBF_kernel','lin_kernel','MLP_kernel','poly_kernel'
    model=tunelssvm(model,'gridsearch','leaveoneoutlssvm',{'misclass'},'code_OneVsOne');
    model=trainlssvm(model);
    yte=simlssvm(model,Xt_test'); 
    rate3(i)=length(find(yte==target_label_test))/length(target_label_test)*100;
    close all;
   %% ELM:
    TrainingData_File=[Yat,Xat];
    TestingData_File=[target_label_test,Xt_test'];
    Elm_Type=1;
    C_elm=100;
    NumberofHiddenNeurons=5000;
    ActivationFunction='radbas';
    [TrainingAccuracy,rate4(i)] = elm(TrainingData_File, TestingData_File, Elm_Type, NumberofHiddenNeurons, ActivationFunction,C_elm);
   %% KELM
    sigma2=0.01;
    C_elm=100;
    [TrainingAccuracy,rate5(i)] = ELM_kernel(TrainingData_File, TestingData_File,sigma2,C_elm); 
end

clc
display('    NN,   SVM,    LSSVM,    ELM    KELM')
fprintf('%f,%f,%f,%f,%f\n',[mean(rate1) mean(rate2) mean(rate3) mean(rate4) mean(rate5)])  
std=[sqrt(mean(rate1.^2)-mean(rate1)^2)/sqrt(length(rate1)),...
    sqrt(mean(rate2.^2)-mean(rate2)^2)/sqrt(length(rate2)),sqrt(mean(rate3.^2)-mean(rate3)^2)/sqrt(length(rate3)),...
    sqrt(mean(rate4.^2)-mean(rate4)^2)/sqrt(length(rate4)),sqrt(mean(rate5.^2)-mean(rate5)^2)/sqrt(length(rate5))];
fprintf('%f,%f,%f,%f,%f\n',std)  