Refactor with simplifications (#19329)

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
Glenn Jocher 2025-02-20 19:43:53 +08:00 committed by GitHub
parent f4307339ad
commit 82b1ce44cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 14 additions and 19 deletions

View file

@ -79,9 +79,7 @@ class YOLOv8Seg:
predictions = self.session.run(None, {self.session.get_inputs()[0].name: processed_image}) predictions = self.session.run(None, {self.session.get_inputs()[0].name: processed_image})
# Post-process # Post-process
results = self.postprocess(im0, processed_image, predictions) return self.postprocess(im0, processed_image, predictions)
return results
def preprocess(self, image, new_shape: Union[Tuple, List] = (640, 640)): def preprocess(self, image, new_shape: Union[Tuple, List] = (640, 640)):
""" """
@ -99,8 +97,7 @@ class YOLOv8Seg:
""" """
image, _, _ = self.__resize_and_pad_image(image=image, new_shape=new_shape) image, _, _ = self.__resize_and_pad_image(image=image, new_shape=new_shape)
image = self.__reshape_image(image=image) image = self.__reshape_image(image=image)
processed_image = image[None] if len(image.shape) == 3 else image return image[None] if len(image.shape) == 3 else image
return processed_image
def __reshape_image(self, image: np.ndarray) -> np.ndarray: def __reshape_image(self, image: np.ndarray) -> np.ndarray:
""" """
@ -117,8 +114,7 @@ class YOLOv8Seg:
""" """
image = image.transpose([2, 0, 1]) image = image.transpose([2, 0, 1])
image = image[np.newaxis, ...] image = image[np.newaxis, ...]
image = np.ascontiguousarray(image).astype(np.float32) / 255 return np.ascontiguousarray(image).astype(np.float32) / 255
return image
def __resize_and_pad_image( def __resize_and_pad_image(
self, image=np.ndarray, new_shape: Union[Tuple, List] = (640, 640), color: Union[Tuple, List] = (114, 114, 114) self, image=np.ndarray, new_shape: Union[Tuple, List] = (640, 640), color: Union[Tuple, List] = (114, 114, 114)

View file

@ -309,9 +309,8 @@ class Exporter:
"WARNING ⚠️ INT8 export requires a missing 'data' arg for calibration. " "WARNING ⚠️ INT8 export requires a missing 'data' arg for calibration. "
f"Using default 'data={self.args.data}'." f"Using default 'data={self.args.data}'."
) )
if tfjs: if tfjs and (ARM64 and LINUX):
if ARM64 and LINUX: raise SystemError("TensorFlow.js export not supported on ARM64 Linux")
raise SystemError("TensorFlow.js export not supported on ARM64 Linux")
# Input # Input
im = torch.zeros(self.args.batch, 3, *self.imgsz).to(self.device) im = torch.zeros(self.args.batch, 3, *self.imgsz).to(self.device)

View file

@ -197,12 +197,13 @@ class AutoBackend(nn.Module):
import onnxruntime import onnxruntime
providers = ["CPUExecutionProvider"] providers = ["CPUExecutionProvider"]
if cuda and "CUDAExecutionProvider" in onnxruntime.get_available_providers(): if cuda:
providers.insert(0, "CUDAExecutionProvider") if "CUDAExecutionProvider" in onnxruntime.get_available_providers():
elif cuda: # Only log warning if CUDA was requested but unavailable providers.insert(0, "CUDAExecutionProvider")
LOGGER.warning("WARNING ⚠️ Failed to start ONNX Runtime with CUDA. Using CPU...") else: # Only log warning if CUDA was requested but unavailable
device = torch.device("cpu") LOGGER.warning("WARNING ⚠️ Failed to start ONNX Runtime with CUDA. Using CPU...")
cuda = False device = torch.device("cpu")
cuda = False
LOGGER.info(f"Using ONNX Runtime {providers[0]}") LOGGER.info(f"Using ONNX Runtime {providers[0]}")
if onnx: if onnx:
session = onnxruntime.InferenceSession(w, providers=providers) session = onnxruntime.InferenceSession(w, providers=providers)
@ -223,7 +224,7 @@ class AutoBackend(nn.Module):
output_names = [x.name for x in session.get_outputs()] output_names = [x.name for x in session.get_outputs()]
metadata = session.get_modelmeta().custom_metadata_map metadata = session.get_modelmeta().custom_metadata_map
dynamic = isinstance(session.get_outputs()[0].shape[0], str) dynamic = isinstance(session.get_outputs()[0].shape[0], str)
fp16 = True if "float16" in session.get_inputs()[0].type else False fp16 = "float16" in session.get_inputs()[0].type
if not dynamic: if not dynamic:
io = session.io_binding() io = session.io_binding()
bindings = [] bindings = []

View file

@ -317,8 +317,7 @@ def model_info(model, detailed=False, verbose=True, imgsz=640):
if len(m._parameters): if len(m._parameters):
for pn, p in m.named_parameters(): for pn, p in m.named_parameters():
LOGGER.info( LOGGER.info(
f"{i:>5g}{mn + '.' + pn:>40}{mt:>20}{p.requires_grad!r:>10}{p.numel():>12g}" f"{i:>5g}{f'{mn}.{pn}':>40}{mt:>20}{p.requires_grad!r:>10}{p.numel():>12g}{str(list(p.shape)):>20}{p.mean():>10.3g}{p.std():>10.3g}{str(p.dtype).replace('torch.', ''):>15}"
f"{str(list(p.shape)):>20}{p.mean():>10.3g}{p.std():>10.3g}{str(p.dtype).replace('torch.', ''):>15}"
) )
else: # layers with no learnable params else: # layers with no learnable params
LOGGER.info(f"{i:>5g}{mn:>40}{mt:>20}{False!r:>10}{0:>12g}{str([]):>20}{'-':>10}{'-':>10}{'-':>15}") LOGGER.info(f"{i:>5g}{mn:>40}{mt:>20}{False!r:>10}{0:>12g}{str([]):>20}{'-':>10}{'-':>10}{'-':>15}")