Skip to content

Commit df80e4b

Browse files
committed
ILSVRC localization raw scripts are uploaded
1 parent fa451e6 commit df80e4b

2 files changed

Lines changed: 290 additions & 0 deletions

File tree

ILSVRC_evaluate_bbox.m

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
2+
3+
datasetName = 'ILSVRCvalSet';
4+
load('imagenet_toolkit/ILSVRC2014_devkit/evaluation/cache_groundtruth.mat');
5+
load('imagenet_toolkit/ILSVRC2014_devkit/data/meta_clsloc.mat');
6+
datasetPath = 'dataset/ILSVRC2012';
7+
load([datasetPath '/imageListVal.mat']);
8+
load('sizeImg_ILSVRC2014.mat');
9+
10+
% datasetName = 'ILSVRCtestSet';
11+
% datasetPath = '/data/vision/torralba/deeplearning/imagenet_toolkit';
12+
% load([datasetPath '/imageListTest.mat']);
13+
14+
15+
nImgs = size(imageList,1);
16+
17+
ground_truth_file='imagenet_toolkit/ILSVRC2014_devkit/data/ILSVRC2014_clsloc_validation_ground_truth.txt';
18+
gt_labels = dlmread(ground_truth_file);
19+
20+
categories_gt = [];
21+
categoryIDMap = containers.Map();
22+
for i=1:numel(synsets)
23+
categories_gt{synsets(i).ILSVRC2014_ID,1} = synsets(i).words;
24+
categories_gt{synsets(i).ILSVRC2014_ID,2} = synsets(i).WNID;
25+
categoryIDMap(synsets(i).WNID) = i;
26+
end
27+
28+
29+
30+
%% network to evaluate
31+
% backpropa-heatmap
32+
%netName = 'caffeNet_imagenet';
33+
%netName = 'googlenetBVLC_imagenet';
34+
%netName = 'VGG16_imagenet';
35+
36+
% CAM-based network
37+
%netName = 'NIN';
38+
%netName = 'CAM_imagenetCNNaveSumDeep';
39+
%netName = 'CAM_googlenetBVLC_imagenet';% the direct output
40+
netName = 'CAM_googlenetBVLCshrink_imagenet';
41+
%netName = 'CAM_googlenetBVLCshrink_imagenet_maxpool';
42+
%netName = 'CAM_VGG16_imagenet';
43+
%netName = 'CAM_alexnet';
44+
45+
load('categoriesImageNet.mat');
46+
47+
visualizationPointer = 0;
48+
49+
topCategoryNum = 5;
50+
predictionResult_bbox1 = zeros(nImgs, topCategoryNum*5);
51+
predictionResult_bbox2 = zeros(nImgs, topCategoryNum*5);
52+
predictionResult_bboxCombine = zeros(nImgs, topCategoryNum*5);
53+
54+
if matlabpool('size')==0
55+
try
56+
matlabpool
57+
catch e
58+
end
59+
end
60+
61+
heatMapFolder = ['heatMap-' datasetName '-' netName];
62+
bbox_threshold = [20, 100, 110];
63+
curParaThreshold = [num2str(bbox_threshold(1)) ' ' num2str(bbox_threshold(2)) ' ' num2str(bbox_threshold(3))];
64+
parfor i=1:size(imageList,1)
65+
curImgIDX = i;
66+
67+
height_original = sizeFull_imageList(curImgIDX,1);%tmp.Height;
68+
weight_original = sizeFull_imageList(curImgIDX,2);%tmp.Width;
69+
70+
[a b c] = fileparts(imageList{curImgIDX,1});
71+
curPath_fullSizeImg = ['/data/vision/torralba/deeplearning/imagenet_toolkit/ILSVRC2012_img_val/' b c];
72+
curMatFile = [heatMapFolder '/' b '.mat'];
73+
[heatMapSet, value_category, IDX_category] = loadHeatMap( curMatFile);
74+
75+
curResult_bbox1 = [];
76+
curResult_bbox2 = [];
77+
curResult_bboxCombine = [];
78+
for j=1:5
79+
curHeatMapFile = [heatMapFolder '/top' num2str(j) '/' b '.jpg'];
80+
81+
curBBoxFile = [heatMapFolder '/top' num2str(j) '/' b '_default.txt'];
82+
%curBBoxFileGraphcut = [heatMapFolder '/top' num2str(j) '/' b '_graphcut.txt'];
83+
curCategory = categories{IDX_category(j),1};
84+
%imwrite(curHeatMap, ['result_bbox/heatmap_tmp' b randString '.jpg']);
85+
if ~exist(curBBoxFile)
86+
%system(['/data/vision/torralba/deeplearning/package/bbox_hui/final ' curHeatMapFile ' ' curBBoxFile]);
87+
88+
system(['/data/vision/torralba/deeplearning/package/bbox_hui_new/./dt_box ' curHeatMapFile ' ' curParaThreshold ' ' curBBoxFile]);
89+
end
90+
curPredictCategory = categories{IDX_category(j),1};
91+
curPredictCategoryID = categories{IDX_category(j),1}(1:9);
92+
curPredictCategoryGTID = categoryIDMap(curPredictCategoryID);
93+
94+
95+
boxData = dlmread(curBBoxFile);
96+
boxData_formulate = [boxData(1:4:end)' boxData(2:4:end)' boxData(1:4:end)'+boxData(3:4:end)' boxData(2:4:end)'+boxData(4:4:end)'];
97+
boxData_formulate = [min(boxData_formulate(:,1),boxData_formulate(:,3)),min(boxData_formulate(:,2),boxData_formulate(:,4)),max(boxData_formulate(:,1),boxData_formulate(:,3)),max(boxData_formulate(:,2),boxData_formulate(:,4))];
98+
99+
% try
100+
% boxDataGraphcut = dlmread(curBBoxFileGraphcut);
101+
% boxData_formulateGraphcut = [boxDataGraphcut(1:4:end)' boxDataGraphcut(2:4:end)' boxDataGraphcut(1:4:end)'+boxDataGraphcut(3:4:end)' boxDataGraphcut(2:4:end)'+boxDataGraphcut(4:4:end)'];
102+
% catch exception
103+
% boxDataGraphcut = dlmread(curBBoxFile);
104+
% boxData_formulateGraphcut = [boxDataGraphcut(1:4:end)' boxDataGraphcut(2:4:end)' boxDataGraphcut(1:4:end)'+boxDataGraphcut(3:4:end)' boxDataGraphcut(2:4:end)'+boxDataGraphcut(4:4:end)'];
105+
% boxData_formulateGraphcut = boxData_formulateGraphcut(1,:);
106+
% end
107+
108+
bbox = boxData_formulate(1,:);
109+
curPredictTuple = [curPredictCategoryGTID bbox(1) bbox(2) bbox(3) bbox(4)];
110+
curResult_bbox1 = [curResult_bbox1 curPredictTuple];
111+
curResult_bboxCombine = [curResult_bboxCombine curPredictTuple];
112+
113+
bbox = boxData_formulate(2,:);
114+
%bbox = boxData_formulateGraphcut(1,:);
115+
curPredictTuple = [curPredictCategoryGTID bbox(1) bbox(2) bbox(3) bbox(4)];
116+
curResult_bbox2 = [curResult_bbox2 curPredictTuple];
117+
118+
curResult_bboxCombine = [curResult_bboxCombine curPredictTuple];
119+
if visualizationPointer == 1
120+
121+
curHeatMap = imread(curHeatMapFile);
122+
curHeatMap = imresize(curHeatMap,[height_original weight_original]);
123+
124+
subplot(1,2,1),hold off, imshow(curPath_fullSizeImg);
125+
hold on
126+
curBox = boxData_formulate(1,:);
127+
rectangle('Position',[curBox(1) curBox(2) curBox(3)-curBox(1) curBox(4)-curBox(2)],'EdgeColor',[1 0 0]);
128+
subplot(1,2,2),imagesc(curHeatMap);
129+
title(curCategory);
130+
waitforbuttonpress
131+
end
132+
end
133+
134+
predictionResult_bbox1(i, :) = curResult_bbox1;
135+
predictionResult_bbox2(i, :) = curResult_bbox2;
136+
predictionResult_bboxCombine(i,:) = curResult_bboxCombine(1:topCategoryNum*5);
137+
disp([netName ' processing ' b])
138+
end
139+
140+
141+
addpath('imagenet_toolkit/ILSVRC2014_devkit/evaluation');
142+
disp([netName '--------bbox1' ]);
143+
[cls_error, clsloc_error] = simpleEvaluation(predictionResult_bbox1);
144+
disp([(1:5)',clsloc_error,cls_error]);
145+
146+
disp([netName '--------bbox2' ]);
147+
[cls_error, clsloc_error] = simpleEvaluation(predictionResult_bbox2);
148+
disp([(1:5)',clsloc_error,cls_error]);
149+
150+
disp([netName '--------bboxCombine' ]);
151+
[cls_error, clsloc_error] = simpleEvaluation(predictionResult_bboxCombine);
152+
disp([(1:5)',clsloc_error,cls_error]);

ILSVRC_generate_heatmap.m

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
% raw script used to generate heatmaps for ILSVRC localization experiment
2+
% please load the necessary packages like matcaffe and ILSVRC toolbox correctly, some functions in matcaffe might be already deprecated.
3+
% you could take it as an example to see how to reproduce the ILSVRC localization experiment.
4+
%
5+
% Bolei Zhou.
6+
7+
addpath('caffeCPU2/matlab/caffe');
8+
9+
modelSetFolder = 'CAMnet';
10+
11+
%% CAMnet
12+
13+
14+
% netName = 'CAM_googlenetBVLC_imagenet';
15+
% model_file = [modelSetFolder '/googlenet_imagenet/bvlc_googlenet.caffemodel'];
16+
% model_def_file = [modelSetFolder '/googlenet_imagenet/deploy.protxt'];
17+
18+
% netName = 'CAM_alexnet';
19+
% model_file = [modelSetFolder '/alexnet/CAMmodels/caffeNetCAM_imagenet_train_iter_100000.caffemodel'];
20+
% model_def_file = [modelSetFolder '/alexnet/deploy_caffeNetCAM.prototxt'];
21+
22+
netName = 'CAM_googlenetBVLCshrink_imagenet';
23+
model_file = [modelSetFolder '/googlenet_imagenet/CAMmodels/imagenet_googleletCAM_train_iter_80000.caffemodel'];
24+
model_def_file = [modelSetFolder '/googlenet_imagenet/deploy_googlenetCAM.prototxt'];
25+
26+
27+
% netName = 'CAM_VGG16_imagenet';
28+
% model_file = [modelSetFolder '/VGGnet/models/vgg16CAM_train_iter_50000.caffemodel'];
29+
% model_def_file = [modelSetFolder '/VGGnet/deploy_vgg16CAM.prototxt'];
30+
31+
32+
%% loading the network
33+
caffe('init', model_def_file, model_file,'test');
34+
caffe('set_mode_gpu');
35+
caffe('set_device',0);
36+
37+
%% testing to predict some image
38+
39+
weights = caffe('get_weights');
40+
weights_LR = squeeze(weights(end,1).weights{1,1});
41+
bias_LR = weights(end,1).weights{2,1};
42+
layernames = caffe('get_names');
43+
response = caffe('get_all_layers');
44+
netInfo = cell(size(layernames,1),3);
45+
for i=1:size(layernames,1)
46+
netInfo{i,1} = layernames{i};
47+
netInfo{i,2} = i;
48+
netInfo{i,3} = size(response{i,1});
49+
end
50+
51+
load('categoriesImageNet.mat');
52+
d = load('/data/vision/torralba/small-projects/bolei_deep/caffe/ilsvrc_2012_mean.mat');
53+
IMAGE_MEAN = d.image_mean;
54+
IMAGE_DIM = 256;
55+
CROPPED_DIM = netInfo{1,3}(1);
56+
57+
weightInfo = cell(size(weights,1),1);
58+
for i=1:size(weights,1)
59+
weightInfo{i,1} = weights(i,1).layer_names;
60+
weightInfo{i,2} = weights(i,1).weights{1,1};
61+
weightInfo{i,3} = size(weights(i,1).weights{1,1});
62+
end
63+
64+
%% testing to predict some image
65+
66+
datasetName = 'ILSVRCvalSet';
67+
datasetPath = '/data/vision/torralba/gigaSUN/deeplearning/dataset/ILSVRC2012';
68+
load([datasetPath '/imageListVal.mat']);
69+
load('sizeImg_ILSVRC2014.mat');
70+
% datasetName = 'ILSVRCtestSet';
71+
% datasetPath = '/data/vision/torralba/deeplearning/imagenet_toolkit';
72+
% load([datasetPath '/imageListTest.mat']);
73+
74+
75+
76+
saveFolder = ['heatMap-' datasetName '-' netName];
77+
if ~exist(saveFolder)
78+
mkdir(saveFolder);
79+
end
80+
for i=1:5
81+
if ~exist([saveFolder '/top' num2str(i)])
82+
mkdir([saveFolder '/top' num2str(i)]);
83+
end
84+
end
85+
86+
for i = 1:size(imageList,1)
87+
curImgIDX = i;
88+
[a b c] = fileparts(imageList{curImgIDX,1});
89+
saveMatFile = [saveFolder '/' b '.mat'];
90+
if ~exist(saveMatFile)
91+
height_original = sizeFull_imageList(curImgIDX,1);%tmp.Height;
92+
weight_original = sizeFull_imageList(curImgIDX,2);%tmp.Width;
93+
94+
95+
curImg = imread(imageList{curImgIDX,1});
96+
97+
if size(curImg,3)==1
98+
curImg = repmat(curImg,[1 1 3]);
99+
end
100+
101+
102+
scores = caffe('forward', {prepare_img(curImg, IMAGE_MEAN, CROPPED_DIM)});
103+
response = caffe('get_all_layers');
104+
scoresMean = mean(squeeze(scores{1}),2);
105+
[value_category, IDX_category] = sort(scoresMean,'descend');
106+
107+
108+
featureObjectSwitchSpatial = squeeze(response{end-3,1});
109+
[curColumnMap] = returnColumnMap(featureObjectSwitchSpatial, weights_LR(:,IDX_category(1:5)));
110+
111+
112+
113+
for j=1:5
114+
curFeatureMap = squeeze(curColumnMap(:,:,j,:));
115+
curFeatureMap_crop = imresize(curFeatureMap,[netInfo{1,3}(1) netInfo{1,3}(2)]);
116+
gradients = zeros([netInfo{1,3}(1) netInfo{1,3}(2) 3 10]);
117+
gradients(:,:,1,:) = curFeatureMap_crop;
118+
gradients(:,:,2,:) = curFeatureMap_crop;
119+
gradients(:,:,3,:) = curFeatureMap_crop;
120+
121+
[alignImgMean alignImgSet] = crop2img(gradients);
122+
alignImgMean = single(alignImgMean);
123+
alignImgMean = imresize(alignImgMean, [height_original weight_original]);
124+
alignImgMean = alignImgMean./max(alignImgMean(:));
125+
126+
127+
imwrite(alignImgMean, [saveFolder '/top' num2str(j) '/' b '.jpg']);
128+
129+
end
130+
value_category = single(value_category);
131+
IDX_category = single(IDX_category);
132+
save(saveMatFile,'value_category','IDX_category');
133+
disp([netName ' processing ' b]);
134+
end
135+
end
136+
137+
138+

0 commit comments

Comments
 (0)