123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- function [ features ] = caffe_features_multiple_images( s_filelist, f_mean, net, settings )
-
- if (nargin<2)
- error ( 'no mean passed');
- end
- if (nargin<3)
- error ( 'no network passed');
- end
- if (nargin<4)
- settings = [];
- end
-
-
- s_layer = getFieldWithDefault ( settings, 's_layer', 'relu7');
- b_apply_bilinear_pooling = getFieldWithDefault ( settings, 'b_apply_bilinear_pooling', false );
- b_skip_normalization_in_bilinear_pooling ...
- = getFieldWithDefault ( settings, 'b_skip_normalization_in_bilinear_pooling', false );
- b_apply_log_M = getFieldWithDefault ( settings, 'b_apply_log_M', false );
- f_sigma = getFieldWithDefault ( settings, 'f_sigma', 1e-5 );
-
-
- b_filelistmode = ischar( s_filelist );
-
- if (b_filelistmode)
-
- fid = fopen( s_filelist );
- s_filelist_to_use = textscan(fid,'%s');
- s_filelist_to_use = s_filelist_to_use{1};
- fclose(fid);
- else
-
- s_filelist_to_use = s_filelist;
- end
-
-
- net_input_shape = net.blobs('data').shape;
- i_batch_size = net_input_shape(4);
-
-
- batch_data = {zeros(net_input_shape(1),...
- net_input_shape(2),...
- net_input_shape(3),...
- i_batch_size,...
- 'single')};
-
-
- slices = 1:i_batch_size:size(s_filelist_to_use,1);
- slices(end+1)=size(s_filelist_to_use,1)+1;
-
-
-
- for i=1:numel(slices)-1
-
-
- if ( ( i > 1 ) && ( mod(i,10) == 0 ) )
- fprintf('Running batch number %i of %i\n',i, numel(slices)-1);
- end
-
-
- for j=slices(i):slices(i+1)-1;
- if (b_filelistmode)
- batch_data{1}(:,:,:,j-slices(i)+1) = caffe_prepare_image(imread( s_filelist_to_use{j} ), f_mean );
- else
- batch_data{1}(:,:,:,j-slices(i)+1) = caffe_prepare_image(s_filelist_to_use{j}, f_mean );
- end
- end
-
-
- [~] = net.forward( batch_data );
-
-
- tmp_feat = net.blobs( s_layer ).get_data();
-
-
- if ( b_apply_bilinear_pooling )
-
-
-
-
- for i_img = 1:i_batch_size
-
- if ( i_batch_size ==1 )
- b_has_spatial_support = ( ndims ( tmp_feat ) == 3 );
- else
- b_has_spatial_support = ( ndims ( tmp_feat ) == 4 );
- end
-
- if ( b_has_spatial_support )
- i_channelCount = size ( tmp_feat, 3);
-
-
- featImg = reshape ( tmp_feat(:,:,:,i_img), [],i_channelCount );
-
-
-
-
- if ( ~b_skip_normalization_in_bilinear_pooling )
-
-
-
-
-
-
- featImg = bsxfun(@times, featImg, 1./( max( 10e-8, sqrt(sum(featImg,2).^2) ) ) );
- end
-
- featImg = featImg'*featImg;
- else
- featImg = tmp_feat(:,i_img)*tmp_feat(:,i_img)';
- end
- if ( b_apply_log_M )
-
-
-
-
- featImg = logm( featImg + f_sigma*eye( size(featImg) ) );
- end
-
-
- featImg = featImg ( logical(tril(ones(size(featImg)))));
-
- featImg = sign(featImg).*sqrt(abs(featImg));
-
- featImg = featImg / sqrt(sum(featImg.^2));
-
-
- if ( ~exist('features','var') )
- features = zeros( size(featImg,1), size(s_filelist_to_use,1), 'single');
- end
-
-
- features( :, slices(i)+i_img-1 ) = featImg;
- end
- else
-
- if ( ndims( tmp_feat ) > 2 )
- tmp_feat = reshape( tmp_feat, ...
- size(tmp_feat,1)*size(tmp_feat,2)*size(tmp_feat,3), ...
- size(tmp_feat,4)...
- );
- end
-
-
- if ( ~exist('features','var') )
- features = zeros( size(tmp_feat,1), size(s_filelist_to_use,1), 'single');
- end
-
-
- features( :, slices(i):(slices(i+1)-1) ) = tmp_feat( :, 1:(slices(i+1)-slices(i)) );
- end
-
-
- end
-
-
- features = double(features);
- end
|