├── matlab ├── compatibility │ └── parallel │ │ ├── numlabs.m │ │ ├── labindex.m │ │ └── gather.m ├── src │ ├── bits │ │ ├── nnconv.cpp │ │ ├── data.cpp │ │ ├── nnbnorm.cpp │ │ ├── datamex.cpp │ │ ├── nnpooling.cpp │ │ ├── nnnormalize.cpp │ │ ├── nnroipooling.cpp │ │ ├── nnsubsample.cpp │ │ ├── nnbilinearsampler.cpp │ │ ├── nnfullyconnected.cpp │ │ ├── impl │ │ │ ├── copy.hpp │ │ │ ├── nnbilinearsampler_cudnn.hpp │ │ │ ├── normalize.hpp │ │ │ ├── nnbias_cudnn.hpp │ │ │ ├── copy_cpu.cpp │ │ │ ├── subsample.hpp │ │ │ ├── im2row.hpp │ │ │ ├── bilinearsampler.hpp │ │ │ ├── nnconv_cudnn.hpp │ │ │ ├── nnpooling_cudnn.hpp │ │ │ ├── copy_gpu.cu │ │ │ ├── nnbnorm_cudnn.hpp │ │ │ ├── pooling.hpp │ │ │ ├── roipooling.hpp │ │ │ └── bnorm.hpp │ │ ├── nnbias.hpp │ │ ├── nnnormalize.hpp │ │ ├── nnbilinearsampler.hpp │ │ ├── nnfullyconnected.hpp │ │ ├── nnsubsample.hpp │ │ ├── nnbias.cpp │ │ ├── nnpooling.hpp │ │ ├── nnroipooling.hpp │ │ ├── imread.hpp │ │ ├── imread.cpp │ │ ├── datamex.hpp │ │ ├── nnconv.hpp │ │ └── nnbnorm.hpp │ ├── vl_nnbnorm.cpp │ ├── vl_nnconv.cpp │ ├── vl_nnconvt.cpp │ ├── vl_nnpool.cpp │ ├── vl_tmove.cpp │ ├── vl_cudatool.cpp │ ├── vl_nnroipool.cpp │ ├── vl_taccummex.cpp │ ├── vl_imreadjpeg.cpp │ ├── vl_imreadjpeg_old.cpp │ ├── vl_nnnormalize.cpp │ └── vl_nnbilinearsampler.cpp ├── xtest │ ├── vl_test_print.m │ ├── suite │ │ ├── nnsigmoid.m │ │ ├── nndropout.m │ │ ├── nnoffset.m │ │ ├── nnsoftmax.m │ │ ├── nnspnorm.m │ │ ├── nnnormalizelp.m │ │ ├── nnrelu.m │ │ ├── nnbnorm.m │ │ ├── nnmnist.m │ │ ├── nnnormalize.m │ │ ├── nnpdist.m │ │ ├── nnsoftmaxloss.m │ │ ├── nnroipool.m │ │ ├── nnconcat.m │ │ └── Scale.m │ ├── vl_test_bnorm.m │ ├── vl_test_economic_relu.m │ ├── vl_test_imreadjpeg.m │ ├── vl_bench_bnorm.m │ ├── vl_test_gpureset.m │ ├── vl_bench_imreadjpeg.m │ └── vl_testnn.m ├── +dagnn │ ├── @DagNN │ │ ├── setLayerInputs.m │ │ ├── setLayerParams.m │ │ ├── setLayerOutputs.m │ │ ├── reset.m │ │ ├── removeLayer.m │ │ ├── renameLayer.m │ │ ├── move.m │ │ ├── addLayer.m │ │ ├── renameVar.m │ │ ├── saveobj.m │ │ ├── getVarSizes.m │ │ └── loadobj.m │ ├── Sigmoid.m │ ├── SoftMax.m │ ├── LRN.m │ ├── NormOffset.m │ ├── SpatialNorm.m │ ├── ElementWise.m │ ├── BilinearSampler.m │ ├── ODistLoss.m │ ├── Scale.m │ ├── DropOut.m │ ├── Pooling.m │ ├── MAC.m │ ├── Loss.m │ ├── Sum.m │ ├── Square2.m │ ├── Square.m │ ├── Concat.m │ ├── Filter.m │ ├── ROIPooling.m │ ├── Crop.m │ ├── Pseudo_Loss.m │ ├── Conv.m │ ├── BatchNorm_origin.m │ ├── ReLU.m │ ├── Pseudo_Loss_Multi_Static.m │ ├── BatchNorm.m │ ├── Pseudo_Loss_Multi_Dynamic_1.m │ └── Pseudo_Loss_Multi_Dynamic_2.m ├── vl_rootnn.m ├── vl_taccum.m ├── vl_nnsigmoid.m ├── simplenn │ ├── vl_simplenn_start_parserv.m │ └── vl_simplenn_move.m ├── vl_nnnoffset.m ├── vl_setupnn.m ├── vl_nnsoftmax.m ├── vl_nnsoftmax_temper.m ├── vl_nncrop.m ├── vl_nnspnorm.m ├── vl_nnconcat.m ├── vl_nnnormalizelp.m ├── vl_nnnormalize.m ├── vl_nnrelu.m ├── vl_nnbnorm.m ├── vl_nnbilinearsampler.m ├── vl_nndropout.m ├── vl_nnroipool.m └── vl_nnsoftmaxloss.m ├── evaluation ├── col_sum.m ├── utils │ ├── process_box_feat.m │ ├── prepare_img.m │ ├── gen_train_sample_xqda.m │ ├── applypca2.m │ ├── compute_AP.m │ ├── MahDist.m │ ├── draw_confusion_matrix.m │ ├── evaluation.m │ ├── gen_train_sample_kissme.m │ ├── compute_r1_multiCam.m │ └── compute_AP_multiCam.m ├── sqdist.m ├── compute_AP_rerank.m ├── draw_confusion_matrix.m ├── evaluation.m └── compute_r1_multiCam.m ├── code ├── tools │ ├── update_pseudo.m │ ├── concat_2net.m │ ├── get_Pseudo_label.m │ ├── get_instanceweights4gan.m │ ├── update_multi_pseudo.m │ ├── label_to_index.m │ ├── rand_same_class.m │ └── rand_diff_class.m ├── prepare_data │ ├── prepare_gan_data.m │ ├── prepare_sMpRL_label4data.m │ └── prepare_data.m ├── get_net_structure │ ├── resnet52_iden_baseline.m │ ├── resnet52_iden_sMpRL.m │ ├── resnet52_iden_MpRL2.m │ └── resnet52_iden_MpRL3.m └── train │ ├── train_res_iden_baseline.m │ ├── train_res_iden_MpRL3.m │ └── train_res_iden_MpRL2.m ├── test ├── getFeature2.m └── test_gallery_query_crazy.m ├── gpu_compile.m ├── train.m └── LICENSE /matlab/compatibility/parallel/numlabs.m: -------------------------------------------------------------------------------- 1 | function n = numlabs() 2 | n = 1 ; 3 | -------------------------------------------------------------------------------- /matlab/compatibility/parallel/labindex.m: -------------------------------------------------------------------------------- 1 | function i = labindex() 2 | i = 1 ; 3 | -------------------------------------------------------------------------------- /matlab/src/bits/nnconv.cpp: -------------------------------------------------------------------------------- 1 | #ifdef ENABLE_GPU 2 | #error "The file nnconv.cu should be compiled instead" 3 | #endif 4 | #include "nnconv.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/bits/data.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "data.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/bits/nnbnorm.cpp: -------------------------------------------------------------------------------- 1 | #ifdef ENABLE_GPU 2 | #error "The file nnbnorm.cu should be compiled instead" 3 | #endif 4 | #include "nnbnorm.cu" 5 | -------------------------------------------------------------------------------- /evaluation/col_sum.m: -------------------------------------------------------------------------------- 1 | function s = col_sum(x) 2 | % COL_SUM Sum for each column. 3 | % A more readable alternative to sum(x,1). 4 | s = sum(x,1); 5 | -------------------------------------------------------------------------------- /matlab/src/bits/datamex.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "datamex.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/bits/nnpooling.cpp: -------------------------------------------------------------------------------- 1 | #ifdef ENABLE_GPU 2 | #error "The file nnpooling.cu should be compiled instead" 3 | #endif 4 | #include "nnpooling.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/vl_nnbnorm.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "vl_nnbnorm.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/vl_nnconv.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "vl_nnconv.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/vl_nnconvt.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "vl_nnconvt.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/vl_nnpool.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "vl_nnpool.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/vl_tmove.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "vl_tmove.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/bits/nnnormalize.cpp: -------------------------------------------------------------------------------- 1 | #ifdef ENABLE_GPU 2 | #error "The file nnnormalize.cu should be compiled instead" 3 | #endif 4 | #include "nnnormalize.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/vl_cudatool.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "vl_cudatool.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/vl_nnroipool.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "vl_nnroipool.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/vl_taccummex.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "vl_taccummex.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/bits/nnroipooling.cpp: -------------------------------------------------------------------------------- 1 | #ifdef ENABLE_GPU 2 | #error "The file nnroipooling.cu should be compiled instead" 3 | #endif 4 | #include "nnroipooling.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/bits/nnsubsample.cpp: -------------------------------------------------------------------------------- 1 | #ifdef ENABLE_GPU 2 | #error "The file nnsubsample.cu should be compiled instead" 3 | #endif 4 | #include "nnsubsample.cu" 5 | 6 | -------------------------------------------------------------------------------- /matlab/src/vl_imreadjpeg.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "vl_imreadjpeg.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/vl_imreadjpeg_old.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "vl_imreadjpeg.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/vl_nnnormalize.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "vl_nnnormalize.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/bits/nnbilinearsampler.cpp: -------------------------------------------------------------------------------- 1 | #ifdef ENABLE_GPU 2 | #error "The file nnbnorm.cu should be compiled instead" 3 | #endif 4 | #include "nnbilinearsampler.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/bits/nnfullyconnected.cpp: -------------------------------------------------------------------------------- 1 | #ifdef ENABLE_GPU 2 | #error "The file nnfullyconnected.cu should be compiled instead" 3 | #endif 4 | #include "nnfullyconnected.cu" 5 | -------------------------------------------------------------------------------- /matlab/src/vl_nnbilinearsampler.cpp: -------------------------------------------------------------------------------- 1 | #if ENABLE_GPU 2 | #error This file should not be compiled with GPU support enabled 3 | #endif 4 | #include "vl_nnbilinearsampler.cu" 5 | -------------------------------------------------------------------------------- /matlab/compatibility/parallel/gather.m: -------------------------------------------------------------------------------- 1 | function x=gather(x) 2 | % GATHER Compatibility stub for the GATHER() function 3 | % GATHER() is a function in the Parallel MATLAB toolbox. MATCONVNET 4 | % can work without it. 5 | -------------------------------------------------------------------------------- /matlab/xtest/vl_test_print.m: -------------------------------------------------------------------------------- 1 | function vl_test_print(varargin) 2 | 3 | addpath(fullfile(vl_rootnn(), 'examples', 'mnist')); 4 | 5 | net = cnn_mnist_init('networkType', 'dagnn'); 6 | net.print(varargin{:}); 7 | 8 | end 9 | 10 | -------------------------------------------------------------------------------- /code/tools/update_pseudo.m: -------------------------------------------------------------------------------- 1 | function labels = update_pseudo( x,labels) 2 | % quick estimate pseudo label 3 | f = vl_nnsoftmax(x); 4 | f = reshape(f,size(x,3),[]); 5 | [f_max,index] = max(f); 6 | %index(f_max<0.1) = 0; 7 | index = gather(index); 8 | gan = (labels==0); 9 | labels(gan) = index(gan); 10 | end -------------------------------------------------------------------------------- /test/getFeature2.m: -------------------------------------------------------------------------------- 1 | function x = getFeature2(net,oim,im_mean,inputname,outputname) 2 | im = bsxfun(@minus,single(oim),im_mean); 3 | net.vars(net.getVarIndex(outputname)).precious = true; 4 | net.eval({inputname,gpuArray(im)}) ; 5 | x = gather(net.vars(net.getVarIndex(outputname)).value); 6 | end 7 | -------------------------------------------------------------------------------- /code/tools/concat_2net.m: -------------------------------------------------------------------------------- 1 | function net = concat_2net(net1,net2) 2 | net1 = net1.saveobj(); 3 | net2 = net2.saveobj(); 4 | net.vars = [net1.vars,net2.vars]; 5 | net.layers = [net1.layers,net2.layers]; 6 | net.params = [net1.params,net2.params]; 7 | net.meta = net1.meta; 8 | net = dagnn.DagNN.loadobj(net); 9 | end 10 | -------------------------------------------------------------------------------- /code/tools/get_Pseudo_label.m: -------------------------------------------------------------------------------- 1 | function f = get_Pseudo_label(net,oim,im_mean,inputname,outputname) 2 | im = bsxfun(@minus,single(oim),im_mean); 3 | net.vars(net.getVarIndex(outputname)).precious = true; 4 | net.eval({inputname,gpuArray(im)}) ; 5 | x = gather(net.vars(net.getVarIndex(outputname)).value); 6 | f = vl_nnsoftmax(x); 7 | end -------------------------------------------------------------------------------- /matlab/xtest/suite/nnsigmoid.m: -------------------------------------------------------------------------------- 1 | classdef nnsigmoid < nntest 2 | methods (Test) 3 | function basic(test) 4 | x = test.randn(5,5,1,1)/test.range ; 5 | y = vl_nnsigmoid(x) ; 6 | dzdy = test.randn(size(y)) ; 7 | dzdx = vl_nnsigmoid(x,dzdy) ; 8 | test.der(@(x) vl_nnsigmoid(x), x, dzdy, dzdx, 1e-3) ; 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /code/tools/get_instanceweights4gan.m: -------------------------------------------------------------------------------- 1 | function [ instanceWeights ] = get_instanceweights4gan(labels, weight) 2 | %GET_INSTANCEWEIGHTS Summary of this function goes here 3 | % Detailed explanation goes here 4 | instanceWeights = ones(size(labels)); 5 | gan = (labels==0); 6 | instanceWeights(gan) = weight; 7 | instanceWeights = reshape(instanceWeights,1,1,1,[]); 8 | 9 | end 10 | 11 | -------------------------------------------------------------------------------- /matlab/+dagnn/@DagNN/setLayerInputs.m: -------------------------------------------------------------------------------- 1 | function v = setLayerInputs(obj, layer, inputs) 2 | %SETLAYERINPUTS Set or change the inputs to a layer 3 | % Example: NET.SETLAYERINPUTS('layerName', {'input1', 'input2', ...}) 4 | 5 | v = [] ; 6 | l = obj.getLayerIndex(layer) ; 7 | for input = inputs 8 | v(end+1) = obj.addVar(char(input)) ; 9 | end 10 | obj.layers(l).inputs = inputs ; 11 | obj.rebuild() ; 12 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nndropout.m: -------------------------------------------------------------------------------- 1 | classdef nndropout < nntest 2 | methods (Test) 3 | function basic(test) 4 | x = test.randn(4,5,10,3) ; 5 | [y,mask] = vl_nndropout(x) ; 6 | dzdy = test.randn(size(y)) ; 7 | dzdx = vl_nndropout(x,dzdy,'mask',mask) ; 8 | test.der(@(x) vl_nndropout(x,'mask',mask), x, dzdy, dzdx, 1e-3*test.range) ; 9 | end 10 | end 11 | end 12 | 13 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nnoffset.m: -------------------------------------------------------------------------------- 1 | classdef nnoffset < nntest 2 | methods (Test) 3 | function basic(test) 4 | param = [.34, .5] ; 5 | x = test.randn(4,5,10,3) ; 6 | y = vl_nnnoffset(x,param) ; 7 | dzdy = test.randn(size(y)) ; 8 | dzdx = vl_nnnoffset(x,param,dzdy) ; 9 | test.der(@(x) vl_nnnoffset(x,param), x, dzdy, dzdx, 1e-3*test.range) ; 10 | end 11 | end 12 | end -------------------------------------------------------------------------------- /matlab/+dagnn/@DagNN/setLayerParams.m: -------------------------------------------------------------------------------- 1 | function v = setLayerParams(obj, layer, params) 2 | %SETLAYEPARAMS Set or change the parameters of a layer 3 | % Example: NET.SETLAYERPARAMS('layerName', {'param1', 'param2', ...}) 4 | 5 | v = [] ; 6 | l = obj.getLayerIndex(layer) ; 7 | for param = params 8 | v(end+1) = obj.addParam(char(param)) ; 9 | end 10 | obj.layers(l).params = params ; 11 | obj.rebuild() ; 12 | -------------------------------------------------------------------------------- /matlab/+dagnn/Sigmoid.m: -------------------------------------------------------------------------------- 1 | classdef Sigmoid < dagnn.ElementWise 2 | methods 3 | function outputs = forward(obj, inputs, params) 4 | outputs{1} = vl_nnsigmoid(inputs{1}) ; 5 | end 6 | 7 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 8 | derInputs{1} = vl_nnsigmoid(inputs{1}, derOutputs{1}) ; 9 | derParams = {} ; 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /gpu_compile.m: -------------------------------------------------------------------------------- 1 | %% cuda-8.0 + cudnn-5.1 2 | 3 | addpath matlab; 4 | addpath examples; 5 | run matlab/vl_setupnn ; 6 | 7 | vl_compilenn('enableGpu', true, ... 8 | 'cudaRoot', '/usr/local/cuda', ... %put cuda path here 9 | 'cudaMethod', 'nvcc', ... 10 | 'enableCudnn',true,... 11 | 'cudnnroot','/usr/local/cuda'); % put cudnn dir path here, which includes two dirs, `include` and `lib64` 12 | 13 | warning('off'); 14 | -------------------------------------------------------------------------------- /matlab/+dagnn/@DagNN/setLayerOutputs.m: -------------------------------------------------------------------------------- 1 | function v = setLayerOutputs(obj, layer, outputs) 2 | %SETLAYEROUTPUTS Set or change the outputs of a layer 3 | % Example: NET.SETLAYEROUTPUTS('layerName', {'output1', 'output2', ...}) 4 | 5 | v = [] ; 6 | l = obj.getLayerIndex(layer) ; 7 | for output = outputs 8 | v(end+1) = obj.addVar(char(output)) ; 9 | end 10 | obj.layers(l).outputs = outputs ; 11 | obj.rebuild() ; 12 | -------------------------------------------------------------------------------- /matlab/vl_rootnn.m: -------------------------------------------------------------------------------- 1 | function root = vl_rootnn() 2 | %VL_ROOTNN Get the root path of the MatConvNet toolbox. 3 | % VL_ROOTNN() returns the path to the MatConvNet toolbox. 4 | 5 | % Copyright (C) 2014 Andrea Vedaldi. 6 | % All rights reserved. 7 | % 8 | % This file is part of the VLFeat library and is made available under 9 | % the terms of the BSD license (see the COPYING file). 10 | 11 | root = fileparts(fileparts(mfilename('fullpath'))) ; 12 | -------------------------------------------------------------------------------- /matlab/xtest/vl_test_bnorm.m: -------------------------------------------------------------------------------- 1 | %% 2 | % Test function to compare nn_bnorm and its GPU/CPU implementation with 3 | % using VLFEAT 4 | %% 5 | 6 | gpu = false; 7 | gpu = true ; 8 | 9 | T = 1 ; 10 | x = randn(64,64,32,32,'single') ; 11 | g = randn(32,1,'single') ; 12 | b = randn(32,1,'single') ; 13 | 14 | if gpu 15 | x = gpuArray(x) ; 16 | g = gpuArray(g) ; 17 | b = gpuArray(b) ; 18 | end 19 | 20 | a=vl_nnbnorm(x,g,b); 21 | a_=vl_nnbnorm_old(x,g,b); 22 | 23 | vl_testsim(a,a_) 24 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nnsoftmax.m: -------------------------------------------------------------------------------- 1 | classdef nnsoftmax < nntest 2 | properties (TestParameter) 3 | h = {1 2 3} 4 | w = {1 2} 5 | end 6 | methods (Test) 7 | function basic(test,h,w) 8 | d = 10 ; 9 | n = 3 ; 10 | x = test.randn(h,w,d,n)/test.range ; 11 | y = vl_nnsoftmax(x) ; 12 | dzdy = test.randn(size(y)) ; 13 | dzdx = vl_nnsoftmax(x, dzdy) ; 14 | test.der(@(x) vl_nnsoftmax(x), x, dzdy, dzdx, 1e-2) ; 15 | end 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nnspnorm.m: -------------------------------------------------------------------------------- 1 | classdef nnspnorm < nntest 2 | methods (Test) 3 | function basic(test) 4 | h = 13 ; 5 | w = 17 ; 6 | d = 4 ; 7 | n = 5 ; 8 | param = [3, 3, 0.1, 0.75] ; 9 | x = test.randn(h,w,d,n) ; 10 | y = vl_nnspnorm(x, param) ; 11 | dzdy = test.rand(h, w, d, n) ; 12 | dzdx = vl_nnspnorm(x, param, dzdy) ; 13 | test.der(@(x) vl_nnspnorm(x,param), x, dzdy, dzdx, test.range * 1e-3) ; 14 | end 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /matlab/+dagnn/@DagNN/reset.m: -------------------------------------------------------------------------------- 1 | function reset(obj) 2 | %RESET Reset the DagNN 3 | % RESET(obj) resets the DagNN obj. The function clears any intermediate value stored in the DagNN 4 | % object, including parameter gradients. It also calls the reset 5 | % function of every layer. 6 | 7 | obj.clearParameterServer() ; 8 | [obj.vars.value] = deal([]) ; 9 | [obj.vars.der] = deal([]) ; 10 | [obj.params.der] = deal([]) ; 11 | for l = 1:numel(obj.layers) 12 | obj.layers(l).block.reset() ; 13 | end 14 | -------------------------------------------------------------------------------- /matlab/+dagnn/SoftMax.m: -------------------------------------------------------------------------------- 1 | classdef SoftMax < dagnn.ElementWise 2 | methods 3 | function outputs = forward(self, inputs, params) 4 | outputs{1} = vl_nnsoftmax(inputs{1}) ; 5 | end 6 | 7 | function [derInputs, derParams] = backward(self, inputs, params, derOutputs) 8 | derInputs{1} = vl_nnsoftmax(inputs{1}, derOutputs{1}) ; 9 | derParams = {} ; 10 | end 11 | 12 | function obj = SoftMax(varargin) 13 | obj.load(varargin) ; 14 | end 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /matlab/+dagnn/LRN.m: -------------------------------------------------------------------------------- 1 | classdef LRN < dagnn.ElementWise 2 | properties 3 | param = [5 1 0.0001/5 0.75] 4 | end 5 | 6 | methods 7 | function outputs = forward(obj, inputs, params) 8 | outputs{1} = vl_nnnormalize(inputs{1}, obj.param) ; 9 | end 10 | 11 | function [derInputs, derParams] = backward(obj, inputs, param, derOutputs) 12 | derInputs{1} = vl_nnnormalize(inputs{1}, obj.param, derOutputs{1}) ; 13 | derParams = {} ; 14 | end 15 | 16 | function obj = LRN(varargin) 17 | obj.load(varargin) ; 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /matlab/+dagnn/NormOffset.m: -------------------------------------------------------------------------------- 1 | classdef NormOffset < dagnn.ElementWise 2 | properties 3 | param = [1 0.5] 4 | end 5 | 6 | methods 7 | function outputs = forward(obj, inputs, params) 8 | outputs{1} = vl_nnnoffset(inputs{1}, obj.param) ; 9 | end 10 | 11 | function [derInputs, derParams] = backward(obj, inputs, param, derOutputs) 12 | derInputs{1} = vl_nnnoffset(inputs{1}, obj.param, derOutputs{1}) ; 13 | derParams = {} ; 14 | end 15 | 16 | function obj = NormOffset(varargin) 17 | obj.load(varargin) ; 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /matlab/+dagnn/SpatialNorm.m: -------------------------------------------------------------------------------- 1 | classdef SpatialNorm < dagnn.ElementWise 2 | properties 3 | param = [2 2 10 2] 4 | end 5 | 6 | methods 7 | function outputs = forward(obj, inputs, params) 8 | outputs{1} = vl_nnspnorm(inputs{1}, obj.param) ; 9 | end 10 | 11 | function [derInputs, derParams] = backward(obj, inputs, param, derOutputs) 12 | derInputs{1} = vl_nnspnorm(inputs{1}, obj.param, derOutputs{1}) ; 13 | derParams = {} ; 14 | end 15 | 16 | function obj = SpatialNorm(varargin) 17 | obj.load(varargin) ; 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /evaluation/utils/process_box_feat.m: -------------------------------------------------------------------------------- 1 | function video_feat = process_box_feat(box_feat, video_info) 2 | 3 | nVideo = size(video_info, 1); 4 | video_feat = zeros(size(box_feat, 1), nVideo); 5 | for n = 1:nVideo 6 | feature_set = box_feat(:, video_info(n, 1):video_info(n, 2)); 7 | video_feat(:, n) = max(feature_set, [], 2); % max pooling 8 | % video_feat(:, n) = mean(feature_set, 2); % avg pooling 9 | end 10 | 11 | %%% normalize train and test features 12 | sum_val = sqrt(sum(video_feat.^2)); 13 | for n = 1:size(video_feat, 1) 14 | video_feat(n, :) = video_feat(n, :)./sum_val; 15 | end -------------------------------------------------------------------------------- /code/tools/update_multi_pseudo.m: -------------------------------------------------------------------------------- 1 | function [ multipse_label ] = update_multi_pseudo( x, c ) 2 | %UPDATE_MULTI_PSEUDO Summary of this function goes here 3 | % Detailed explanation goes here 4 | f = vl_nnsoftmax(x); 5 | f = reshape(f,size(x,3),[]); 6 | iden_num = size(f,1); 7 | label = 1/iden_num:1/iden_num:1; 8 | multipse_label_temp = repmat(label',1,size(f,2)); 9 | [~, idx] = sort(f,1); 10 | multipse_label = multipse_label_temp(idx); 11 | 12 | gan = (c~=0); 13 | multipse_label(:,gan) = 0; 14 | multipse_label = reshape(multipse_label,1,1,size(multipse_label,1),size(multipse_label,2)); 15 | 16 | end 17 | 18 | -------------------------------------------------------------------------------- /code/prepare_data/prepare_gan_data.m: -------------------------------------------------------------------------------- 1 | dataset_name = 'Market-1501-v15.09.15'; 2 | number_samples = 24000; 3 | 4 | load(strcat('./data/url_data.mat')); 5 | p = dir(strcat('/home/yan/datasets/',dataset_name,'/generated_54000_256/*.jpg')); 6 | 7 | num = numel(imdb.images.data); 8 | for i=1:number_samples 9 | url = strcat('/home/yan/datasets/',dataset_name,'/generated_54000_256/',p(i).name); 10 | imdb.images.data(num + i) =cellstr(url); 11 | imdb.images.label(num + i) = 0; 12 | imdb.images.set(num + i) = 1; 13 | end 14 | 15 | save(strcat('./data/url_data_gan_',num2str(number_samples),'.mat'),'imdb','-v7.3'); -------------------------------------------------------------------------------- /matlab/+dagnn/ElementWise.m: -------------------------------------------------------------------------------- 1 | classdef ElementWise < dagnn.Layer 2 | %ELEMENTWISE DagNN layers that operate at individual spatial locations 3 | methods 4 | function [outputSizes, transforms] = forwardGeometry(self, inputSizes, paramSizes) 5 | outputSizes = inputSizes ; 6 | transforms = {eye(6)} ; 7 | end 8 | 9 | function rfs = getReceptiveFields(obj) 10 | rfs.size = [1 1] ; 11 | rfs.stride = [1 1] ; 12 | rfs.offset = [1 1] ; 13 | end 14 | 15 | function outputSizes = getOutputSizes(obj, inputSizes) 16 | outputSizes = inputSizes ; 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /code/tools/label_to_index.m: -------------------------------------------------------------------------------- 1 | function [ ci ] = label_to_index( x, c ) 2 | %LABEL_TO_INDEX Summary of this function goes here 3 | % Detailed explanation goes here 4 | 5 | inputSize = [size(x,1) size(x,2) size(x,3) size(x,4)]; 6 | labelSize = [size(c,1) size(c,2) size(c,3) size(c,4)]; 7 | 8 | numPixelsPerImage = prod(inputSize(1:2)) ; 9 | numPixels = numPixelsPerImage * inputSize(4) ; 10 | imageVolume = numPixelsPerImage * inputSize(3) ; 11 | 12 | n = reshape(0:numPixels-1,labelSize) ; 13 | offset = 1 + mod(n, numPixelsPerImage) + ... 14 | imageVolume * fix(n / numPixelsPerImage) ; 15 | ci = offset + numPixelsPerImage * max(c - 1,0) ; 16 | 17 | end 18 | 19 | -------------------------------------------------------------------------------- /evaluation/utils/prepare_img.m: -------------------------------------------------------------------------------- 1 | function [ im_data ] = prepare_img( im_data, mean_data, im_size ) 2 | %PREPARE_IMG Summary of this function goes here 3 | % Detailed explanation goes here 4 | IMAGE_DIM = im_size; 5 | % Convert an image returned by Matlab's imread to im_data in caffe's data 6 | % format: W x H x C with BGR channels 7 | im_data = im_data(:, :, [3, 2, 1]); % permute channels from RGB to BGR 8 | im_data = permute(im_data, [2, 1, 3]); % flip width and height 9 | im_data = single(im_data); % convert from uint8 to single 10 | im_data = imresize(im_data, [IMAGE_DIM IMAGE_DIM], 'bilinear'); % resize im_data 11 | im_data = im_data - mean_data; % subtract mean_data (already in W x H x C, BGR) 12 | end 13 | 14 | -------------------------------------------------------------------------------- /matlab/vl_taccum.m: -------------------------------------------------------------------------------- 1 | function a = vl_taccum(alpha, a, beta, b) 2 | %VL_TACCUM Compute A = alpha A + beta B 3 | % A = VL_TACCUM(ALPHA, A, BETA, B) computes efficiently A = alpha A 4 | % + beta B. For GPU arrays, it performs its computation in place, by 5 | % modifiying A without creating an additional copy. 6 | 7 | % Copyright (C) 2016 Andrea Vedaldi. 8 | % All rights reserved. 9 | % 10 | % This file is part of the VLFeat library and is made available under 11 | % the terms of the BSD license (see the COPYING file). 12 | 13 | if isscalar(a) 14 | a = alpha * a + beta * b ; 15 | return ; 16 | elseif isa(a, 'gpuArray') 17 | vl_taccummex(alpha, a, beta, b, 'inplace') ; 18 | else 19 | a = vl_taccummex(alpha, a, beta, b) ; 20 | end 21 | -------------------------------------------------------------------------------- /code/tools/rand_same_class.m: -------------------------------------------------------------------------------- 1 | % function output = rand_same_class(imdb,index) 2 | % list = find(imdb.images.reallabel==imdb.images.reallabel(index)); 3 | % selected = randi(numel(list)); 4 | % output = list(selected); 5 | % while(output==index || imdb.images.set(output)~=1) 6 | % selected = randi(numel(list)); 7 | % output = list(selected); 8 | % end 9 | % end 10 | % 11 | 12 | function output = rand_same_class(imdb,index) 13 | list = find(imdb.images.label==imdb.images.label(index)); 14 | selected = randi(numel(list)); 15 | output = list(selected); 16 | while(output==index || imdb.images.set(output)~=1) 17 | selected = randi(numel(list)); 18 | output = list(selected); 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /matlab/+dagnn/@DagNN/removeLayer.m: -------------------------------------------------------------------------------- 1 | function removeLayer(obj, layerName) 2 | %REMOVELAYER Remove a layer from the network 3 | % REMOVELAYER(OBJ, NAME) removes the layer NAME from the DagNN object 4 | % OBJ. NAME can be a string or a cell array of strings. 5 | 6 | % Copyright (C) 2015 Karel Lenc and Andrea Vedaldi. 7 | % All rights reserved. 8 | % 9 | % This file is part of the VLFeat library and is made available under 10 | % the terms of the BSD license (see the COPYING file). 11 | 12 | if ischar(layerName), layerName = {layerName}; end; 13 | idxs = obj.getLayerIndex(layerName); 14 | if any(isnan(idxs)) 15 | error('Invalid layer name `%s`', ... 16 | strjoin(layerName(isnan(idxs)), ', ')); 17 | end 18 | obj.layers(idxs) = [] ; 19 | obj.rebuild() ; 20 | -------------------------------------------------------------------------------- /matlab/vl_nnsigmoid.m: -------------------------------------------------------------------------------- 1 | function out = vl_nnsigmoid(x,dzdy) 2 | %VL_NNSIGMOID CNN sigmoid nonlinear unit. 3 | % Y = VL_NNSIGMOID(X) computes the sigmoid of the data X. X can 4 | % have an arbitrary size. The sigmoid is defined as follows: 5 | % 6 | % SIGMOID(X) = 1 / (1 + EXP(-X)). 7 | % 8 | % DZDX = VL_NNSIGMOID(X, DZDY) computes the derivative of the 9 | % block projected onto DZDY. DZDX and DZDY have the same 10 | % dimensions as X and Y respectively. 11 | 12 | % Copyright (C) 2015 Karel Lenc. 13 | % All rights reserved. 14 | % 15 | % This file is part of the VLFeat library and is made available under 16 | % the terms of the BSD license (see the COPYING file). 17 | 18 | y = 1 ./ (1 + exp(-x)); 19 | 20 | if nargin <= 1 || isempty(dzdy) 21 | out = y ; 22 | else 23 | out = dzdy .* (y .* (1 - y)) ; 24 | end 25 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nnnormalizelp.m: -------------------------------------------------------------------------------- 1 | classdef nnnormalizelp < nntest 2 | properties (TestParameter) 3 | h = {1 2 3 4} 4 | w = {1 2 3 4} 5 | d = {2 3 4} 6 | p = {2 4} 7 | end 8 | 9 | methods (Test) 10 | function basicl2(test, h,w,d) 11 | x = test.randn(h,w,d,3) ; 12 | y = vl_nnnormalizelp(x) ; 13 | dzdy = test.rand(size(y))-0.5 ; 14 | dzdx = vl_nnnormalizelp(x,dzdy) ; 15 | test.der(@(x) vl_nnnormalizelp(x), x, dzdy, dzdx, 1e-4, 0.3) ; 16 | end 17 | 18 | function lp(test, p) 19 | x = test.randn(2,3,5,3) / test.range ; 20 | y = vl_nnnormalizelp(x, [], 'p', p) ; 21 | dzdy = test.rand(size(y))-0.5 ; 22 | dzdx = vl_nnnormalizelp(x,dzdy, 'p', p) ; 23 | test.der(@(x) vl_nnnormalizelp(x,[],'p',p), x, dzdy, dzdx, 1e-4, 0.3) ; 24 | end 25 | 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/copy.hpp: -------------------------------------------------------------------------------- 1 | // @file copy.hpp 2 | // @brief Copy and other data operations 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2015-16 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__copy__ 14 | #define __vl__copy__ 15 | 16 | #include "../data.hpp" 17 | 18 | namespace vl { namespace impl { 19 | 20 | template 21 | struct operations 22 | { 23 | typedef type data_type ; 24 | static vl::ErrorCode copy(data_type * dest, data_type const * src, size_t numElements) ; 25 | static vl::ErrorCode fill(data_type * dest, size_t numElements, data_type value) ; 26 | } ; 27 | } } 28 | 29 | #endif /* defined(__vl__copy__) */ 30 | -------------------------------------------------------------------------------- /evaluation/sqdist.m: -------------------------------------------------------------------------------- 1 | function m = sqdist(p, q, A) 2 | % SQDIST Squared Euclidean or Mahalanobis distance. 3 | % SQDIST(p,q) returns m(i,j) = (p(:,i) - q(:,j))'*(p(:,i) - q(:,j)). 4 | % SQDIST(p,q,A) returns m(i,j) = (p(:,i) - q(:,j))'*A*(p(:,i) - q(:,j)). 5 | % The Lightspeed Matlab toolbox 6 | % Written by Tom Minka 7 | 8 | [d, pn] = size(p); 9 | [d, qn] = size(q); 10 | 11 | if pn == 0 || qn == 0 12 | m = zeros(pn,qn); 13 | return 14 | end 15 | 16 | if nargin == 2 17 | 18 | pmag = col_sum(p .* p); 19 | qmag = col_sum(q .* q); 20 | m = repmat(qmag, pn, 1) + repmat(pmag', 1, qn) - 2*p'*q; 21 | %m = ones(pn,1)*qmag + pmag'*ones(1,qn) - 2*p'*q; 22 | 23 | else 24 | 25 | Ap = A*p; 26 | Aq = A*q; 27 | pmag = col_sum(p .* Ap); 28 | qmag = col_sum(q .* Aq); 29 | m = repmat(qmag, pn, 1) + repmat(pmag', 1, qn) - 2*p'*Aq; 30 | 31 | end 32 | -------------------------------------------------------------------------------- /matlab/simplenn/vl_simplenn_start_parserv.m: -------------------------------------------------------------------------------- 1 | function vl_simplenn_start_parserv(net, ps) 2 | %VL_SIMPLENN_START_PARSERV Setup a parameter server for this network 3 | % VL_SIMPLENN_START_PARSERV(NET, PS) registers the network 4 | % parameter derivatives with the specified ParameterServer instance 5 | % PS and then starts the server. 6 | 7 | for i = 1:numel(net.layers) 8 | for j = 1:numel(net.layers{i}.weights) 9 | value = net.layers{i}.weights{j} ; 10 | name = sprintf('l%d_%d',i,j) ; 11 | if strcmp(class(value),'gpuArray') 12 | deviceType = 'gpu' ; 13 | dataType = classUnderlying(value) ; 14 | else 15 | deviceType = 'cpu' ; 16 | dataType = class(value) ; 17 | end 18 | ps.register(... 19 | name, ... 20 | size(value), ... 21 | dataType, ... 22 | deviceType) ; 23 | end 24 | end 25 | ps.start() ; 26 | -------------------------------------------------------------------------------- /matlab/+dagnn/BilinearSampler.m: -------------------------------------------------------------------------------- 1 | % Wrapper for BilinearSampler block: 2 | % (c) 2016 Ankush Gupta 3 | 4 | classdef BilinearSampler < dagnn.Layer 5 | methods 6 | function outputs = forward(obj, inputs, params) 7 | outputs = vl_nnbilinearsampler(inputs{1}, inputs{2}); 8 | outputs = {outputs}; 9 | end 10 | 11 | function [derInputs, derParams] = backward(obj, inputs, param, derOutputs) 12 | [dX,dG] = vl_nnbilinearsampler(inputs{1}, inputs{2}, derOutputs{1}); 13 | derInputs = {dX,dG}; 14 | derParams = {}; 15 | end 16 | 17 | function outputSizes = getOutputSizes(obj, inputSizes) 18 | xSize = inputSizes{1}; 19 | gSize = inputSizes{2}; 20 | outputSizes = {[gSize(2), gSize(3), xSize(3), xSize(4)]}; 21 | end 22 | 23 | function obj = BilinearSampler(varargin) 24 | obj.load(varargin); 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nnrelu.m: -------------------------------------------------------------------------------- 1 | classdef nnrelu < nntest 2 | properties 3 | x 4 | end 5 | 6 | methods (TestClassSetup) 7 | function data(test,device) 8 | % make sure that all elements in x are different. in this way, 9 | % we can compute numerical derivatives reliably by adding a delta < .5. 10 | x = test.randn(15,14,3,2) ; 11 | x(:) = randperm(numel(x))' ; 12 | % avoid non-diff value for test 13 | x(x==0)=1 ; 14 | test.x = x ; 15 | test.range = 10 ; 16 | if strcmp(device,'gpu'), test.x = gpuArray(test.x) ; end 17 | end 18 | end 19 | 20 | methods (Test) 21 | function basic(test) 22 | x = test.x ; 23 | y = vl_nnrelu(x) ; 24 | dzdy = test.randn(size(y)) ; 25 | dzdx = vl_nnrelu(x,dzdy) ; 26 | test.der(@(x) vl_nnrelu(x), x, dzdy, dzdx, 1e-2 * test.range) ; 27 | end 28 | end 29 | end 30 | -------------------------------------------------------------------------------- /matlab/+dagnn/ODistLoss.m: -------------------------------------------------------------------------------- 1 | classdef ODistLoss < dagnn.Loss 2 | 3 | methods 4 | function outputs = forward(obj, inputs, params) 5 | result = abs(bsxfun(@minus,inputs{1},inputs{2})); 6 | result(result<1) = result(result<1).^2; 7 | outputs{1} = sum(result(:)); 8 | n = obj.numAveraged ; 9 | m = n + size(inputs{1},4) ; 10 | obj.average = (n * obj.average + gather(outputs{1})) / m ; 11 | obj.numAveraged = m ; 12 | end 13 | 14 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 15 | bp = bsxfun(@minus,inputs{1},inputs{2}); 16 | bp(bp<-1) = -0.5; 17 | bp(bp>1) = 0.5; 18 | derInputs{1} = 2.*bsxfun(@times,bp,derOutputs{1}); 19 | derInputs{2} = []; 20 | derParams = {} ; 21 | end 22 | 23 | function obj = ODistLoss(varargin) 24 | obj.load(varargin{:}) ; 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /matlab/+dagnn/@DagNN/renameLayer.m: -------------------------------------------------------------------------------- 1 | function renameLayer(obj, oldName, newName, varargin) 2 | %RENAMELAYER Rename a layer 3 | % RENAMELAYER(OLDNAME, NEWNAME) changes the name of the layer 4 | % OLDNAME into NEWNAME. NEWNAME should not be the name of an 5 | % existing layer. 6 | 7 | opts.quiet = false ; 8 | opts = vl_argparse(opts, varargin) ; 9 | 10 | % Find the layer to rename 11 | v = obj.getLayerIndex(oldName) ; 12 | if isnan(v) 13 | % There is no such layer, nothing to do 14 | if ~opts.quiet 15 | warning('There is no layer ''%s''.', oldName) ; 16 | end 17 | return ; 18 | end 19 | 20 | % Check if newName is an existing layer 21 | newNameExists = any(strcmp(newName, {obj.layers.name})) ; 22 | if newNameExists 23 | error('There is already a layer ''%s''.', newName) ; 24 | end 25 | 26 | % Replace oldName with newName in all the layers 27 | obj.layers(v).name = newName ; 28 | obj.rebuild() ; 29 | -------------------------------------------------------------------------------- /code/tools/rand_diff_class.m: -------------------------------------------------------------------------------- 1 | % function output = rand_diff_class(imdb,index,maxlabel) 2 | % % uniform possible for every train image 3 | % output = randi(numel(imdb.images.reallabel)); 4 | % while(imdb.images.reallabel(output) == imdb.images.reallabel(index) || imdb.images.set(output)~=1 ... 5 | % || (imdb.images.reallabel(output)<=maxlabel && imdb.images.reallabel(index)>maxlabel) ... 6 | % || (imdb.images.reallabel(output)>maxlabel && imdb.images.reallabel(index)<=maxlabel)) 7 | % output = randi(numel(imdb.images.reallabel)); 8 | % end 9 | % end 10 | 11 | 12 | function output = rand_diff_class(imdb,index) 13 | % uniform possible for every train image 14 | output = randi(numel(imdb.images.label)); 15 | while(imdb.images.label(output) == imdb.images.label(index) || imdb.images.set(output)~=1) 16 | output = randi(numel(imdb.images.label)); 17 | end 18 | end -------------------------------------------------------------------------------- /evaluation/utils/gen_train_sample_xqda.m: -------------------------------------------------------------------------------- 1 | function [train_sample1, train_sample2, label1, label2] = gen_train_sample_xqda(label, cam, train_feat) 2 | 3 | uni_label = unique(label); 4 | ind1 = []; 5 | ind2 = []; 6 | label1 = []; 7 | label2 = []; 8 | for n = 1:length(uni_label) 9 | pos = find(label == uni_label(n)); 10 | 11 | perm = randperm(length(pos)); 12 | tmp1 = pos(perm(1:floor(length(pos)/2))); 13 | tmp2 = pos(perm(floor(length(pos)/2)+1:floor(length(pos)/2)*2)); 14 | cam1 = cam(tmp1); 15 | cam2 = cam(tmp2); 16 | pos2 = find(cam1~=cam2); 17 | tmp1 = tmp1(pos2); 18 | tmp2 = tmp2(pos2); 19 | ind1 = [ind1; tmp1]; 20 | ind2 = [ind2; tmp2]; 21 | label1 = [label1; repmat(uni_label(n), [length(tmp1), 1])]; 22 | label2 = [label2; repmat(uni_label(n), [length(tmp2), 1])]; 23 | end 24 | train_sample1 = train_feat(:, ind1)'; 25 | train_sample2 = train_feat(:, ind2)'; -------------------------------------------------------------------------------- /matlab/xtest/vl_test_economic_relu.m: -------------------------------------------------------------------------------- 1 | % VL_TEST_ECONOMIC_RELU 2 | function vl_test_economic_relu() 3 | 4 | x = randn(11,12,8,'single'); 5 | w = randn(5,6,8,9,'single'); 6 | b = randn(1,9,'single') ; 7 | 8 | net.layers{1} = struct('type', 'conv', ... 9 | 'filters', w, ... 10 | 'biases', b, ... 11 | 'stride', 1, ... 12 | 'pad', 0); 13 | net.layers{2} = struct('type', 'relu') ; 14 | 15 | res = vl_simplenn(net, x) ; 16 | dzdy = randn(size(res(end).x), 'like', res(end).x) ; 17 | clear res ; 18 | 19 | res_ = vl_simplenn(net, x, dzdy) ; 20 | res__ = vl_simplenn(net, x, dzdy, [], 'conserveMemory', true) ; 21 | 22 | a=whos('res_') ; 23 | b=whos('res__') ; 24 | assert(a.bytes > b.bytes) ; 25 | vl_testsim(res_(1).dzdx,res__(1).dzdx,1e-4) ; 26 | vl_testsim(res_(1).dzdw{1},res__(1).dzdw{1},1e-4) ; 27 | vl_testsim(res_(1).dzdw{2},res__(1).dzdw{2},1e-4) ; 28 | -------------------------------------------------------------------------------- /matlab/+dagnn/@DagNN/move.m: -------------------------------------------------------------------------------- 1 | function move(obj, device) 2 | %MOVE Move the DagNN to either CPU or GPU 3 | % MOVE(obj, 'cpu') moves the DagNN obj to the CPU. 4 | % 5 | % MOVE(obj, 'gpu') moves the DagNN obj to the GPU. 6 | 7 | % Copyright (C) 2015 Karel Lenc and Andrea Vedaldi. 8 | % All rights reserved. 9 | % 10 | % This file is part of the VLFeat library and is made available under 11 | % the terms of the BSD license (see the COPYING file). 12 | 13 | obj.reset() ; 14 | obj.device = device ; 15 | switch device 16 | case 'gpu' 17 | for i=1:numel(obj.params) 18 | obj.params(i).value = gpuArray(obj.params(i).value) ; 19 | end 20 | case 'cpu' 21 | for i=1:numel(obj.params) 22 | obj.params(i).value = gather(obj.params(i).value) ; 23 | end 24 | otherwise 25 | error('DEVICE must be either ''cpu'' or ''gpu''.') ; 26 | end 27 | for l = 1:numel(obj.layers) 28 | obj.layers(l).block.move(device) ; 29 | end 30 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nnbnorm.m: -------------------------------------------------------------------------------- 1 | classdef nnbnorm < nntest 2 | properties (TestParameter) 3 | rows = {2 8 13} 4 | cols = {2 8 17} 5 | numDims = {1 3 4} 6 | batchSize = {2 7} 7 | end 8 | methods (Test) 9 | function basic(test, rows, cols, numDims, batchSize) 10 | r = rows ; 11 | c = cols ; 12 | nd = numDims ; 13 | bs = batchSize ; 14 | x = test.randn(r, c, nd, bs) ; 15 | %g = test.randn(1, 1, nd, 1) ; 16 | %b = test.randn(1, 1, nd, 1) ; 17 | g = test.randn(nd, 1) / test.range ; 18 | b = test.randn(nd, 1) / test.range ; 19 | 20 | y = vl_nnbnorm(x,g,b) ; 21 | dzdy = test.randn(size(y)) ; 22 | [dzdx,dzdg,dzdb] = vl_nnbnorm(x,g,b,dzdy) ; 23 | 24 | test.der(@(x) vl_nnbnorm(x,g,b), x, dzdy, dzdx, test.range * 1e-3) ; 25 | test.der(@(g) vl_nnbnorm(x,g,b), g, dzdy, dzdg, 1e-2) ; 26 | test.der(@(b) vl_nnbnorm(x,g,b), b, dzdy, dzdb, 1e-3) ; 27 | end 28 | end 29 | end -------------------------------------------------------------------------------- /matlab/src/bits/nnbias.hpp: -------------------------------------------------------------------------------- 1 | // @file nnbias.hpp 2 | // @brief Bias block 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2015 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__nnbias__ 14 | #define __vl__nnbias__ 15 | 16 | #include "data.hpp" 17 | 18 | namespace vl { 19 | 20 | vl::ErrorCode 21 | nnbias_forward(vl::Context& context, 22 | vl::Tensor output, double outputMult, 23 | vl::Tensor data, double dataMult, 24 | vl::Tensor biases, double biasesMult) ; 25 | 26 | vl::ErrorCode 27 | nnbias_backward(vl::Context& context, 28 | vl::Tensor derData, double derDataMult, 29 | vl::Tensor derBiases, double derBiasesMult, 30 | vl::Tensor derOutput, double derOutputMult) ; 31 | } 32 | 33 | #endif /* defined(__vl__nnbias__) */ 34 | -------------------------------------------------------------------------------- /matlab/vl_nnnoffset.m: -------------------------------------------------------------------------------- 1 | function y = vl_nnnoffset(x, param, dzdy) 2 | %VL_NNNOFFSET CNN norm-dependent offset. 3 | % Y = VL_NNNOFFSET(X, PARAM) subtracts from each element of X the 4 | % weighted norm of the feature channels: 5 | % 6 | % X(i,j,k) = X(i,j,k) - PARAM(1) * L(i,j) ^ PARAM(2) 7 | % 8 | % where 9 | % 10 | % L(i,j) = sum_K X(i,j,k)^2 11 | % 12 | % DZDX = VL_NNNOFFSET(X, PARAM, DZDY) computes the derivative of the 13 | % block projected onto DZDY. DZDX and DZDY have the same dimensions 14 | % as X and Y respectively. 15 | 16 | % Copyright (C) 2014 Andrea Vedaldi. 17 | % All rights reserved. 18 | % 19 | % This file is part of the VLFeat library and is made available under 20 | % the terms of the BSD license (see the COPYING file). 21 | 22 | L = sum(x.^2,3) ; 23 | L = max(L, 1e-8) ; 24 | 25 | if nargin <= 2 26 | y = bsxfun(@minus, x, param(1)*L.^param(2)) ; 27 | else 28 | y = dzdy - bsxfun(@times, (2*param(1)*param(2))* x, sum(dzdy,3) .* (L.^(param(2)-1))) ; 29 | end 30 | -------------------------------------------------------------------------------- /evaluation/utils/applypca2.m: -------------------------------------------------------------------------------- 1 | function [ux,u,m] = applypca2(X) 2 | % [ux,u,m] = applypca(X) 3 | % 4 | % finds principal components of 5 | % 6 | % input: 7 | % x input data (each column is an input vector) 8 | % 9 | % output: 10 | % ux data projected onto pca (1st row=leading principal component) 11 | % u the orthogonal matrix with all principal components 12 | % m mean of the data 13 | % 14 | % You can recover the original data with 15 | % 16 | % xoriginal=u*ux+repmat(m,1,size(ux,2)); 17 | % 18 | % See also pca 19 | % 20 | % copyright by Kilian Q. Weinberger, 2006 21 | 22 | % [u,v]=pca(X); 23 | [d,N] = size(X); 24 | 25 | mm = mean(X,2); 26 | X = X - mm*ones(1,N); % remove mean from data 27 | 28 | cc = cov(X,1); % compute covariance 29 | [cvv,cdd] = eig(cc); % compute eignvectors 30 | [zz,ii] = sort(diag(-cdd)); % sort according to eigenvalues 31 | evects = cvv(:,ii); % pick leading eigenvectors 32 | u = X*evects; 33 | 34 | 35 | m=mean(X,2); 36 | ux=u'*(X-repmat(m,1,size(X,2))); 37 | 38 | -------------------------------------------------------------------------------- /train.m: -------------------------------------------------------------------------------- 1 | if ~exist('./data') 2 | mkdir('./data'); 3 | end 4 | 5 | if ~exist('./result') 6 | mkdir('./result'); 7 | end 8 | 9 | addpath(genpath('./code')); 10 | addpath(genpath('./data')); 11 | addpath(genpath('./result')); 12 | 13 | if ~exist('./data/url_data.mat','file') 14 | prepare_data; 15 | end 16 | if ~exist('./data/url_data_gan_24000.mat','file') 17 | prepare_gan_data; 18 | end 19 | if ~exist('./data/sMpRL_24000.mat','file') 20 | prepare_sMpRL_label4data; 21 | end 22 | 23 | 24 | num_predefined_classes = 751; 25 | dropoutrate = 0.75; 26 | 27 | num_real = 12936; 28 | num_gan = 24000; 29 | 30 | %% tran the baseline model 31 | train_res_iden_baseline(num_predefined_classes, dropoutrate); 32 | 33 | %% train 34 | train_res_iden_sMpRL(num_predefined_classes, dropoutrate, num_real, num_gan); 35 | train_res_iden_MpRL2(num_predefined_classes, dropoutrate, num_real, num_gan); 36 | train_res_iden_MpRL3(num_predefined_classes, dropoutrate, num_real, num_gan); 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /matlab/vl_setupnn.m: -------------------------------------------------------------------------------- 1 | function vl_setupnn() 2 | %VL_SETUPNN Setup the MatConvNet toolbox. 3 | % VL_SETUPNN() function adds the MatConvNet toolbox to MATLAB path. 4 | 5 | % Copyright (C) 2014-15 Andrea Vedaldi. 6 | % All rights reserved. 7 | % 8 | % This file is part of the VLFeat library and is made available under 9 | % the terms of the BSD license (see the COPYING file). 10 | 11 | root = vl_rootnn() ; 12 | addpath(fullfile(root, 'matlab')) ; 13 | addpath(fullfile(root, 'matlab', 'mex')) ; 14 | addpath(fullfile(root, 'matlab', 'simplenn')) ; 15 | addpath(fullfile(root, 'matlab', 'xtest')) ; 16 | addpath(fullfile(root, 'examples')) ; 17 | 18 | if ~exist('gather') 19 | warning('The MATLAB Parallel Toolbox does not seem to be installed. Activating compatibility functions.') ; 20 | addpath(fullfile(root, 'matlab', 'compatibility', 'parallel')) ; 21 | end 22 | 23 | if numel(dir(fullfile(root, 'matlab', 'mex', 'vl_nnconv.mex*'))) == 0 24 | warning('MatConvNet is not compiled. Consider running `vl_compilenn`.'); 25 | end 26 | -------------------------------------------------------------------------------- /matlab/xtest/vl_test_imreadjpeg.m: -------------------------------------------------------------------------------- 1 | function vl_test_imreadjpeg 2 | % VL_TEST_IMREADJPEG 3 | 4 | % Test basic file reading capability 5 | for t=1:6 6 | files{t} = which(sprintf('office_%d.jpg', t)) ; 7 | end 8 | ims = vl_imreadjpeg(files) ; 9 | 10 | % Test inserting a non-image file 11 | files_ = files ; 12 | files_{3} = [mfilename('fullpath') '.m']; 13 | ims_ = vl_imreadjpeg(files_) ; 14 | for t=setdiff(1:6,3) 15 | assert(isequal(ims{t},ims_{t})) ; 16 | end 17 | 18 | % Test inserting a non-esiting file 19 | files__ = files_ ; 20 | files__{4} = 'idontexist.jpg' ; 21 | ims__ = vl_imreadjpeg(files__) ; 22 | for t=setdiff(1:6,[3 4]) 23 | assert(isequal(ims{t},ims__{t})) ; 24 | end 25 | 26 | for n = 1:4 27 | % Test prefetching 28 | vl_imreadjpeg(files,'prefetch', 'numThreads', n) ; 29 | ims___ = vl_imreadjpeg(files) ; 30 | assert(isequal(ims,ims___)) ; 31 | 32 | % Hardening: test prefetching, clearing mex, fetching 33 | vl_imreadjpeg(files,'prefetch') ; 34 | clear mex ; 35 | ims___ = vl_imreadjpeg(files, 'numThreads', n) ; 36 | assert(isequal(ims,ims___)) ; 37 | end 38 | -------------------------------------------------------------------------------- /matlab/vl_nnsoftmax.m: -------------------------------------------------------------------------------- 1 | function Y = vl_nnsoftmax(X,dzdY) 2 | %VL_NNSOFTMAX CNN softmax. 3 | % Y = VL_NNSOFTMAX(X) applies the softmax operator the data X. X 4 | % has dimension H x W x D x N, packing N arrays of W x H 5 | % D-dimensional vectors. 6 | % 7 | % D can be thought of as the number of possible classes and the 8 | % function computes the softmax along the D dimension. Often W=H=1, 9 | % but this is not a requirement, as the operator is applied 10 | % convolutionally at all spatial locations. 11 | % 12 | % DZDX = VL_NNSOFTMAX(X, DZDY) computes the derivative of the block 13 | % projected onto DZDY. DZDX and DZDY have the same dimensions as 14 | % X and Y respectively. 15 | 16 | % Copyright (C) 2014 Andrea Vedaldi. 17 | % All rights reserved. 18 | % 19 | % This file is part of the VLFeat library and is made available under 20 | % the terms of the BSD license (see the COPYING file). 21 | 22 | E = exp(bsxfun(@minus, X, max(X,[],3))) ; 23 | L = sum(E,3) ; 24 | Y = bsxfun(@rdivide, E, L) ; 25 | 26 | if nargin <= 1, return ; end 27 | 28 | % backward 29 | Y = Y .* bsxfun(@minus, dzdY, sum(dzdY .* Y, 3)) ; 30 | -------------------------------------------------------------------------------- /matlab/src/bits/nnnormalize.hpp: -------------------------------------------------------------------------------- 1 | // @file nnnormalize.hpp 2 | // @brief Normalization block 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2014-16 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__nnnormalize__ 14 | #define __vl__nnnormalize__ 15 | 16 | #include "data.hpp" 17 | #include 18 | 19 | namespace vl { 20 | 21 | vl::ErrorCode 22 | nnlrn_forward(vl::Context& context, 23 | vl::Tensor output, 24 | vl::Tensor data, 25 | size_t normDetph, 26 | double kappa, double alpha, double beta) ; 27 | 28 | vl::ErrorCode 29 | nnlrn_backward(vl::Context& context, 30 | vl::Tensor derData, 31 | vl::Tensor data, 32 | vl::Tensor derOutput, 33 | size_t normDetph, 34 | double kappa, double alpha, double beta) ; 35 | } 36 | 37 | #endif /* defined(__vl__nnnormalize__) */ 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 YAN HUANG 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /matlab/vl_nnsoftmax_temper.m: -------------------------------------------------------------------------------- 1 | function Y = vl_nnsoftmax(X,temp,dzdY) 2 | %VL_NNSOFTMAX CNN softmax. 3 | % Y = VL_NNSOFTMAX(X) applies the softmax operator the data X. X 4 | % has dimension H x W x D x N, packing N arrays of W x H 5 | % D-dimensional vectors. 6 | % 7 | % D can be thought of as the number of possible classes and the 8 | % function computes the softmax along the D dimension. Often W=H=1, 9 | % but this is not a requirement, as the operator is applied 10 | % convolutionally at all spatial locations. 11 | % 12 | % DZDX = VL_NNSOFTMAX(X, DZDY) computes the derivative of the block 13 | % projected onto DZDY. DZDX and DZDY have the same dimensions as 14 | % X and Y respectively. 15 | 16 | % Copyright (C) 2014 Andrea Vedaldi. 17 | % All rights reserved. 18 | % 19 | % This file is part of the VLFeat library and is made available under 20 | % the terms of the BSD license (see the COPYING file). 21 | 22 | E = exp(bsxfun(@minus, X, max(X,[],3))./temper) ; 23 | L = sum(E,3) ; 24 | Y = bsxfun(@rdivide, E, L) ; 25 | 26 | if nargin <= 1, return ; end 27 | 28 | % backward 29 | Y = Y .* bsxfun(@minus, dzdY, sum(dzdY .* Y, 3)) ; 30 | -------------------------------------------------------------------------------- /evaluation/compute_AP_rerank.m: -------------------------------------------------------------------------------- 1 | function [ap, cmc] = compute_AP(good_image, junk_image, index) 2 | 3 | cmc = zeros(length(index), 1); 4 | ngood = length(good_image); 5 | 6 | old_recall = 0; 7 | old_precision = 1.0; 8 | ap = 0; 9 | intersect_size = 0; 10 | j = 0; 11 | good_now = 0; 12 | njunk = 0; 13 | for n = 1:length(index) 14 | flag = 0; 15 | if ~isempty(find(good_image == index(n), 1)) 16 | cmc(n-njunk:end) = 1; 17 | flag = 1; % good image 18 | good_now = good_now+1; 19 | end 20 | if ~isempty(find(junk_image == index(n), 1)) 21 | njunk = njunk + 1; 22 | continue; % junk image 23 | end 24 | 25 | if flag == 1%good 26 | intersect_size = intersect_size + 1; 27 | end 28 | recall = intersect_size/ngood; 29 | precision = intersect_size/(j + 1); 30 | ap = ap + (recall - old_recall)*((old_precision+precision)/2); 31 | old_recall = recall; 32 | old_precision = precision; 33 | j = j+1; 34 | 35 | if good_now == ngood 36 | return; 37 | end 38 | end 39 | 40 | end 41 | 42 | 43 | -------------------------------------------------------------------------------- /evaluation/utils/compute_AP.m: -------------------------------------------------------------------------------- 1 | function [ap, cmc] = compute_AP(good_image, junk_image, index) 2 | 3 | cmc = zeros(length(index), 1); 4 | ngood = length(good_image); 5 | 6 | old_recall = 0; 7 | old_precision = 1.0; 8 | ap = 0; 9 | intersect_size = 0; 10 | j = 0; 11 | good_now = 0; 12 | njunk = 0; 13 | for n = 1:length(index) 14 | flag = 0; 15 | if ~isempty(find(good_image == index(n), 1)) 16 | cmc(n-njunk:end) = 1; 17 | flag = 1; % good image 18 | good_now = good_now+1; 19 | end 20 | if ~isempty(find(junk_image == index(n), 1)) 21 | njunk = njunk + 1; 22 | continue; % junk image 23 | end 24 | 25 | if flag == 1%good 26 | intersect_size = intersect_size + 1; 27 | end 28 | recall = intersect_size/ngood; 29 | precision = intersect_size/(j + 1); 30 | ap = ap + (recall - old_recall)*((old_precision+precision)/2); 31 | old_recall = recall; 32 | old_precision = precision; 33 | j = j+1; 34 | 35 | if good_now == ngood 36 | return; 37 | end 38 | end 39 | 40 | end 41 | 42 | 43 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nnmnist.m: -------------------------------------------------------------------------------- 1 | classdef nnmnist < nntest 2 | properties (TestParameter) 3 | networkType = {'dagnn', 'simplenn'} 4 | end 5 | 6 | methods (TestClassSetup) 7 | function init(test) 8 | addpath(fullfile(vl_rootnn, 'examples', 'mnist')); 9 | end 10 | end 11 | 12 | methods (Test) 13 | function valErrorRate(test, networkType) 14 | clear mex ; % will reset GPU, remove MCN to avoid crashing 15 | % MATLAB on exit (BLAS issues?) 16 | if strcmp(test.dataType, 'double'), return ; end 17 | switch test.currentDevice 18 | case 'cpu' 19 | gpus = []; 20 | case 'gpu' 21 | gpus = 1; 22 | end 23 | trainOpts = struct('numEpochs', 1, 'continue', false, 'gpus', gpus, ... 24 | 'plotStatistics', false); 25 | if strcmp(networkType, 'simplenn') 26 | trainOpts.errorLabels = {'error', 'top5err'} ; 27 | end 28 | [~, info] = cnn_mnist('train', trainOpts, 'networkType', networkType); 29 | test.verifyLessThan(info.train.error, 0.08); 30 | test.verifyLessThan(info.val.error, 0.025); 31 | end 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/nnbilinearsampler_cudnn.hpp: -------------------------------------------------------------------------------- 1 | // @file nnbilinearsampler_cudnn.hpp 2 | // @brief BilinearSampler CuDNN-based implementation. 3 | // @author Ankush Gupta, Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2016 Ankush Gupta and Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__bilinearsampler_cudnn__ 14 | #define __vl__bilinearsampler_cudnn__ 15 | 16 | #include "../data.hpp" 17 | #include "cudnn.h" 18 | 19 | namespace vl { namespace impl { 20 | 21 | template 22 | struct nnbilinearsampler_cudnn 23 | { 24 | static vl::ErrorCode 25 | forward(Context& context, 26 | Tensor output, 27 | Tensor data, 28 | Tensor grid) ; 29 | 30 | static vl::ErrorCode 31 | backward(Context& context, 32 | Tensor derData, 33 | Tensor derGrid, 34 | Tensor data, 35 | Tensor grid, 36 | Tensor derOutput) ; 37 | } ; 38 | 39 | } } 40 | 41 | #endif /* defined(__vl__nnbilinearsampler_cudnn__) */ 42 | -------------------------------------------------------------------------------- /matlab/src/bits/nnbilinearsampler.hpp: -------------------------------------------------------------------------------- 1 | // @file nnbilinearsampler.hpp 2 | // @brief Bilinear sampler block 3 | // @author Ankush Gupta 4 | // @author Andrea Vedaldi 5 | 6 | /* 7 | Copyright (C) 2016- Ankush Gupta and Andrea Vedaldi. 8 | All rights reserved. 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__nnbilinearsampler__ 14 | #define __vl__nnbilinearsampler__ 15 | 16 | #include "data.hpp" 17 | #include 18 | 19 | namespace vl { 20 | vl::ErrorCode 21 | nnbilinearsampler_forward(vl::Context& context, 22 | vl::Tensor output, 23 | vl::Tensor data, 24 | vl::Tensor grid) ; 25 | 26 | vl::ErrorCode 27 | nnbilinearsampler_backward(vl::Context& context, 28 | vl::Tensor derData, 29 | vl::Tensor derGrid, 30 | vl::Tensor data, 31 | vl::Tensor grid, 32 | vl::Tensor derOutput) ; 33 | } 34 | 35 | #endif /* defined(__vl__nnbilinearsampler__) */ 36 | -------------------------------------------------------------------------------- /matlab/xtest/vl_bench_bnorm.m: -------------------------------------------------------------------------------- 1 | function vl_bench_bnorm(gpu) 2 | if nargin < 1 3 | gpu = false ; 4 | end 5 | 6 | T = 100 ; 7 | x = randn(64,64,32,32,'single') ; 8 | g = randn(32,1,'single') ; 9 | b = randn(32,1,'single') ; 10 | 11 | if gpu 12 | x = gpuArray(x) ; 13 | g = gpuArray(g) ; 14 | b = gpuArray(b) ; 15 | end 16 | 17 | tic 18 | for t=1:T 19 | y = vl_nnbnorm(x,g,b) ; 20 | end 21 | if gpu, wait(gpuDevice) ; end 22 | fprintf('new: %f\n',toc); 23 | 24 | tic 25 | for t=1:T 26 | y_ = vl_nnbnorm_old(x,g,b) ; 27 | end 28 | if gpu, wait(gpuDevice) ; end 29 | fprintf('old: %f\n',toc); 30 | 31 | dzdy = randn(size(y),'single') ; 32 | if gpu 33 | dzdy = gpuArray(dzdy) ; 34 | end 35 | 36 | tic 37 | for t=1:T 38 | [a,b,c] = vl_nnbnorm(x,g,b,dzdy) ; 39 | end 40 | if gpu, wait(gpuDevice) ; end 41 | fprintf('new deriv: %f\n',toc); 42 | 43 | tic 44 | for t=1:T 45 | [a_,b_,c_] = vl_nnbnorm_old(x,g,b,dzdy) ; 46 | end 47 | if gpu, wait(gpuDevice) ; end 48 | fprintf('old deriv: %f\n',toc); 49 | 50 | vl_testsim(y,y_); 51 | vl_testsim(a,a_); 52 | vl_testsim(b,b_); 53 | vl_testsim(c,c_); 54 | end 55 | -------------------------------------------------------------------------------- /evaluation/draw_confusion_matrix.m: -------------------------------------------------------------------------------- 1 | % calculate and draw confusion matrix 2 | function [ap_mat, r1_mat] = draw_confusion_matrix(ap, r1, queryCam) 3 | 4 | ap_mat = zeros(6, 6); 5 | r1_mat = zeros(6, 6); 6 | count1 = zeros(6, 6); 7 | count2 = zeros(6, 6); 8 | for n = 1:length(queryCam) 9 | for k = 1:6 10 | cc = queryCam(n); 11 | ap_mat(cc, k) = ap_mat(cc, k) + ap(n, k); 12 | if ap(n, k) ~= 0 13 | count1(cc, k) = count1(cc, k) + 1; 14 | end 15 | if r1(n, k) >= 0 16 | r1_mat(cc, k) = r1_mat(cc, k) + r1(n, k); 17 | count2(cc, k) = count2(cc, k) + 1; 18 | end 19 | end 20 | end 21 | num_class = 6; 22 | ap_mat = ap_mat./count1; 23 | name_class{1} = 'Cam1'; 24 | name_class{2} = 'Cam2'; 25 | name_class{3} = 'Cam3'; 26 | name_class{4} = 'Cam4'; 27 | name_class{5} = 'Cam5'; 28 | name_class{6} = 'Cam6'; 29 | draw_cm(ap_mat,name_class,num_class); 30 | 31 | r1_mat = r1_mat./count2; 32 | name_class{1} = 'Cam1'; 33 | name_class{2} = 'Cam2'; 34 | name_class{3} = 'Cam3'; 35 | name_class{4} = 'Cam4'; 36 | name_class{5} = 'Cam5'; 37 | name_class{6} = 'Cam6'; 38 | draw_cm(r1_mat,name_class,num_class); -------------------------------------------------------------------------------- /matlab/+dagnn/Scale.m: -------------------------------------------------------------------------------- 1 | classdef Scale < dagnn.ElementWise 2 | properties 3 | size 4 | hasBias = true 5 | end 6 | 7 | methods 8 | 9 | function outputs = forward(obj, inputs, params) 10 | args = horzcat(inputs, params) ; 11 | outputs{1} = bsxfun(@times, args{1}, args{2}) ; 12 | if obj.hasBias 13 | outputs{1} = bsxfun(@plus, outputs{1}, args{3}) ; 14 | end 15 | end 16 | 17 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 18 | args = horzcat(inputs, params) ; 19 | sz = [size(args{2}) 1 1 1 1] ; 20 | sz = sz(1:4) ; 21 | dargs{1} = bsxfun(@times, derOutputs{1}, args{2}) ; 22 | dargs{2} = derOutputs{1} .* args{1} ; 23 | for k = find(sz == 1) 24 | dargs{2} = sum(dargs{2}, k) ; 25 | end 26 | if obj.hasBias 27 | dargs{3} = derOutputs{1} ; 28 | for k = find(sz == 1) 29 | dargs{3} = sum(dargs{3}, k) ; 30 | end 31 | end 32 | derInputs = dargs(1:numel(inputs)) ; 33 | derParams = dargs(numel(inputs)+(1:numel(params))) ; 34 | end 35 | 36 | function obj = Scale(varargin) 37 | obj.load(varargin) ; 38 | end 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /evaluation/utils/MahDist.m: -------------------------------------------------------------------------------- 1 | function dist = MahDist(M, Xg, Xp) 2 | %% function dist = MahDist(M, Xg, Xp) 3 | % Mahalanobis distance 4 | % 5 | % Input: 6 | % : the metric kernel 7 | % : features of the gallery samples. Size: [n, d] 8 | % [Xp]: features of the probe samples. Optional. Size: [m, d] 9 | % 10 | % Note: MahDist(M, Xg) is the same as MahDist(M, Xg, Xg). 11 | % 12 | % Output: 13 | % dist: the computed distance matrix between Xg and Xp 14 | % 15 | % Reference: 16 | % Shengcai Liao, Yang Hu, Xiangyu Zhu, and Stan Z. Li. Person 17 | % re-identification by local maximal occurrence representation and metric 18 | % learning. In IEEE Conference on Computer Vision and Pattern Recognition, 2015. 19 | % 20 | % Version: 1.0 21 | % Date: 2014-05-15 22 | % 23 | % Author: Shengcai Liao 24 | % Institute: National Laboratory of Pattern Recognition, 25 | % Institute of Automation, Chinese Academy of Sciences 26 | % Email: scliao@nlpr.ia.ac.cn 27 | 28 | 29 | if nargin == 2 30 | D = Xg * M * Xg'; 31 | u = diag(D); 32 | dist = bsxfun(@plus, u, u') - 2 * D; 33 | else 34 | u = sum( (Xg * M) .* Xg, 2); 35 | v = sum( (Xp * M) .* Xp, 2); 36 | dist = bsxfun(@plus, u, v') - 2 * Xg * M * Xp'; 37 | end 38 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/normalize.hpp: -------------------------------------------------------------------------------- 1 | // @file normalize.hpp 2 | // @brief Normalize block implementation 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2014-16 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__normalize__ 14 | #define __vl__normalize__ 15 | 16 | #include "../data.hpp" 17 | #include 18 | 19 | namespace vl { namespace impl { 20 | 21 | template 22 | struct lrn 23 | { 24 | static vl::ErrorCode 25 | forward(type* output, 26 | type const* data, 27 | size_t height, size_t width, size_t depth, size_t size, 28 | size_t normDetph, 29 | type kappa, type alpha, type beta) ; 30 | 31 | static vl::ErrorCode 32 | backward(type* derData, 33 | type const* data, 34 | type const* derOutput, 35 | size_t height, size_t width, size_t depth, size_t size, 36 | size_t normDetph, 37 | type kappa, type alpha, type beta) ; 38 | } ; 39 | 40 | } } 41 | 42 | #endif /* __vl__normalize__ */ 43 | -------------------------------------------------------------------------------- /evaluation/evaluation.m: -------------------------------------------------------------------------------- 1 | function [CMC, map, r1_pairwise, ap_pairwise] = evaluation(dist, label_gallery, label_query, cam_gallery, cam_query) 2 | 3 | junk0 = find(label_gallery == -1); 4 | ap = zeros(size(dist, 2), 1); 5 | CMC = []; 6 | r1_pairwise = zeros(size(dist, 2), 6);% pairwise rank 1 precision 7 | ap_pairwise = zeros(size(dist, 2), 6); % pairwise average precision 8 | 9 | for k = 1:size(dist, 2) 10 | k; 11 | score = dist(:, k); 12 | q_label = label_query(k); 13 | q_cam = cam_query(k); 14 | pos = find(label_gallery == q_label); 15 | pos2 = cam_gallery(pos) ~= q_cam; 16 | good_image = pos(pos2); 17 | pos3 = cam_gallery(pos) == q_cam; 18 | junk = pos(pos3); 19 | junk_image = [junk0; junk]; 20 | [~, index] = sort(score, 'ascend'); 21 | [ap(k), CMC(:, k)] = compute_AP(good_image, junk_image, index); 22 | %ap_pairwise(k, :) = compute_AP_multiCam(good_image, junk, index, q_cam, cam_gallery); % compute pairwise AP for single query 23 | %r1_pairwise(k, :) = compute_r1_multiCam(good_image, junk, index, q_cam, cam_gallery); % pairwise rank 1 precision with single query 24 | end 25 | CMC = sum(CMC, 2)./size(dist, 2); 26 | CMC = CMC'; 27 | ap(isnan(ap) == 1) = 0; 28 | map = sum(ap)/length(ap); -------------------------------------------------------------------------------- /matlab/src/bits/impl/nnbias_cudnn.hpp: -------------------------------------------------------------------------------- 1 | // @file nnbias_blas.hpp 2 | // @brief biasolution block CuDNN-based implementation. 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2015 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__nnbias_cudnn__ 14 | #define __vl__nnbias_cudnn__ 15 | 16 | #include "../data.hpp" 17 | #include "cudnn.h" 18 | 19 | namespace vl { namespace impl { 20 | 21 | // todo: data type should be handled internally? 22 | 23 | template 24 | struct nnbias_cudnn 25 | { 26 | static vl::ErrorCode 27 | forward(vl::Context& context, 28 | vl::Tensor output, double outputMult, 29 | vl::Tensor data, double dataMult, 30 | vl::Tensor biases, double biasesMult) ; 31 | 32 | static vl::ErrorCode 33 | backward(vl::Context& context, 34 | vl::Tensor derData, double derDataMult, 35 | vl::Tensor derBiases, double derBiasesMult, 36 | vl::Tensor derOutput, double derOutputMult) ; 37 | } ; 38 | 39 | } } 40 | 41 | #endif /* defined(__vl__nnbias_cudnn__) */ 42 | -------------------------------------------------------------------------------- /matlab/src/bits/nnfullyconnected.hpp: -------------------------------------------------------------------------------- 1 | // @file nnfullyconnected.hpp 2 | // @brief Fully-connected block 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2014-16 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | 14 | #ifndef __vl__nnfullyconnected__ 15 | #define __vl__nnfullyconnected__ 16 | 17 | #include "data.hpp" 18 | 19 | namespace vl { 20 | 21 | vl::ErrorCode 22 | nnfullyconnected_forward(vl::Context& context, 23 | vl::Tensor output, 24 | vl::Tensor data, 25 | vl::Tensor filters, 26 | vl::Tensor biases) ; 27 | 28 | vl::ErrorCode 29 | nnfullyconnected_backward(vl::Context& context, 30 | vl::Tensor derData, 31 | vl::Tensor derFilters, 32 | vl::Tensor derBiases, 33 | vl::Tensor data, 34 | vl::Tensor filters, 35 | vl::Tensor derOutput) ; 36 | } 37 | 38 | 39 | #endif /* defined(__vl__nnfullyconnected__) */ 40 | -------------------------------------------------------------------------------- /evaluation/utils/draw_confusion_matrix.m: -------------------------------------------------------------------------------- 1 | % calculate and draw confusion matrix 2 | function [ap_mat, r1_mat] = draw_confusion_matrix(ap, r1, queryCam) 3 | 4 | ap_mat = zeros(6, 6); 5 | r1_mat = zeros(6, 6); 6 | count1 = zeros(6, 6); 7 | count2 = zeros(6, 6); 8 | for n = 1:length(queryCam) 9 | for k = 1:6 10 | ap_mat(queryCam(n), k) = ap_mat(queryCam(n), k) + ap(n, k); 11 | if ap(n, k) ~= 0 12 | count1(queryCam(n), k) = count1(queryCam(n), k) + 1; 13 | end 14 | if r1(n, k) >= 0 15 | r1_mat(queryCam(n), k) = r1_mat(queryCam(n), k) + r1(n, k); 16 | count2(queryCam(n), k) = count2(queryCam(n), k) + 1; 17 | end 18 | end 19 | end 20 | num_class = 6; 21 | ap_mat = ap_mat./count1; 22 | name_class{1} = 'Cam1'; 23 | name_class{2} = 'Cam2'; 24 | name_class{3} = 'Cam3'; 25 | name_class{4} = 'Cam4'; 26 | name_class{5} = 'Cam5'; 27 | name_class{6} = 'Cam6'; 28 | draw_cm(ap_mat,name_class,num_class); 29 | 30 | r1_mat = r1_mat./count2; 31 | name_class{1} = 'Cam1'; 32 | name_class{2} = 'Cam2'; 33 | name_class{3} = 'Cam3'; 34 | name_class{4} = 'Cam4'; 35 | name_class{5} = 'Cam5'; 36 | name_class{6} = 'Cam6'; 37 | draw_cm(r1_mat,name_class,num_class); -------------------------------------------------------------------------------- /evaluation/utils/evaluation.m: -------------------------------------------------------------------------------- 1 | function [CMC, map, r1_pairwise, ap_pairwise] = evaluation(dist, label_gallery, label_query, cam_gallery, cam_query) 2 | 3 | junk0 = find(label_gallery == -1); 4 | ap = zeros(size(dist, 2), 1); 5 | CMC = []; 6 | r1_pairwise = zeros(size(dist, 2), 6);% pairwise rank 1 precision 7 | ap_pairwise = zeros(size(dist, 2), 6); % pairwise average precision 8 | 9 | for k = 1:size(dist, 2) 10 | k; 11 | score = dist(:, k); 12 | q_label = label_query(k); 13 | q_cam = cam_query(k); 14 | pos = find(label_gallery == q_label); 15 | pos2 = cam_gallery(pos) ~= q_cam; 16 | good_image = pos(pos2); 17 | pos3 = cam_gallery(pos) == q_cam; 18 | junk = pos(pos3); 19 | junk_image = [junk0; junk]; 20 | [~, index] = sort(score, 'ascend'); 21 | [ap(k), CMC(:, k)] = compute_AP(good_image, junk_image, index); 22 | %ap_pairwise(k, :) = compute_AP_multiCam(good_image, junk, index, q_cam, cam_gallery); % compute pairwise AP for single query 23 | %r1_pairwise(k, :) = compute_r1_multiCam(good_image, junk, index, q_cam, cam_gallery); % pairwise rank 1 precision with single query 24 | end 25 | CMC = sum(CMC, 2)./size(dist, 2); 26 | CMC = CMC'; 27 | ap(isnan(ap) == 1) = 0; 28 | map = sum(ap)/length(ap); -------------------------------------------------------------------------------- /matlab/src/bits/nnsubsample.hpp: -------------------------------------------------------------------------------- 1 | // @file nnsubsample.hpp 2 | // @brief Subsamping block 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2014-16 Andrea Vedaldi and Karel Lenc. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__nnsubsample__ 14 | #define __vl__nnsubsample__ 15 | 16 | #include "data.hpp" 17 | 18 | namespace vl { 19 | 20 | vl::ErrorCode 21 | nnsubsample_forward(vl::Context& context, 22 | vl::Tensor output, 23 | vl::Tensor data, 24 | vl::Tensor biases, 25 | int strideY, int strideX, 26 | int padTop, int padBottom, 27 | int padLeft, int padRight) ; 28 | 29 | vl::ErrorCode 30 | nnsubsample_backward(vl::Context& context, 31 | vl::Tensor derData, 32 | vl::Tensor derBiases, 33 | vl::Tensor derOutput, 34 | int strideY, int strideX, 35 | int padTop, int padBottom, 36 | int padLeft, int padRight) ; 37 | } 38 | 39 | #endif /* defined(__vl__nnsubsample__) */ 40 | -------------------------------------------------------------------------------- /matlab/+dagnn/DropOut.m: -------------------------------------------------------------------------------- 1 | classdef DropOut < dagnn.ElementWise 2 | properties 3 | rate = 0.5 4 | frozen = false 5 | end 6 | 7 | properties (Transient) 8 | mask 9 | end 10 | 11 | methods 12 | function outputs = forward(obj, inputs, params) 13 | if strcmp(obj.net.mode, 'test') 14 | outputs = inputs ; 15 | return ; 16 | end 17 | if obj.frozen & ~isempty(obj.mask) 18 | outputs{1} = vl_nndropout(inputs{1}, 'mask', obj.mask) ; 19 | else 20 | [outputs{1}, obj.mask] = vl_nndropout(inputs{1}, 'rate', obj.rate) ; 21 | end 22 | end 23 | 24 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 25 | if strcmp(obj.net.mode, 'test') 26 | derInputs = derOutputs ; 27 | derParams = {} ; 28 | return ; 29 | end 30 | derInputs{1} = vl_nndropout(inputs{1}, derOutputs{1}, 'mask', obj.mask) ; 31 | derParams = {} ; 32 | end 33 | 34 | % --------------------------------------------------------------------- 35 | function obj = DropOut(varargin) 36 | obj.load(varargin{:}) ; 37 | end 38 | 39 | function obj = reset(obj) 40 | reset@dagnn.ElementWise(obj) ; 41 | obj.mask = [] ; 42 | obj.frozen = false ; 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/copy_cpu.cpp: -------------------------------------------------------------------------------- 1 | // @file copy_cpu.cpp 2 | // @brief Copy and other data operations (CPU) 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2015-16 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #include "copy.hpp" 14 | #include 15 | 16 | namespace vl { namespace impl { 17 | 18 | template 19 | struct operations 20 | { 21 | typedef type data_type ; 22 | 23 | static vl::ErrorCode 24 | copy(data_type * dest, 25 | data_type const * src, 26 | size_t numElements) 27 | { 28 | memcpy(dest, src, numElements * sizeof(data_type)) ; 29 | return VLE_Success ; 30 | } 31 | 32 | static vl::ErrorCode 33 | fill(data_type * dest, 34 | size_t numElements, 35 | data_type value) 36 | { 37 | for (size_t k = 0 ; k < numElements ; ++k) { 38 | dest[k] = value ; 39 | } 40 | return VLE_Success ; 41 | } 42 | } ; 43 | 44 | } } 45 | 46 | template struct vl::impl::operations ; 47 | 48 | #ifdef ENABLE_DOUBLE 49 | template struct vl::impl::operations ; 50 | #endif 51 | 52 | 53 | -------------------------------------------------------------------------------- /matlab/vl_nncrop.m: -------------------------------------------------------------------------------- 1 | function y = vl_nncrop(x, crop, dzdy, inputSize) 2 | %VL_NNCROP CNN crop. 3 | % Y = VL_NNCROP(X, CROP) crops the input X spatially. CROP specifies the 4 | % amount of cropping as [TOP, BOTTOM, LEFT, RIGHT]. 5 | % 6 | % DZDX = VL_NNCROP(X, CROP, DZDY) computes the derivative DZDX of the 7 | % function projected on the output derivative DZDY. DZDX has the same 8 | % dimension as X and DZDY the same dimension as Y. 9 | % 10 | % DZDX = VL_NNCROP([], CROP, DZDY, INPUTSIZE) is an alternative to 11 | % the previous call in which X is omitted and its size is passed as 12 | % INPUTSIZE. 13 | 14 | % Copyright (C) 2015 Sebastien Ehrhardt and Andrea Vedaldi. 15 | % All rights reserved. 16 | % 17 | % This file is part of the VLFeat library and is made available under 18 | % the terms of the BSD license (see the COPYING file). 19 | 20 | if nargin < 4 21 | sz = [size(x,1) size(x,2) size(x,3) size(x,4)] ; 22 | else 23 | sz = inputSize ; 24 | end 25 | 26 | sv = 1 + crop(1) : sz(1) - crop(2) ; 27 | su = 1 + crop(3) : sz(2) - crop(4) ; 28 | 29 | if nargin <= 2 || isempty(dzdy) 30 | y = x(sv, su, :, :) ; 31 | else 32 | if isa(dzdy, 'gpuArray') 33 | y = gpuArray.zeros(sz, classUnderlying(dzdy)) ; 34 | else 35 | y = zeros(sz, class(dzdy)) ; 36 | end 37 | y(sv, su, :, :) = dzdy ; 38 | end 39 | -------------------------------------------------------------------------------- /matlab/xtest/vl_test_gpureset.m: -------------------------------------------------------------------------------- 1 | for explictMexReset = [false] 2 | 3 | % reset the same GPU device 4 | for t = 1:6 5 | if explictMexReset, clear mex ; end 6 | if mod(t-1,2) == 0 7 | disp('vl_test_gpureset: resetting GPU') ; 8 | gpuDevice(1) ; 9 | else 10 | disp('vl_test_gpureset: not resetting GPU') ; 11 | end 12 | if t > 1, disp(a) ; end 13 | a = gpuArray(single(ones(10))) ; 14 | b = gpuArray(single(ones(5))) ; 15 | c = vl_nnconv(a,b,[],'nocudnn') ; 16 | end 17 | 18 | % resetting GPU arguments to a MEX file should fail properly 19 | a = gpuArray(single(ones(10))) ; 20 | b = gpuArray(single(ones(5))) ; 21 | c = vl_nnconv(a,b,[],'nocudnn') ; 22 | 23 | gpuDevice(1) ; 24 | disp(a) ; 25 | try 26 | c = vl_nnconv(a,b,[],'nocudnn') ; 27 | catch e 28 | assert(strcmp('parallel:gpu:array:InvalidData', e.identifier)) ; 29 | end 30 | 31 | % switch GPU devices 32 | if gpuDeviceCount > 1 33 | disp('vl_text_gpureset: test switching GPU device') ; 34 | for t = 1:gpuDeviceCount 35 | if explictMexReset, clear mex ; end 36 | fprintf('vl_test_gpureset: switching to gpu %d\n', t) ; 37 | gpuDevice(t) ; 38 | a = gpuArray(single(ones(10))) ; 39 | b = gpuArray(single(ones(5))) ; 40 | c = vl_nnconv(a,b,[],'nocudnn') ; 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /code/get_net_structure/resnet52_iden_baseline.m: -------------------------------------------------------------------------------- 1 | function net = resnet52_iden_baseline(num_predefined_classes, dropoutrate) 2 | %The baseline model 3 | netStruct = load('./result/imagenet-resnet-50-dag.mat') ; 4 | net = dagnn.DagNN.loadobj(netStruct) ; 5 | net.removeLayer('fc1000'); 6 | net.removeLayer('prob'); 7 | %---------setting1 8 | for i = 1:numel(net.params) 9 | if(mod(i,2)==0) 10 | net.params(i).learningRate=0.02; 11 | else net.params(i).learningRate=0.001; 12 | end 13 | name = net.params(i).name; 14 | if(name(1)=='b') 15 | net.params(i).weightDecay=0; 16 | end 17 | end 18 | 19 | %--- 20 | net.params(1).learningRate = 0.0001; 21 | dropoutBlock = dagnn.DropOut('rate',dropoutrate); 22 | net.addLayer('dropout',dropoutBlock,{'pool5'},{'pool5d'},{}); 23 | fc751Block = dagnn.Conv('size',[1 1 2048 num_predefined_classes],'hasBias',true,'stride',[1,1],'pad',[0,0,0,0]); 24 | net.addLayer('fc751',fc751Block,{'pool5d'},{'prediction'},{'fc751f','fc751b'}); 25 | net.addLayer('softmaxloss',dagnn.Loss('loss','softmaxlog'),{'prediction','label'},'objective'); 26 | net.addLayer('top1err', dagnn.Loss('loss', 'classerror'), ... 27 | {'prediction','label'}, 'top1err') ; 28 | net.addLayer('top5err', dagnn.Loss('loss', 'topkerror', ... 29 | 'opts', {'topK',5}), ... 30 | {'prediction','label'}, 'top5err') ; 31 | net.initParams(); 32 | end 33 | 34 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/subsample.hpp: -------------------------------------------------------------------------------- 1 | // @file subsampling.hpp 2 | // @brief Subsampling block implementation 3 | // @author Andrea Vedaldi 4 | // @author Karel Lenc 5 | 6 | /* 7 | Copyright (C) 2014-16 Andrea Vedaldi and Karel Lenc. 8 | All rights reserved. 9 | 10 | This file is part of the VLFeat library and is made available under 11 | the terms of the BSD license (see the COPYING file). 12 | */ 13 | 14 | #ifndef VL_NNSUBSAMPLE_H 15 | #define VL_NNSUBSAMPLE_H 16 | 17 | #include "../data.hpp" 18 | #include 19 | 20 | namespace vl { namespace impl { 21 | 22 | template 23 | struct subsample { 24 | 25 | static vl::ErrorCode 26 | forward(vl::Context& context, 27 | type* output, 28 | type const* data, 29 | size_t height, size_t width, size_t depth, 30 | size_t strideY, size_t strideX, 31 | size_t padTop, size_t padBottom, size_t padLeft, size_t padRight) ; 32 | 33 | static vl::ErrorCode 34 | backward(vl::Context& context, 35 | type* derData, 36 | type const* derOutput, 37 | size_t height, size_t width, size_t depth, 38 | size_t strideY, size_t strideX, 39 | size_t padTop, size_t padBottom, size_t padLeft, size_t padRight) ; 40 | } ; 41 | 42 | } } 43 | 44 | #endif /* defined(VL_NNSUBSAMPLE_H) */ 45 | -------------------------------------------------------------------------------- /matlab/+dagnn/Pooling.m: -------------------------------------------------------------------------------- 1 | classdef Pooling < dagnn.Filter 2 | properties 3 | method = 'max' 4 | poolSize = [1 1] 5 | opts = {'cuDNN'} 6 | end 7 | 8 | methods 9 | function outputs = forward(self, inputs, params) 10 | outputs{1} = vl_nnpool(inputs{1}, self.poolSize, ... 11 | 'pad', self.pad, ... 12 | 'stride', self.stride, ... 13 | 'method', self.method, ... 14 | self.opts{:}) ; 15 | end 16 | 17 | function [derInputs, derParams] = backward(self, inputs, params, derOutputs) 18 | derInputs{1} = vl_nnpool(inputs{1}, self.poolSize, derOutputs{1}, ... 19 | 'pad', self.pad, ... 20 | 'stride', self.stride, ... 21 | 'method', self.method, ... 22 | self.opts{:}) ; 23 | derParams = {} ; 24 | end 25 | 26 | function kernelSize = getKernelSize(obj) 27 | kernelSize = obj.poolSize ; 28 | end 29 | 30 | function outputSizes = getOutputSizes(obj, inputSizes) 31 | outputSizes = getOutputSizes@dagnn.Filter(obj, inputSizes) ; 32 | outputSizes{1}(3) = inputSizes{1}(3) ; 33 | end 34 | 35 | function obj = Pooling(varargin) 36 | obj.load(varargin) ; 37 | end 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /matlab/+dagnn/@DagNN/addLayer.m: -------------------------------------------------------------------------------- 1 | function addLayer(obj, name, block, inputs, outputs, params, varargin) 2 | %ADDLAYER Adds a layer to a DagNN 3 | % ADDLAYER(NAME, LAYER, INPUTS, OUTPUTS, PARAMS) adds the 4 | % specified layer to the network. NAME is a string with the layer 5 | % name, used as a unique indentifier. BLOCK is the object 6 | % implementing the layer, which should be a subclass of the 7 | % Layer. INPUTS, OUTPUTS are cell arrays of variable names, and 8 | % PARAMS of parameter names. 9 | % 10 | % See Also REMOVELAYER(). 11 | opts.skipRebuild = false; 12 | opts = vl_argparse(opts, varargin); 13 | 14 | index = find(strcmp(name, {obj.layers.name})) ; 15 | if ~isempty(index), error('There is already a layer with name ''%s''.', name), end 16 | index = numel(obj.layers) + 1 ; 17 | 18 | if nargin < 6, params = {} ; end 19 | if ischar(inputs), inputs = {inputs} ; end 20 | if ischar(outputs), outputs = {outputs} ; end 21 | if ischar(params), params = {params} ; end 22 | 23 | obj.layers(index) = struct(... 24 | 'name', {name}, ... 25 | 'inputs', {inputs}, ... 26 | 'outputs', {outputs}, ... 27 | 'params', {params}, ... 28 | 'inputIndexes', {[]}, ... 29 | 'outputIndexes', {[]}, ... 30 | 'paramIndexes', {[]}, ... 31 | 'forwardTime', {[]}, ... 32 | 'backwardTime', {[]}, ... 33 | 'block', {block}) ; 34 | obj.layers(index).block.attach(obj, index) ; 35 | if ~opts.skipRebuild, obj.rebuild() ; end ; 36 | -------------------------------------------------------------------------------- /matlab/vl_nnspnorm.m: -------------------------------------------------------------------------------- 1 | function y = vl_nnspnorm(x, param, dzdy) 2 | %VL_NNSPNORM CNN spatial normalization. 3 | % Y = VL_NNSPNORM(X, PARAM) computes the spatial normalization of 4 | % the data X with parameters PARAM = [PH PW ALPHA BETA]. Here PH and 5 | % PW define the size of the spatial neighbourhood used for 6 | % nomalization. 7 | % 8 | % For each feature channel, the function computes the sum of squares 9 | % of X inside each rectangle, N2(i,j). It then divides each element 10 | % of X as follows: 11 | % 12 | % Y(i,j) = X(i,j) / (1 + ALPHA * N2(i,j))^BETA. 13 | % 14 | % DZDX = VL_NNSPNORM(X, PARAM, DZDY) computes the derivative of the 15 | % block projected onto DZDY. DZDX and DZDY have the same dimensions 16 | % as X and Y respectively. 17 | 18 | % Copyright (C) 2015 Karel Lenc and Andrea Vedaldi. 19 | % All rights reserved. 20 | % 21 | % This file is part of the VLFeat library and is made available under 22 | % the terms of the BSD license (see the COPYING file). 23 | 24 | pad = floor((param(1:2)-1)/2) ; 25 | pad = [pad ; param(1:2)-1-pad] ; 26 | 27 | n2 = vl_nnpool(x.*x, param(1:2), 'method', 'avg', 'pad', pad) ; 28 | f = 1 + param(3) * n2 ; 29 | 30 | if nargin <= 2 || isempty(dzdy) 31 | y = f.^(-param(4)) .* x ; 32 | else 33 | t = vl_nnpool(x.*x, param(1:2), f.^(-param(4)-1) .* dzdy .* x, 'method', 'avg', 'pad', pad) ; 34 | y = f.^(-param(4)) .* dzdy - 2 * param(3)*param(4) * x .* t ; 35 | end -------------------------------------------------------------------------------- /code/get_net_structure/resnet52_iden_sMpRL.m: -------------------------------------------------------------------------------- 1 | function net = resnet52_iden_sMpRL(num_predefined_classes, dropoutrate) 2 | netStruct = load('./result/imagenet-resnet-50-dag.mat') ; 3 | net = dagnn.DagNN.loadobj(netStruct) ; 4 | net.removeLayer('fc1000'); 5 | net.removeLayer('prob'); 6 | %---------setting1 7 | for i = 1:numel(net.params) 8 | if(mod(i,2)==0) 9 | net.params(i).learningRate=0.02; 10 | else net.params(i).learningRate=0.001; 11 | end 12 | name = net.params(i).name; 13 | if(name(1)=='b') 14 | net.params(i).weightDecay=0; 15 | end 16 | end 17 | %--- 18 | net.params(1).learningRate = 0.0001; 19 | dropoutBlock = dagnn.DropOut('rate',dropoutrate); 20 | net.addLayer('dropout',dropoutBlock,{'pool5'},{'pool5d'},{}); 21 | fc751Block = dagnn.Conv('size',[1 1 2048 num_predefined_classes],'hasBias',true,'stride',[1,1],'pad',[0,0,0,0]); 22 | net.addLayer('fc751',fc751Block,{'pool5d'},{'prediction'},{'fc751f','fc751b'}); 23 | % Here we use pseudo loss. We will assign a dynamic label. 24 | net.addLayer('loss_pseudo',dagnn.Pseudo_Loss_Multi_Static(),{'prediction','label', 'multipse_label'},'objective_multi_pseudo'); 25 | 26 | net.addLayer('top1err', dagnn.Loss('loss', 'classerror'), ... 27 | {'prediction','label'}, 'top1err') ; 28 | net.addLayer('top5err', dagnn.Loss('loss', 'topkerror', ... 29 | 'opts', {'topK',5}), ... 30 | {'prediction','label'}, 'top5err') ; 31 | net.initParams(); 32 | end 33 | -------------------------------------------------------------------------------- /matlab/simplenn/vl_simplenn_move.m: -------------------------------------------------------------------------------- 1 | function net = vl_simplenn_move(net, destination) 2 | %VL_SIMPLENN_MOVE Move a SimpleNN network between CPU and GPU. 3 | % NET = VL_SIMPLENN_MOVE(NET, 'gpu') moves the network to the 4 | % current GPU device. NET = VL_SIMPLENN_MOVE(NET, 'cpu') moves the 5 | % network to the CPU. 6 | % 7 | % See also: VL_SIMPLENN(). 8 | 9 | % Copyright (C) 2014-15 Andrea Vedaldi. 10 | % All rights reserved. 11 | % 12 | % This file is part of the VLFeat library and is made available under 13 | % the terms of the BSD license (see the COPYING file). 14 | 15 | switch destination 16 | case 'gpu', moveop = @(x) gpuArray(x) ; 17 | case 'cpu', moveop = @(x) gather(x) ; 18 | otherwise, error('Unknown destination ''%s''.', destination) ; 19 | end 20 | for l=1:numel(net.layers) 21 | switch net.layers{l}.type 22 | case {'conv', 'convt', 'bnorm'} 23 | for f = {'filters', 'biases', 'filtersMomentum', 'biasesMomentum'} 24 | f = char(f) ; 25 | if isfield(net.layers{l}, f) 26 | net.layers{l}.(f) = moveop(net.layers{l}.(f)) ; 27 | end 28 | end 29 | for f = {'weights', 'momentum'} 30 | f = char(f) ; 31 | if isfield(net.layers{l}, f) 32 | for j=1:numel(net.layers{l}.(f)) 33 | net.layers{l}.(f){j} = moveop(net.layers{l}.(f){j}) ; 34 | end 35 | end 36 | end 37 | otherwise 38 | % nothing to do ? 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nnnormalize.m: -------------------------------------------------------------------------------- 1 | classdef nnnormalize < nntest 2 | properties (TestParameter) 3 | group = {2 3 4 5 6 8 9 10 11 12 13 14 15 16 17} 4 | sgroup = {2 3 4 5 6 7} 5 | end 6 | 7 | methods (Test) 8 | function basic(test, group) 9 | param = [group, .1, .5, .75] ; 10 | x = test.randn(3,2,10,4) ; 11 | y = vl_nnnormalize(x,param) ; 12 | dzdy = test.rand(size(y))-0.5 ; 13 | dzdx = vl_nnnormalize(x,param,dzdy) ; 14 | test.der(@(x) vl_nnnormalize(x,param), x, dzdy, dzdx, test.range * 1e-3, 0.3) ; 15 | end 16 | 17 | function compare_to_naive(test, sgroup) 18 | param = [sgroup, .1, .5, .75] ; 19 | x = test.randn(3,2,10,4) ; 20 | y = vl_nnnormalize(gather(x),param) ; 21 | y_ = test.zeros(size(y)) ; 22 | x_ = gather(x) ; 23 | for i=1:size(x,1) 24 | for j=1:size(x,2) 25 | for n=1:size(x,4) 26 | t = test.zeros(1,1,size(x,3),1) ; 27 | t(1,1,:,1) = (param(2) + param(3)*conv(squeeze(x_(i,j,:,n)).^2, ... 28 | ones(param(1),1), 'same')).^(-param(4)) ; 29 | y_(i,j,:,n) = x_(i,j,:,n) .* t ; 30 | end 31 | end 32 | end 33 | test.eq(y,y_) ; 34 | end 35 | 36 | function l2(test) 37 | x = test.randn(1,1,10,1) ; 38 | y = vl_nnnormalize(x, [20, 0, 1, .5]) ; 39 | test.eq(sum(y(:).^2), test.toDataType(1), 1e-2) ; 40 | end 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /matlab/vl_nnconcat.m: -------------------------------------------------------------------------------- 1 | function y = vl_nnconcat(inputs, dim, dzdy, varargin) 2 | %VL_NNCONCAT CNN concatenate multiple inputs. 3 | % Y = VL_NNCONCAT(INPUTS, DIM) concatenates the inputs in the cell 4 | % array INPUTS along dimension DIM generating an output Y. 5 | % 6 | % DZDINPUTS = VL_NNCONCAT(INPUTS, DIM, DZDY) computes the derivatives 7 | % of the block projected onto DZDY. DZDINPUTS has one element for 8 | % each element of INPUTS, each of which is an array that has the same 9 | % dimensions of the corresponding array in INPUTS. 10 | 11 | % Copyright (C) 2015 Karel Lenc and Andrea Vedaldi. 12 | % All rights reserved. 13 | % 14 | % This file is part of the VLFeat library and is made available under 15 | % the terms of the BSD license (see the COPYING file). 16 | 17 | opts.inputSizes = [] ; 18 | opts = vl_argparse(opts, varargin, 'nonrecursive') ; 19 | 20 | if nargin < 2, dim = 3; end; 21 | if nargin < 3, dzdy = []; end; 22 | 23 | if isempty(dzdy) 24 | y = cat(dim, inputs{:}); 25 | else 26 | if isempty(opts.inputSizes) 27 | opts.inputSizes = cellfun(@(inp) [size(inp,1),size(inp,2),size(inp,3),size(inp,4)], inputs, 'UniformOutput', false) ; 28 | end 29 | start = 1 ; 30 | y = cell(1, numel(opts.inputSizes)) ; 31 | s.type = '()' ; 32 | s.subs = {':', ':', ':', ':'} ; 33 | for i = 1:numel(opts.inputSizes) 34 | stop = start + opts.inputSizes{i}(dim) ; 35 | s.subs{dim} = start:stop-1 ; 36 | y{i} = subsref(dzdy,s) ; 37 | start = stop ; 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /code/prepare_data/prepare_sMpRL_label4data.m: -------------------------------------------------------------------------------- 1 | if ~exist('./data') 2 | mkdir('./data'); 3 | end 4 | 5 | load('./data/url_data_gan_24000.mat'); 6 | 7 | % First, you should train the baseline method, the sMpRL require the baseline on real data to assign sMpRL on generated data 8 | path = 'res52_iden_baseline'; 9 | netStruct = load(strcat('./result/', path, '/net-epoch-50.mat')); 10 | net = dagnn.DagNN.loadobj(netStruct.net); 11 | 12 | clear netStruct; 13 | net.mode = 'test'; 14 | net.move('gpu') ; 15 | 16 | net.conserveMemory = true; 17 | im_mean = net.meta(1).normalization.averageImage; 18 | 19 | idx = find(imdb.images.label==0); 20 | prediction = []; 21 | 22 | for i = idx(1):200:numel(imdb.images.label) 23 | disp(i); 24 | oim = []; 25 | str=[]; 26 | for j=1:min(200,numel(imdb.images.label)-i+1) 27 | str = imdb.images.data{i+j-1}; 28 | imt = imresize(imread(str),[224,224]); 29 | oim = cat(4,oim,imt); 30 | end 31 | temp = get_Pseudo_label(net,oim,im_mean,'data','prediction'); 32 | prediction = cat(4,prediction,temp); 33 | end 34 | prediction = reshape(prediction,size(prediction,3),[]); 35 | 36 | iden_num = size(prediction,1); 37 | label = 1/iden_num:1/iden_num:1; 38 | multipse_label = zeros(size(prediction)); 39 | for i=1:size(prediction,2) 40 | x = prediction(:,i); 41 | [~, idx] = sort(x); 42 | multipse_label(:,i) = label(idx)'; 43 | i 44 | end 45 | save(fullfile('./data/sMpRL_24000.mat'),'multipse_label','prediction', '-v7.3'); -------------------------------------------------------------------------------- /matlab/src/bits/nnbias.cpp: -------------------------------------------------------------------------------- 1 | #ifdef ENABLE_GPU 2 | #error "The file nnsubsample.cu should be compiled instead" 3 | #endif 4 | #include "nnbias.cu" 5 | 6 | /** 7 | @brief nnbias_forward 8 | @param context context. 9 | @param output output tensor $\by$ [output]. 10 | @param outputMult output tensor multiplier $\alpha$. 11 | @param data data tensor $\bx$. 12 | @param dataMult data tensor multiplier $\beta$. 13 | @param biases biases tensor $\bb$. 14 | @param biasesMult biases tensor multiplier $\gamma$. 15 | 16 | The function computes 17 | @f[ 18 | y_{ijkd} \leftarrow 19 | \alpha y_{ijkd} + 20 | \beta x_{ijkd} + 21 | \gamma b_k. 22 | @f] 23 | 24 | @a data can be the null tensor, in which case this tensor 25 | is dropped in the summation. 26 | */ 27 | 28 | /** 29 | @brief nnbias_backward 30 | @param context context. 31 | @param derData data derivative tensor $d\bx$ [output]. 32 | @param derDataMult data derivative tensor multiplier $\eta$. 33 | @param derBiases biases derivative tensor $d\bb$ [output]. 34 | @param derBiasesMult biased derivative tensor multiplier $\tau$. 35 | @param data data tensor $\bx$. 36 | @param dataMult data tensor multiplier $\beta$. 37 | @param biases biases tensor $\bb$. 38 | @param biasesMult biases tensor multiplier $\gamma$. 39 | 40 | If @a derData is the null tensor, this derivative is not comptued and 41 | @param biases can also be null. 42 | 43 | If @a derBiases is the null tensor, this derivative is not computed and 44 | @param data can also be null. 45 | */ 46 | -------------------------------------------------------------------------------- /matlab/src/bits/nnpooling.hpp: -------------------------------------------------------------------------------- 1 | // @file nnpooling.hpp 2 | // @brief Pooling block 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2014-16 Andrea Vedaldi and Karel Lenc. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__nnpooling__ 14 | #define __vl__nnpooling__ 15 | 16 | #include "data.hpp" 17 | #include 18 | 19 | namespace vl { 20 | 21 | enum PoolingMethod { vlPoolingMax, vlPoolingAverage } ; 22 | 23 | vl::ErrorCode 24 | nnpooling_forward(vl::Context& context, 25 | vl::Tensor output, 26 | vl::Tensor data, 27 | PoolingMethod method, 28 | int poolHeight, int poolWidth, 29 | int strideY, int strideX, 30 | int padTop, int padBottom, 31 | int padLeft, int padRight) ; 32 | 33 | vl::ErrorCode 34 | nnpooling_backward(vl::Context& context, 35 | vl::Tensor derData, 36 | vl::Tensor data, 37 | vl::Tensor derOutput, 38 | PoolingMethod method, 39 | int poolHeight, int poolWidth, 40 | int strideY, int strideX, 41 | int padTop, int padBottom, 42 | int padLeft, int padRight) ; 43 | } 44 | 45 | #endif /* defined(__vl__nnpooling__) */ 46 | -------------------------------------------------------------------------------- /matlab/+dagnn/MAC.m: -------------------------------------------------------------------------------- 1 | classdef MAC < dagnn.Filter 2 | % global average pooling 3 | properties 4 | method = 'avg' 5 | poolSize = [1 1] 6 | opts = {'cuDNN'} 7 | end 8 | 9 | methods 10 | function outputs = forward(self, inputs, params) 11 | h = size(inputs{1},1); 12 | w = size(inputs{1},2); 13 | self.poolSize = [h,w]; 14 | outputs{1} = vl_nnpool(inputs{1}, self.poolSize, ... 15 | 'pad', self.pad, ... 16 | 'stride', self.stride, ... 17 | 'method', self.method, ... 18 | self.opts{:}) ; 19 | end 20 | 21 | function [derInputs, derParams] = backward(self, inputs, params, derOutputs) 22 | derInputs{1} = vl_nnpool(inputs{1}, self.poolSize, derOutputs{1}, ... 23 | 'pad', self.pad, ... 24 | 'stride', self.stride, ... 25 | 'method', self.method, ... 26 | self.opts{:}) ; 27 | derParams = {} ; 28 | end 29 | 30 | function kernelSize = getKernelSize(obj) 31 | kernelSize = obj.poolSize ; 32 | end 33 | 34 | function outputSizes = getOutputSizes(obj, inputSizes) 35 | outputSizes = getOutputSizes@dagnn.Filter(obj, inputSizes) ; 36 | outputSizes{1}(3) = inputSizes{1}(3) ; 37 | end 38 | 39 | function obj = MAC(varargin) 40 | obj.load(varargin) ; 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /matlab/src/bits/nnroipooling.hpp: -------------------------------------------------------------------------------- 1 | // @file nnroipooling.hpp 2 | // @brief Spatial Pyramid block 3 | // @author Hakan Bilen 4 | // @author Abishek Dutta 5 | // @author Andrea Vedaldi 6 | /* 7 | Copyright (C) 2016 Hakan Bilen, Abishek Dutta, and Andrea Vedaldi. 8 | All rights reserved. 9 | 10 | This file is part of the VLFeat library and is made available under 11 | the terms of the BSD license (see the COPYING file). 12 | */ 13 | 14 | #ifndef __vl__nnroipooling__ 15 | #define __vl__nnroipooling__ 16 | 17 | #include "data.hpp" 18 | #include 19 | 20 | namespace vl { 21 | enum ROIPoolingMethod { vlROIPoolingMax, vlROIPoolingAverage } ; 22 | 23 | vl::ErrorCode 24 | nnroipooling_forward(vl::Context& context, 25 | vl::Tensor output, 26 | vl::Tensor data, 27 | vl::Tensor rois, 28 | ROIPoolingMethod method, 29 | int const subdivisions[2], 30 | double const transform[6]) ; 31 | 32 | vl::ErrorCode 33 | nnroipooling_backward(vl::Context& context, 34 | vl::Tensor derData, 35 | vl::Tensor data, 36 | vl::Tensor rois, 37 | vl::Tensor derOutput, 38 | ROIPoolingMethod method, 39 | int const subdivisions[2], 40 | double const transform[6]) ; 41 | } 42 | 43 | #endif /* defined(__vl__nnroipooling__) */ 44 | -------------------------------------------------------------------------------- /matlab/+dagnn/Loss.m: -------------------------------------------------------------------------------- 1 | classdef Loss < dagnn.ElementWise 2 | properties 3 | loss = 'softmaxlog' 4 | opts = {} 5 | end 6 | 7 | properties (Transient) 8 | average = 0 9 | numAveraged = 0 10 | end 11 | 12 | methods 13 | function outputs = forward(obj, inputs, params) 14 | outputs{1} = vl_nnloss(inputs{1}, inputs{2}, [], 'loss', obj.loss) ; 15 | n = obj.numAveraged ; 16 | m = n + size(inputs{1},4) ; 17 | obj.average = (n * obj.average + gather(outputs{1})) / m ; 18 | obj.numAveraged = m ; 19 | end 20 | 21 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 22 | derInputs{1} = vl_nnloss(inputs{1}, inputs{2}, derOutputs{1}, 'loss', obj.loss, obj.opts{:}) ; 23 | derInputs{2} = [] ; 24 | derParams = {} ; 25 | end 26 | 27 | function reset(obj) 28 | obj.average = 0 ; 29 | obj.numAveraged = 0 ; 30 | end 31 | 32 | function outputSizes = getOutputSizes(obj, inputSizes, paramSizes) 33 | outputSizes{1} = [1 1 1 inputSizes{1}(4)] ; 34 | end 35 | 36 | function rfs = getReceptiveFields(obj) 37 | % the receptive field depends on the dimension of the variables 38 | % which is not known until the network is run 39 | rfs(1,1).size = [NaN NaN] ; 40 | rfs(1,1).stride = [NaN NaN] ; 41 | rfs(1,1).offset = [NaN NaN] ; 42 | rfs(2,1) = rfs(1,1) ; 43 | end 44 | 45 | function obj = Loss(varargin) 46 | obj.load(varargin) ; 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/im2row.hpp: -------------------------------------------------------------------------------- 1 | // @file im2row.hpp 2 | // @brief Stack image patches as matrix rows 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2014-16 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__im2row__ 14 | #define __vl__im2row__ 15 | 16 | #include "../data.hpp" 17 | #include 18 | 19 | namespace vl { namespace impl { 20 | 21 | template 22 | struct im2row { 23 | 24 | static vl::ErrorCode 25 | forward(vl::Context& context, 26 | type* stacked, 27 | type const* data, 28 | size_t height, size_t width, size_t depth, 29 | size_t windowHeight, size_t windowWidth, 30 | size_t strideY, size_t strideX, 31 | size_t padTop, size_t padBottom, size_t padLeft, size_t padRight, 32 | int dilateY, int dialteX) ; 33 | 34 | static vl::ErrorCode 35 | backward(vl::Context& context, 36 | type* data, 37 | type const* stacked, 38 | size_t height, size_t width, size_t depth, 39 | size_t windowHeight, size_t windowWidth, 40 | size_t strideY, size_t strideX, 41 | size_t padTop, size_t padBottom, size_t padLeft, size_t padRight, 42 | int dilateY, int dilateX) ; 43 | } ; 44 | 45 | } } 46 | 47 | #endif /* defined(__vl__im2row__) */ 48 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/bilinearsampler.hpp: -------------------------------------------------------------------------------- 1 | // @file bilinearsampler.hpp 2 | // @brief Bilinear sampler implementation 3 | // @author Ankush Gupta 4 | // @author Andrea Vedaldi 5 | 6 | /* 7 | Copyright (C) 2016- Ankush Gupta and Andrea Vedaldi. 8 | All rights reserved. 9 | 10 | This file is part of the VLFeat library and is made available under 11 | the terms of the BSD license (see the COPYING file). 12 | */ 13 | 14 | #ifndef VL_BILINEARSAMPLER_H 15 | #define VL_BILINEARSAMPLER_H 16 | 17 | #include "../data.hpp" 18 | #include 19 | 20 | // defines the dispatcher for CUDA kernels: 21 | namespace vl { namespace impl { 22 | 23 | template 24 | struct bilinearsampler { 25 | 26 | static vl::ErrorCode 27 | forward(Context& context, 28 | type* output, 29 | type const* data, 30 | type const* grid, 31 | size_t outHeight, size_t outWidth, size_t outDepth, size_t outCardinality, 32 | size_t inHeight, size_t inWidth, size_t inCardinality) ; 33 | 34 | 35 | static vl::ErrorCode 36 | backward(Context& context, 37 | type* derData, 38 | type* derGrid, 39 | type const* data, 40 | type const* grid, 41 | type const* derOutput, 42 | size_t outHeight, size_t outWidth, size_t outDepth, size_t outCardinality, 43 | size_t inHeight, size_t inWidth, size_t inCardinality) ; 44 | } ; 45 | 46 | } } 47 | 48 | #endif /* defined(VL_BILINEARSAMPLER_H) */ 49 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/nnconv_cudnn.hpp: -------------------------------------------------------------------------------- 1 | // @file nnconv_blas.hpp 2 | // @brief Convolution block CuDNN-based implementation. 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2015-16 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__nnconv_cudnn__ 14 | #define __vl__nnconv_cudnn__ 15 | 16 | #include "../data.hpp" 17 | #include "cudnn.h" 18 | 19 | namespace vl { namespace impl { 20 | 21 | template 22 | struct nnconv_cudnn 23 | { 24 | static vl::ErrorCode 25 | forward(Context& context, 26 | Tensor output, double outputMult, 27 | Tensor data, double dataMult, 28 | Tensor filters, 29 | Tensor biases, 30 | int strideX, int strideY, 31 | int padLeft, int padRight, 32 | int padTop, int padBottom, 33 | int dilateX, int dilateY) ; 34 | 35 | static vl::ErrorCode 36 | backward(Context& context, 37 | Tensor derData, 38 | Tensor derFilters, 39 | Tensor derBiases, 40 | Tensor data, 41 | Tensor filters, 42 | Tensor derOutput, 43 | int strideX, int strideY, 44 | int padLeft, int padRight, 45 | int padTop, int padBottom, 46 | int dilateX, int dilateY) ; 47 | } ; 48 | 49 | } } 50 | #endif /* defined(__vl__nnconv_cudnn__) */ 51 | -------------------------------------------------------------------------------- /matlab/+dagnn/Sum.m: -------------------------------------------------------------------------------- 1 | classdef Sum < dagnn.ElementWise 2 | %SUM DagNN sum layer 3 | % The SUM layer takes the sum of all its inputs and store the result 4 | % as its only output. 5 | 6 | properties (Transient) 7 | numInputs 8 | end 9 | 10 | methods 11 | function outputs = forward(obj, inputs, params) 12 | obj.numInputs = numel(inputs) ; 13 | outputs{1} = inputs{1} ; 14 | for k = 2:obj.numInputs 15 | outputs{1} = outputs{1} + inputs{k} ; 16 | end 17 | end 18 | 19 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 20 | for k = 1:obj.numInputs 21 | derInputs{k} = derOutputs{1} ; 22 | end 23 | derParams = {} ; 24 | end 25 | 26 | function outputSizes = getOutputSizes(obj, inputSizes) 27 | outputSizes{1} = inputSizes{1} ; 28 | for k = 2:numel(inputSizes) 29 | if all(~isnan(inputSizes{k})) && all(~isnan(outputSizes{1})) 30 | if ~isequal(inputSizes{k}, outputSizes{1}) 31 | warning('Sum layer: the dimensions of the input variables is not the same.') ; 32 | end 33 | end 34 | end 35 | end 36 | 37 | function rfs = getReceptiveFields(obj) 38 | numInputs = numel(obj.net.layers(obj.layerIndex).inputs) ; 39 | rfs.size = [1 1] ; 40 | rfs.stride = [1 1] ; 41 | rfs.offset = [1 1] ; 42 | rfs = repmat(rfs, numInputs, 1) ; 43 | end 44 | 45 | function obj = Sum(varargin) 46 | obj.load(varargin) ; 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /matlab/+dagnn/@DagNN/renameVar.m: -------------------------------------------------------------------------------- 1 | function renameVar(obj, oldName, newName, varargin) 2 | %RENAMEVAR Rename a variable 3 | % RENAMEVAR(OLDNAME, NEWNAME) changes the name of the variable 4 | % OLDNAME into NEWNAME. NEWNAME should not be the name of an 5 | % existing variable. 6 | 7 | opts.quiet = false ; 8 | opts = vl_argparse(opts, varargin) ; 9 | 10 | % Find the variable to rename 11 | v = obj.getVarIndex(oldName) ; 12 | if isnan(v) 13 | % There is no such a variable, nothing to do 14 | if ~opts.quiet 15 | warning('There is no variable ''%s''.', oldName) ; 16 | end 17 | return ; 18 | end 19 | 20 | % Check if newName is an existing variable 21 | newNameExists = any(strcmp(newName, {obj.vars.name})) ; 22 | 23 | % Replace oldName with newName in all the layers 24 | for l = 1:numel(obj.layers) 25 | for f = {'inputs', 'outputs'} 26 | f = char(f) ; 27 | sel = find(strcmp(oldName, obj.layers(l).(f))) ; 28 | [obj.layers(l).(f){sel}] = deal(newName) ; 29 | end 30 | end 31 | 32 | % If newVariable is a variable in the graph, then there is not 33 | % anything else to do. obj.rebuild() will remove the slot 34 | % in obj.vars() for oldName as that variable becomes unused. 35 | % 36 | % If, however, newVariable is not in the graph already, then 37 | % the slot in obj.vars() is preserved and only the variable name 38 | % is changed. 39 | 40 | if ~newNameExists 41 | obj.vars(v).name = newName ; 42 | % update variable name hash otherwise rebuild() won't find this var 43 | % corectly 44 | obj.varNames.(newName) = v ; 45 | end 46 | 47 | obj.rebuild() ; 48 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/nnpooling_cudnn.hpp: -------------------------------------------------------------------------------- 1 | // @file nnpooling_blas.hpp 2 | // @brief Pooling block CuDNN-based implementation. 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2015-16 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__nnpooling_cudnn__ 14 | #define __vl__nnpooling_cudnn__ 15 | 16 | #include "../nnpooling.hpp" 17 | #include "../data.hpp" 18 | #include "cudnn.h" 19 | 20 | 21 | namespace vl { namespace impl { 22 | 23 | // todo: data type should be handled internally? 24 | 25 | template 26 | struct nnpooling_cudnn 27 | { 28 | static vl::ErrorCode 29 | forward(Context& context, 30 | Tensor output, 31 | Tensor data, 32 | vl::PoolingMethod method, 33 | int poolHeight, int poolWidth, 34 | int strideY, int strideX, 35 | int padTop, int padBottom, 36 | int padLeft, int padRight) ; 37 | 38 | static vl::ErrorCode 39 | backward(Context& context, 40 | Tensor derData, 41 | Tensor data, 42 | Tensor output, 43 | Tensor derOutput, 44 | vl::PoolingMethod method, 45 | int poolHeight, int poolWidth, 46 | int strideY, int strideX, 47 | int padTop, int padBottom, 48 | int padLeft, int padRight) ; 49 | }; 50 | 51 | } } 52 | 53 | #endif /* defined(__vl__nnpooling_cudnn__) */ 54 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nnpdist.m: -------------------------------------------------------------------------------- 1 | classdef nnpdist < nntest 2 | properties (TestParameter) 3 | oneToOne = {false, true} 4 | noRoot = {false, true} 5 | p = {.5 1 2 3} 6 | aggregate = {false, true} 7 | end 8 | methods (Test) 9 | function basic(test,oneToOne, noRoot, p, aggregate) 10 | if aggregate 11 | % make it smaller to avoid numerical derivative issues with 12 | % float 13 | h = 3 ; 14 | w = 2 ; 15 | else 16 | h = 13 ; 17 | w = 17 ; 18 | end 19 | d = 4 ; 20 | n = 5 ; 21 | x = test.randn(h,w,d,n) ; 22 | if oneToOne 23 | x0 = test.randn(h,w,d,n) ; 24 | else 25 | x0 = test.randn(1,1,d,n) ; 26 | end 27 | opts = {'noRoot', noRoot, 'aggregate', aggregate} ; 28 | 29 | y = vl_nnpdist(x, x0, p, opts{:}) ; 30 | 31 | % make sure they are not too close in any dimension as this may be a 32 | % problem for the finite difference dereivatives as one could 33 | % approach 0 which is not differentiable for some p-norms 34 | 35 | s = abs(bsxfun(@minus, x, x0)) < test.range*1e-1 ; 36 | x(s) = x(s) + 5*test.range ; 37 | 38 | dzdy = test.rand(size(y)) ; 39 | [dzdx, dzdx0] = vl_nnpdist(x,x0,p,dzdy,opts{:}) ; 40 | test.der(@(x) vl_nnpdist(x,x0,p,opts{:}), x, dzdy, dzdx, test.range * 1e-3) ; 41 | if oneToOne 42 | % Pdist does not implement backprop of the bsxfun 43 | test.der(@(x0) vl_nnpdist(x,x0,p,opts{:}), x0, dzdy, dzdx0, test.range * 1e-3) ; 44 | end 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /code/get_net_structure/resnet52_iden_MpRL2.m: -------------------------------------------------------------------------------- 1 | function net = resnet52_iden_MpRL2(num_predefined_classes, dropoutrate) 2 | netStruct = load('./result/imagenet-resnet-50-dag.mat') ; 3 | net = dagnn.DagNN.loadobj(netStruct) ; 4 | net.removeLayer('fc1000'); 5 | net.removeLayer('prob'); 6 | %---------setting1 7 | for i = 1:numel(net.params) 8 | if(mod(i,2)==0) 9 | net.params(i).learningRate=0.02; 10 | else net.params(i).learningRate=0.001; 11 | end 12 | name = net.params(i).name; 13 | if(name(1)=='b') 14 | net.params(i).weightDecay=0; 15 | end 16 | end 17 | %--- 18 | net.params(1).learningRate = 0.0001; 19 | dropoutBlock = dagnn.DropOut('rate',dropoutrate); 20 | net.addLayer('dropout',dropoutBlock,{'pool5'},{'pool5d'},{}); 21 | fc751Block = dagnn.Conv('size',[1 1 2048 num_predefined_classes],'hasBias',true,'stride',[1,1],'pad',[0,0,0,0]); 22 | net.addLayer('fc751',fc751Block,{'pool5d'},{'prediction'},{'fc751f','fc751b'}); 23 | 24 | % Here we use pseudo loss. We will assign multi dynamic labels. 25 | net.addLayer('loss_pseudo',dagnn.Pseudo_Loss_Multi_Dynamic_1(),{'prediction','label'},'objective_multi_pseudo'); 26 | 27 | % The original softmax loss is used to train the first 20 epoches for stability. 28 | net.addLayer('loss',dagnn.Loss('loss','softmaxlog'),{'prediction','label'},'objective'); 29 | 30 | 31 | net.addLayer('top1err', dagnn.Loss('loss', 'classerror'), ... 32 | {'prediction','label'}, 'top1err') ; 33 | net.addLayer('top5err', dagnn.Loss('loss', 'topkerror', ... 34 | 'opts', {'topK',5}), ... 35 | {'prediction','label'}, 'top5err') ; 36 | net.initParams(); 37 | end 38 | -------------------------------------------------------------------------------- /code/get_net_structure/resnet52_iden_MpRL3.m: -------------------------------------------------------------------------------- 1 | function net = resnet52_iden_MpRL3(num_predefined_classes, dropoutrate) 2 | netStruct = load('./result/imagenet-resnet-50-dag.mat') ; 3 | net = dagnn.DagNN.loadobj(netStruct) ; 4 | net.removeLayer('fc1000'); 5 | net.removeLayer('prob'); 6 | %---------setting1 7 | for i = 1:numel(net.params) 8 | if(mod(i,2)==0) 9 | net.params(i).learningRate=0.02; 10 | else net.params(i).learningRate=0.001; 11 | end 12 | name = net.params(i).name; 13 | if(name(1)=='b') 14 | net.params(i).weightDecay=0; 15 | end 16 | end 17 | %--- 18 | net.params(1).learningRate = 0.0001; 19 | dropoutBlock = dagnn.DropOut('rate',dropoutrate); 20 | net.addLayer('dropout',dropoutBlock,{'pool5'},{'pool5d'},{}); 21 | fc751Block = dagnn.Conv('size',[1 1 2048 num_predefined_classes],'hasBias',true,'stride',[1,1],'pad',[0,0,0,0]); 22 | net.addLayer('fc751',fc751Block,{'pool5d'},{'prediction'},{'fc751f','fc751b'}); 23 | 24 | % Here we use pseudo loss. We will assign multi dynamic labels. 25 | net.addLayer('loss_pseudo',dagnn.Pseudo_Loss_Multi_Dynamic_2(),{'prediction','label'},'objective_multi_pseudo'); 26 | 27 | % The original softmax loss is used to train the first 20 epoches for stability. 28 | net.addLayer('loss',dagnn.Loss('loss','softmaxlog'),{'prediction','label'},'objective'); 29 | 30 | 31 | net.addLayer('top1err', dagnn.Loss('loss', 'classerror'), ... 32 | {'prediction','label'}, 'top1err') ; 33 | net.addLayer('top5err', dagnn.Loss('loss', 'topkerror', ... 34 | 'opts', {'topK',5}), ... 35 | {'prediction','label'}, 'top5err') ; 36 | net.initParams(); 37 | end 38 | -------------------------------------------------------------------------------- /code/prepare_data/prepare_data.m: -------------------------------------------------------------------------------- 1 | clear; 2 | dataset_name = 'Market-1501-v15.09.15'; 3 | 4 | p = strcat('/home/yan/datasets/',dataset_name,'/bounding_box_train/'); 5 | pp = strcat('/home/yan/datasets/',dataset_name,'/bounding_box_train/*.jpg'); 6 | pw = strcat('/home/yan/datasets/',dataset_name,'/bounding_box_train_256/'); 7 | if ~exist(pw) 8 | mkdir(pw); 9 | end 10 | imdb.meta.sets=['train','test']; 11 | file = dir(pp); 12 | counter_data=1; 13 | counter_last = 1; 14 | class = 0; 15 | c_last = ''; 16 | cc = []; 17 | for i=1:length(file) 18 | url = strcat(p,file(i).name); 19 | im = imread(url); 20 | im = imresize(im,[256,256]); % save 256*256 image in advance for faster IO 21 | url256 = strcat(pw,file(i).name); 22 | imwrite(im,url256); 23 | imdb.images.data(counter_data) = cellstr(url256); 24 | c = strsplit(file(i).name,'_'); 25 | if(~isequal(c{1},c_last)) 26 | class = class + 1; 27 | fprintf('%d::%d\n',class,counter_data-counter_last); 28 | cc=[cc;counter_data-counter_last]; 29 | c_last = c{1}; 30 | counter_last = counter_data; 31 | end 32 | imdb.images.label(:,counter_data) = class; 33 | counter_data = counter_data + 1; 34 | end 35 | s = counter_data-1; 36 | imdb.images.set = ones(1,s); 37 | imdb.images.set(:,randi(s,[round(0.1*s),1])) = 2; 38 | 39 | % no validation for small class 40 | cc = [cc;9]; 41 | cc = cc(2:end); 42 | list = find(imdb.images.set==2); 43 | for i=1:numel(list) 44 | if cc(imdb.images.label(list(i)))<10 45 | imdb.images.set(list(i))=1; 46 | end 47 | end 48 | 49 | save(strcat('./data/url_data.mat'),'imdb','-v7.3'); 50 | -------------------------------------------------------------------------------- /matlab/vl_nnnormalizelp.m: -------------------------------------------------------------------------------- 1 | function y = vl_nnnormalizelp(x,dzdy,varargin) 2 | %VL_NNNORMALIZELP CNN Lp normalization 3 | % Y = VL_NNNORMALIZELP(X) normalizes in Lp norm each spatial 4 | % location in the array X: 5 | % 6 | % Y(i,j,k) = X(i,j,k) / sum_q (X(i,j,q).^p + epsilon)^(1/p) 7 | % 8 | % DZDX = VL_NNNORMALIZELP(X, DZDY) computes the derivative of the 9 | % function with respect to X projected onto DZDY. 10 | % 11 | % VL_NNNORMALIZE(___, 'opts', val, ...) takes the following options: 12 | % 13 | % `p`:: 2 14 | % The exponent of the Lp norm. Warning: currently only even 15 | % exponents are supported. 16 | % 17 | % `epsilon`:: 0.01 18 | % The constant added to the sum of p-powers before taking the 19 | % 1/p square root (see the formula above). 20 | % 21 | % `spatial`:: `false` 22 | % If `true`, sum along the two spatial dimensions instead of 23 | % along the feature channels. 24 | % 25 | % See also: VL_NNNORMALIZE(). 26 | 27 | opts.epsilon = 1e-2 ; 28 | opts.p = 2 ; 29 | opts.spatial = false ; 30 | opts = vl_argparse(opts, varargin, 'nonrecursive') ; 31 | 32 | if ~opts.spatial 33 | massp = sum(x.^opts.p,3) + opts.epsilon ; 34 | else 35 | massp = sum(sum(x.^opts.p,1),2) + opts.epsilon ; 36 | end 37 | mass = massp.^(1/opts.p) ; 38 | y = bsxfun(@rdivide, x, mass) ; 39 | 40 | if nargin < 2 || isempty(dzdy) 41 | return ; 42 | else 43 | dzdy = bsxfun(@rdivide, dzdy, mass) ; 44 | if ~opts.spatial 45 | tmp = sum(dzdy .* x, 3) ; 46 | else 47 | tmp = sum(sum(dzdy .* x, 1),2); 48 | end 49 | y = dzdy - bsxfun(@times, tmp, bsxfun(@rdivide, x.^(opts.p-1), massp)) ; 50 | end 51 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nnsoftmaxloss.m: -------------------------------------------------------------------------------- 1 | classdef nnsoftmaxloss < nntest 2 | properties (TestParameter) 3 | weighed = {false true} 4 | multilab = {false true} 5 | end 6 | 7 | methods (Test) 8 | function basic(test, multilab, weighed) 9 | C = 10 ; 10 | n = 3 ; 11 | if multilab 12 | c = reshape(mod(0:3*4*n-1,C)+1, 3, 4, 1, n) ; 13 | else 14 | c = reshape([7 2 1],1,1,1,[]) ; 15 | end 16 | if weighed 17 | c = cat(3, c, test.rand(size(c))) ; 18 | end 19 | 20 | % compare direct and indirect composition; this cannot 21 | % take large test.ranges 22 | x = test.rand(3,4,C,n)/test.range + 0.001 ; % non-negative 23 | y = vl_nnsoftmaxloss(x,c) ; 24 | if size(c,3) == 1 25 | opts = {'loss','log'} ; 26 | else 27 | opts = {'loss','log','instanceWeights',c(:,:,2,:)} ; 28 | end 29 | y_ = vl_nnloss(vl_nnsoftmax(x),c(:,:,1,:),[],opts{:}) ; 30 | dzdy = test.randn(size(y)) ; 31 | dzdx = vl_nnsoftmaxloss(x,c,dzdy) ; 32 | dzdx_ = vl_nnsoftmax(x,vl_nnloss(vl_nnsoftmax(x),c(:,:,1,:),dzdy,opts{:})) ; 33 | test.eq(y,y_) ; 34 | test.eq(dzdx,dzdx_) ; 35 | test.der(@(x) vl_nnsoftmaxloss(x,c), x, dzdy, dzdx, 0.001, -5e1) ; 36 | 37 | % now larger input range 38 | x = test.rand(3,4,C,n) + test.range * 0.001 ; % non-negative 39 | y = vl_nnsoftmaxloss(x,c) ; 40 | dzdy = test.randn(size(y)) ; 41 | dzdx = vl_nnsoftmaxloss(x,c,dzdy) ; 42 | test.der(@(x) vl_nnsoftmaxloss(x,c), ... 43 | x, dzdy, dzdx, test.range * 0.001, -5e1) ; 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /matlab/vl_nnnormalize.m: -------------------------------------------------------------------------------- 1 | %VL_NNNORMALIZE CNN Local Response Normalization (LRN) 2 | % Y = VL_NNORMALIZE(X, PARAM) computes the so-called Local Response 3 | % Normalization (LRN) operator. This operator performs a 4 | % channel-wise sliding window normalization of each column of the 5 | % input array X. The normalized output is given by: 6 | % 7 | % Y(i,j,k) = X(i,j,k) / L(i,j,k)^BETA 8 | % 9 | % where the normalization factor is given by 10 | % 11 | % L(i,j,k) = KAPPA + ALPHA * (sum_{q in Q(k)} X(i,j,k)^2, 12 | % 13 | % PARAM = [N KAPPA ALPHA BETA], and N is the size of the window. The 14 | % window Q(k) is defined as: 15 | % 16 | % Q(k) = [max(1, k-FLOOR((N-1)/2)), min(D, k+CEIL((N-1)/2))]. 17 | % 18 | % where D is the number of feature channels in X. Note in particular 19 | % that, by setting N >= 2D, the function can be used to normalize 20 | % all the channels as a single group (useful to achieve L2 21 | % normalization). 22 | % 23 | % DZDX = VL_NNORMALIZE(X, PARAM, DZDY) computes the derivative of 24 | % the block projected onto DZDY. DZDX and DZDY have the same 25 | % dimensions as X and Y respectively. 26 | % 27 | % **Remark:** Some CNN libraries (e.g. Caffe) use a slightly 28 | % different convention for the parameters of the LRN. Caffe in 29 | % particular uses the convention: 30 | % 31 | % PARAM_CAFFE = [N KAPPA N*ALPHA BETA] 32 | % 33 | % i.e. the ALPHA paramter is multiplied by N. 34 | 35 | % Copyright (C) 2014 Andrea Vedaldi. 36 | % All rights reserved. 37 | % 38 | % This file is part of the VLFeat library and is made available under 39 | % the terms of the BSD license (see the COPYING file). 40 | -------------------------------------------------------------------------------- /matlab/+dagnn/@DagNN/saveobj.m: -------------------------------------------------------------------------------- 1 | function s = saveobj(obj) 2 | %SAVEOBJ Save a DagNN to a vanilla MATLAB structure 3 | % S = OBJ.SAVEOBJ() saves the DagNN OBJ to a vanilla MATLAB 4 | % structure S. This is particularly convenient to preserve future 5 | % compatibility and to ship networks that are pure structures, 6 | % instead of embedding dependencies to code. 7 | % 8 | % The object can be reconstructe by `obj = DagNN.loadobj(s)`. 9 | % 10 | % As a side-effect the network is being reset (all variables are cleared) 11 | % and is transfered to CPU. 12 | % 13 | % See Also: dagnn.DagNN.loadobj, dagnn.DagNN.reset 14 | 15 | % Copyright (C) 2015-2016 Karel Lenc and Andrea Vedaldi. 16 | % All rights reserved. 17 | % 18 | % This file is part of the VLFeat library and is made available under 19 | % the terms of the BSD license (see the COPYING file). 20 | 21 | device = obj.device ; 22 | obj.move('cpu') ; 23 | s.vars = struct(... 24 | 'name', {obj.vars.name}, ... 25 | 'precious', {obj.vars.precious}) ; 26 | s.params = struct(... 27 | 'name', {obj.params.name}, ... 28 | 'value', {obj.params.value}, ... 29 | 'learningRate', {obj.params.learningRate}, ... 30 | 'weightDecay', {obj.params.weightDecay}) ; 31 | s.layers = struct(... 32 | 'name', {obj.layers.name}, ... 33 | 'type', {[]}, ... 34 | 'inputs', {obj.layers.inputs}, ... 35 | 'outputs', {obj.layers.outputs}, ... 36 | 'params', {obj.layers.params}, ... 37 | 'block', {[]}) ; 38 | s.meta = obj.meta ; 39 | 40 | for l = 1:numel(obj.layers) 41 | block = obj.layers(l).block ; 42 | slayer = block.save() ; 43 | s.layers(l).type = class(block) ; 44 | s.layers(l).block = slayer ; 45 | end 46 | -------------------------------------------------------------------------------- /matlab/+dagnn/Square2.m: -------------------------------------------------------------------------------- 1 | classdef Square2 < dagnn.ElementWise 2 | properties 3 | dim = 3 4 | end 5 | 6 | properties (Transient) 7 | inputSizes = {} 8 | end 9 | 10 | methods 11 | function outputs = forward(obj, inputs, params) 12 | outputs{1} = inputs{1}*inputs{2} ; 13 | obj.inputSizes = cellfun(@size, inputs, 'UniformOutput', false) ; 14 | end 15 | 16 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 17 | derInputs{1} = inputs{2}; 18 | derInputs{2} = inputs{1}; 19 | derParams = {} ; 20 | end 21 | 22 | function reset(obj) 23 | obj.inputSizes = {} ; 24 | end 25 | 26 | function outputSizes = getOutputSizes(obj, inputSizes) 27 | sz = inputSizes{1} ; 28 | outputSizes{1} = sz ; 29 | end 30 | 31 | function rfs = getReceptiveFields(obj) 32 | numInputs = numel(obj.net.layers(obj.layerIndex).inputs) ; 33 | if obj.dim == 3 || obj.dim == 4 34 | rfs = getReceptiveFields@dagnn.ElementWise(obj) ; 35 | rfs = repmat(rfs, numInputs, 1) ; 36 | else 37 | for i = 1:numInputs 38 | rfs(i,1).size = [NaN NaN] ; 39 | rfs(i,1).stride = [NaN NaN] ; 40 | rfs(i,1).offset = [NaN NaN] ; 41 | end 42 | end 43 | end 44 | 45 | function load(obj, varargin) 46 | s = dagnn.Layer.argsToStruct(varargin{:}) ; 47 | % backward file compatibility 48 | if isfield(s, 'numInputs'), s = rmfield(s, 'numInputs') ; end 49 | load@dagnn.Layer(obj, s) ; 50 | end 51 | 52 | function obj = Square2(varargin) 53 | obj.load(varargin{:}) ; 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /matlab/src/bits/imread.hpp: -------------------------------------------------------------------------------- 1 | // @file imread.hpp 2 | // @brief Image reader 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2015-16 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__imread__ 14 | #define __vl__imread__ 15 | 16 | #include "data.hpp" 17 | 18 | namespace vl { 19 | 20 | #define VL_IMAGE_ERROR_MSG_MAX_LENGTH 256 21 | 22 | struct ImageShape 23 | { 24 | size_t height ; 25 | size_t width ; 26 | size_t depth ; 27 | 28 | ImageShape() ; 29 | ImageShape(size_t height, size_t width, size_t depth) ; 30 | ImageShape(ImageShape const & im) ; 31 | ImageShape & operator = (ImageShape const & im) ; 32 | bool operator == (ImageShape const & im) ; 33 | 34 | size_t getNumElements() const ; 35 | void clear() ; 36 | } ; 37 | 38 | class Image 39 | { 40 | public: 41 | Image() ; 42 | Image(Image const & im) ; 43 | Image(ImageShape const & shape, float * memory = NULL) ; 44 | ImageShape const & getShape() const ; 45 | float * getMemory() const ; 46 | void clear() ; 47 | 48 | protected: 49 | ImageShape shape ; 50 | float * memory ; 51 | } ; 52 | 53 | class ImageReader 54 | { 55 | public: 56 | ImageReader() ; 57 | ~ImageReader() ; 58 | vl::ErrorCode readShape(ImageShape & image, char const * fileName) ; 59 | vl::ErrorCode readPixels(float * memory, char const * fileName) ; 60 | char const * getLastErrorMessage() const ; 61 | 62 | private: 63 | class Impl ; 64 | Impl * impl ; 65 | } ; 66 | } 67 | 68 | #endif 69 | -------------------------------------------------------------------------------- /matlab/+dagnn/@DagNN/getVarSizes.m: -------------------------------------------------------------------------------- 1 | function sizes = getVarSizes(obj, inputSizes) 2 | %GETVARSIZES Get the size of the variables 3 | % SIZES = GETVARSIZES(OBJ, INPUTSIZES) computes the SIZES of the 4 | % DagNN variables given the size of the inputs. `inputSizes` is 5 | % a cell array of the type `{'inputName', inputSize, ...}` 6 | % Returns a cell array with sizes of all network variables. 7 | % 8 | % Example, compute the storage needed for a batch size of 256 for an 9 | % imagenet-like network: 10 | % ``` 11 | % batch_size = 256; single_num_bytes = 4; 12 | % input_size = [net.meta.normalization.imageSize, batch_size]; 13 | % var_sizes = net.getVarSizes({'data', input_size}); 14 | % fprintf('Network activations will take %.2fMiB in single.\n', ... 15 | % sum(prod(cell2mat(var_sizes, 1))) * single_num_bytes ./ 1024^3); 16 | % ``` 17 | 18 | % Copyright (C) 2015 Andrea Vedaldi, Karel Lenc. 19 | % All rights reserved. 20 | % 21 | % This file is part of the VLFeat library and is made available under 22 | % the terms of the BSD license (see the COPYING file). 23 | 24 | nv = numel(obj.vars) ; 25 | sizes = num2cell(NaN(nv, 4),2)' ; 26 | 27 | for i = 1:2:numel(inputSizes) 28 | v = obj.getVarIndex(inputSizes{i}) ; 29 | if isnan(v) 30 | error('Variable `%s` not found in the network.', inputSizes{i}); 31 | end; 32 | if isempty(inputSizes{i+1}) 33 | sizes{v} = [0 0 0 0] ; 34 | else 35 | sizes{v} = [inputSizes{i+1}(:)' ones(1, 4 - numel(inputSizes{i+1}))] ; 36 | end 37 | end 38 | 39 | for layer = obj.layers(obj.executionOrder) 40 | in = layer.inputIndexes ; 41 | out = layer.outputIndexes ; 42 | sizes(out) = layer.block.getOutputSizes(sizes(in)) ; 43 | end 44 | -------------------------------------------------------------------------------- /evaluation/utils/gen_train_sample_kissme.m: -------------------------------------------------------------------------------- 1 | function [idxa,idxb,flag] = gen_train_sample_kissme(label_train, cam_train) 2 | 3 | % generate ground truth pairs for training 4 | uni_label = unique(label_train); 5 | idxa = []; % index of the first image in a pair 6 | idxb = []; % index of the second image in a pair 7 | flag = []; % indicate whether two images are of the same identity 8 | for n = 1:length(uni_label) 9 | curr_label = uni_label(n); 10 | pos = find(label_train == uni_label(n)); 11 | if length(pos) == 1 12 | pos = [pos, pos]; 13 | end 14 | comb = nchoosek(pos,2); 15 | idxa = [idxa; comb(:, 1)]; 16 | idxb = [idxb; comb(:, 2)]; 17 | end 18 | % remove pairs from the same camera 19 | cam1 = cam_train(idxa); 20 | cam2 = cam_train(idxb); 21 | Eq_pos = find(cam1 == cam2); 22 | diff_pos = setdiff(1:length(idxa), Eq_pos); 23 | idxa = idxa(diff_pos); 24 | idxb = idxb(diff_pos); 25 | nPos = length(idxa); 26 | flag = [flag; ones(nPos, 1)]; 27 | 28 | % generate negative training pairs 29 | nTrainImg = length(label_train); 30 | rand_pos = ceil(rand(150000, 2).*nTrainImg); 31 | ID1 = label_train(rand_pos(:, 1)); 32 | ID2 = label_train(rand_pos(:, 2)); 33 | Eq_pos = find(ID1 == ID2); 34 | diff_pos = setdiff(1:150000, Eq_pos); % remove pairs of the same identity 35 | rand_pos = rand_pos(diff_pos, :); 36 | cam1 = cam_train(rand_pos(:, 1)); 37 | cam2 = cam_train(rand_pos(:, 2)); 38 | Eq_pos = find(cam1 == cam2); 39 | diff_pos = setdiff(1:length(rand_pos), Eq_pos);% remove pairs of the same camera 40 | 41 | %%%% training image pairs and their ground truth labels %%%%%%%% 42 | idxa = [idxa; rand_pos(diff_pos(1:nPos), 1)]; 43 | idxb = [idxb; rand_pos(diff_pos(1:nPos), 2)]; 44 | flag = [flag; zeros(nPos, 1)]; -------------------------------------------------------------------------------- /matlab/+dagnn/Square.m: -------------------------------------------------------------------------------- 1 | classdef Square < dagnn.ElementWise 2 | properties 3 | dim = 3 4 | end 5 | 6 | properties (Transient) 7 | inputSizes = {} 8 | end 9 | 10 | methods 11 | function outputs = forward(obj, inputs, params) 12 | outputs{1} = bsxfun(@minus,inputs{1},inputs{2}).^2 ; 13 | obj.inputSizes = cellfun(@size, inputs, 'UniformOutput', false) ; 14 | end 15 | 16 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 17 | derInputs{1} = 2.*bsxfun(@times,(inputs{1}-inputs{2}),derOutputs{1}); 18 | derInputs{2} = -derInputs{1}; 19 | derParams = {} ; 20 | end 21 | 22 | function reset(obj) 23 | obj.inputSizes = {} ; 24 | end 25 | 26 | function outputSizes = getOutputSizes(obj, inputSizes) 27 | sz = inputSizes{1} ; 28 | outputSizes{1} = sz ; 29 | end 30 | 31 | function rfs = getReceptiveFields(obj) 32 | numInputs = numel(obj.net.layers(obj.layerIndex).inputs) ; 33 | if obj.dim == 3 || obj.dim == 4 34 | rfs = getReceptiveFields@dagnn.ElementWise(obj) ; 35 | rfs = repmat(rfs, numInputs, 1) ; 36 | else 37 | for i = 1:numInputs 38 | rfs(i,1).size = [NaN NaN] ; 39 | rfs(i,1).stride = [NaN NaN] ; 40 | rfs(i,1).offset = [NaN NaN] ; 41 | end 42 | end 43 | end 44 | 45 | function load(obj, varargin) 46 | s = dagnn.Layer.argsToStruct(varargin{:}) ; 47 | % backward file compatibility 48 | if isfield(s, 'numInputs'), s = rmfield(s, 'numInputs') ; end 49 | load@dagnn.Layer(obj, s) ; 50 | end 51 | 52 | function obj = Square(varargin) 53 | obj.load(varargin{:}) ; 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nnroipool.m: -------------------------------------------------------------------------------- 1 | classdef nnroipool < nntest 2 | properties 3 | x 4 | end 5 | 6 | properties (TestParameter) 7 | method = {'avg', 'max'} 8 | subdivisions = {[1 1], [2 1], [1 2], [3 7], [16 16]} 9 | end 10 | 11 | methods (TestClassSetup) 12 | function data(test,device) 13 | % make sure that all elements in x are different. in this way, 14 | % we can compute numerical derivatives reliably by adding a delta < .5. 15 | x = test.randn(15,14,3,2) ; 16 | x(:) = randperm(numel(x))' ; 17 | test.x = x ; 18 | test.range = 10 ; 19 | if strcmp(device,'gpu'), test.x = gpuArray(test.x) ; end 20 | end 21 | end 22 | 23 | methods (Test) 24 | function basic(test,method,subdivisions) 25 | R = [1 1 1 2 2 2 1 1 ; 26 | 0 1 2 0 1 2 1 1 ; 27 | 0 4 3 0 1 2 1 1 ; 28 | 15 5 6 15 4 2 9 0 ; 29 | 14 7 9 14 4 8 1 0] ; 30 | R = test.toDevice(test.toDataType(R)) ; 31 | x = test.x ; 32 | args = {'method', method, 'subdivisions', subdivisions} ; 33 | y = vl_nnroipool(x,R,args{:}) ; 34 | dzdy = test.randn(size(y)) ; 35 | dzdx = vl_nnroipool(x,R,dzdy,args{:}) ; 36 | test.der(@(x) vl_nnroipool(x,R,args{:}), ... 37 | x, dzdy, dzdx, test.range * 1e-2) ; 38 | end 39 | 40 | function identity(test,method) 41 | x = test.toDevice(test.toDataType((2:10)'*(1:10))) ; 42 | R = test.toDevice(test.toDataType([1, 1, 1, 9, 10])) ; 43 | T = [0 1 0 ; 1 0 0] ; 44 | opts = {'method', method, ... 45 | 'subdivisions', [9,10], ... 46 | 'transform', T} ; 47 | y = vl_nnroipool(x,R,opts{:}) ; 48 | test.eq(x,y) ; 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/copy_gpu.cu: -------------------------------------------------------------------------------- 1 | // @file copy_gpu.cu 2 | // @brief Copy and other data operations (GPU) 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2015-16 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #include "copy.hpp" 14 | #include "../datacu.hpp" 15 | #include 16 | 17 | template __global__ void 18 | fill_kernel (type * data, type value, size_t size) 19 | { 20 | int index = threadIdx.x + blockIdx.x * blockDim.x ; 21 | if (index < size) data[index] = value ; 22 | } 23 | 24 | namespace vl { namespace impl { 25 | 26 | template 27 | struct operations 28 | { 29 | typedef type data_type ; 30 | 31 | static vl::ErrorCode 32 | copy(data_type * dest, 33 | data_type const * src, 34 | size_t numElements) 35 | { 36 | cudaMemcpy(dest, src, numElements * sizeof(data_type), cudaMemcpyDeviceToDevice) ; 37 | return VLE_Success ; 38 | } 39 | 40 | static vl::ErrorCode 41 | fill(data_type * dest, 42 | size_t numElements, 43 | data_type value) 44 | { 45 | fill_kernel 46 | <<>> 47 | (dest, numElements, value) ; 48 | 49 | cudaError_t error = cudaGetLastError() ; 50 | if (error != cudaSuccess) { 51 | return VLE_Cuda ; 52 | } 53 | return VLE_Success ; 54 | } 55 | } ; 56 | 57 | } } 58 | 59 | template struct vl::impl::operations ; 60 | 61 | #ifdef ENABLE_DOUBLE 62 | template struct vl::impl::operations ; 63 | #endif -------------------------------------------------------------------------------- /matlab/vl_nnrelu.m: -------------------------------------------------------------------------------- 1 | function y = vl_nnrelu(x,dzdy,varargin) 2 | %VL_NNRELU CNN rectified linear unit. 3 | % Y = VL_NNRELU(X) applies the rectified linear unit to the data 4 | % X. X can have arbitrary size. 5 | % 6 | % DZDX = VL_NNRELU(X, DZDY) computes the derivative of the block 7 | % projected onto DZDY. DZDX and DZDY have the same dimensions as 8 | % X and Y respectively. 9 | % 10 | % VL_NNRELU(...,'OPT',VALUE,...) takes the following options: 11 | % 12 | % `Leak`:: 0 13 | % Set the leak factor, a non-negative number. Y is equal to X if 14 | % X is not smaller than zero; otherwise, Y is equal to X 15 | % multipied by the leak factor. By default, the leak factor is 16 | % zero; for values greater than that one obtains the leaky ReLU 17 | % unit. 18 | % 19 | % ADVANCED USAGE 20 | % 21 | % As a further optimization, in the backward computation it is 22 | % possible to replace X with Y, namely, if Y = VL_NNRELU(X), then 23 | % VL_NNRELU(X,DZDY) gives the same result as VL_NNRELU(Y,DZDY). 24 | % This is useful because it means that the buffer X does not need to 25 | % be remembered in the backward pass. 26 | 27 | % Copyright (C) 2014-15 Andrea Vedaldi. 28 | % All rights reserved. 29 | % 30 | % This file is part of the VLFeat library and is made available under 31 | % the terms of the BSD license (see the COPYING file). 32 | 33 | opts.leak = 0 ; 34 | opts = vl_argparse(opts, varargin, 'nonrecursive') ; 35 | 36 | if opts.leak == 0 37 | if nargin <= 1 || isempty(dzdy) 38 | y = max(x, 0) ; 39 | else 40 | y = dzdy .* (x > 0) ; 41 | end 42 | else 43 | if nargin <= 1 || isempty(dzdy) 44 | y = x .* (opts.leak + (1 - opts.leak) * (x > 0)) ; 45 | else 46 | y = dzdy .* (opts.leak + (1 - opts.leak) * (x > 0)) ; 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /matlab/+dagnn/Concat.m: -------------------------------------------------------------------------------- 1 | classdef Concat < dagnn.ElementWise 2 | properties 3 | dim = 3 4 | end 5 | 6 | properties (Transient) 7 | inputSizes = {} 8 | end 9 | 10 | methods 11 | function outputs = forward(obj, inputs, params) 12 | outputs{1} = vl_nnconcat(inputs, obj.dim) ; 13 | obj.inputSizes = cellfun(@size, inputs, 'UniformOutput', false) ; 14 | end 15 | 16 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 17 | derInputs = vl_nnconcat(inputs, obj.dim, derOutputs{1}, 'inputSizes', obj.inputSizes) ; 18 | derParams = {} ; 19 | end 20 | 21 | function reset(obj) 22 | obj.inputSizes = {} ; 23 | end 24 | 25 | function outputSizes = getOutputSizes(obj, inputSizes) 26 | sz = inputSizes{1} ; 27 | for k = 2:numel(inputSizes) 28 | sz(obj.dim) = sz(obj.dim) + inputSizes{k}(obj.dim) ; 29 | end 30 | outputSizes{1} = sz ; 31 | end 32 | 33 | function rfs = getReceptiveFields(obj) 34 | numInputs = numel(obj.net.layers(obj.layerIndex).inputs) ; 35 | if obj.dim == 3 || obj.dim == 4 36 | rfs = getReceptiveFields@dagnn.ElementWise(obj) ; 37 | rfs = repmat(rfs, numInputs, 1) ; 38 | else 39 | for i = 1:numInputs 40 | rfs(i,1).size = [NaN NaN] ; 41 | rfs(i,1).stride = [NaN NaN] ; 42 | rfs(i,1).offset = [NaN NaN] ; 43 | end 44 | end 45 | end 46 | 47 | function load(obj, varargin) 48 | s = dagnn.Layer.argsToStruct(varargin{:}) ; 49 | % backward file compatibility 50 | if isfield(s, 'numInputs'), s = rmfield(s, 'numInputs') ; end 51 | load@dagnn.Layer(obj, s) ; 52 | end 53 | 54 | function obj = Concat(varargin) 55 | obj.load(varargin{:}) ; 56 | end 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /matlab/src/bits/imread.cpp: -------------------------------------------------------------------------------- 1 | // @file imread.cpp 2 | // @brief Image reader 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2015-16 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #include "imread.hpp" 14 | #include 15 | 16 | vl::ImageShape::ImageShape() 17 | : height(0), width(0), depth(0) 18 | { } 19 | 20 | vl::ImageShape::ImageShape(size_t height, size_t width, size_t depth) 21 | : height(height), width(width), depth(depth) 22 | { } 23 | 24 | vl::ImageShape::ImageShape(ImageShape const & im) 25 | : height(im.height), width(im.width), depth(im.depth) 26 | { } 27 | 28 | vl::ImageShape & vl::ImageShape::operator =(vl::ImageShape const & im) 29 | { 30 | height = im.height ; 31 | width = im.width ; 32 | depth = im.depth ; 33 | return *this ; 34 | } 35 | 36 | bool vl::ImageShape::operator == (vl::ImageShape const & im) 37 | { 38 | return 39 | (height == im.height) & 40 | (width == im.width) & 41 | (depth == im.depth) ; 42 | } 43 | 44 | size_t vl::ImageShape::getNumElements() const 45 | { 46 | return height*width*depth ; 47 | } 48 | 49 | void vl::ImageShape::clear() 50 | { 51 | height = 0 ; 52 | width = 0 ; 53 | depth = 0 ; 54 | } 55 | 56 | vl::Image::Image() 57 | : shape(), memory(NULL) 58 | { } 59 | 60 | vl::Image::Image(Image const & im) 61 | : shape(im.shape), memory(im.memory) 62 | { } 63 | 64 | vl::Image::Image(vl::ImageShape const & shape, float * memory) 65 | : shape(shape), memory(memory) 66 | { } 67 | 68 | vl::ImageShape const & vl::Image::getShape() const { return shape ; } 69 | float * vl::Image::getMemory() const { return memory ; } 70 | 71 | void vl::Image::clear() 72 | { 73 | shape.clear() ; 74 | memory = 0 ; 75 | } 76 | -------------------------------------------------------------------------------- /matlab/+dagnn/Filter.m: -------------------------------------------------------------------------------- 1 | classdef Filter < dagnn.Layer 2 | properties 3 | pad = [0 0 0 0] 4 | stride = [1 1] 5 | dilate = [1 1] 6 | end 7 | methods 8 | function set.pad(obj, pad) 9 | if numel(pad) == 1 10 | obj.pad = [pad pad pad pad] ; 11 | elseif numel(pad) == 2 12 | obj.pad = pad([1 1 2 2]) ; 13 | else 14 | obj.pad = pad ; 15 | end 16 | end 17 | 18 | function set.stride(obj, stride) 19 | if numel(stride) == 1 20 | obj.stride = [stride stride] ; 21 | else 22 | obj.stride = stride ; 23 | end 24 | end 25 | 26 | function set.dilate(obj, dilate) 27 | if numel(dilate) == 1 28 | obj.dilate = [dilate dilate] ; 29 | else 30 | obj.dilate = dilate ; 31 | end 32 | end 33 | 34 | function kernelSize = getKernelSize(obj) 35 | kernelSize = [1 1] ; 36 | end 37 | 38 | function outputSizes = getOutputSizes(obj, inputSizes) 39 | ks = obj.getKernelSize() ; 40 | ke = (ks - 1) .* obj.dilate + 1 ; 41 | outputSizes{1} = [... 42 | fix((inputSizes{1}(1) + obj.pad(1) + obj.pad(2) - ke(1)) / obj.stride(1)) + 1, ... 43 | fix((inputSizes{1}(2) + obj.pad(3) + obj.pad(4) - ke(2)) / obj.stride(2)) + 1, ... 44 | 1, ... 45 | inputSizes{1}(4)] ; 46 | end 47 | 48 | function rfs = getReceptiveFields(obj) 49 | ks = obj.getKernelSize() ; 50 | ke = (ks - 1) .* obj.dilate + 1 ; 51 | y1 = 1 - obj.pad(1) ; 52 | y2 = 1 - obj.pad(1) + ke(1) - 1 ; 53 | x1 = 1 - obj.pad(3) ; 54 | x2 = 1 - obj.pad(3) + ke(2) - 1 ; 55 | h = y2 - y1 + 1 ; 56 | w = x2 - x1 + 1 ; 57 | rfs.size = [h, w] ; 58 | rfs.stride = obj.stride ; 59 | rfs.offset = [y1+y2, x1+x2]/2 ; 60 | end 61 | end 62 | end 63 | -------------------------------------------------------------------------------- /matlab/xtest/suite/nnconcat.m: -------------------------------------------------------------------------------- 1 | classdef nnconcat < nntest 2 | methods (Test) 3 | function basic(test) 4 | pick = @(i,x) x{i} ; 5 | sz = [4,5,10,3] ; 6 | for dim = 1:3 7 | sz1 = sz ; sz1(dim) = 3 ; 8 | sz2 = sz ; sz2(dim) = 7 ; 9 | sz3 = sz ; sz3(dim) = 2 ; 10 | x1 = test.randn(sz1) ; 11 | x2 = test.randn(sz2) ; 12 | x3 = test.randn(sz3) ; 13 | 14 | y = vl_nnconcat({x1, x2, x3}, dim) ; 15 | test.verifyEqual(size(y,dim), size(x1,dim)+size(x2,dim)+size(x3,dim)) ; 16 | dzdy = test.randn(size(y)) ; 17 | dzdx = vl_nnconcat({x1, x2, x3} ,dim, dzdy) ; 18 | 19 | test.der(@(x1) vl_nnconcat({x1, x2, x3},dim), x1, dzdy, dzdx{1}, 1e-3*test.range) ; 20 | test.der(@(x2) vl_nnconcat({x1, x2, x3},dim), x2, dzdy, dzdx{2}, 1e-3*test.range) ; 21 | test.der(@(x3) vl_nnconcat({x1, x2, x3},dim), x3, dzdy, dzdx{3}, 1e-3*test.range) ; 22 | end 23 | end 24 | 25 | function by_size(test) 26 | pick = @(i,x) x{i} ; 27 | sz = [4,5,10,3] ; 28 | for dim = 1:3 29 | sz1 = sz ; sz1(dim) = 3 ; 30 | sz2 = sz ; sz2(dim) = 7 ; 31 | sz3 = sz ; sz3(dim) = 2 ; 32 | x1 = test.randn(sz1) ; 33 | x2 = test.randn(sz2) ; 34 | x3 = test.randn(sz3) ; 35 | 36 | y = vl_nnconcat({x1, x2, x3}, dim) ; 37 | test.verifyEqual(size(y,dim), size(x1,dim)+size(x2,dim)+size(x3,dim)) ; 38 | dzdy = test.randn(size(y)) ; 39 | dzdx = vl_nnconcat({}, dim, dzdy, 'inputSizes', {sz1, sz2, sz3}) ; 40 | 41 | test.der(@(x1) vl_nnconcat({x1, x2, x3},dim), x1, dzdy, dzdx{1}, 1e-3*test.range) ; 42 | test.der(@(x2) vl_nnconcat({x1, x2, x3},dim), x2, dzdy, dzdx{2}, 1e-3*test.range) ; 43 | test.der(@(x3) vl_nnconcat({x1, x2, x3},dim), x3, dzdy, dzdx{3}, 1e-3*test.range) ; 44 | end 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /matlab/+dagnn/ROIPooling.m: -------------------------------------------------------------------------------- 1 | classdef ROIPooling < dagnn.Layer 2 | % DAGNN.ROIPOOLING Region of interest pooling layer 3 | 4 | % Copyright (C) 2016 Hakan Bilen. 5 | % All rights reserved. 6 | % 7 | % This file is part of the VLFeat library and is made available under 8 | % the terms of the BSD license (see the COPYING file). 9 | 10 | properties 11 | method = 'max' 12 | subdivisions = [6 6] 13 | transform = 1 14 | flatten = false 15 | end 16 | 17 | methods 18 | function outputs = forward(obj, inputs, params) 19 | numROIs = numel(inputs{2}) / 5 ; 20 | outputs{1} = vl_nnroipool(... 21 | inputs{1}, inputs{2}, ... 22 | 'subdivisions', obj.subdivisions, ... 23 | 'transform', obj.transform, ... 24 | 'method', obj.method) ; 25 | if obj.flatten 26 | outputs{1} = reshape(outputs{1},1,1,[],numROIs) ; 27 | end 28 | end 29 | 30 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 31 | numROIs = numel(inputs{2}) / 5 ; 32 | if obj.flatten 33 | % unflatten 34 | derOutputs{1} = reshape(... 35 | derOutputs{1},obj.subdivisions(1),obj.subdivisions(2),[],numROIs) ; 36 | end 37 | derInputs{1} = vl_nnroipool(... 38 | inputs{1}, inputs{2}, derOutputs{1}, ... 39 | 'subdivisions', obj.subdivisions, ... 40 | 'transform', obj.transform, ... 41 | 'method', obj.method) ; 42 | derInputs{2} = []; 43 | derParams = {} ; 44 | end 45 | 46 | function outputSizes = getOutputSizes(obj, inputSizes) 47 | if isempty(inputSizes{1}) 48 | n = 0 ; 49 | else 50 | n = prod(inputSizes{2})/5 ; 51 | end 52 | outputSizes{1} = [obj.subdivisions, inputSizes{1}(3), n] ; 53 | end 54 | 55 | function obj = ROIPooling(varargin) 56 | obj.load(varargin) ; 57 | end 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /matlab/xtest/vl_bench_imreadjpeg.m: -------------------------------------------------------------------------------- 1 | % VL_BENCH_IMREADJPEG Evaluates the speed of imreadjpeg 2 | 3 | numThreads = 4 ; 4 | base = 'data/bench-imreadjpeg' ; 5 | 6 | files = {} ; 7 | files = dir(fullfile(base,'*.jpg')) ; 8 | files = fullfile(base, {files.name}) ; 9 | if numel(files) > 256, files = files(1:256) ; end 10 | 11 | for preallocate = [true, false] 12 | opts={'verbose','verbose', 'preallocate', preallocate} ; 13 | for t=1:4 14 | % simple read 15 | fprintf('direct read single thread\n') ; 16 | clear ims ; 17 | tic ; 18 | ims = vl_imreadjpeg(files, 'numThreads', 1, opts{:}) ; 19 | directSingle(t) = toc ; 20 | fprintf(' done\n') ; 21 | pause(1) ; 22 | 23 | % simple read 24 | fprintf('direct read multi thread\n') ; 25 | clear ims ; 26 | tic ; 27 | ims = vl_imreadjpeg(files, 'numThreads', numThreads, opts{:}) ; 28 | direct(t) = toc ; 29 | fprintf(' done\n') ; 30 | pause(1) ; 31 | 32 | % threaded read 33 | fprintf('issue prefetch\n') ; 34 | tic ; 35 | vl_imreadjpeg(files, 'prefetch', opts{:}) ; 36 | prefetch(t) = toc ; 37 | fprintf(' done [pause 6]\n') ; 38 | pause(6) 39 | 40 | fprintf('prefetched read\n') ; 41 | clear ims_ ; % do not accoutn for the time requried to delete this 42 | tic ; 43 | ims_ = vl_imreadjpeg(files, opts{:}) ; 44 | indirect(t) = toc ; 45 | pause(1) ; 46 | end 47 | 48 | n = numel(ims) ; 49 | fprintf('** test results preallcoate %d\n', preallocate) ; 50 | fprintf('\tsingle tread: %.1f pm %.1f\n', mean(n./directSingle), std(n./directSingle)) ; 51 | fprintf('\t%d threads: %.1f pm %.1f\n', numThreads, mean(n./direct), std(n./direct)) ; 52 | fprintf('\tissue prefetch: %.1f pm %.1f\n', mean(n./prefetch), std(n./prefetch)) ; 53 | fprintf('\tretrieve prefetched: %.1f pm %.1f\n', mean(n./indirect), std(n./indirect)) ; 54 | fprintf('\n\n') ; 55 | end 56 | 57 | return 58 | -------------------------------------------------------------------------------- /matlab/+dagnn/Crop.m: -------------------------------------------------------------------------------- 1 | classdef Crop < dagnn.ElementWise 2 | %CROP DagNN cropping layer. 3 | % This is a pecurial layer from FCN. It crops inputs{1} to 4 | % match the size of inputs{2} (starting with a base crop amount). 5 | % A future version 6 | 7 | properties 8 | crop = [0 0] 9 | end 10 | 11 | properties (Transient) 12 | inputSizes = {} 13 | end 14 | 15 | methods 16 | function crop = getAdaptedCrops(obj) 17 | cropv = obj.inputSizes{1}(1) - obj.inputSizes{2}(1) ; 18 | cropu = obj.inputSizes{1}(2) - obj.inputSizes{2}(2) ; 19 | cropv1 = max(0, cropv - obj.crop(1)) ; 20 | cropu1 = max(0, cropu - obj.crop(2)) ; 21 | crop = [cropv - cropv1, cropv1, cropu - cropu1, cropu1] ; 22 | end 23 | 24 | function outputs = forward(obj, inputs, params) 25 | obj.inputSizes = cellfun(@size, inputs, 'UniformOutput', false) ; 26 | adjCrop = obj.getAdaptedCrops() ; 27 | outputs{1} = vl_nncrop(inputs{1}, adjCrop) ; 28 | end 29 | 30 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 31 | adjCrop = obj.getAdaptedCrops() ; 32 | derInputs{1} = vl_nncrop(inputs{1}, adjCrop, derOutputs{1}, obj.inputSizes{1}) ; 33 | derInputs{2} = [] ; 34 | derParams = {} ; 35 | end 36 | 37 | function reset(obj) 38 | obj.inputSizes = {} ; 39 | end 40 | 41 | function outputSizes = getOutputSizes(obj, inputSizes) 42 | obj.inputSizes = inputSizes ; 43 | crop = obj.getAdaptedCrops() ; 44 | outputSizes{1} = inputSizes{1} - [crop(1)+crop(2), crop(3)+crop(4), 0, 0] ; 45 | end 46 | 47 | function rfs = getReceptiveFields(obj) 48 | rfs(1,1).size = [1 1] ; 49 | rfs(1,1).stride = [1 1] ; 50 | rfs(1,1).offset = 1 + obj.crop ; 51 | rfs(2,1).size = [] ; 52 | rfs(2,1).stride = [] ; 53 | rfs(2,1).offset = [] ; 54 | end 55 | 56 | function obj = Crop(varargin) 57 | obj.load(varargin) ; 58 | end 59 | end 60 | end 61 | -------------------------------------------------------------------------------- /matlab/+dagnn/Pseudo_Loss.m: -------------------------------------------------------------------------------- 1 | classdef Pseudo_Loss < dagnn.Loss 2 | 3 | methods 4 | function outputs = forward(obj, inputs, params) 5 | labels = inputs{2}; 6 | % get pseudo label every iteration 7 | labels_new = update_pseudo(inputs{1},labels); 8 | instanceWeights = ones(size(labels)); 9 | gan = (labels==0); 10 | instanceWeights(gan) = 0.1; 11 | instanceWeights = reshape(instanceWeights,1,1,1,[]); 12 | outputs{1} = vl_nnloss(inputs{1},labels_new, [], 'loss', obj.loss,'instanceWeights',instanceWeights) ; 13 | n = obj.numAveraged ; 14 | m = n + size(inputs{1},4) ; 15 | obj.average = (n * obj.average + gather(outputs{1})) / m ; 16 | obj.numAveraged = m ; 17 | end 18 | 19 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 20 | labels = inputs{2}; 21 | labels_new = update_pseudo(inputs{1},labels); 22 | instanceWeights = ones(size(labels)); 23 | gan = (labels==0); 24 | instanceWeights(gan) = 0.1; 25 | instanceWeights = reshape(instanceWeights,1,1,1,[]); 26 | derInputs{1} = vl_nnloss(inputs{1}, labels_new, derOutputs{1}, 'loss', obj.loss, 'instanceWeights',instanceWeights,obj.opts{:}) ; 27 | derInputs{2} = [] ; 28 | derParams = {} ; 29 | end 30 | 31 | function reset(obj) 32 | obj.average = 0 ; 33 | obj.numAveraged = 0 ; 34 | end 35 | 36 | function outputSizes = getOutputSizes(obj, inputSizes, paramSizes) 37 | outputSizes{1} = [1 1 1 inputSizes{1}(4)] ; 38 | end 39 | 40 | function rfs = getReceptiveFields(obj) 41 | % the receptive field depends on the dimension of the variables 42 | % which is not known until the network is run 43 | rfs(1,1).size = [NaN NaN] ; 44 | rfs(1,1).stride = [NaN NaN] ; 45 | rfs(1,1).offset = [NaN NaN] ; 46 | rfs(2,1) = rfs(1,1) ; 47 | end 48 | 49 | function obj = Loss(varargin) 50 | obj.load(varargin) ; 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /matlab/+dagnn/Conv.m: -------------------------------------------------------------------------------- 1 | classdef Conv < dagnn.Filter 2 | properties 3 | size = [0 0 0 0] 4 | hasBias = true 5 | opts = {'cuDNN'} 6 | end 7 | 8 | methods 9 | function outputs = forward(obj, inputs, params) 10 | if ~obj.hasBias, params{2} = [] ; end 11 | outputs{1} = vl_nnconv(... 12 | inputs{1}, params{1}, params{2}, ... 13 | 'pad', obj.pad, ... 14 | 'stride', obj.stride, ... 15 | 'dilate', obj.dilate, ... 16 | obj.opts{:}) ; 17 | end 18 | 19 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 20 | if ~obj.hasBias, params{2} = [] ; end 21 | [derInputs{1}, derParams{1}, derParams{2}] = vl_nnconv(... 22 | inputs{1}, params{1}, params{2}, derOutputs{1}, ... 23 | 'pad', obj.pad, ... 24 | 'stride', obj.stride, ... 25 | 'dilate', obj.dilate, ... 26 | obj.opts{:}) ; 27 | end 28 | 29 | function kernelSize = getKernelSize(obj) 30 | kernelSize = obj.size(1:2) ; 31 | end 32 | 33 | function outputSizes = getOutputSizes(obj, inputSizes) 34 | outputSizes = getOutputSizes@dagnn.Filter(obj, inputSizes) ; 35 | outputSizes{1}(3) = obj.size(4) ; 36 | end 37 | 38 | function params = initParams(obj) 39 | % Xavier improved 40 | sc = sqrt(2 / prod(obj.size(1:3))) ; 41 | %sc = sqrt(2 / prod(obj.size([1 2 4]))) ; 42 | params{1} = randn(obj.size,'single') * sc ; 43 | if obj.hasBias 44 | params{2} = zeros(obj.size(4),1,'single') ; 45 | end 46 | end 47 | 48 | function set.size(obj, ksize) 49 | % make sure that ksize has 4 dimensions 50 | ksize = [ksize(:)' 1 1 1 1] ; 51 | obj.size = ksize(1:4) ; 52 | end 53 | 54 | function obj = Conv(varargin) 55 | obj.load(varargin) ; 56 | % normalize field by implicitly calling setters defined in 57 | % dagnn.Filter and here 58 | obj.size = obj.size ; 59 | obj.stride = obj.stride ; 60 | obj.pad = obj.pad ; 61 | end 62 | end 63 | end 64 | -------------------------------------------------------------------------------- /evaluation/utils/compute_r1_multiCam.m: -------------------------------------------------------------------------------- 1 | function r1 = compute_r1_multiCam(good_image, junk_image, index, queryCam, testCam) 2 | good_cam = testCam(good_image); 3 | good_cam_uni = unique(good_cam); 4 | r1 = ones(1, 6)-3; 5 | 6 | % on the same camera 7 | good_cam_now = queryCam; 8 | ngood = length(junk_image)-1; 9 | junk_image_now = [good_image; index(1)]; 10 | good_image_now = setdiff(junk_image, index(1)); 11 | good_now = 0; 12 | for n = 1:length(index) 13 | flag = 0; 14 | if ngood == 0 15 | r1(good_cam_now) = -1; 16 | break; 17 | end 18 | if ~isempty(find(good_image_now == index(n), 1)) 19 | flag = 1; % good image 20 | good_now = good_now+1; 21 | end 22 | if ~isempty(find(junk_image_now == index(n), 1)) 23 | continue; % junk image 24 | end 25 | if flag == 0 26 | r1(good_cam_now) = 0; 27 | break; 28 | end 29 | if flag == 1%good 30 | r1(good_cam_now) = 1; 31 | break; 32 | end 33 | end 34 | 35 | for k = 1:length(good_cam_uni) 36 | good_cam_now = good_cam_uni(k); 37 | ngood = length(find(good_cam == good_cam_now)); 38 | pos_junk = find(good_cam ~= good_cam_now); 39 | junk_image_now = [junk_image; good_image(pos_junk)]; 40 | pos_good = find(good_cam == good_cam_now); 41 | good_image_now = good_image(pos_good); 42 | for n = 1:length(index) 43 | flag = 0; 44 | if ngood == 0 45 | r1(good_cam_now) = -1; 46 | break; 47 | end 48 | if ~isempty(find(good_image_now == index(n), 1)) 49 | flag = 1; % good image 50 | end 51 | if ~isempty(find(junk_image_now == index(n), 1)) 52 | continue; % junk image 53 | end 54 | 55 | if flag == 0 56 | r1(good_cam_now) = 0; 57 | break; 58 | end 59 | if flag == 1%good 60 | r1(good_cam_now) = 1; 61 | break; 62 | end 63 | end 64 | end 65 | 66 | end 67 | 68 | 69 | -------------------------------------------------------------------------------- /evaluation/compute_r1_multiCam.m: -------------------------------------------------------------------------------- 1 | function r1 = compute_r1_multiCam(good_image, junk_image1,junk_image2, index, queryCam, testCam) 2 | good_cam = testCam(good_image); 3 | good_cam_uni = unique(good_cam); 4 | r1 = ones(1, 6)-3; 5 | 6 | % on the same camera 7 | good_cam_now = queryCam; 8 | ngood = length(junk_image2); 9 | junk_image_now = [good_image junk_image1']; 10 | good_image_now = junk_image2; 11 | good_now = 0; 12 | for n = 1:length(index) 13 | flag = 0; 14 | if ngood == 0 15 | r1(good_cam_now) = -1; 16 | break; 17 | end 18 | if ~isempty(find(good_image_now == index(n), 1)) 19 | flag = 1; % good image 20 | good_now = good_now+1; 21 | end 22 | if ~isempty(find(junk_image_now == index(n), 1)) 23 | continue; % junk image 24 | end 25 | if flag == 0 26 | r1(good_cam_now) = 0; 27 | break; 28 | end 29 | if flag == 1%good 30 | r1(good_cam_now) = 1; 31 | break; 32 | end 33 | end 34 | 35 | for k = 1:length(good_cam_uni) 36 | good_cam_now = good_cam_uni(k); 37 | ngood = length(find(good_cam == good_cam_now)); 38 | pos_junk = find(good_cam ~= good_cam_now); 39 | junk_image_now = [junk_image1' junk_image2' good_image(pos_junk)]; 40 | pos_good = find(good_cam == good_cam_now); 41 | good_image_now = good_image(pos_good); 42 | for n = 1:length(index) 43 | flag = 0; 44 | if ngood == 0 45 | r1(good_cam_now) = -1; 46 | break; 47 | end 48 | if ~isempty(find(good_image_now == index(n), 1)) 49 | flag = 1; % good image 50 | end 51 | if ~isempty(find(junk_image_now == index(n), 1)) 52 | continue; % junk image 53 | end 54 | 55 | if flag == 0 56 | r1(good_cam_now) = 0; 57 | break; 58 | end 59 | if flag == 1%good 60 | r1(good_cam_now) = 1; 61 | break; 62 | end 63 | end 64 | end 65 | 66 | end 67 | 68 | 69 | -------------------------------------------------------------------------------- /matlab/xtest/suite/Scale.m: -------------------------------------------------------------------------------- 1 | classdef Scale < nntest 2 | properties 3 | x 4 | a 5 | b 6 | end 7 | 8 | properties (TestParameter) 9 | dim = {1 2 3 4} 10 | end 11 | 12 | methods (TestClassSetup) 13 | function data(test,device) 14 | test.x = test.randn(15,14,3,2) ; 15 | test.a = test.randn(15,14,3,2) ; 16 | test.b = test.randn(15,14,3,2) ; 17 | end 18 | end 19 | 20 | methods (Test) 21 | function data_and_parameters(test, dim) 22 | x = test.x ; 23 | a = test.a ; 24 | b = test.b ; 25 | 26 | a = sum(a, dim) ; 27 | b = sum(b, dim) ; 28 | 29 | scale = dagnn.Scale('size', size(a), 'hasBias', true) ; 30 | 31 | output = scale.forward({x}, {a,b}) ; 32 | dzdy = test.randn(size(output{1})) ; 33 | [derInputs, derParams] = scale.backward({x}, {a,b}, {dzdy}) ; 34 | 35 | pick = @(x) x{1} ; 36 | dzdx = derInputs{1} ; 37 | dzda = derParams{1} ; 38 | dzdb = derParams{2} ; 39 | 40 | test.der(@(x) pick(scale.forward({x},{a,b})), x, dzdy, dzdx, 1e-2 * test.range) ; 41 | test.der(@(a) pick(scale.forward({x},{a,b})), a, dzdy, dzda, 1e-2 * test.range) ; 42 | test.der(@(b) pick(scale.forward({x},{a,b})), b, dzdy, dzdb, 1e-2 * test.range) ; 43 | end 44 | 45 | function data_only(test, dim) 46 | x = test.x ; 47 | a = test.a ; 48 | b = test.b ; 49 | 50 | a = sum(a, dim) ; 51 | b = sum(b, dim) ; 52 | 53 | scale = dagnn.Scale('size', size(a), 'hasBias', true) ; 54 | 55 | output = scale.forward({x,a,b}, {}) ; 56 | dzdy = test.randn(size(output{1})) ; 57 | [derInputs, derParams] = scale.backward({x,a,b}, {}, {dzdy}) ; 58 | 59 | pick = @(x) x{1} ; 60 | dzdx = derInputs{1} ; 61 | dzda = derInputs{2} ; 62 | dzdb = derInputs{3} ; 63 | 64 | test.der(@(x) pick(scale.forward({x,a,b},{})), x, dzdy, dzdx, 1e-2 * test.range) ; 65 | test.der(@(a) pick(scale.forward({x,a,b},{})), a, dzdy, dzda, 1e-2 * test.range) ; 66 | test.der(@(b) pick(scale.forward({x,a,b},{})), b, dzdy, dzdb, 1e-2 * test.range) ; 67 | end 68 | end 69 | end 70 | -------------------------------------------------------------------------------- /matlab/+dagnn/BatchNorm_origin.m: -------------------------------------------------------------------------------- 1 | classdef BatchNorm < dagnn.ElementWise 2 | properties 3 | numChannels 4 | epsilon = 1e-5 5 | opts = {'NoCuDNN'} % ours seems slightly faster 6 | end 7 | 8 | properties (Transient) 9 | moments 10 | end 11 | 12 | methods 13 | function outputs = forward(obj, inputs, params) 14 | if strcmp(obj.net.mode, 'test') 15 | outputs{1} = vl_nnbnorm(inputs{1}, params{1}, params{2}, ... 16 | 'moments', params{3}, ... 17 | 'epsilon', obj.epsilon, ... 18 | obj.opts{:}) ; 19 | else 20 | [outputs{1},obj.moments] = ... 21 | vl_nnbnorm(inputs{1}, params{1}, params{2}, ... 22 | 'epsilon', obj.epsilon, ... 23 | obj.opts{:}) ; 24 | end 25 | end 26 | 27 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 28 | [derInputs{1}, derParams{1}, derParams{2}, derParams{3}] = ... 29 | vl_nnbnorm(inputs{1}, params{1}, params{2}, derOutputs{1}, ... 30 | 'epsilon', obj.epsilon, ... 31 | 'moments', obj.moments, ... 32 | obj.opts{:}) ; 33 | obj.moments = [] ; 34 | % multiply the moments update by the number of images in the batch 35 | % this is required to make the update additive for subbatches 36 | % and will eventually be normalized away 37 | derParams{3} = derParams{3} * size(inputs{1},4) ; 38 | end 39 | 40 | % --------------------------------------------------------------------- 41 | function obj = BatchNorm(varargin) 42 | obj.load(varargin{:}) ; 43 | end 44 | 45 | function params = initParams(obj) 46 | params{1} = ones(obj.numChannels,1,'single') ; 47 | params{2} = zeros(obj.numChannels,1,'single') ; 48 | params{3} = zeros(obj.numChannels,2,'single') ; 49 | end 50 | 51 | function attach(obj, net, index) 52 | attach@dagnn.ElementWise(obj, net, index) ; 53 | p = net.getParamIndex(net.layers(index).params{3}) ; 54 | net.params(p).trainMethod = 'average' ; 55 | net.params(p).learningRate = 0.1 ; 56 | end 57 | end 58 | end 59 | -------------------------------------------------------------------------------- /matlab/src/bits/datamex.hpp: -------------------------------------------------------------------------------- 1 | // @file datamex.hpp 2 | // @brief Basic data structures (MEX support) 3 | // @author Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2015-16 Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__datamex__ 14 | #define __vl__datamex__ 15 | 16 | #include "mex.h" 17 | 18 | #if ENABLE_GPU 19 | #include "gpu/mxGPUArray.h" 20 | #endif 21 | 22 | #include "data.hpp" 23 | 24 | namespace vl { 25 | 26 | class MexTensor ; 27 | 28 | class MexContext : public Context 29 | { 30 | public: 31 | MexContext() ; 32 | ~MexContext() ; 33 | 34 | protected: 35 | #if ENABLE_GPU 36 | vl::ErrorCode initGpu() ; 37 | vl::ErrorCode validateGpu() ; 38 | mxArray * canary ; // if it breathes, the GPU state is valid 39 | bool gpuIsInitialized ; 40 | #endif 41 | 42 | friend class MexTensor ; 43 | } ; 44 | 45 | class MexTensor : public Tensor 46 | { 47 | public: 48 | MexTensor(MexContext & context) ; 49 | vl::ErrorCode init(mxArray const * array) ; 50 | vl::ErrorCode init(DeviceType deviceType, DataType dataType, TensorShape const & shape) ; 51 | vl::ErrorCode initWithZeros(DeviceType deviceType, DataType dataType, TensorShape const & shape) ; 52 | vl::ErrorCode initWithValue(DeviceType deviceType, DataType dataType, TensorShape const & shape, double value) ; 53 | 54 | void makePersistent() ; 55 | mxArray * relinquish() ; 56 | void clear() ; 57 | ~MexTensor() ; 58 | 59 | size_t getMemorySize() const ; 60 | 61 | protected: 62 | MexContext & context ; 63 | mxArray const * array ; 64 | #ifdef ENABLE_GPU 65 | mxGPUArray const * gpuArray ; 66 | #endif 67 | bool isArrayOwner ; 68 | 69 | private: // prevention 70 | MexTensor(MexTensor const &) ; 71 | MexTensor & operator= (MexTensor & tensor) ; 72 | vl::ErrorCode initHelper(DeviceType deviceType, DataType dataType, TensorShape const & shape, bool fillWithZeros = false) ; 73 | } ; 74 | 75 | void print(char const * str, MexTensor const & tensor) ; 76 | 77 | void mexThrowError(Context const& context, vl::ErrorCode error) ; 78 | } 79 | 80 | 81 | #endif /* defined(__vl__datamex__) */ 82 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/nnbnorm_cudnn.hpp: -------------------------------------------------------------------------------- 1 | // @file nnbnorm_cudnn.hpp 2 | // @brief bnorm CuDNN-based implementation. 3 | // @author Ankush Gupta, Andrea Vedaldi 4 | 5 | /* 6 | Copyright (C) 2016 Ankush Gupta and Andrea Vedaldi. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__bnorm_cudnn__ 14 | #define __vl__bnorm_cudnn__ 15 | 16 | #include "../data.hpp" 17 | #include "cudnn.h" 18 | 19 | namespace vl { namespace impl { 20 | 21 | template 22 | struct nnbnorm_cudnn 23 | { 24 | static vl::ErrorCode 25 | forward(vl::Context& context, 26 | vl::Tensor output, 27 | vl::Tensor moments, 28 | vl::Tensor data, 29 | vl::Tensor multipliers, 30 | vl::Tensor biases, 31 | double epsilon) ; 32 | 33 | static vl::ErrorCode 34 | forward_given_moments(vl::Context& context, 35 | vl::Tensor output, 36 | vl::Tensor moments, 37 | vl::Tensor data, 38 | vl::Tensor multipliers, 39 | vl::Tensor biases) ; 40 | 41 | static vl::ErrorCode 42 | backward(Context& context, 43 | vl::Tensor derData, 44 | vl::Tensor derMultipliers, 45 | vl::Tensor derBiases, 46 | vl::Tensor moments, 47 | vl::Tensor data, 48 | vl::Tensor multipliers, 49 | vl::Tensor biases, 50 | vl::Tensor derOutput, 51 | double epsilon) ; 52 | 53 | static vl::ErrorCode 54 | backward_given_moments(Context& context, 55 | vl::Tensor derData, 56 | vl::Tensor derMultipliers, 57 | vl::Tensor derBiases, 58 | vl::Tensor moments, 59 | vl::Tensor data, 60 | vl::Tensor multipliers, 61 | vl::Tensor biases, 62 | vl::Tensor derOutput, 63 | double epsilon) ; 64 | } ; 65 | 66 | } } 67 | 68 | #endif /* defined(__vl__nnbnorm_cudnn__) */ 69 | -------------------------------------------------------------------------------- /matlab/+dagnn/@DagNN/loadobj.m: -------------------------------------------------------------------------------- 1 | function obj = loadobj(s) 2 | % LOADOBJ Initialize a DagNN object from a structure. 3 | % OBJ = LOADOBJ(S) initializes a DagNN objet from the structure 4 | % S. It is the opposite of S = OBJ.SAVEOBJ(). 5 | % If S is a string, initializes the DagNN object with data 6 | % from a mat-file S. Otherwise, if S is an instance of `dagnn.DagNN`, 7 | % returns S. 8 | 9 | % Copyright (C) 2015 Karel Lenc and Andrea Vedaldi. 10 | % All rights reserved. 11 | % 12 | % This file is part of the VLFeat library and is made available under 13 | % the terms of the BSD license (see the COPYING file). 14 | 15 | if ischar(s) s = load(s); end 16 | if isstruct(s) 17 | assert(isfield(s, 'layers'), 'Invalid model.'); 18 | if ~isstruct(s.layers) 19 | warning('The model appears to be `simplenn` model. Using `fromSimpleNN` instead.'); 20 | obj = dagnn.DagNN.fromSimpleNN(s); 21 | return; 22 | end 23 | obj = dagnn.DagNN() ; 24 | try 25 | for l = 1:numel(s.layers) 26 | constr = str2func(s.layers(l).type) ; 27 | block = constr() ; 28 | block.load(struct(s.layers(l).block)) ; 29 | obj.addLayer(... 30 | s.layers(l).name, ... 31 | block, ... 32 | s.layers(l).inputs, ... 33 | s.layers(l).outputs, ... 34 | s.layers(l).params,... 35 | 'skipRebuild', true) ; 36 | end 37 | catch e % Make sure the DagNN object is in valid state 38 | obj.rebuild(); 39 | rethrow(e); 40 | end 41 | obj.rebuild(); 42 | if isfield(s, 'params') 43 | for f = setdiff(fieldnames(s.params)','name') 44 | f = char(f) ; 45 | for i = 1:numel(s.params) 46 | p = obj.getParamIndex(s.params(i).name) ; 47 | obj.params(p).(f) = s.params(i).(f) ; 48 | end 49 | end 50 | end 51 | if isfield(s, 'vars') 52 | for f = setdiff(fieldnames(s.vars)','name') 53 | f = char(f) ; 54 | for i = 1:numel(s.vars) 55 | p = obj.getVarIndex(s.vars(i).name) ; 56 | obj.vars(p).(f) = s.vars(i).(f) ; 57 | end 58 | end 59 | end 60 | for f = setdiff(fieldnames(s)', {'vars','params','layers'}) 61 | f = char(f) ; 62 | obj.(f) = s.(f) ; 63 | end 64 | elseif isa(s, 'dagnn.DagNN') 65 | obj = s ; 66 | else 67 | error('Unknown data type %s for `loadobj`.', class(s)); 68 | end 69 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/pooling.hpp: -------------------------------------------------------------------------------- 1 | // @file pooling.hpp 2 | // @brief Pooling block implementation 3 | // @author Andrea Vedaldi 4 | // @author Karel Lenc 5 | 6 | /* 7 | Copyright (C) 2014-16 Andrea Vedaldi and Karel Lenc. 8 | All rights reserved. 9 | 10 | This file is part of the VLFeat library and is made available under 11 | the terms of the BSD license (see the COPYING file). 12 | */ 13 | 14 | #ifndef VL_POOLING_H 15 | #define VL_POOLING_H 16 | 17 | #include "../data.hpp" 18 | #include 19 | 20 | namespace vl { namespace impl { 21 | 22 | template 23 | struct pooling_max { 24 | typedef type data_type ; 25 | 26 | static vl::ErrorCode 27 | forward(data_type* output, 28 | data_type const* data, 29 | size_t height, size_t width, size_t depth, 30 | size_t poolHeight, size_t poolWidth, 31 | size_t strideY, size_t strideX, 32 | size_t padTop, size_t padBottom, size_t padLeft, size_t padRight) ; 33 | 34 | static vl::ErrorCode 35 | backward(data_type* derData, 36 | data_type const* data, 37 | data_type const* derOutput, 38 | size_t height, size_t width, size_t depth, 39 | size_t poolHeight, size_t poolWidth, 40 | size_t strideY, size_t strideX, 41 | size_t padTop, size_t padBottom, size_t padLeft, size_t padRight) ; 42 | } ; 43 | 44 | template 45 | struct pooling_average { 46 | typedef type data_type ; 47 | 48 | static vl::ErrorCode 49 | forward(data_type* output, 50 | data_type const* data, 51 | size_t height, size_t width, size_t depth, 52 | size_t poolHeight, size_t poolWidth, 53 | size_t strideY, size_t strideX, 54 | size_t padTop, size_t padBottom, size_t padLeft, size_t padRight) ; 55 | 56 | static vl::ErrorCode 57 | backward(type* derData, 58 | type const* derOutput, 59 | size_t height, size_t width, size_t depth, 60 | size_t poolHeight, size_t poolWidth, 61 | size_t strideY, size_t strideX, 62 | size_t padTop, size_t padBottom, size_t padLeft, size_t padRight) ; 63 | } ; 64 | 65 | } } 66 | 67 | #endif /* defined(VL_POOLING_H) */ 68 | -------------------------------------------------------------------------------- /code/train/train_res_iden_baseline.m: -------------------------------------------------------------------------------- 1 | 2 | function train_res_iden_baseline(num_predefined_classes, dropoutrate, varargin) 3 | % ------------------------------------------------------------------------- 4 | % Part 4.1: prepare the data 5 | % ------------------------------------------------------------------------- 6 | 7 | % Load character dataset 8 | imdb = load(strcat('./data/url_data.mat')); 9 | imdb = imdb.imdb; 10 | 11 | imdb.images.set(:) = 1; 12 | 13 | % ------------------------------------------------------------------------- 14 | % Part 4.2: initialize a CNN architecture 15 | % ------------------------------------------------------------------------- 16 | net = resnet52_iden_baseline(num_predefined_classes, dropoutrate); 17 | net.conserveMemory = true; 18 | net.meta.normalization.averageImage = reshape([105.6615,99.1308,97.9115],1,1,3); 19 | % ------------------------------------------------------------------------- 20 | % Part 4.3: train and evaluate the CNN 21 | % ------------------------------------------------------------------------- 22 | opts.train.averageImage = net.meta.normalization.averageImage; 23 | opts.train.batchSize = 64; 24 | opts.train.continue = false; 25 | opts.train.gpus = 1; 26 | opts.train.prefetch = false ; 27 | opts.train.nesterovUpdate = true ; 28 | opts.train.expDir = './result/res52_iden_baseline'; 29 | opts.train.derOutputs = {'objective', 1} ; 30 | %opts.train.gamma = 0.9; 31 | opts.train.momentum = 0.9; 32 | %opts.train.constraint = 100; 33 | opts.train.learningRate = [0.1*ones(1,40),0.01*ones(1,10)] ; 34 | opts.train.weightDecay = 0.0001; 35 | opts.train.numEpochs = numel(opts.train.learningRate) ; 36 | [opts, ~] = vl_argparse(opts.train, varargin) ; 37 | % Call training function in MatConvNet 38 | [net,info] = cnn_train_dag(net, imdb, @getBatch,opts) ; 39 | 40 | % -------------------------------------------------------------------- 41 | function inputs = getBatch(imdb,batch,opts) 42 | % -------------------------------------------------------------------- 43 | im_url = imdb.images.data(batch) ; 44 | im = vl_imreadjpeg(im_url,'Pack','Resize',[224,224],'Flip',... 45 | 'CropLocation','random','CropSize',[0.85,1],... 46 | 'Interpolation', 'bicubic','NumThreads',8); 47 | labels = imdb.images.label(batch); 48 | oim = bsxfun(@minus,im{1},opts.averageImage); 49 | inputs = {'data',gpuArray(oim),'label',labels}; -------------------------------------------------------------------------------- /matlab/+dagnn/ReLU.m: -------------------------------------------------------------------------------- 1 | classdef ReLU < dagnn.ElementWise 2 | properties 3 | useShortCircuit = true 4 | leak = 0 5 | opts = {} 6 | end 7 | 8 | methods 9 | function outputs = forward(obj, inputs, params) 10 | outputs{1} = vl_nnrelu(inputs{1}, [], ... 11 | 'leak', obj.leak, obj.opts{:}) ; 12 | end 13 | 14 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 15 | derInputs{1} = vl_nnrelu(inputs{1}, derOutputs{1}, ... 16 | 'leak', obj.leak, ... 17 | obj.opts{:}) ; 18 | derParams = {} ; 19 | end 20 | 21 | function forwardAdvanced(obj, layer) 22 | if ~obj.useShortCircuit || ~obj.net.conserveMemory 23 | forwardAdvanced@dagnn.Layer(obj, layer) ; 24 | return ; 25 | end 26 | net = obj.net ; 27 | in = layer.inputIndexes ; 28 | out = layer.outputIndexes ; 29 | net.vars(out).value = vl_nnrelu(net.vars(in).value, [], ... 30 | 'leak', obj.leak, ... 31 | obj.opts{:}) ; 32 | net.numPendingVarRefs(in) = net.numPendingVarRefs(in) - 1; 33 | if ~net.vars(in).precious & net.numPendingVarRefs(in) == 0 34 | net.vars(in).value = [] ; 35 | end 36 | end 37 | 38 | function backwardAdvanced(obj, layer) 39 | if ~obj.useShortCircuit || ~obj.net.conserveMemory 40 | backwardAdvanced@dagnn.Layer(obj, layer) ; 41 | return ; 42 | end 43 | net = obj.net ; 44 | in = layer.inputIndexes ; 45 | out = layer.outputIndexes ; 46 | 47 | if isempty(net.vars(out).der), return ; end 48 | 49 | derInput = vl_nnrelu(net.vars(out).value, net.vars(out).der, ... 50 | 'leak', obj.leak, obj.opts{:}) ; 51 | 52 | if ~net.vars(out).precious 53 | net.vars(out).der = [] ; 54 | net.vars(out).value = [] ; 55 | end 56 | 57 | if net.numPendingVarRefs(in) == 0 58 | net.vars(in).der = derInput ; 59 | else 60 | net.vars(in).der = net.vars(in).der + derInput ; 61 | end 62 | net.numPendingVarRefs(in) = net.numPendingVarRefs(in) + 1 ; 63 | end 64 | 65 | function obj = ReLU(varargin) 66 | obj.load(varargin) ; 67 | end 68 | end 69 | end 70 | -------------------------------------------------------------------------------- /matlab/src/bits/impl/roipooling.hpp: -------------------------------------------------------------------------------- 1 | // @file roipooling.hpp 2 | // @brief roipooling block implementation 3 | // @author Hakan Bilen 4 | // @author Abishek Dutta 5 | // @author Andrea Vedaldi 6 | 7 | /* 8 | Copyright (C) 2016 Hakan Bilen, Abishek Dutta, and Andrea Vedaldi. 9 | All rights reserved. 10 | 11 | This file is part of the VLFeat library and is made available under 12 | the terms of the BSD license (see the COPYING file). 13 | */ 14 | 15 | #ifndef VL_ROIPOOLING_H 16 | #define VL_ROIPOOLING_H 17 | 18 | #include "../data.hpp" 19 | #include 20 | 21 | namespace vl { namespace impl { 22 | 23 | template 24 | struct roipooling_max { 25 | typedef type data_type ; 26 | 27 | static vl::ErrorCode 28 | forward(type* output, 29 | type const* data, 30 | size_t height, size_t width, size_t depth, size_t size, 31 | type const* rois, 32 | size_t numROIs, 33 | int const subdivisions[2], 34 | double const transform[6]) ; 35 | 36 | static vl::ErrorCode 37 | backward(type* derData, 38 | type const* data, 39 | size_t height, size_t width, size_t depth, size_t size, 40 | type const* rois, 41 | size_t numROIs, 42 | type const* derOutput, 43 | int const subdivisions[2], 44 | double const transform[6]) ; 45 | }; 46 | 47 | template 48 | struct roipooling_average { 49 | typedef type data_type ; 50 | 51 | static vl::ErrorCode 52 | forward(type* output, 53 | type const* data, 54 | size_t height, size_t width, size_t depth, size_t size, 55 | type const* rois, 56 | size_t numROIs, 57 | int const subdivisions[2], 58 | double const transform[6]) ; 59 | 60 | static vl::ErrorCode 61 | backward(type* derData, 62 | type const* data, // <- todo: this is not needed for avg pooling 63 | size_t height, size_t width, size_t depth, size_t size, 64 | type const* rois, 65 | size_t numROIs, 66 | type const* derOutput, 67 | int const subdivisions[2], 68 | double const transform[6]) ; 69 | }; 70 | } } 71 | #endif /* defined(VL_ROIPOOLING_H) */ 72 | -------------------------------------------------------------------------------- /matlab/src/bits/nnconv.hpp: -------------------------------------------------------------------------------- 1 | // @file nnconv.cu 2 | // @brief Convolution block 3 | // @author Andrea Vedaldi 4 | // @author Max Jaderberg 5 | 6 | /* 7 | Copyright (C) 2014 Andrea Vedaldi and Max Jaderberg 8 | Copyright (C) 2015-16 Andrea Vedaldi. 9 | 10 | All rights reserved. 11 | 12 | This file is part of the VLFeat library and is made available under 13 | the terms of the BSD license (see the COPYING file). 14 | */ 15 | 16 | #ifndef __vl__nnconv__ 17 | #define __vl__nnconv__ 18 | 19 | #include "data.hpp" 20 | 21 | namespace vl { 22 | 23 | vl::ErrorCode 24 | nnconv_forward(vl::Context& context, 25 | vl::Tensor output, double outputMult, 26 | vl::Tensor data, double dataMult, 27 | vl::Tensor filters, 28 | vl::Tensor biases, 29 | int strideY, int strideX, 30 | int padTop, int padBottom, 31 | int padLeft, int padRight, 32 | int dilateY, int dilateX) ; 33 | 34 | vl::ErrorCode 35 | nnconv_backward(vl::Context& context, 36 | vl::Tensor derData, 37 | vl::Tensor derFilters, 38 | vl::Tensor derBiases, 39 | vl::Tensor data, 40 | vl::Tensor filters, 41 | vl::Tensor derOutput, 42 | int strideY, int strideX, 43 | int padTop, int padBottom, 44 | int padLeft, int padRight, 45 | int dilateY, int dilateX) ; 46 | 47 | vl::ErrorCode 48 | nnconvt_forward(vl::Context& context, 49 | vl::Tensor output, 50 | vl::Tensor data, 51 | vl::Tensor filters, 52 | vl::Tensor biases, 53 | int upsampleY, int upsampleX, 54 | int cropTop, int cropBottom, 55 | int cropLeft, int cropRight) ; 56 | 57 | vl::ErrorCode 58 | nnconvt_backward(vl::Context& context, 59 | vl::Tensor derData, 60 | vl::Tensor derFilters, 61 | vl::Tensor derBiases, 62 | vl::Tensor data, 63 | vl::Tensor filters, 64 | vl::Tensor derOutput, 65 | int upsampleY, int upsampleX, 66 | int cropTop, int cropBottom, 67 | int cropLeft, int cropRight) ; 68 | } 69 | 70 | 71 | #endif /* defined(__vl__nnconv__) */ 72 | -------------------------------------------------------------------------------- /matlab/src/bits/nnbnorm.hpp: -------------------------------------------------------------------------------- 1 | // @file nnbnorm.hpp 2 | // @brief Batch normalizatoion block 3 | // @author Sebastien Ehrhardt 4 | // @author Andrea Vedaldi 5 | 6 | /* 7 | Copyright (C) 2015-16 Sebastien Ehrhardt and Andrea Vedaldi. 8 | All rights reserved. 9 | 10 | This file is part of the VLFeat library and is made available under 11 | the terms of the BSD license (see the COPYING file). 12 | */ 13 | 14 | #ifndef __vl__nnbnorm__ 15 | #define __vl__nnbnorm__ 16 | 17 | #include "data.hpp" 18 | #include 19 | 20 | namespace vl { 21 | 22 | // This version computes mean and sigma 23 | vl::ErrorCode 24 | nnbnorm_forward(vl::Context& context, 25 | vl::Tensor output, 26 | vl::Tensor moments, // [output: can pass null] 27 | vl::Tensor data, 28 | vl::Tensor filters, 29 | vl::Tensor biases, 30 | double epsilon) ; 31 | 32 | // This version uses the mean and sigma specified 33 | vl::ErrorCode 34 | nnbnorm_forward_given_moments(vl::Context& context, 35 | vl::Tensor output, 36 | vl::Tensor moments, // input 37 | vl::Tensor data, 38 | vl::Tensor filters, 39 | vl::Tensor biases) ; 40 | 41 | vl::ErrorCode 42 | nnbnorm_backward(vl::Context& context, 43 | vl::Tensor derData, 44 | vl::Tensor derFilters, 45 | vl::Tensor derBiaises, 46 | vl::Tensor moments, 47 | vl::Tensor data, 48 | vl::Tensor filters, 49 | vl::Tensor biases, 50 | vl::Tensor derOutput, 51 | double epsilon) ; 52 | 53 | vl::ErrorCode 54 | nnbnorm_backward_given_moments(vl::Context& context, 55 | vl::Tensor derData, 56 | vl::Tensor derFilters, 57 | vl::Tensor derBiaises, 58 | vl::Tensor moments, 59 | vl::Tensor data, 60 | vl::Tensor filters, 61 | vl::Tensor biases, 62 | vl::Tensor derOutput, 63 | double epsilon) ; 64 | } 65 | 66 | #endif /* defined(__vl__nnbnorm__) */ 67 | -------------------------------------------------------------------------------- /matlab/vl_nnbnorm.m: -------------------------------------------------------------------------------- 1 | %VL_NNBNORM CNN batch normalisation. 2 | % Y = VL_NNBNORM(X,G,B) applies batch normalization to the input 3 | % X. Batch normalization is defined as: 4 | % 5 | % Y(i,j,k,t) = G(k) * (X(i,j,k,t) - mu(k)) / sigma(k) + B(k) 6 | % 7 | % where: 8 | % 9 | % mu(k) = mean_ijt X(i,j,k,t), 10 | % sigma2(k) = mean_ijt (X(i,j,k,t) - mu(k))^2, 11 | % sigma(k) = sqrt(sigma2(k) + EPSILON) 12 | % 13 | % are respectively the per-channel mean, variance, and standard 14 | % deviation of each feature channel in the data X. The parameters 15 | % G(k) and B(k) are multiplicative and additive constants use to 16 | % scale each data channel. 17 | % 18 | % Means and variances are accumulated across all the data items 19 | % (images) stored in the 4D tensor X (from which the name batch 20 | % normalization is derived). The constant EPSILON is used to 21 | % regularize the computation of sigma(k) and to avoid division by 22 | % zero. 23 | % 24 | % [DZDX,DZDG,DZDB] = VL_NNBNORM(X,G,B,DZDY) computes the derviatives 25 | % of the block projected onto DZDY. DZDX, DZDG, DZDB and DZDY have 26 | % the same dimensions as X, G, B, and Y respectivey. 27 | % 28 | % Optionally, [Y,MOMENTS] = VL_NNBNORM(...) and 29 | % [DZDX,DZDG,DZDB,MOMENTS] = VL_NNBNORM(...,DZDY) return the values 30 | % of the vectors mu and sigma in the formulas above. Here, MOMENTS 31 | % is a DEPTH x 2 array [MU, SIGMA]. 32 | % 33 | % VL_NNBNROM(..., 'Option', value) takes the following options: 34 | % 35 | % `Epsilon`:: 1e-4 36 | % Specifies the constant EPSILON in the formuals above. 37 | % 38 | % `Moments`:: unspecified 39 | % Specifies an array MOMENTS with the values of mu and sigma to 40 | % use instead of computing them according to the equations 41 | % above. This is useful to disable batch normalization during 42 | % testing. 43 | % 44 | % `CuDNN`:: specified 45 | % If specified, turns on CuDNN. CuDNN is on by default. This 46 | % option can be useful to undo the effect of a previous 47 | % `NoCuDNN` option in the argument list. 48 | % 49 | % `NoCuDNN`:: not specified 50 | % If specified, turns off CuDNN. 51 | % 52 | % See also: VL_NNNORMALIZE(). 53 | 54 | % Copyright (C) 2015 Sébastien Ehrhardt, Karel Lenc and Andrea Vedaldi. 55 | % All rights reserved. 56 | % 57 | % This file is part of the VLFeat library and is made available under 58 | % the terms of the BSD license (see the COPYING file). 59 | -------------------------------------------------------------------------------- /matlab/+dagnn/Pseudo_Loss_Multi_Static.m: -------------------------------------------------------------------------------- 1 | classdef Pseudo_Loss_Multi_Static < dagnn.Loss 2 | 3 | properties 4 | rate = 0; 5 | valid_pseudo_num = 0; 6 | end 7 | 8 | methods 9 | function outputs = forward(obj, inputs, params) 10 | x = inputs{1}; 11 | c = inputs{2}; 12 | c = reshape(c,1,1,1,size(c,2)); 13 | multipse_label = inputs{3}; 14 | 15 | obj.valid_pseudo_num = length(find(multipse_label~=0))/length(find(c==0)); 16 | 17 | ci = label_to_index(x, c); 18 | 19 | [Xmax,index] = max(x,[],3) ; 20 | ex = exp(bsxfun(@minus, x, Xmax)) ; 21 | t1 = Xmax + log(sum(ex,3)) - x(ci) ; 22 | K = size(x,3); 23 | % label smooth for all 24 | part1 = t1; % softmax 25 | part2 = (bsxfun(@times,sum(multipse_label,3),log(sum(ex,3))) - ... 26 | sum(bsxfun(@times,multipse_label,bsxfun(@minus,x,Xmax)),3)) * (2*K) / ((2*K-obj.valid_pseudo_num+1)*obj.valid_pseudo_num); % label smooth 27 | 28 | t = (1-obj.rate) * part1 + obj.rate *part2; 29 | gan = find(c==0); 30 | t(gan) = part2(gan); % remove the softmax part for generated image. 31 | outputs{1} = sum(t(:)); 32 | 33 | n = obj.numAveraged ; 34 | m = n + size(inputs{1},4) ; 35 | obj.average = (n * obj.average + gather(outputs{1})) / m ; 36 | obj.numAveraged = m ; 37 | end 38 | 39 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 40 | x = inputs{1}; 41 | c = inputs{2}; 42 | c = reshape(c,1,1,1,size(c,2)); 43 | multipse_label = inputs{3}; 44 | 45 | ci = label_to_index(x, c); 46 | 47 | K = size(x,3); 48 | Xmax = max(x,[],3) ; 49 | ex = exp(bsxfun(@minus, x, Xmax)) ; 50 | part1 = bsxfun(@rdivide, ex, sum(ex,3)); % Y = E/L (softmax result) 51 | real = find(c~=0); 52 | part1(ci(real)) = part1(ci(real)) - 1 ; % for evey gt, d(gt) = Y-1 53 | part2 = bsxfun(@minus,bsxfun(@rdivide, ex, sum(ex,3)), multipse_label * (2*K) / ((2*K-obj.valid_pseudo_num+1)*obj.valid_pseudo_num)); 54 | y = (1-obj.rate) * part1 + obj.rate *part2; % When we set opt.rate=0, it equals to softmax loss 55 | gan = find(c==0); 56 | y(:,:,:,gan) = part2(:,:,:,gan); % We set opts.gan=1 57 | derInputs{1} = y; 58 | derInputs{2} = []; 59 | derInputs{3} = []; 60 | derParams = {}; 61 | end 62 | 63 | function obj = Loss(varargin) 64 | obj.load(varargin) ; 65 | end 66 | 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /matlab/xtest/vl_testnn.m: -------------------------------------------------------------------------------- 1 | function vl_testnn(varargin) 2 | %VL_TESTNN Run MatConvNet test suite 3 | % VL_TESTNN('option', value, ...) takes the following options: 4 | % `cpu`:: true 5 | % Run the CPU tests. 6 | % 7 | % `gpu`:: false 8 | % Run the GPU tests. 9 | % 10 | % `single`:: true 11 | % Perform tests in single precision. 12 | % 13 | % `double`:: false 14 | % Perform tests in double precision. 15 | % 16 | % `command`:: `'nn'` 17 | % Run only tests which name starts with the specified substring. 18 | % E.g. `vl_testnn('command', 'nnloss') would run only the nnloss tests. 19 | % 20 | % `break`:: false 21 | % Stop tests in case of error. 22 | % 23 | % `tapFile`:: '' 24 | % Output the test results to a file. If a specified file does 25 | % exist it is overwritten. 26 | % 27 | % This function uses the Matlab unit testing framework which was 28 | % introduced in Matlab R2013a (v8.1). 29 | 30 | % Copyright (C) 2015-16 Andrea Vedaldi, Karel Lenc. 31 | % All rights reserved. 32 | % 33 | % This file is part of the VLFeat library and is made available under 34 | % the terms of the BSD license (see the COPYING file). 35 | 36 | opts.cpu = true ; 37 | opts.gpu = false ; 38 | opts.single = true ; 39 | opts.double = false ; 40 | opts.command = 'nn' ; 41 | opts.break = false ; 42 | opts.tapFile = ''; 43 | opts = vl_argparse(opts, varargin) ; 44 | 45 | import matlab.unittest.constraints.* ; 46 | import matlab.unittest.selectors.* ; 47 | import matlab.unittest.plugins.TAPPlugin; 48 | import matlab.unittest.plugins.ToFile; 49 | 50 | % Choose which tests to run 51 | sel = HasName(StartsWithSubstring(opts.command)) ; 52 | if ~opts.gpu 53 | sel = sel & ~HasName(ContainsSubstring('device=gpu')) ; 54 | end 55 | if ~opts.cpu 56 | sel = sel & ~HasName(ContainsSubstring('device=cpu')) ; 57 | end 58 | if ~opts.double 59 | sel = sel & ~HasName(ContainsSubstring('dataType=double')) ; 60 | end 61 | if ~opts.single 62 | sel = sel & ~HasName(ContainsSubstring('dataType=single')) ; 63 | end 64 | 65 | % Run tests 66 | root = fileparts(mfilename('fullpath')) ; 67 | suite = matlab.unittest.TestSuite.fromFolder(fullfile(root, 'suite'), sel) ; 68 | runner = matlab.unittest.TestRunner.withTextOutput('Verbosity',3); 69 | if opts.break 70 | runner.addPlugin(matlab.unittest.plugins.StopOnFailuresPlugin) ; 71 | end 72 | if ~isempty(opts.tapFile) 73 | if exist(opts.tapFile, 'file') 74 | delete(opts.tapFile); 75 | end 76 | runner.addPlugin(TAPPlugin.producingOriginalFormat(ToFile(opts.tapFile))); 77 | end 78 | result = runner.run(suite); 79 | display(result) 80 | -------------------------------------------------------------------------------- /matlab/vl_nnbilinearsampler.m: -------------------------------------------------------------------------------- 1 | %VL_NNBILIEARSAMPLER CNN spatial bilinear resampling 2 | % Y = VL_NNBILINEARSAMPLER(X,GRID) resamples image X at the spatial 3 | % locations specified by GRID using bilinear interpolation. 4 | % 5 | % X is a array of dimension H x W x C x N, where (H,W) are the 6 | % height and width of the image, C is the number of feature 7 | % channels, and N is the number of images in the batch. 8 | % 9 | % GRID is an array of dimension 2 x Ho x Wo x No, where (Ho,Wo) are 10 | % the height and width of the output image and No the number of 11 | % output images in the output batch Y. The output array Y has 12 | % dimensions Ho x Wo x C x No. The same resampling grid is used for 13 | % all input feature channels, but each output image in the batchY 14 | % uses its own grid. 15 | % 16 | % For output image n, GRID(1,:,:,n) specifies the vertical location 17 | % v of a sample in the input image X and GRID(2,:,:,n) the 18 | % horizontal location u. The convention follows standard 19 | % impelemntations of this operator in the literature. Namely: 20 | % 21 | % 1. The grid coordinates are normalized in the range [-1,1]. This 22 | % means that (-1,-1) is the center of the upper-left pixel in the 23 | % input image and (+1,+1) the center of the bottom-right pixel. 24 | % 25 | % 2. The V,U coordiante planes are stacked in the fisrt dimension of 26 | % GRID instead of in the third, as it would be more natural in 27 | % MatConvNet (as these could be interpreted as 'channels' in 28 | % GRID). 29 | % 30 | % Furthre, No can be a multiple of N; in this case, it is assumed 31 | % that there are No/N transforms per input image, hence, the 32 | % transforms [1 ... No/N] are applied to the first image, [No/N+1 33 | % ... 2*No/N] are applied to the second image, etc. 34 | % 35 | % [DX, DGRID] = VL_NNBILINEARSAMPLER(X, GRID, DY) computes the 36 | % derivatives of the block projected onto DY. DX, DGRID, DY have the 37 | % same dimensions as X, GRID and Y, respectively. 38 | % 39 | % ## CUDNN SUPPORT 40 | % 41 | % If compiled in, the function will use cuDNN's 42 | % implementation. Note, cuDNN v5 or higher is required. 43 | % You can use the 'NoCudnn' option to disable 44 | % cuDNN or 'CuDNN' to activate it back again (the 45 | % choice sticks until MATLAB purges the MEX files for any reason). 46 | 47 | % Copyright (C) 2016 Ankush Gupta and Andrea Vedaldi. 48 | % All rights reserved. 49 | % 50 | % This file is part of the VLFeat library and is made available under 51 | % the terms of the BSD license (see the COPYING file). 52 | -------------------------------------------------------------------------------- /test/test_gallery_query_crazy.m: -------------------------------------------------------------------------------- 1 | % In this file, we densely extract the feature 2 | % We extract feature from 256,256 image and mirrored image. 3 | % -----Please change the file path in this script. ---- 4 | clear; 5 | path = 'train_2stream_baseline'; 6 | netStruct = load(strcat('./result/',path,'/net-epoch-60.mat')); 7 | if ~exist(strcat('./test/',path)) 8 | mkdir(strcat('./test/',path)); 9 | end 10 | dataset_name = 'Market-1501-v15.09.15'; 11 | %--------add L2-norm 12 | net = dagnn.DagNN.loadobj(netStruct.net); 13 | net.addLayer('lrn_test',dagnn.LRN('param',[4096,0,1,0.5]),{'pool5'},{'pool5n'},{}); 14 | clear netStruct; 15 | net.mode = 'test'; 16 | net.move('gpu') ; 17 | net.conserveMemory = true; 18 | im_mean = net.meta(1).normalization.averageImage; 19 | %im_mean = imresize(im_mean,[224,224]); 20 | p = dir(strcat('/home/yan/datasets/',dataset_name,'/bounding_box_test/*.jpg')); 21 | ff = []; 22 | %------------------------------ 23 | for i = 1:200:numel(p) 24 | disp(i); 25 | oim = []; 26 | str=[]; 27 | for j=1:min(200,numel(p)-i+1) 28 | str = strcat('/home/yan/datasets/',dataset_name,'/bounding_box_test/',p(i+j-1).name); 29 | imt = imresize(imread(str),[256,256]); 30 | oim = cat(4,oim,imt); 31 | end 32 | f = getFeature2(net,oim,im_mean,'data','pool5n'); 33 | f = sum(sum(f,1),2); 34 | f2 = getFeature2(net,fliplr(oim),im_mean,'data','pool5n'); 35 | f2 = sum(sum(f2,1),2); 36 | f = f+f2; 37 | size4 = size(f,4); 38 | f = reshape(f,[],size4)'; 39 | s = sqrt(sum(f.^2,2)); 40 | dim = size(f,2); 41 | s = repmat(s,1,dim); 42 | f = f./s; 43 | ff = cat(1,ff,f); 44 | end 45 | save(strcat('./test/',path,'/resnet_gallery.mat'),'ff','-v7.3'); 46 | %} 47 | 48 | %---------query 49 | p = dir(strcat('/home/yan/datasets/',dataset_name,'/query/*.jpg')); 50 | ff = []; 51 | for i = 1:200:numel(p) 52 | disp(i); 53 | oim = []; 54 | str=[]; 55 | for j=1:min(200,numel(p)-i+1) 56 | str = strcat('/home/yan/datasets/',dataset_name,'/query/',p(i+j-1).name); 57 | imt = imresize(imread(str),[256,256]); 58 | oim = cat(4,oim,imt); 59 | end 60 | f = getFeature2(net,oim,im_mean,'data','pool5n'); 61 | f = sum(sum(f,1),2); 62 | f2 = getFeature2(net,fliplr(oim),im_mean,'data','pool5n'); 63 | f2 = sum(sum(f2,1),2); 64 | f = f+f2; 65 | size4 = size(f,4); 66 | f = reshape(f,[],size4)'; 67 | s = sqrt(sum(f.^2,2)); 68 | dim = size(f,2); 69 | s = repmat(s,1,dim); 70 | f = f./s; 71 | ff = cat(1,ff,f); 72 | end 73 | save(strcat('./test/',path,'/resnet_query.mat'),'ff','-v7.3'); -------------------------------------------------------------------------------- /matlab/src/bits/impl/bnorm.hpp: -------------------------------------------------------------------------------- 1 | // @file bnorm.hpp 2 | // @brief Batch Normalization block implementation 3 | // @author Sebastien Ehrhardt 4 | 5 | /* 6 | Copyright (C) 2015-16 Sebastien Ehrhardt. 7 | All rights reserved. 8 | 9 | This file is part of the VLFeat library and is made available under 10 | the terms of the BSD license (see the COPYING file). 11 | */ 12 | 13 | #ifndef __vl__bnorm__ 14 | #define __vl__bnorm__ 15 | 16 | #include "../data.hpp" 17 | #include 18 | 19 | namespace vl { namespace impl { 20 | 21 | template 22 | struct bnorm 23 | { 24 | static vl::ErrorCode 25 | forward(Context& context, 26 | type* output, 27 | type* moments, // can be null and it will be allocated size_ternally 28 | type const* data, 29 | type const* multipliers, 30 | type const* biases, 31 | size_t height, size_t width, size_t depth, size_t size, 32 | type epsilon) ; 33 | 34 | static vl::ErrorCode 35 | forward_given_moments(Context& context, 36 | type* output, 37 | type const* moments, 38 | type const* data, 39 | type const* multipliers, 40 | type const* biases, 41 | size_t height, size_t width, size_t depth, size_t size) ; 42 | 43 | static vl::ErrorCode 44 | backward(Context& context, 45 | type* derData, 46 | type* derMultipliers, 47 | type* derBiases, 48 | type* moments, // can be null and it will be allocated size_ternally 49 | type const* data, 50 | type const* multipliers, 51 | type const* biases, 52 | type const* derOutput, 53 | size_t height, size_t width, size_t depth, size_t size, 54 | type epsilon) ; 55 | 56 | static vl::ErrorCode 57 | backward_given_moments(Context& context, 58 | type* derData, 59 | type* derMultipliers, 60 | type* derBiases, 61 | type const* moments, 62 | type const* data, 63 | type const* multipliers, 64 | type const* biases, 65 | type const* derOutput, 66 | size_t height, size_t width, size_t depth, size_t size, 67 | type epsilon) ; 68 | } ; 69 | 70 | } } 71 | #endif /* __vl__bnorm__ */ 72 | -------------------------------------------------------------------------------- /matlab/vl_nndropout.m: -------------------------------------------------------------------------------- 1 | function [y,mask] = vl_nndropout(x,varargin) 2 | %VL_NNDROPOUT CNN dropout. 3 | % [Y,MASK] = VL_NNDROPOUT(X) applies dropout to the data X. MASK 4 | % is the randomly sampled dropout mask. Both Y and MASK have the 5 | % same size as X. 6 | % 7 | % VL_NNDROPOUT(X, 'rate', R) sets the dropout rate to R. Rate is defined 8 | % as a probability of a variable *not* to be zeroed (i.e. it is the 9 | % expected value of MASK). 10 | % 11 | % [DZDX] = VL_NNDROPOUT(X, DZDY, 'mask', MASK) computes the 12 | % derivatives of the blocks projected onto DZDY. Note that MASK must 13 | % be specified in order to compute the derivative consistently with 14 | % the MASK randomly sampled in the forward pass. DZDX and DZDY have 15 | % the same dimesnions as X and Y respectivey. 16 | % 17 | % Note that in the original paper on dropout, at test time the 18 | % network weights for the dropout layers are scaled down to 19 | % compensate for having all the neurons active. In this 20 | % implementation the dropout function itself already does this 21 | % compensation during training. So at test time no alterations are 22 | % required. 23 | 24 | % Copyright (C) 2014-16 Andrea Vedaldi, Karel Lenc. 25 | % All rights reserved. 26 | % 27 | % This file is part of the VLFeat library and is made available under 28 | % the terms of the BSD license (see the COPYING file). 29 | 30 | opts.rate = 0.5 ; 31 | opts.mask = [] ; 32 | 33 | backMode = numel(varargin) > 0 && ~ischar(varargin{1}) ; 34 | if backMode 35 | dzdy = varargin{1} ; 36 | opts = vl_argparse(opts, varargin(2:end)) ; 37 | else 38 | opts = vl_argparse(opts, varargin) ; 39 | end 40 | 41 | % determine mask 42 | scale = 1 / (1 - opts.rate) ; 43 | if isa(x, 'gpuArray') 44 | dataType = classUnderlying(x) ; 45 | else 46 | dataType = class(x) ; 47 | end 48 | switch dataType 49 | case 'single' 50 | scale = single(scale) ; 51 | case 'double' 52 | scale = double(scale) ; 53 | end 54 | 55 | if backMode && isempty(opts.mask) 56 | warning('vl_nndropout: when using in backward mode, the mask should be specified') ; 57 | end 58 | if isempty(opts.mask) 59 | % product determines data type 60 | if isa(x,'gpuArray') 61 | opts.mask = scale * (gpuArray.rand(size(x), 'single') >= opts.rate) ; 62 | else 63 | opts.mask = scale * (rand(size(x), 'single') >= opts.rate) ; 64 | end 65 | end 66 | 67 | % Apply dropout mask. Note that mask is either `single` or `double` 68 | % and a CPU or GPU array like the input argument `x`. 69 | if ~backMode 70 | y = opts.mask .* x ; 71 | else 72 | y = opts.mask .* dzdy ; 73 | end 74 | mask = opts.mask ; 75 | -------------------------------------------------------------------------------- /matlab/vl_nnroipool.m: -------------------------------------------------------------------------------- 1 | % VL_NNROIPOOL CNN region of interest pooling. 2 | % Y = VL_NNROIPOOL(X, ROIS) pools each feature channel in X in 3 | % the specified regions of interest ROIS. ROIS is a 5 x K array 4 | % containing K regions. Each region has five coordinates `[t, u0, 5 | % v0, u1, v1]` where `u0`, `v0` is the upper-left corner of a ROI, 6 | % `u1`, `v1` is the bottom-right corner, and `t` is the index of the 7 | % image that contains the region. Spatial coordiantes start at (1,1), 8 | % with `u` indexing the horizontal axis and `v` the vertical one. 9 | % The image indeces ranges from 1 to the number of images stored 10 | % in the tensor X. 11 | % 12 | % If X has C feature channels, then the output Y is a 1 x 1 x C x K 13 | % array, with one image instance per region. Arguments can be SINGLE 14 | % or DOUBLE and CPU or GPU arrays; however, they must all be of the 15 | % same type (unless empty). 16 | % 17 | % DZDX = VL_NNROIPOOL(X, ROIS, DZDY) computes the derivative of 18 | % the layer projected on DZDY with respect to X. 19 | % 20 | % VL_NNROIPOOL(___, 'opt', value, ...) accepts the following 21 | % options: 22 | % 23 | % `Method`:: `'max'` 24 | % Choose between `'max'` and `'avg'` (average) pooling. 25 | % 26 | % `Subdivisions`:: `[1 1]` 27 | % Specifies the number [SH,SW] of vertical and horizontal tiles of 28 | % a region. This makes the output a SH x SW x C x K array. 29 | % 30 | % `Transform`:: `1` 31 | % Specifies a spatial transformation to apply to region vertices before 32 | % they are applied to the input tensor. If T is a scalar, then 33 | % the transformation is a scaling centered at the origin: 34 | % 35 | % u' = T (u - 1) + 1, 36 | % v' = T (v - 1) + 1. 37 | % 38 | % If T is a 2D vector, then different scaling factors for the 39 | % `u` and `v` can be specified. Finally, if T is a 2 x 2 matrix, then: 40 | % 41 | % u' = T(1,1) u + T(1,2) v + T(1,3), 42 | % v' = T(2,1) u + T(2,2) v + T(2,3). 43 | % 44 | % Note that only the upper-left and bottom-right corners of each 45 | % rectangular region are transformed. Thus this is mostly useful 46 | % for axis-aligned transformations; the generality of the expression 47 | % allows, however, to swap `u` and `v`, which may be needed 48 | % to match different conventions for the box coordiantes. 49 | % 50 | % See also: VL_NNPOOL(). 51 | 52 | % Copyright (C) 2016 Hakan Bilen, Abishek Dutta, and Andrea Vedaldi. 53 | % All rights reserved. 54 | % 55 | % This file is part of the VLFeat library and is made available under 56 | % the terms of the BSD license (see the COPYING file). 57 | -------------------------------------------------------------------------------- /matlab/+dagnn/BatchNorm.m: -------------------------------------------------------------------------------- 1 | classdef BatchNorm < dagnn.ElementWise 2 | properties 3 | numChannels 4 | use_global = false 5 | global_state = [] 6 | epsilon = 1e-5 7 | opts = {'NoCuDNN'} % ours seems slightly faster 8 | end 9 | 10 | properties (Transient) 11 | moments 12 | end 13 | 14 | methods 15 | function outputs = forward(obj, inputs, params) 16 | if (obj.use_global || strcmp(obj.net.mode, 'test')) 17 | outputs{1} = vl_nnbnorm(inputs{1}, params{1}, params{2}, ... 18 | 'moments', params{3}, ... 19 | 'epsilon', obj.epsilon, ... 20 | obj.opts{:}) ; 21 | else 22 | [outputs{1},obj.moments] = ... 23 | vl_nnbnorm(inputs{1}, params{1}, params{2}, ... 24 | 'epsilon', obj.epsilon, ... 25 | obj.opts{:}) ; 26 | end 27 | end 28 | 29 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 30 | if (obj.use_global) 31 | [derInputs{1}, derParams{1}, derParams{2}, derParams{3}] = ... 32 | vl_nnbnorm(inputs{1}, params{1}, params{2}, derOutputs{1}, ... 33 | 'epsilon', obj.epsilon, ... 34 | 'moments', params{3}, ... 35 | obj.opts{:}) ; 36 | else 37 | [derInputs{1}, derParams{1}, derParams{2}, derParams{3}] = ... 38 | vl_nnbnorm(inputs{1}, params{1}, params{2}, derOutputs{1}, ... 39 | 'epsilon', obj.epsilon, ... 40 | 'moments', obj.moments, ... 41 | obj.opts{:}) ; 42 | end 43 | obj.moments = [] ; 44 | % multiply the moments update by the number of images in the batch 45 | % this is required to make the update additive for subbatches 46 | % and will eventually be normalized away 47 | derParams{3} = derParams{3} * size(inputs{1},4) ; 48 | end 49 | 50 | % --------------------------------------------------------------------- 51 | function obj = BatchNorm(varargin) 52 | obj.load(varargin{:}) ; 53 | end 54 | 55 | function params = initParams(obj) 56 | params{1} = ones(obj.numChannels,1,'single') ; 57 | params{2} = zeros(obj.numChannels,1,'single') ; 58 | params{3} = zeros(obj.numChannels,2,'single') ; 59 | end 60 | 61 | function attach(obj, net, index) 62 | attach@dagnn.ElementWise(obj, net, index) ; 63 | p = net.getParamIndex(net.layers(index).params{3}) ; 64 | net.params(p).trainMethod = 'average' ; 65 | net.params(p).learningRate = 0.1 ; 66 | end 67 | end 68 | end 69 | -------------------------------------------------------------------------------- /matlab/+dagnn/Pseudo_Loss_Multi_Dynamic_1.m: -------------------------------------------------------------------------------- 1 | classdef Pseudo_Loss_Multi_Dynamic_1 < dagnn.Loss 2 | 3 | properties 4 | rate = 0; 5 | multipse_label = []; 6 | if_instanceW = false; 7 | instanceWeights = []; 8 | end 9 | 10 | methods 11 | function outputs = forward(obj, inputs, params) 12 | x = inputs{1}; 13 | c = inputs{2}; 14 | if obj.if_instanceW == true 15 | obj.instanceWeights = get_instanceweights4gan(c, 0.1); 16 | end 17 | obj.multipse_label = gpuArray(update_multi_pseudo(x, c)); 18 | c = reshape(c,1,1,1,size(c,2)); 19 | ci = label_to_index(x, c); 20 | 21 | [Xmax,index] = max(x,[],3) ; 22 | ex = exp(bsxfun(@minus, x, Xmax)) ; 23 | t1 = Xmax + log(sum(ex,3)) - x(ci) ; 24 | K = size(x,3); 25 | % label smooth for all 26 | part1 = t1; % softmax 27 | part2 = (bsxfun(@times,sum(obj.multipse_label,3),log(sum(ex,3))) - ... 28 | sum(bsxfun(@times,obj.multipse_label,bsxfun(@minus,x,Xmax)),3)) * 2/(K+1); % label smooth 29 | 30 | t = (1-obj.rate) * part1 + obj.rate *part2; 31 | gan = find(c==0); 32 | t(gan) = part2(gan); % remove the softmax part for generated image. 33 | 34 | if ~isempty(obj.instanceWeights) 35 | outputs{1} = obj.instanceWeights(:)' * t(:) ; 36 | else 37 | outputs{1} = sum(t(:)); 38 | end 39 | 40 | n = obj.numAveraged ; 41 | m = n + size(inputs{1},4) ; 42 | obj.average = (n * obj.average + gather(outputs{1})) / m ; 43 | obj.numAveraged = m ; 44 | end 45 | 46 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 47 | x = inputs{1}; 48 | c = inputs{2}; 49 | c = reshape(c,1,1,1,size(c,2)); 50 | 51 | ci = label_to_index(x, c); 52 | 53 | K = size(x,3); 54 | Xmax = max(x,[],3) ; 55 | ex = exp(bsxfun(@minus, x, Xmax)) ; 56 | part1 = bsxfun(@rdivide, ex, sum(ex,3)); % Y = E/L (softmax result) 57 | real = find(c~=0); 58 | part1(ci(real)) = part1(ci(real)) - 1 ; % for evey gt, d(gt) = Y-1 59 | part2 = bsxfun(@minus,bsxfun(@rdivide, ex, sum(ex,3)), obj.multipse_label * 2/(K+1)); 60 | y = (1-obj.rate) * part1 + obj.rate *part2; % When we set opt.rate=0, it equals to softmax loss 61 | gan = find(c==0); 62 | y(:,:,:,gan) = part2(:,:,:,gan); % We set opts.gan=1 63 | if ~isempty(obj.instanceWeights) 64 | y = bsxfun(@times, obj.instanceWeights, y) ; 65 | end 66 | derInputs{1} = y; 67 | derInputs{2} = []; 68 | derParams = {}; 69 | end 70 | 71 | function obj = Loss(varargin) 72 | obj.load(varargin) ; 73 | end 74 | 75 | end 76 | end 77 | -------------------------------------------------------------------------------- /matlab/+dagnn/Pseudo_Loss_Multi_Dynamic_2.m: -------------------------------------------------------------------------------- 1 | classdef Pseudo_Loss_Multi_Dynamic_2 < dagnn.Loss 2 | 3 | properties 4 | rate = 0; 5 | multipse_label = []; 6 | if_instanceW = true; 7 | instanceWeights = []; 8 | end 9 | 10 | methods 11 | function outputs = forward(obj, inputs, params) 12 | x = inputs{1}; 13 | c = inputs{2}; 14 | if obj.if_instanceW == true 15 | obj.instanceWeights = get_instanceweights4gan(c, 0.1); 16 | end 17 | obj.multipse_label = gpuArray(update_multi_pseudo(x, c)); 18 | c = reshape(c,1,1,1,size(c,2)); 19 | ci = label_to_index(x, c); 20 | 21 | [Xmax,index] = max(x,[],3) ; 22 | ex = exp(bsxfun(@minus, x, Xmax)) ; 23 | t1 = Xmax + log(sum(ex,3)) - x(ci) ; 24 | K = size(x,3); 25 | % label smooth for all 26 | part1 = t1; % softmax 27 | part2 = (bsxfun(@times,sum(obj.multipse_label,3),log(sum(ex,3))) - ... 28 | sum(bsxfun(@times,obj.multipse_label,bsxfun(@minus,x,Xmax)),3)) * 2/(K+1); % label smooth 29 | 30 | t = (1-obj.rate) * part1 + obj.rate *part2; 31 | gan = find(c==0); 32 | t(gan) = part2(gan); % remove the softmax part for generated image. 33 | 34 | if ~isempty(obj.instanceWeights) 35 | outputs{1} = obj.instanceWeights(:)' * t(:) ; 36 | else 37 | outputs{1} = sum(t(:)); 38 | end 39 | 40 | n = obj.numAveraged ; 41 | m = n + size(inputs{1},4) ; 42 | obj.average = (n * obj.average + gather(outputs{1})) / m ; 43 | obj.numAveraged = m ; 44 | end 45 | 46 | function [derInputs, derParams] = backward(obj, inputs, params, derOutputs) 47 | x = inputs{1}; 48 | c = inputs{2}; 49 | c = reshape(c,1,1,1,size(c,2)); 50 | 51 | ci = label_to_index(x, c); 52 | 53 | K = size(x,3); 54 | Xmax = max(x,[],3) ; 55 | ex = exp(bsxfun(@minus, x, Xmax)) ; 56 | part1 = bsxfun(@rdivide, ex, sum(ex,3)); % Y = E/L (softmax result) 57 | real = find(c~=0); 58 | part1(ci(real)) = part1(ci(real)) - 1 ; % for evey gt, d(gt) = Y-1 59 | part2 = bsxfun(@minus,bsxfun(@rdivide, ex, sum(ex,3)), obj.multipse_label * 2/(K+1)); 60 | y = (1-obj.rate) * part1 + obj.rate *part2; % When we set opt.rate=0, it equals to softmax loss 61 | gan = find(c==0); 62 | y(:,:,:,gan) = part2(:,:,:,gan); % We set opts.gan=1 63 | if ~isempty(obj.instanceWeights) 64 | y = bsxfun(@times, obj.instanceWeights, y) ; 65 | end 66 | derInputs{1} = y; 67 | derInputs{2} = []; 68 | derParams = {}; 69 | end 70 | 71 | function obj = Loss(varargin) 72 | obj.load(varargin) ; 73 | end 74 | 75 | end 76 | end 77 | -------------------------------------------------------------------------------- /code/train/train_res_iden_MpRL3.m: -------------------------------------------------------------------------------- 1 | function train_res_iden_MpRL3(num_predefined_classes, dropoutrate, num_real, num_gan, varargin) 2 | % ------------------------------------------------------------------------- 3 | % Part 4.1: prepare the data 4 | % ------------------------------------------------------------------------- 5 | 6 | % Load character dataset 7 | imdb = load(strcat('./data/url_data_gan_24000.mat')) ; 8 | imdb = imdb.imdb; 9 | 10 | imdb.images.data(num_real+num_gan+1:end) = []; 11 | imdb.images.label(num_real+num_gan+1:end) = []; 12 | imdb.images.set(num_real+num_gan+1:end) = []; 13 | 14 | imdb.images.set(:) = 1; 15 | 16 | % ------------------------------------------------------------------------- 17 | % Part 4.2: initialize a CNN architecture 18 | % ------------------------------------------------------------------------- 19 | net = resnet52_iden_MpRL3(num_predefined_classes, dropoutrate); 20 | net.conserveMemory = true; 21 | net.meta.normalization.averageImage = reshape([105.6615,99.1308,97.9115],1,1,3); 22 | % ------------------------------------------------------------------------- 23 | % Part 4.3: train and evaluate the CNN 24 | % ------------------------------------------------------------------------- 25 | opts.train.averageImage = net.meta.normalization.averageImage; 26 | opts.train.batchSize = 64; 27 | opts.train.continue = true; 28 | opts.train.gpus = 1; 29 | opts.train.prefetch = false ; 30 | opts.train.nesterovUpdate = true ; 31 | opts.train.expDir = './result/res52_iden_MpRL3'; 32 | % opts.train.derOutputs = {'objective', 0,'objective_multi_pseudo',1} ; 33 | opts.train.derOutputs = {'objective',1,'objective_multi_pseudo',0} ; 34 | %opts.train.gamma = 0.9; 35 | opts.train.momentum = 0.9; 36 | %opts.train.constraint = 100; 37 | opts.train.learningRate = [0.1*ones(1,40),0.01*ones(1,10)] ; 38 | opts.train.weightDecay = 0.0005; 39 | opts.train.numEpochs = numel(opts.train.learningRate) ; 40 | [opts, ~] = vl_argparse(opts.train, varargin) ; 41 | % Call training function in MatConvNet 42 | [net,info] = cnn_train_dag(net, imdb, @getBatch,opts) ; 43 | 44 | % -------------------------------------------------------------------- 45 | function inputs = getBatch(imdb,batch,opts) 46 | % -------------------------------------------------------------------- 47 | if(opts.epoch>20) % after 20 epoch we start using pseudo label. 48 | opts.derOutputs = {'objective', 0,'objective_multi_pseudo',1} ; 49 | end 50 | im_url = imdb.images.data(batch) ; 51 | im = vl_imreadjpeg(im_url,'Pack','Resize',[224,224],'Flip',... 52 | 'CropLocation','random','CropSize',[0.85,1],... 53 | 'Interpolation', 'bicubic','NumThreads',8); 54 | labels = imdb.images.label(batch); 55 | 56 | oim = bsxfun(@minus,im{1},opts.averageImage); 57 | inputs = {'data',gpuArray(oim),'label',labels}; 58 | -------------------------------------------------------------------------------- /code/train/train_res_iden_MpRL2.m: -------------------------------------------------------------------------------- 1 | function train_res_iden_MpRL2(num_predefined_classes, dropoutrate, num_real, num_gan, varargin) 2 | % ------------------------------------------------------------------------- 3 | % Part 4.1: prepare the data 4 | % ------------------------------------------------------------------------- 5 | 6 | % Load character dataset 7 | imdb = load('./data/url_data_gan_24000.mat') ; 8 | imdb = imdb.imdb; 9 | 10 | imdb.images.data(num_real+num_gan+1:end) = []; 11 | imdb.images.label(num_real+num_gan+1:end) = []; 12 | imdb.images.set(num_real+num_gan+1:end) = []; 13 | 14 | imdb.images.set(:) = 1; 15 | 16 | % ------------------------------------------------------------------------- 17 | % Part 4.2: initialize a CNN architecture 18 | % ------------------------------------------------------------------------- 19 | net = resnet52_iden_MpRL2(num_predefined_classes, dropoutrate); 20 | net.conserveMemory = true; 21 | net.meta.normalization.averageImage = reshape([105.6615,99.1308,97.9115],1,1,3); 22 | % ------------------------------------------------------------------------- 23 | % Part 4.3: train and evaluate the CNN 24 | % ------------------------------------------------------------------------- 25 | opts.train.averageImage = net.meta.normalization.averageImage; 26 | opts.train.batchSize = 64; 27 | opts.train.continue = true; 28 | opts.train.gpus = 1; 29 | opts.train.prefetch = false ; 30 | opts.train.nesterovUpdate = true ; 31 | opts.train.expDir = strcat('./result/res52_iden_MpRL2',num2str(num_gan)); 32 | % opts.train.derOutputs = {'objective', 0,'objective_multi_pseudo',1} ; 33 | opts.train.derOutputs = {'objective',0,'objective_multi_pseudo',1} ; 34 | %opts.train.gamma = 0.9; 35 | opts.train.momentum = 0.9; 36 | %opts.train.constraint = 100; 37 | opts.train.learningRate = [0.1*ones(1,40),0.01*ones(1,10)] ; 38 | opts.train.weightDecay = 0.0005; 39 | opts.train.numEpochs = numel(opts.train.learningRate) ; 40 | [opts, ~] = vl_argparse(opts.train, varargin) ; 41 | % Call training function in MatConvNet 42 | [net,info] = cnn_train_dag(net, imdb, @getBatch,opts) ; 43 | 44 | % -------------------------------------------------------------------- 45 | function inputs = getBatch(imdb,batch,opts) 46 | % -------------------------------------------------------------------- 47 | % if(opts.epoch>0) % after 20 epoch we start using pseudo label. 48 | % opts.derOutputs = {'objective', 0,'objective_multi_pseudo',1} ; 49 | % end 50 | im_url = imdb.images.data(batch) ; 51 | im = vl_imreadjpeg(im_url,'Pack','Resize',[224,224],'Flip',... 52 | 'CropLocation','random','CropSize',[0.85,1],... 53 | 'Interpolation', 'bicubic','NumThreads',8); 54 | labels = imdb.images.label(batch); 55 | 56 | oim = bsxfun(@minus,im{1},opts.averageImage); 57 | inputs = {'data',gpuArray(oim),'label',labels}; 58 | -------------------------------------------------------------------------------- /evaluation/utils/compute_AP_multiCam.m: -------------------------------------------------------------------------------- 1 | function ap_multi = compute_AP_multiCam(good_image, junk_image, index, queryCam, testCam) 2 | good_cam = testCam(good_image); 3 | good_cam_uni = unique(good_cam); 4 | ap_multi = zeros(1, 6); 5 | 6 | % on the same camera 7 | good_cam_now = queryCam; 8 | ngood = length(junk_image)-1; 9 | junk_image_now = [good_image; index(1)]; 10 | good_image_now = setdiff(junk_image, index(1)); 11 | old_recall = 0; 12 | old_precision = 1.0; 13 | ap = 0; 14 | intersect_size = 0; 15 | j = 0; 16 | good_now = 0; 17 | for n = 1:length(index) 18 | flag = 0; 19 | if ~isempty(find(good_image_now == index(n), 1)) 20 | flag = 1; % good image 21 | good_now = good_now+1; 22 | end 23 | if ~isempty(find(junk_image_now == index(n), 1)) 24 | continue; % junk image 25 | end 26 | 27 | if flag == 1%good 28 | intersect_size = intersect_size + 1; 29 | end 30 | if ngood == 0 31 | ap_multi(good_cam_now) = 0; 32 | break; 33 | end 34 | recall = intersect_size/ngood; 35 | precision = intersect_size/(j + 1); 36 | ap = ap + (recall - old_recall)*((old_precision+precision)/2); 37 | old_recall = recall; 38 | old_precision = precision; 39 | j = j+1; 40 | 41 | if good_now == ngood 42 | ap_multi(good_cam_now) = ap; 43 | break; 44 | end 45 | end 46 | 47 | for k = 1:length(good_cam_uni) 48 | good_cam_now = good_cam_uni(k); 49 | ngood = length(find(good_cam == good_cam_now)); 50 | pos_junk = find(good_cam ~= good_cam_now); 51 | junk_image_now = [junk_image; good_image(pos_junk)]; 52 | pos_good = find(good_cam == good_cam_now); 53 | good_image_now = good_image(pos_good); 54 | old_recall = 0; 55 | old_precision = 1.0; 56 | ap = 0; 57 | intersect_size = 0; 58 | j = 0; 59 | good_now = 0; 60 | for n = 1:length(index) 61 | flag = 0; 62 | if ~isempty(find(good_image_now == index(n), 1)) 63 | flag = 1; % good image 64 | good_now = good_now+1; 65 | end 66 | if ~isempty(find(junk_image_now == index(n), 1)) 67 | continue; % junk image 68 | end 69 | 70 | if flag == 1%good 71 | intersect_size = intersect_size + 1; 72 | end 73 | recall = intersect_size/ngood; 74 | precision = intersect_size/(j + 1); 75 | ap = ap + (recall - old_recall)*((old_precision+precision)/2); 76 | old_recall = recall; 77 | old_precision = precision; 78 | j = j+1; 79 | 80 | if good_now == ngood 81 | ap_multi(good_cam_now) = ap; 82 | break; 83 | end 84 | end 85 | end 86 | 87 | end 88 | 89 | 90 | -------------------------------------------------------------------------------- /matlab/vl_nnsoftmaxloss.m: -------------------------------------------------------------------------------- 1 | function y = vl_nnsoftmaxloss(x,c,dzdy) 2 | %VL_NNSOFTMAXLOSS CNN combined softmax and logistic loss. 3 | % **Deprecated: use `vl_nnloss` instead** 4 | % 5 | % Y = VL_NNSOFTMAX(X, C) applies the softmax operator followed by 6 | % the logistic loss the data X. X has dimension H x W x D x N, 7 | % packing N arrays of W x H D-dimensional vectors. 8 | % 9 | % C contains the class labels, which should be integers in the range 10 | % 1 to D. C can be an array with either N elements or with dimensions 11 | % H x W x 1 x N dimensions. In the fist case, a given class label is 12 | % applied at all spatial locations; in the second case, different 13 | % class labels can be specified for different locations. 14 | % 15 | % DZDX = VL_NNSOFTMAXLOSS(X, C, DZDY) computes the derivative of the 16 | % block projected onto DZDY. DZDX and DZDY have the same dimensions 17 | % as X and Y respectively. 18 | 19 | % Copyright (C) 2014-15 Andrea Vedaldi. 20 | % All rights reserved. 21 | % 22 | % This file is part of the VLFeat library and is made available under 23 | % the terms of the BSD license (see the COPYING file). 24 | 25 | % work around a bug in MATLAB, where native cast() would slow 26 | % progressively 27 | if isa(x, 'gpuArray') 28 | switch classUnderlying(x) ; 29 | case 'single', cast = @(z) single(z) ; 30 | case 'double', cast = @(z) double(z) ; 31 | end 32 | else 33 | switch class(x) 34 | case 'single', cast = @(z) single(z) ; 35 | case 'double', cast = @(z) double(z) ; 36 | end 37 | end 38 | 39 | %X = X + 1e-6 ; 40 | sz = [size(x,1) size(x,2) size(x,3) size(x,4)] ; 41 | 42 | if numel(c) == sz(4) 43 | % one label per image 44 | c = reshape(c, [1 1 1 sz(4)]) ; 45 | end 46 | if size(c,1) == 1 & size(c,2) == 1 47 | c = repmat(c, [sz(1) sz(2)]) ; 48 | end 49 | 50 | % one label per spatial location 51 | sz_ = [size(c,1) size(c,2) size(c,3) size(c,4)] ; 52 | assert(isequal(sz_, [sz(1) sz(2) sz_(3) sz(4)])) ; 53 | assert(sz_(3)==1 | sz_(3)==2) ; 54 | 55 | % class c = 0 skips a spatial location 56 | mass = cast(c(:,:,1,:) > 0) ; 57 | if sz_(3) == 2 58 | % the second channel of c (if present) is used as weights 59 | mass = mass .* c(:,:,2,:) ; 60 | c(:,:,2,:) = [] ; 61 | end 62 | 63 | % convert to indexes 64 | c = c - 1 ; 65 | c_ = 0:numel(c)-1 ; 66 | c_ = 1 + ... 67 | mod(c_, sz(1)*sz(2)) + ... 68 | (sz(1)*sz(2)) * max(c(:), 0)' + ... 69 | (sz(1)*sz(2)*sz(3)) * floor(c_/(sz(1)*sz(2))) ; 70 | 71 | % compute softmaxloss 72 | xmax = max(x,[],3) ; 73 | ex = exp(bsxfun(@minus, x, xmax)) ; 74 | 75 | %n = sz(1)*sz(2) ; 76 | if nargin <= 2 77 | t = xmax + log(sum(ex,3)) - reshape(x(c_), [sz(1:2) 1 sz(4)]) ; 78 | y = sum(sum(sum(mass .* t,1),2),4) ; 79 | else 80 | y = bsxfun(@rdivide, ex, sum(ex,3)) ; 81 | y(c_) = y(c_) - 1; 82 | y = bsxfun(@times, y, bsxfun(@times, mass, dzdy)) ; 83 | end 84 | --------------------------------------------------------------------------------