Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make it compitable with other torch devices which support fp16 dtype #2668

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions annotator/midas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ 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]

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
Expand Down
Loading