Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import spaces
|
2 |
|
3 |
-
#
|
4 |
def initialize_cuda_models():
|
5 |
import torch
|
6 |
import os
|
@@ -21,12 +21,14 @@ def initialize_cuda_models():
|
|
21 |
|
22 |
base_path = 'yisol/IDM-VTON'
|
23 |
|
24 |
-
# Load all the models
|
|
|
|
|
25 |
unet = UNet2DConditionModel.from_pretrained(
|
26 |
base_path,
|
27 |
subfolder="unet",
|
28 |
torch_dtype=torch.float16,
|
29 |
-
)
|
30 |
unet.requires_grad_(False)
|
31 |
|
32 |
tokenizer_one = AutoTokenizer.from_pretrained(
|
@@ -48,32 +50,32 @@ def initialize_cuda_models():
|
|
48 |
base_path,
|
49 |
subfolder="text_encoder",
|
50 |
torch_dtype=torch.float16,
|
51 |
-
)
|
52 |
text_encoder_two = CLIPTextModelWithProjection.from_pretrained(
|
53 |
base_path,
|
54 |
subfolder="text_encoder_2",
|
55 |
torch_dtype=torch.float16,
|
56 |
-
)
|
57 |
|
58 |
image_encoder = CLIPVisionModelWithProjection.from_pretrained(
|
59 |
base_path,
|
60 |
subfolder="image_encoder",
|
61 |
torch_dtype=torch.float16,
|
62 |
-
)
|
63 |
|
64 |
vae = AutoencoderKL.from_pretrained(
|
65 |
base_path,
|
66 |
subfolder="vae",
|
67 |
torch_dtype=torch.float16,
|
68 |
-
)
|
69 |
|
70 |
UNet_Encoder = UNet2DConditionModel_ref.from_pretrained(
|
71 |
base_path,
|
72 |
subfolder="unet_encoder",
|
73 |
torch_dtype=torch.float16,
|
74 |
-
)
|
75 |
|
76 |
-
# Initialize auxiliary models
|
77 |
parsing_model = Parsing(0)
|
78 |
openpose_model = OpenPose(0)
|
79 |
|
@@ -110,10 +112,10 @@ def initialize_cuda_models():
|
|
110 |
|
111 |
pipe.unet_encoder = UNet_Encoder
|
112 |
|
113 |
-
return pipe, openpose_model, parsing_model, tensor_transform
|
114 |
|
115 |
-
# Initialize models once
|
116 |
-
pipe, openpose_model, parsing_model, tensor_transform = initialize_cuda_models()
|
117 |
|
118 |
from PIL import Image
|
119 |
import numpy as np
|
@@ -137,8 +139,7 @@ def pil_to_binary_mask(pil_image, threshold=0):
|
|
137 |
|
138 |
@spaces.GPU
|
139 |
def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denoise_steps, seed):
|
140 |
-
|
141 |
-
|
142 |
openpose_model.preprocessor.body_estimation.model.to(device)
|
143 |
pipe.to(device)
|
144 |
pipe.unet_encoder.to(device)
|
@@ -173,7 +174,9 @@ def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denois
|
|
173 |
human_img_arg = _apply_exif_orientation(human_img.resize((384, 512)))
|
174 |
human_img_arg = convert_PIL_to_numpy(human_img_arg, format="BGR")
|
175 |
|
176 |
-
args = apply_net.create_argument_parser().parse_args(
|
|
|
|
|
177 |
pose_img = args.func(args, human_img_arg)
|
178 |
pose_img = pose_img[:, :, ::-1]
|
179 |
pose_img = Image.fromarray(pose_img).resize((768, 1024))
|
|
|
1 |
import spaces
|
2 |
|
3 |
+
# Function to initialize all CUDA-related imports and models
|
4 |
def initialize_cuda_models():
|
5 |
import torch
|
6 |
import os
|
|
|
21 |
|
22 |
base_path = 'yisol/IDM-VTON'
|
23 |
|
24 |
+
# Load all the models on GPU 0
|
25 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
26 |
+
|
27 |
unet = UNet2DConditionModel.from_pretrained(
|
28 |
base_path,
|
29 |
subfolder="unet",
|
30 |
torch_dtype=torch.float16,
|
31 |
+
).to(device)
|
32 |
unet.requires_grad_(False)
|
33 |
|
34 |
tokenizer_one = AutoTokenizer.from_pretrained(
|
|
|
50 |
base_path,
|
51 |
subfolder="text_encoder",
|
52 |
torch_dtype=torch.float16,
|
53 |
+
).to(device)
|
54 |
text_encoder_two = CLIPTextModelWithProjection.from_pretrained(
|
55 |
base_path,
|
56 |
subfolder="text_encoder_2",
|
57 |
torch_dtype=torch.float16,
|
58 |
+
).to(device)
|
59 |
|
60 |
image_encoder = CLIPVisionModelWithProjection.from_pretrained(
|
61 |
base_path,
|
62 |
subfolder="image_encoder",
|
63 |
torch_dtype=torch.float16,
|
64 |
+
).to(device)
|
65 |
|
66 |
vae = AutoencoderKL.from_pretrained(
|
67 |
base_path,
|
68 |
subfolder="vae",
|
69 |
torch_dtype=torch.float16,
|
70 |
+
).to(device)
|
71 |
|
72 |
UNet_Encoder = UNet2DConditionModel_ref.from_pretrained(
|
73 |
base_path,
|
74 |
subfolder="unet_encoder",
|
75 |
torch_dtype=torch.float16,
|
76 |
+
).to(device)
|
77 |
|
78 |
+
# Initialize auxiliary models on CPU if they don't strictly require GPU
|
79 |
parsing_model = Parsing(0)
|
80 |
openpose_model = OpenPose(0)
|
81 |
|
|
|
112 |
|
113 |
pipe.unet_encoder = UNet_Encoder
|
114 |
|
115 |
+
return pipe, openpose_model, parsing_model, tensor_transform, device
|
116 |
|
117 |
+
# Initialize models and device once
|
118 |
+
pipe, openpose_model, parsing_model, tensor_transform, device = initialize_cuda_models()
|
119 |
|
120 |
from PIL import Image
|
121 |
import numpy as np
|
|
|
139 |
|
140 |
@spaces.GPU
|
141 |
def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denoise_steps, seed):
|
142 |
+
# Using GPU 0
|
|
|
143 |
openpose_model.preprocessor.body_estimation.model.to(device)
|
144 |
pipe.to(device)
|
145 |
pipe.unet_encoder.to(device)
|
|
|
174 |
human_img_arg = _apply_exif_orientation(human_img.resize((384, 512)))
|
175 |
human_img_arg = convert_PIL_to_numpy(human_img_arg, format="BGR")
|
176 |
|
177 |
+
args = apply_net.create_argument_parser().parse_args(
|
178 |
+
('show', './configs/densepose_rcnn_R_50_FPN_s1x.yaml', './ckpt/densepose/model_final_162be9.pkl', 'dp_segm', '-v', '--opts', 'MODEL.DEVICE', 'cuda')
|
179 |
+
)
|
180 |
pose_img = args.func(args, human_img_arg)
|
181 |
pose_img = pose_img[:, :, ::-1]
|
182 |
pose_img = Image.fromarray(pose_img).resize((768, 1024))
|