diff --git a/ultralytics/utils/torch_utils.py b/ultralytics/utils/torch_utils.py index 2f7fd7b6..1eb17e45 100644 --- a/ultralytics/utils/torch_utils.py +++ b/ultralytics/utils/torch_utils.py @@ -457,12 +457,12 @@ def get_flops(model, imgsz=640): try: # Method 1: Use stride-based input tensor stride = max(int(model.stride.max()), 32) if hasattr(model, "stride") else 32 # max stride - im = torch.empty((1, p.shape[1], stride, stride), device=p.device) # input image in BCHW format + im = torch.empty((1, p.shape[1], stride, stride), device=p.device, dtype=p.dtype) # input image in BCHW flops = thop.profile(deepcopy(model), inputs=[im], verbose=False)[0] / 1e9 * 2 # stride GFLOPs return flops * imgsz[0] / stride * imgsz[1] / stride # imgsz GFLOPs except Exception: # Method 2: Use actual image size (required for RTDETR models) - im = torch.empty((1, p.shape[1], *imgsz), device=p.device) # input image in BCHW format + im = torch.empty((1, p.shape[1], *imgsz), device=p.device, dtype=p.dtype) # input image in BCHW format return thop.profile(deepcopy(model), inputs=[im], verbose=False)[0] / 1e9 * 2 # imgsz GFLOPs except Exception: return 0.0 @@ -487,14 +487,14 @@ def get_flops_with_torch_profiler(model, imgsz=640): try: # Use stride size for input tensor stride = (max(int(model.stride.max()), 32) if hasattr(model, "stride") else 32) * 2 # max stride - im = torch.empty((1, p.shape[1], stride, stride), device=p.device) # input image in BCHW format + im = torch.empty((1, p.shape[1], stride, stride), device=p.device, dtype=p.dtype) # input image in BCHW with torch.profiler.profile(with_flops=True) as prof: model(im) flops = sum(x.flops for x in prof.key_averages()) / 1e9 flops = flops * imgsz[0] / stride * imgsz[1] / stride # 640x640 GFLOPs except Exception: # Use actual image size for input tensor (i.e. required for RTDETR models) - im = torch.empty((1, p.shape[1], *imgsz), device=p.device) # input image in BCHW format + im = torch.empty((1, p.shape[1], *imgsz), device=p.device, dtype=p.dtype) # input image in BCHW format with torch.profiler.profile(with_flops=True) as prof: model(im) flops = sum(x.flops for x in prof.key_averages()) / 1e9