mirror of
https://github.com/ultralytics/ultralytics.git
synced 2025-09-15 15:48:41 +08:00
Refactor with simplifications (#19329)
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
parent
f4307339ad
commit
82b1ce44cc
@ -79,9 +79,7 @@ class YOLOv8Seg:
|
||||
predictions = self.session.run(None, {self.session.get_inputs()[0].name: processed_image})
|
||||
|
||||
# Post-process
|
||||
results = self.postprocess(im0, processed_image, predictions)
|
||||
|
||||
return results
|
||||
return self.postprocess(im0, processed_image, predictions)
|
||||
|
||||
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.__reshape_image(image=image)
|
||||
processed_image = image[None] if len(image.shape) == 3 else image
|
||||
return processed_image
|
||||
return image[None] if len(image.shape) == 3 else image
|
||||
|
||||
def __reshape_image(self, image: np.ndarray) -> np.ndarray:
|
||||
"""
|
||||
@ -117,8 +114,7 @@ class YOLOv8Seg:
|
||||
"""
|
||||
image = image.transpose([2, 0, 1])
|
||||
image = image[np.newaxis, ...]
|
||||
image = np.ascontiguousarray(image).astype(np.float32) / 255
|
||||
return image
|
||||
return np.ascontiguousarray(image).astype(np.float32) / 255
|
||||
|
||||
def __resize_and_pad_image(
|
||||
self, image=np.ndarray, new_shape: Union[Tuple, List] = (640, 640), color: Union[Tuple, List] = (114, 114, 114)
|
||||
|
||||
@ -309,9 +309,8 @@ class Exporter:
|
||||
"WARNING ⚠️ INT8 export requires a missing 'data' arg for calibration. "
|
||||
f"Using default 'data={self.args.data}'."
|
||||
)
|
||||
if tfjs:
|
||||
if ARM64 and LINUX:
|
||||
raise SystemError("TensorFlow.js export not supported on ARM64 Linux")
|
||||
if tfjs and (ARM64 and LINUX):
|
||||
raise SystemError("TensorFlow.js export not supported on ARM64 Linux")
|
||||
|
||||
# Input
|
||||
im = torch.zeros(self.args.batch, 3, *self.imgsz).to(self.device)
|
||||
|
||||
@ -197,12 +197,13 @@ class AutoBackend(nn.Module):
|
||||
import onnxruntime
|
||||
|
||||
providers = ["CPUExecutionProvider"]
|
||||
if cuda and "CUDAExecutionProvider" in onnxruntime.get_available_providers():
|
||||
providers.insert(0, "CUDAExecutionProvider")
|
||||
elif cuda: # Only log warning if CUDA was requested but unavailable
|
||||
LOGGER.warning("WARNING ⚠️ Failed to start ONNX Runtime with CUDA. Using CPU...")
|
||||
device = torch.device("cpu")
|
||||
cuda = False
|
||||
if cuda:
|
||||
if "CUDAExecutionProvider" in onnxruntime.get_available_providers():
|
||||
providers.insert(0, "CUDAExecutionProvider")
|
||||
else: # Only log warning if CUDA was requested but unavailable
|
||||
LOGGER.warning("WARNING ⚠️ Failed to start ONNX Runtime with CUDA. Using CPU...")
|
||||
device = torch.device("cpu")
|
||||
cuda = False
|
||||
LOGGER.info(f"Using ONNX Runtime {providers[0]}")
|
||||
if onnx:
|
||||
session = onnxruntime.InferenceSession(w, providers=providers)
|
||||
@ -223,7 +224,7 @@ class AutoBackend(nn.Module):
|
||||
output_names = [x.name for x in session.get_outputs()]
|
||||
metadata = session.get_modelmeta().custom_metadata_map
|
||||
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:
|
||||
io = session.io_binding()
|
||||
bindings = []
|
||||
|
||||
@ -317,8 +317,7 @@ def model_info(model, detailed=False, verbose=True, imgsz=640):
|
||||
if len(m._parameters):
|
||||
for pn, p in m.named_parameters():
|
||||
LOGGER.info(
|
||||
f"{i:>5g}{mn + '.' + pn:>40}{mt:>20}{p.requires_grad!r:>10}{p.numel():>12g}"
|
||||
f"{str(list(p.shape)):>20}{p.mean():>10.3g}{p.std():>10.3g}{str(p.dtype).replace('torch.', ''):>15}"
|
||||
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}"
|
||||
)
|
||||
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}")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user