-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbatch_instanceTrainId_to_dets.m
122 lines (107 loc) · 4.37 KB
/
batch_instanceTrainId_to_dets.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
% ------------------------------------------------------------------------
% Copyright (C)
% Torr Vision Group (TVG)
% University of Oxford - UK
%
% Qizhu Li <[email protected]>
% August 2018
% ------------------------------------------------------------------------
% This file is part of the weakly-supervised training method presented in:
% Qizhu Li*, Anurag Arnab*, Philip H.S. Torr,
% "Weakly- and Semi-Supervised Panoptic Segmentation,"
% European Conference on Computer Vision (ECCV) 2018.
% Please consider citing the paper if you use this code.
% ------------------------------------------------------------------------
% This script demos extracting bounding box information from Cityscapes
% instance ground truth (*_gt<Fine,Coarse>_instanceTrainIds.png files) for
% generation of iterative ground truths for weakly-supervised experiments
%
% It is a batch processor. Set split to 'train', 'val', or 'train_extra'
% as desired. Make sure the instance ground truth files are found in
% data/Cityscapes/gt<Fine,Coarse>/<train,val,train_extra>/ respectively.
% If `visualise` is set to true, make sure the original RGB files are
% available in data/Cityscapes/leftImg8bit/<train,val,train_extra>/
% following the standard Cityscapes data folder organisation.
% -----------------------------------------------------------------------
clearvars;
addpath utils
addpath scripts
addpath visualisation
%% Configure
split = 'train'; % train, val or train_extra
% set is_panoptic flag to include image-level stuff class dets
is_panoptic = true;
% set incl_grps flag to include thing groups present in Cityscapes
% annotation
incl_grps = true;
% trainIds of stuff classes
stuff_classes = 0:10;
% trainIds of thing classes
thing_classes = 11:18;
% class names
load objectName19.mat
% ignore label
ignore_label = 255;
% force overwrite
force_overwrite = true;
% visualise
visualise = false;
visualise_save_template = '%s_leftImg8bit.png';
load utils/colormapcs.mat
%% Parse
switch split
case 'train'
instanceTrainId_dir = 'data/Cityscapes/gtFine/train';
instanceTrainId_template = '%s_gtFine_instanceTrainIds.png';
list_path = 'data/Cityscapes/lists/train_id.txt';
save_dir = 'data/Cityscapes/gtFine_bboxes/train/%s'; % panoptic or thing-only as the child folder
save_template = '%s_leftImg8bit.mat';
rgb_dir = 'data/Cityscapes/leftImg8bit/train';
case 'val'
instanceTrainId_dir = 'data/Cityscapes/gtFine/val';
instanceTrainId_template = '%s_gtFine_instanceTrainIds.png';
list_path = 'data/Cityscapes/lists/val_id.txt';
save_dir = 'data/Cityscapes/gtFine_bboxes/val/%s'; % panoptic or thing-only as the child folder
save_template = '%s_leftImg8bit.mat';
rgb_dir = 'data/Cityscapes/leftImg8bit/val';
case 'train_extra'
instanceTrainId_dir = 'data/Cityscapes/gtCoarse/train_extra';
instanceTrainId_template = '%s_gtCoarse_instanceTrainIds.png';
list_path = 'data/Cityscapes/lists/train_extra_id.txt';
save_dir = 'data/Cityscapes/gtCoarse_bboxes/train_extra/%s'; % panoptic or thing-only as the child folder
save_template = '%s_leftImg8bit.mat';
rgb_dir = 'data/Cityscapes/leftImg8bit/train_extra';
otherwise
error('Unrecognised data split.');
end
if is_panoptic
save_dir = sprintf(save_dir, 'panoptic');
else
save_dir = sprintf(save_dir, 'thing-only');
end
if ~exist(save_dir, 'dir')
mkdir(save_dir);
end
list = importdata(list_path);
%% Run the extraction
for k = 1:length(list)
id = list{k};
save_path = fullfile(save_dir, sprintf(save_template, id));
% if the save_path file exists, and we don't require force-overwrite, skip the current file
if ~force_overwrite && exist(save_path, 'file')
continue;
end
city = strtok(id, '_');
label_path = fullfile(instanceTrainId_dir, city, sprintf(instanceTrainId_template, id));
label = imread(label_path);
dets = instanceTrainId_to_dets(label, is_panoptic, incl_grps, ...
stuff_classes, thing_classes, objectNames, ignore_label);
save(fullfile(save_path), 'dets');
if visualise
vis_im = visualise_bboxes(id, objectNames, cmap, rgb_dir, save_dir);
imwrite(vis_im, fullfile(save_dir, sprintf(visualise_save_template, id)));
end
if mod(k, 100) == 0
fprintf('[%s] Processed %d/%d\n', char(datetime), k, length(list));
end
end