-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
308 lines (264 loc) · 11.7 KB
/
Copy pathinference.py
File metadata and controls
308 lines (264 loc) · 11.7 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
# Copyright (c) Facebook, Inc. and its affiliates.
"""
Detectron2 training script with a plain training loop.
This script reads a given config file and runs the training or evaluation.
It is an entry point that is able to train standard models in detectron2.
In order to let one script support training of many models,
this script contains logic that are specific to these built-in models and therefore
may not be suitable for your own project.
For example, your research project perhaps only needs a single "evaluator".
Therefore, we recommend you to use detectron2 as a library and take
this file as an example of how to use the library.
You may want to write your own script with your datasets and other customizations.
Compared to "train_net.py", this script supports fewer default features.
It also includes fewer abstraction, therefore is easier to add custom logic.
"""
import logging
import pickle
import os
import yaml
import time
import datetime
import itertools
from collections import OrderedDict
import torch
import torch.nn as nn
from tqdm import tqdm
from yacs.config import CfgNode as CN
from torch.nn.parallel import DistributedDataParallel
from contextlib import ExitStack, contextmanager
from pathlib import Path
from matplotlib import pyplot as plt
import detectron2.utils.comm as comm
import detectron2.data.transforms as T
from detectron2.checkpoint import DetectionCheckpointer, PeriodicCheckpointer
from detectron2.config import get_cfg
from detectron2.data import (
MetadataCatalog,
DatasetMapper,
build_detection_test_loader,
build_detection_train_loader,
)
from detectron2.engine import default_argument_parser, default_setup, default_writers, launch
from detectron2.evaluation import (
CityscapesInstanceEvaluator,
CityscapesSemSegEvaluator,
COCOEvaluator,
COCOPanopticEvaluator,
DatasetEvaluators,
LVISEvaluator,
PascalVOCDetectionEvaluator,
SemSegEvaluator,
inference_on_dataset,
print_csv_format,
)
from detectron2.modeling import build_model
from detectron2.solver import build_lr_scheduler, build_optimizer
from detectron2.utils.events import EventStorage
from dataset import setup_dataset
from model_zoo.RetinaNetPoint import RetinaNetPoint
from evaluation.COCOPointEvaluator import COCOPointEvaluator
from detectron2.utils.comm import get_world_size
from detectron2.utils.logger import log_every_n_seconds
torch.multiprocessing.set_sharing_strategy('file_system')
logger = logging.getLogger("detectron2")
import pdb
@contextmanager
def inference_context(model):
"""
A context where the model is temporarily changed to eval mode,
and restored to previous mode afterwards.
Args:
model: a torch Module
"""
training_mode = model.training
model.eval()
yield
model.train(training_mode)
def plot_preds(preds, save_dir):
for pred in tqdm(preds):
image = pred['image']
xs = pred['points'][:, 0]
ys = pred['points'][:, 1]
save_path = os.path.join(save_dir, "images", pred['file_name'].split('/')[-1])
# Create the plot
fig, ax = plt.subplots()
ax.imshow(image.permute(1, 2, 0))
ax.plot(xs, ys, 'rx')
fig.savefig(save_path, dpi=150)
plt.close(fig)
del fig, ax
def record_preds(results, preds, data, optimal_confidence, dataset_name):
result = {k: v for k, v in data.items()}
# Remove predictions with confidence level below optimal confidence
if len(preds) > 0:
# Get the index of the first element in scores which has score below optimal confidence
idx_lim = 0
while idx_lim < preds.scores.shape[0] and preds.scores[idx_lim] > optimal_confidence:
idx_lim += 1
valid_preds = preds[:idx_lim]
else:
valid_preds = preds
result['points'] = valid_preds.pred_boxes.get_centers().to('cpu')
result['scores'] = valid_preds.scores.to('cpu')
# Append to the results dictionary
results[dataset_name].append(result)
def do_inference(cfg, inference_cfg, model, resume=False, distributed=False):
logger = logging.getLogger("detectron2")
model.eval()
# Initialize results dict
results = {dataset_name: [] for dataset_name in cfg.DATASETS.TEST}
# Detectron2 style preparation
num_devices = get_world_size()
start_time = time.perf_counter()
total_data_time = 0
total_compute_time = 0
total_record_time = 0
# compared to "train_net.py", we do not support accurate timing and
# precise BN here, because they are not trivial to implement in a small training loop
for dataset_name in cfg.DATASETS.TEST:
data_loader = build_detection_test_loader(cfg, dataset_name)
total = len(data_loader) # inference data loader must have a fixed length
num_warmup = min(5, total - 1)
logger.info("Start inference on {} batches of dataset {}".format(len(data_loader), dataset_name))
with ExitStack() as stack:
if isinstance(model, nn.Module):
stack.enter_context(inference_context(model))
stack.enter_context(torch.no_grad())
with torch.no_grad():
# Run inference
start_data_time = time.perf_counter()
for idx, data in enumerate(data_loader):
total_data_time += time.perf_counter() - start_data_time
if idx == num_warmup:
start_time = time.perf_counter()
total_data_time = 0
total_compute_time = 0
total_record_time = 0
# Run inference
start_compute_time = time.perf_counter()
preds = model(data)
if torch.cuda.is_available():
torch.cuda.synchronize()
total_compute_time += time.perf_counter() - start_compute_time
# Record the predictions
start_record_time = time.perf_counter()
record_preds(results, preds[0]['instances'], data[0], inference_cfg.OPTIMAL_CONFIDENCE, dataset_name)
total_record_time += time.perf_counter() - start_record_time
iters_after_start = idx + 1 - num_warmup * int(idx >= num_warmup)
data_seconds_per_iter = total_data_time / iters_after_start
compute_seconds_per_iter = total_compute_time / iters_after_start
record_seconds_per_iter = total_record_time / iters_after_start
total_seconds_per_iter = (time.perf_counter() - start_time) / iters_after_start
if idx >= num_warmup * 2 or compute_seconds_per_iter > 5:
eta = datetime.timedelta(seconds=int(total_seconds_per_iter * (total - idx - 1)))
log_every_n_seconds(
logging.INFO,
(
f"Inference done {idx + 1}/{total}. "
f"Dataloading: {data_seconds_per_iter:.4f} s/iter. "
f"Inference: {compute_seconds_per_iter:.4f} s/iter. "
f"Record: {record_seconds_per_iter:.4f} s/iter. "
f"Total: {total_seconds_per_iter:.4f} s/iter. "
f"ETA={eta}"
),
n=5,
name="detectron2"
)
start_data_time = time.perf_counter()
# Measure the time only for this worker (before the synchronization barrier)
total_time = time.perf_counter() - start_time
total_time_str = str(datetime.timedelta(seconds=total_time))
# NOTE this format is parsed by grep
logger.info(
"Total inference time: {} ({:.6f} s / iter per device, on {} devices)".format(
total_time_str, total_time / (total - num_warmup), num_devices
)
)
total_compute_time_str = str(datetime.timedelta(seconds=int(total_compute_time)))
logger.info(
"Total inference pure compute time: {} ({:.6f} s / iter per device, on {} devices)".format(
total_compute_time_str, total_compute_time / (total - num_warmup), num_devices
)
)
# Synchronize distributed processes
if distributed:
comm.synchronize()
# Create save directories
results_dir = os.path.join(inference_cfg.RESULTS_DIR)
Path(results_dir).mkdir(parents=True, exist_ok=True)
for dataset_name in cfg.DATASETS.TEST:
Path(os.path.join(results_dir, dataset_name)).mkdir(parents=True, exist_ok=True)
# Loop through all predictions and save them as PKL files
for dataset_name in cfg.DATASETS.TEST:
logger.info(f"Saving predictions for {dataset_name}")
results_dataset = results[dataset_name]
save_dir = os.path.join(results_dir, dataset_name)
Path(os.path.join(save_dir, "annotations")).mkdir(parents=True, exist_ok=True)
Path(os.path.join(save_dir, "images")).mkdir(parents=True, exist_ok=True)
for pred in results_dataset:
encoding = pred['file_name'].split('/')[-1].split('.')[0]
save_path = os.path.join(save_dir, "annotations", encoding + ".pkl")
save_data = {
'points': pred['points'],
'scores': pred['scores']
}
with open(save_path, 'wb') as f:
pickle.dump(save_data, f, protocol=pickle.HIGHEST_PROTOCOL)
if torch.cuda.is_available():
torch.cuda.synchronize()
# Plot predictions
if inference_cfg.PLOT_PREDS:
logger.info(f"Plotting predictions for {dataset_name}")
plot_preds(results_dataset, save_dir)
def setup_inference(args):
assert "INFERENCE" in args.opts, "No inference config file supplied!"
inference_cfg_idx = args.opts.index("INFERENCE")
inference_cfg_path = args.opts[inference_cfg_idx + 1]
with open(inference_cfg_path) as f:
inference_cfg = CN(yaml.load(f, Loader=yaml.UnsafeLoader))
# Remove custom parameters from args
del args.opts[inference_cfg_idx]
del args.opts[inference_cfg_idx]
return inference_cfg
def setup(args):
"""
Create configs and perform basic setups.
"""
# Extract custom configs
inference_cfg = setup_inference(args)
cfg = get_cfg()
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
default_setup(
cfg, args
) # if you don't like any of the default setup, write your own setup code
return cfg, inference_cfg
def main(args):
cfg, inference_cfg = setup(args)
# Setup the dataset
datasets_dir = "/var/storage/myeghiaz/Detection" # all datasets are stored in this directory
setup_dataset(cfg, datasets_dir)
model = build_model(cfg)
DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load(
cfg.MODEL.WEIGHTS, resume=True
)
distributed = comm.get_world_size() > 1
if distributed:
model = DistributedDataParallel(
model, device_ids=[comm.get_local_rank()], broadcast_buffers=False
)
do_inference(cfg, inference_cfg, model, resume=True, distributed=distributed)
if __name__ == "__main__":
args = default_argument_parser().parse_args()
print("Command Line Args:", args)
launch(
main,
args.num_gpus,
num_machines=args.num_machines,
machine_rank=args.machine_rank,
dist_url=args.dist_url,
args=(args,),
)