From ec2b6c0d182a0897e7ea007e38fc9f45fcce0c0f Mon Sep 17 00:00:00 2001 From: smthemex <138738845+smthemex@users.noreply.github.com> Date: Sat, 2 Nov 2024 09:05:11 +0800 Subject: [PATCH] Update common.py --- Sapiens_Pytorch/common.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Sapiens_Pytorch/common.py b/Sapiens_Pytorch/common.py index 3bd47a1..97640e1 100644 --- a/Sapiens_Pytorch/common.py +++ b/Sapiens_Pytorch/common.py @@ -13,7 +13,7 @@ from PIL import Image import numpy as np import matplotlib.pyplot as plt from .classes_and_palettes import GOLIATH_PALETTE, GOLIATH_CLASSES - +device_ = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu" class TaskType(Enum): DEPTH = "depth" NORMAL = "normal" @@ -54,7 +54,7 @@ class ImageProcessor: ]) def process_image(self, image: Image.Image, model,select_obj,RGB_BG, model_dtype: torch.dtype = torch.float32): - input_tensor = self.transform_fn(image).unsqueeze(0).to("cuda").to(model_dtype) + input_tensor = self.transform_fn(image).unsqueeze(0).to(device_).to(model_dtype) preds = ModelManager.run_model(model, input_tensor, image.height, image.width) mask = preds.squeeze(0).cpu().numpy() @@ -105,8 +105,7 @@ class ImageProcessorDepth: def process_image(self, image: Image.Image, depth_model, if_seg,seg_in,RGB_BG, model_dtype: torch.dtype = torch.float32): - - input_tensor = self.transform_fn(image).unsqueeze(0).to("cuda").to(model_dtype) + input_tensor = self.transform_fn(image).unsqueeze(0).to(device_).to(model_dtype) depth_output = ModelManager.run_model_depth(depth_model, input_tensor, image.height, image.width).to(torch.float32) depth_map = depth_output.squeeze().cpu().numpy() @@ -144,7 +143,8 @@ class ImageProcessorNormal: def process_image(self, image: Image.Image, normal_model, if_seg,seg_in,RGB_BG, model_dtype: torch.dtype = torch.float32): # Load models here instead of storing them as class attributes - input_tensor = self.transform_fn(image).unsqueeze(0).to("cuda").to(model_dtype) + + input_tensor = self.transform_fn(image).unsqueeze(0).to(device_).to(model_dtype) # Run normal estimation normal_output = ModelManager.run_model_depth(normal_model, input_tensor, image.height, image.width).to(torch.float32) @@ -260,4 +260,4 @@ def get_mask(draw): mask_image = Image.merge('RGBA', (bg, bg, bg, alpha)) mask_image = torch.from_numpy(np.array(mask_image).astype(np.float32) / 255.0).unsqueeze(0) real_mask = torch.tensor([mask_image[0, :, :, 3].tolist()]) - return real_mask \ No newline at end of file + return real_mask