|
class DetectMultiBackend(nn.Module): |
|
|
|
def __init__(self, weights='yolo.pt', device=torch.device('cpu'), dnn=False, data=None, fp16=False, fuse=True): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from models.experimental import attempt_download, attempt_load |
|
|
|
super().__init__() |
|
w = str(weights[0] if isinstance(weights, list) else weights) |
|
pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle, triton = self._model_type(w) |
|
fp16 &= pt or jit or onnx or engine |
|
nhwc = coreml or saved_model or pb or tflite or edgetpu |
|
stride = 32 |
|
cuda = torch.cuda.is_available() and device.type != 'cpu' |
|
if not (pt or triton): |
|
w = attempt_download(w) |
|
|
|
if pt: |
|
model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse) |
|
stride = max(int(model.stride.max()), 32) |
|
names = model.module.names if hasattr(model, 'module') else model.names |
|
model.half() if fp16 else model.float() |
|
self.model = model |
|
elif jit: |
|
LOGGER.info(f'Loading {w} for TorchScript inference...') |
|
extra_files = {'config.txt': ''} |
|
model = torch.jit.load(w, _extra_files=extra_files, map_location=device) |
|
model.half() if fp16 else model.float() |
|
if extra_files['config.txt']: |
|
d = json.loads(extra_files['config.txt'], |
|
object_hook=lambda d: {int(k) if k.isdigit() else k: v |
|
for k, v in d.items()}) |
|
stride, names = int(d['stride']), d['names'] |
|
elif dnn: |
|
LOGGER.info(f'Loading {w} for ONNX OpenCV DNN inference...') |
|
check_requirements('opencv-python>=4.5.4') |
|
net = cv2.dnn.readNetFromONNX(w) |
|
elif onnx: |
|
LOGGER.info(f'Loading {w} for ONNX Runtime inference...') |
|
check_requirements(('onnx', 'onnxruntime-gpu' if cuda else 'onnxruntime')) |
|
import onnxruntime |
|
providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider'] |
|
session = onnxruntime.InferenceSession(w, providers=providers) |
|
output_names = [x.name for x in session.get_outputs()] |
|
meta = session.get_modelmeta().custom_metadata_map |
|
if 'stride' in meta: |
|
stride, names = int(meta['stride']), eval(meta['names']) |
|
elif xml: |
|
LOGGER.info(f'Loading {w} for OpenVINO inference...') |
|
check_requirements('openvino') |
|
from openvino.runtime import Core, Layout, get_batch |
|
ie = Core() |
|
if not Path(w).is_file(): |
|
w = next(Path(w).glob('*.xml')) |
|
network = ie.read_model(model=w, weights=Path(w).with_suffix('.bin')) |
|
if network.get_parameters()[0].get_layout().empty: |
|
network.get_parameters()[0].set_layout(Layout("NCHW")) |
|
batch_dim = get_batch(network) |
|
if batch_dim.is_static: |
|
batch_size = batch_dim.get_length() |
|
executable_network = ie.compile_model(network, device_name="CPU") |
|
stride, names = self._load_metadata(Path(w).with_suffix('.yaml')) |
|
elif engine: |
|
LOGGER.info(f'Loading {w} for TensorRT inference...') |
|
import tensorrt as trt |
|
check_version(trt.__version__, '7.0.0', hard=True) |
|
if device.type == 'cpu': |
|
device = torch.device('cuda:0') |
|
Binding = namedtuple('Binding', ('name', 'dtype', 'shape', 'data', 'ptr')) |
|
logger = trt.Logger(trt.Logger.INFO) |
|
with open(w, 'rb') as f, trt.Runtime(logger) as runtime: |
|
model = runtime.deserialize_cuda_engine(f.read()) |
|
context = model.create_execution_context() |
|
bindings = OrderedDict() |
|
output_names = [] |
|
fp16 = False |
|
dynamic = False |
|
for i in range(model.num_bindings): |
|
name = model.get_binding_name(i) |
|
dtype = trt.nptype(model.get_binding_dtype(i)) |
|
if model.binding_is_input(i): |
|
if -1 in tuple(model.get_binding_shape(i)): |
|
dynamic = True |
|
context.set_binding_shape(i, tuple(model.get_profile_shape(0, i)[2])) |
|
if dtype == np.float16: |
|
fp16 = True |
|
else: |
|
output_names.append(name) |
|
shape = tuple(context.get_binding_shape(i)) |
|
im = torch.from_numpy(np.empty(shape, dtype=dtype)).to(device) |
|
bindings[name] = Binding(name, dtype, shape, im, int(im.data_ptr())) |
|
binding_addrs = OrderedDict((n, d.ptr) for n, d in bindings.items()) |
|
batch_size = bindings['images'].shape[0] |
|
elif coreml: |
|
LOGGER.info(f'Loading {w} for CoreML inference...') |
|
import coremltools as ct |
|
model = ct.models.MLModel(w) |
|
elif saved_model: |
|
LOGGER.info(f'Loading {w} for TensorFlow SavedModel inference...') |
|
import tensorflow as tf |
|
keras = False |
|
model = tf.keras.models.load_model(w) if keras else tf.saved_model.load(w) |
|
elif pb: |
|
LOGGER.info(f'Loading {w} for TensorFlow GraphDef inference...') |
|
import tensorflow as tf |
|
|
|
def wrap_frozen_graph(gd, inputs, outputs): |
|
x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=""), []) |
|
ge = x.graph.as_graph_element |
|
return x.prune(tf.nest.map_structure(ge, inputs), tf.nest.map_structure(ge, outputs)) |
|
|
|
def gd_outputs(gd): |
|
name_list, input_list = [], [] |
|
for node in gd.node: |
|
name_list.append(node.name) |
|
input_list.extend(node.input) |
|
return sorted(f'{x}:0' for x in list(set(name_list) - set(input_list)) if not x.startswith('NoOp')) |
|
|
|
gd = tf.Graph().as_graph_def() |
|
with open(w, 'rb') as f: |
|
gd.ParseFromString(f.read()) |
|
frozen_func = wrap_frozen_graph(gd, inputs="x:0", outputs=gd_outputs(gd)) |
|
elif tflite or edgetpu: |
|
try: |
|
from tflite_runtime.interpreter import Interpreter, load_delegate |
|
except ImportError: |
|
import tensorflow as tf |
|
Interpreter, load_delegate = tf.lite.Interpreter, tf.lite.experimental.load_delegate, |
|
if edgetpu: |
|
LOGGER.info(f'Loading {w} for TensorFlow Lite Edge TPU inference...') |
|
delegate = { |
|
'Linux': 'libedgetpu.so.1', |
|
'Darwin': 'libedgetpu.1.dylib', |
|
'Windows': 'edgetpu.dll'}[platform.system()] |
|
interpreter = Interpreter(model_path=w, experimental_delegates=[load_delegate(delegate)]) |
|
else: |
|
LOGGER.info(f'Loading {w} for TensorFlow Lite inference...') |
|
interpreter = Interpreter(model_path=w) |
|
interpreter.allocate_tensors() |
|
input_details = interpreter.get_input_details() |
|
output_details = interpreter.get_output_details() |
|
|
|
with contextlib.suppress(zipfile.BadZipFile): |
|
with zipfile.ZipFile(w, "r") as model: |
|
meta_file = model.namelist()[0] |
|
meta = ast.literal_eval(model.read(meta_file).decode("utf-8")) |
|
stride, names = int(meta['stride']), meta['names'] |
|
elif tfjs: |
|
raise NotImplementedError('ERROR: YOLO TF.js inference is not supported') |
|
elif paddle: |
|
LOGGER.info(f'Loading {w} for PaddlePaddle inference...') |
|
check_requirements('paddlepaddle-gpu' if cuda else 'paddlepaddle') |
|
import paddle.inference as pdi |
|
if not Path(w).is_file(): |
|
w = next(Path(w).rglob('*.pdmodel')) |
|
weights = Path(w).with_suffix('.pdiparams') |
|
config = pdi.Config(str(w), str(weights)) |
|
if cuda: |
|
config.enable_use_gpu(memory_pool_init_size_mb=2048, device_id=0) |
|
predictor = pdi.create_predictor(config) |
|
input_handle = predictor.get_input_handle(predictor.get_input_names()[0]) |
|
output_names = predictor.get_output_names() |
|
elif triton: |
|
LOGGER.info(f'Using {w} as Triton Inference Server...') |
|
check_requirements('tritonclient[all]') |
|
from utils.triton import TritonRemoteModel |
|
model = TritonRemoteModel(url=w) |
|
nhwc = model.runtime.startswith("tensorflow") |
|
else: |
|
raise NotImplementedError(f'ERROR: {w} is not a supported format') |
|
|
|
|
|
if 'names' not in locals(): |
|
names = yaml_load(data)['names'] if data else {i: f'class{i}' for i in range(999)} |
|
if names[0] == 'n01440764' and len(names) == 1000: |
|
names = yaml_load(ROOT / 'data/ImageNet.yaml')['names'] |
|
|
|
self.__dict__.update(locals()) |
|
|
|
def forward(self, im, augment=False, visualize=False): |
|
|
|
b, ch, h, w = im.shape |
|
if self.fp16 and im.dtype != torch.float16: |
|
im = im.half() |
|
if self.nhwc: |
|
im = im.permute(0, 2, 3, 1) |
|
|
|
if self.pt: |
|
y = self.model(im, augment=augment, visualize=visualize) if augment or visualize else self.model(im) |
|
elif self.jit: |
|
y = self.model(im) |
|
elif self.dnn: |
|
im = im.cpu().numpy() |
|
self.net.setInput(im) |
|
y = self.net.forward() |
|
elif self.onnx: |
|
im = im.cpu().numpy() |
|
y = self.session.run(self.output_names, {self.session.get_inputs()[0].name: im}) |
|
elif self.xml: |
|
im = im.cpu().numpy() |
|
y = list(self.executable_network([im]).values()) |
|
elif self.engine: |
|
if self.dynamic and im.shape != self.bindings['images'].shape: |
|
i = self.model.get_binding_index('images') |
|
self.context.set_binding_shape(i, im.shape) |
|
self.bindings['images'] = self.bindings['images']._replace(shape=im.shape) |
|
for name in self.output_names: |
|
i = self.model.get_binding_index(name) |
|
self.bindings[name].data.resize_(tuple(self.context.get_binding_shape(i))) |
|
s = self.bindings['images'].shape |
|
assert im.shape == s, f"input size {im.shape} {'>' if self.dynamic else 'not equal to'} max model size {s}" |
|
self.binding_addrs['images'] = int(im.data_ptr()) |
|
self.context.execute_v2(list(self.binding_addrs.values())) |
|
y = [self.bindings[x].data for x in sorted(self.output_names)] |
|
elif self.coreml: |
|
im = im.cpu().numpy() |
|
im = Image.fromarray((im[0] * 255).astype('uint8')) |
|
|
|
y = self.model.predict({'image': im}) |
|
if 'confidence' in y: |
|
box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]]) |
|
conf, cls = y['confidence'].max(1), y['confidence'].argmax(1).astype(np.float) |
|
y = np.concatenate((box, conf.reshape(-1, 1), cls.reshape(-1, 1)), 1) |
|
else: |
|
y = list(reversed(y.values())) |
|
elif self.paddle: |
|
im = im.cpu().numpy().astype(np.float32) |
|
self.input_handle.copy_from_cpu(im) |
|
self.predictor.run() |
|
y = [self.predictor.get_output_handle(x).copy_to_cpu() for x in self.output_names] |
|
elif self.triton: |
|
y = self.model(im) |
|
else: |
|
im = im.cpu().numpy() |
|
if self.saved_model: |
|
y = self.model(im, training=False) if self.keras else self.model(im) |
|
elif self.pb: |
|
y = self.frozen_func(x=self.tf.constant(im)) |
|
else: |
|
input = self.input_details[0] |
|
int8 = input['dtype'] == np.uint8 |
|
if int8: |
|
scale, zero_point = input['quantization'] |
|
im = (im / scale + zero_point).astype(np.uint8) |
|
self.interpreter.set_tensor(input['index'], im) |
|
self.interpreter.invoke() |
|
y = [] |
|
for output in self.output_details: |
|
x = self.interpreter.get_tensor(output['index']) |
|
if int8: |
|
scale, zero_point = output['quantization'] |
|
x = (x.astype(np.float32) - zero_point) * scale |
|
y.append(x) |
|
y = [x if isinstance(x, np.ndarray) else x.numpy() for x in y] |
|
y[0][..., :4] *= [w, h, w, h] |
|
|
|
if isinstance(y, (list, tuple)): |
|
return self.from_numpy(y[0]) if len(y) == 1 else [self.from_numpy(x) for x in y] |
|
else: |
|
return self.from_numpy(y) |
|
|
|
def from_numpy(self, x): |
|
return torch.from_numpy(x).to(self.device) if isinstance(x, np.ndarray) else x |
|
|
|
def warmup(self, imgsz=(1, 3, 640, 640)): |
|
|
|
warmup_types = self.pt, self.jit, self.onnx, self.engine, self.saved_model, self.pb, self.triton |
|
if any(warmup_types) and (self.device.type != 'cpu' or self.triton): |
|
im = torch.empty(*imgsz, dtype=torch.half if self.fp16 else torch.float, device=self.device) |
|
for _ in range(2 if self.jit else 1): |
|
self.forward(im) |
|
|
|
@staticmethod |
|
def _model_type(p='path/to/model.pt'): |
|
|
|
|
|
from export import export_formats |
|
from utils.downloads import is_url |
|
sf = list(export_formats().Suffix) |
|
if not is_url(p, check=False): |
|
check_suffix(p, sf) |
|
url = urlparse(p) |
|
types = [s in Path(p).name for s in sf] |
|
types[8] &= not types[9] |
|
triton = not any(types) and all([any(s in url.scheme for s in ["http", "grpc"]), url.netloc]) |
|
return types + [triton] |
|
|
|
@staticmethod |
|
def _load_metadata(f=Path('path/to/meta.yaml')): |
|
|
|
if f.exists(): |
|
d = yaml_load(f) |
|
return d['stride'], d['names'] |
|
return None, None |
|
|