Add model format (ONNX/Engine) & input size detection (based on file header, not on filename) for RTMO
Browse files- rtmo_demo.py +1 -5
- rtmo_demo_batch.py +1 -2
- rtmo_gpu.py +42 -4
rtmo_demo.py
CHANGED
|
@@ -20,11 +20,7 @@ if __name__ == "__main__":
|
|
| 20 |
|
| 21 |
model = args.model_path # 'rtmo-s_8xb32-600e_body7-640x640.onnx'
|
| 22 |
|
| 23 |
-
|
| 24 |
-
model_input_size = (416,416) if 'rtmo-t' in model.lower() and not args.yolo_nas_pose else (640,640)
|
| 25 |
-
|
| 26 |
-
body = RTMO_GPU(model=model,
|
| 27 |
-
model_input_size=model_input_size, is_yolo_nas_pose=args.yolo_nas_pose)
|
| 28 |
|
| 29 |
for mp4_path in Path(args.path).glob('*'):
|
| 30 |
|
|
|
|
| 20 |
|
| 21 |
model = args.model_path # 'rtmo-s_8xb32-600e_body7-640x640.onnx'
|
| 22 |
|
| 23 |
+
body = RTMO_GPU(model=model, is_yolo_nas_pose=args.yolo_nas_pose)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
for mp4_path in Path(args.path).glob('*'):
|
| 26 |
|
rtmo_demo_batch.py
CHANGED
|
@@ -74,10 +74,9 @@ if __name__ == "__main__":
|
|
| 74 |
args = parser.parse_args()
|
| 75 |
|
| 76 |
onnx_model = args.model_path # Example: 'rtmo-s_8xb32-600e_body7-640x640.onnx'
|
| 77 |
-
model_input_size = (416, 416) if 'rtmo-t' in onnx_model.lower() else (640, 640)
|
| 78 |
|
| 79 |
# Instantiate the RTMO_GPU_Batch instead of RTMO_GPU
|
| 80 |
-
body_estimator = RTMO_GPU_Batch(
|
| 81 |
|
| 82 |
for mp4_path in Path(args.path).glob('*'):
|
| 83 |
process_video(str(mp4_path), body_estimator, args.batch_size)
|
|
|
|
| 74 |
args = parser.parse_args()
|
| 75 |
|
| 76 |
onnx_model = args.model_path # Example: 'rtmo-s_8xb32-600e_body7-640x640.onnx'
|
|
|
|
| 77 |
|
| 78 |
# Instantiate the RTMO_GPU_Batch instead of RTMO_GPU
|
| 79 |
+
body_estimator = RTMO_GPU_Batch(model=onnx_model)
|
| 80 |
|
| 81 |
for mp4_path in Path(args.path).glob('*'):
|
| 82 |
process_video(str(mp4_path), body_estimator, args.batch_size)
|
rtmo_gpu.py
CHANGED
|
@@ -237,6 +237,43 @@ def draw_skeleton(img,
|
|
| 237 |
raise NotImplementedError
|
| 238 |
return img
|
| 239 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
class RTMO_GPU(object):
|
| 241 |
|
| 242 |
def preprocess(self, img: np.ndarray):
|
|
@@ -388,7 +425,6 @@ class RTMO_GPU(object):
|
|
| 388 |
|
| 389 |
def __init__(self,
|
| 390 |
model: str = None,
|
| 391 |
-
model_input_size: tuple = (640, 640),
|
| 392 |
mean: tuple = None,
|
| 393 |
std: tuple = None,
|
| 394 |
device: str = 'cuda',
|
|
@@ -399,13 +435,15 @@ class RTMO_GPU(object):
|
|
| 399 |
raise FileNotFoundError(f"The specified ONNX model file was not found: {model}")
|
| 400 |
|
| 401 |
self.model = model
|
| 402 |
-
if model
|
| 403 |
self.model_format = 'onnx'
|
| 404 |
-
|
|
|
|
| 405 |
self.model_format = 'engine'
|
| 406 |
from polygraphy.backend.common import BytesFromPath
|
| 407 |
from polygraphy.backend.trt import EngineFromBytes, TrtRunner, load_plugins
|
| 408 |
load_plugins(plugins=['libmmdeploy_tensorrt_ops.so'])
|
|
|
|
| 409 |
else:
|
| 410 |
raise TypeError("Your model is neither ONNX nor Engine !")
|
| 411 |
|
|
@@ -431,7 +469,7 @@ class RTMO_GPU(object):
|
|
| 431 |
engine = EngineFromBytes(BytesFromPath(model))
|
| 432 |
self.session = TrtRunner(engine)
|
| 433 |
|
| 434 |
-
self.model_input_size =
|
| 435 |
self.mean = mean
|
| 436 |
self.std = std
|
| 437 |
self.device = device
|
|
|
|
| 237 |
raise NotImplementedError
|
| 238 |
return img
|
| 239 |
|
| 240 |
+
def is_onnx_model(model_path):
|
| 241 |
+
try:
|
| 242 |
+
import onnx
|
| 243 |
+
onnx_model = onnx.load(model_path)
|
| 244 |
+
onnx.checker.check_model(onnx_model)
|
| 245 |
+
return True
|
| 246 |
+
except Exception as e:
|
| 247 |
+
return False
|
| 248 |
+
|
| 249 |
+
def is_trt_engine(model_path):
|
| 250 |
+
try:
|
| 251 |
+
from polygraphy.backend.common import BytesFromPath
|
| 252 |
+
from polygraphy.backend.trt import EngineFromBytes
|
| 253 |
+
engine = EngineFromBytes(BytesFromPath(model_path))
|
| 254 |
+
return engine is not None
|
| 255 |
+
except Exception:
|
| 256 |
+
return False
|
| 257 |
+
|
| 258 |
+
def get_onnx_input_shapes(model_path):
|
| 259 |
+
from polygraphy.backend.onnx.loader import OnnxFromPath
|
| 260 |
+
from polygraphy.backend.onnx import infer_shapes
|
| 261 |
+
model = OnnxFromPath(model_path)()
|
| 262 |
+
model = infer_shapes(model)
|
| 263 |
+
input_shapes = {inp.name: inp.type.tensor_type.shape for inp in model.graph.input}
|
| 264 |
+
return {name: [dim.dim_value if dim.dim_value > 0 else 'Dynamic' for dim in shape_proto.dim]
|
| 265 |
+
for name, shape_proto in input_shapes.items()}
|
| 266 |
+
|
| 267 |
+
def get_trt_input_shapes(model_path):
|
| 268 |
+
input_shapes = {}
|
| 269 |
+
import tensorrt as trt
|
| 270 |
+
with open(model_path, "rb") as f, trt.Runtime(trt.Logger(trt.Logger.WARNING)) as runtime:
|
| 271 |
+
engine = runtime.deserialize_cuda_engine(f.read())
|
| 272 |
+
for binding in engine:
|
| 273 |
+
if engine.binding_is_input(binding):
|
| 274 |
+
input_shapes[binding] = engine.get_binding_shape(binding)
|
| 275 |
+
return input_shapes
|
| 276 |
+
|
| 277 |
class RTMO_GPU(object):
|
| 278 |
|
| 279 |
def preprocess(self, img: np.ndarray):
|
|
|
|
| 425 |
|
| 426 |
def __init__(self,
|
| 427 |
model: str = None,
|
|
|
|
| 428 |
mean: tuple = None,
|
| 429 |
std: tuple = None,
|
| 430 |
device: str = 'cuda',
|
|
|
|
| 435 |
raise FileNotFoundError(f"The specified ONNX model file was not found: {model}")
|
| 436 |
|
| 437 |
self.model = model
|
| 438 |
+
if is_onnx_model(model):
|
| 439 |
self.model_format = 'onnx'
|
| 440 |
+
self.input_shape = get_onnx_input_shapes(self.model)['input']
|
| 441 |
+
elif is_trt_engine(model):
|
| 442 |
self.model_format = 'engine'
|
| 443 |
from polygraphy.backend.common import BytesFromPath
|
| 444 |
from polygraphy.backend.trt import EngineFromBytes, TrtRunner, load_plugins
|
| 445 |
load_plugins(plugins=['libmmdeploy_tensorrt_ops.so'])
|
| 446 |
+
self.input_shape = get_trt_input_shapes(self.model)['input']
|
| 447 |
else:
|
| 448 |
raise TypeError("Your model is neither ONNX nor Engine !")
|
| 449 |
|
|
|
|
| 469 |
engine = EngineFromBytes(BytesFromPath(model))
|
| 470 |
self.session = TrtRunner(engine)
|
| 471 |
|
| 472 |
+
self.model_input_size = self.input_shape[2:4] # B, C, H, W,
|
| 473 |
self.mean = mean
|
| 474 |
self.std = std
|
| 475 |
self.device = device
|