-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
150 lines (121 loc) · 5.21 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
import argparse
import os
from test import create_test_data_loader
from typing import Dict
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_cv2
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.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)
classes = model.CLASSES
def visualize_single_image(image_name, image, output):
# plot bounding boxes on image
image = image.numpy().transpose(1, 2, 0)
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
image = plot_bounding_boxes_on_image_cv2(
image=image,
boxes=output["boxes"],
labels=output["labels"],
scores=output.get("scores", None),
classes=classes,
show_conf=args.show_conf,
font_scale=args.font_scale,
box_thick=args.box_thick,
fill_alpha=args.fill_alpha,
text_box_color=args.text_box_color,
text_font_color=args.text_font_color,
text_alpha=args.text_alpha,
)
cv2.imwrite(os.path.join(args.show_dir, os.path.basename(image_name)), image)
# 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 = lambda x: visualize_single_image(**x[0])
[None for _ in tqdm(data_loader)]
if __name__ == "__main__":
inference()