代码是使用matconvnet+R2018b
主要包括:
1、特征提取
2、计算map
3、检索:这里是使用官方提供的边界框,对查询图像进行裁剪,然后进行查询。
addpath('helpers');
addpath('matlab');
run './matlab/vl_setupnn';gnd_paris6k = load('./groundtruth/gnd_paris6k.mat');
net = load('imagenet-vgg-verydeep-16.mat');fprintf("starting extracting database feature!\n");
vecs = zeros(4096,numel(gnd_paris6k.imlist));
for i = 1:numel(gnd_paris6k.imlist)nameImg = strcat('./datasets/paris6k/',gnd_paris6k.imlist{i},'.jpg');oriImg = imread(nameImg);image = single(oriImg);image = imresize(image, net.meta.normalization.imageSize(1:2)) ; image = image - net.meta.normalization.averageImage ;res = vl_simplenn(net,image);feat = res(34).x;vecs(:,i) = feat;if mod(i,100) == 0iend
endfprintf("extracting query image!\n");
qimlist = {gnd_paris6k.imlist{gnd_paris6k.qidx}};
qim = arrayfun(@(x) crop_qim(['./datasets/paris6k/',qimlist{x},'.jpg'],gnd_paris6k.gnd(x).bbx),1:numel(gnd_paris6k.qidx),'un',0);
qvecs = zeros(4096,numel(gnd_paris6k.qidx));
for i = 1:numel(gnd_paris6k.qidx)ori = strcat('./datasets/paris6k/',gnd_paris6k.imlist{gnd_paris6k.qidx(i)},'.jpg');Img = imread(ori);image = single(Img);image = imresize(image, net.meta.normalization.imageSize(1:2)); image = image - net.meta.normalization.averageImage;res = vl_simplenn(net,image);feat = res(34).x;qvecs(:,i) = feat;fprintf('Extracting Image %d ... \n', i);
endfprintf("Initial filtering...\n");
[sim,ranks] = sort(vecs'*qvecs,'descend');
map = compute_map(ranks,gnd_paris6k.gnd);
fprintf('mAP (4096), without re-ranking = %.4f\n', map);% rand均匀分布的随机数
query = floor(rand*numel(gnd_paris6k.qidx));
figure;
im_index = gnd_paris6k.qidx(query);
image = imread(strcat(['./datasets/paris6k/',gnd_paris6k.imlist{im_index},'.jpg']));
imshow(image);
bounding_box_tmp = gnd_paris6k.gnd(query).bbx;
bounding_box = zeros(1,4);
bounding_box(1) = bounding_box_tmp(1);
bounding_box(2) = bounding_box_tmp(2);
bounding_box(3) = bounding_box_tmp(3) - bounding_box_tmp(1);
bounding_box(4) = bounding_box_tmp(4) - bounding_box_tmp(2);
rectangle('Position',bounding_box,'EdgeColor','g','LineWidth',2);
title(strcat('query',string(query)));% 显示检索的结果
figure;
for i = 1:10[image,map] = imread(strcat('./datasets/paris6k/',gnd_paris6k.imlist{ranks(i,query)},'.jpg'));subplot(2,5,i), imshow(image,map);if ismember(ranks(i,query),gnd_paris6k.gnd(query).ok )rectangle('Position', [1 1 size(image,2)-1 size(image,1)-1] ,'EdgeColor','g','LineWidth',2);elseif ismember(ranks(i,query),gnd_paris6k.gnd(query).junk )rectangle('Position', [1 1 size(image,2)-1 size(image,1)-1] ,'EdgeColor','b','LineWidth',2);elserectangle('Position', [1 1 size(image,2)-1 size(image,1)-1] ,'EdgeColor','r','LineWidth',2); end
endsave('./datasets/features/paris6k.mat','vecs','qvecs');