-
Notifications
You must be signed in to change notification settings - Fork 12
/
inference.py
165 lines (135 loc) · 5.63 KB
/
inference.py
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
import argparse
import os
from functools import partial
from test import create_test_data_loader
from typing import Dict, List, Tuple
import accelerate
import cv2
import numpy as np
import torch
import torch.utils.data as data
from accelerate import Accelerator
from PIL import Image
from tqdm import tqdm
from util.lazy_load import Config
from util.logger import setup_logger
from util.utils import load_checkpoint, load_state_dict
from util.visualize import plot_bounding_boxes_on_image
def is_image(file_path):
try:
img = Image.open(file_path)
img.close()
return True
except:
return False
def parse_args():
parser = argparse.ArgumentParser(description="Inference a detector")
# dataset parameters
parser.add_argument("--image-dir", type=str, required=True)
parser.add_argument("--workers", type=int, default=2)
# model parameters
parser.add_argument("--model-config", type=str, required=True)
parser.add_argument("--checkpoint", type=str, required=True)
# visualization parameters
parser.add_argument("--show-dir", type=str, default=None)
parser.add_argument("--show-conf", type=float, default=0.5)
# plot parameters
parser.add_argument("--font-scale", type=float, default=1.0)
parser.add_argument("--box-thick", type=int, default=1)
parser.add_argument("--fill-alpha", type=float, default=0.2)
parser.add_argument("--text-box-color", type=int, nargs="+", default=(255, 255, 255))
parser.add_argument("--text-font-color", type=int, nargs="+", default=None)
parser.add_argument("--text-alpha", type=float, default=1.0)
# engine parameters
parser.add_argument("--seed", type=int, default=42)
args = parser.parse_args()
return args
class InferenceDataset(data.Dataset):
def __init__(self, root):
self.images = [os.path.join(root, img) for img in os.listdir(root)]
self.images = [img for img in self.images if is_image(img)]
assert len(self.images) > 0, "No images found"
def __len__(self):
return len(self.images)
def __getitem__(self, index):
cv2.setNumThreads(0)
cv2.ocl.setUseOpenCL(False)
image = cv2.imdecode(np.fromfile(self.images[index], dtype=np.uint8), -1)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB).transpose(2, 0, 1)
return torch.tensor(image)
def inference():
args = parse_args()
# set fixed seed and deterministic_algorithms
accelerator = Accelerator()
accelerate.utils.set_seed(args.seed, device_specific=False)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
# deterministic in low version pytorch leads to RuntimeError
# torch.use_deterministic_algorithms(True, warn_only=True)
# setup logger
for logger_name in ["py.warnings", "accelerate", os.path.basename(os.getcwd())]:
setup_logger(distributed_rank=accelerator.local_process_index, name=logger_name)
dataset = InferenceDataset(args.image_dir)
data_loader = create_test_data_loader(
dataset, accelerator=accelerator, batch_size=1, num_workers=args.workers
)
# get inference results from model output
model = Config(args.model_config).model.eval()
checkpoint = load_checkpoint(args.checkpoint)
if isinstance(checkpoint, Dict) and "model" in checkpoint:
checkpoint = checkpoint["model"]
load_state_dict(model, checkpoint)
model = accelerator.prepare_model(model)
with torch.inference_mode():
predictions = []
for index, images in enumerate(tqdm(data_loader)):
prediction = model(images)[0]
# change torch.Tensor to CPU
for key in prediction:
prediction[key] = prediction[key].to("cpu", non_blocking=True)
image_name = data_loader.dataset.images[index]
image = images[0].to("cpu", non_blocking=True)
prediction = {"image_name": image_name, "image": image, "output": prediction}
predictions.append(prediction)
# save visualization results
if args.show_dir:
os.makedirs(args.show_dir, exist_ok=True)
# create a dummy dataset for visualization with multi-workers
data_loader = create_test_data_loader(
predictions, accelerator=accelerator, batch_size=1, num_workers=args.workers
)
data_loader.collate_fn = partial(_visualize_batch_for_infer, classes=model.CLASSES, **vars(args))
[None for _ in tqdm(data_loader)]
def _visualize_batch_for_infer(
batch: Tuple[Dict],
classes: List[str],
show_conf: float = 0.0,
show_dir: str = None,
font_scale: float = 1.0,
box_thick: int = 3,
fill_alpha: float = 0.2,
text_box_color: Tuple[int] = (255, 255, 255),
text_font_color: Tuple[int] = None,
text_alpha: float = 0.5,
**kwargs, # Not useful
):
image_name, image, output = batch[0].values()
# plot bounding boxes on image
image = image.numpy().transpose(1, 2, 0)
image = plot_bounding_boxes_on_image(
image=image,
boxes=output["boxes"],
labels=output["labels"],
scores=output.get("scores", None),
classes=classes,
show_conf=show_conf,
font_scale=font_scale,
box_thick=box_thick,
fill_alpha=fill_alpha,
text_box_color=text_box_color,
text_font_color=text_font_color,
text_alpha=text_alpha,
)
cv2.imwrite(os.path.join(show_dir, os.path.basename(image_name)), image[:, :, ::-1])
if __name__ == "__main__":
inference()