From ca2f5d31c2b2502be407c43a32aa682b2751631f Mon Sep 17 00:00:00 2001 From: lhq Date: Thu, 29 Feb 2024 14:40:34 +0800 Subject: [PATCH] 1. specify dtype for specific devices 2. convert the depth to float after model forward --- annotator/midas/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/annotator/midas/__init__.py b/annotator/midas/__init__.py index dc247615f..204d6c0b6 100644 --- a/annotator/midas/__init__.py +++ b/annotator/midas/__init__.py @@ -18,14 +18,14 @@ def apply_midas(input_image, a=np.pi * 2.0, bg_th=0.1): if model is None: model = MiDaSInference(model_type="dpt_hybrid") if devices.get_device_for("controlnet").type != 'mps': - model = model.to(devices.get_device_for("controlnet")) + model = model.to(devices.get_device_for("controlnet"), dtype=devices.dtype) assert input_image.ndim == 3 image_depth = input_image with torch.no_grad(): image_depth = torch.from_numpy(image_depth).float() if devices.get_device_for("controlnet").type != 'mps': - image_depth = image_depth.to(devices.get_device_for("controlnet")) + image_depth = image_depth.to(devices.get_device_for("controlnet"), dtype=devices.dtype) image_depth = image_depth / 127.5 - 1.0 image_depth = rearrange(image_depth, 'h w c -> 1 c h w') depth = model(image_depth)[0] @@ -33,10 +33,10 @@ def apply_midas(input_image, a=np.pi * 2.0, bg_th=0.1): depth_pt = depth.clone() depth_pt -= torch.min(depth_pt) depth_pt /= torch.max(depth_pt) - depth_pt = depth_pt.cpu().numpy() + depth_pt = depth_pt.float().cpu().numpy() depth_image = (depth_pt * 255.0).clip(0, 255).astype(np.uint8) - depth_np = depth.cpu().numpy() + depth_np = depth.float().cpu().numpy() x = cv2.Sobel(depth_np, cv2.CV_32F, 1, 0, ksize=3) y = cv2.Sobel(depth_np, cv2.CV_32F, 0, 1, ksize=3) z = np.ones_like(x) * a