charbel-malo
commited on
Commit
β’
4133d16
1
Parent(s):
3b8c36f
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,3 @@
|
|
1 |
-
|
2 |
-
#### APP.PY CODE END ###
|
3 |
-
|
4 |
import os
|
5 |
import spaces
|
6 |
import cv2
|
@@ -36,7 +33,7 @@ parser.add_argument("--out_dir", help="Default Output directory", default=os.get
|
|
36 |
parser.add_argument("--batch_size", help="Gpu batch size", default=32)
|
37 |
parser.add_argument("--cuda", action="store_true", help="Enable cuda", default=False)
|
38 |
parser.add_argument("--colab", action="store_true", help="Enable colab mode", default=False)
|
39 |
-
parser.add_argument("--device", default="cuda
|
40 |
user_args = parser.parse_args()
|
41 |
|
42 |
@spaces.GPU
|
@@ -121,7 +118,7 @@ else:
|
|
121 |
print("\n********** Running on CPU **********\n")
|
122 |
PROVIDER = ["CPUExecutionProvider"]
|
123 |
|
124 |
-
device =
|
125 |
EMPTY_CACHE = lambda: torch.cuda.empty_cache() if device == "cuda" else None
|
126 |
|
127 |
## ------------------------------ LOAD MODELS ------------------------------
|
@@ -148,7 +145,7 @@ def load_face_swapper_model(path="./assets/pretrained_models/inswapper_128.onnx"
|
|
148 |
def load_face_parser_model(path="./assets/pretrained_models/79999_iter.pth"):
|
149 |
global FACE_PARSER
|
150 |
if FACE_PARSER is None:
|
151 |
-
FACE_PARSER = init_parsing_model(path, device=
|
152 |
|
153 |
@spaces.GPU(enable_queue=True)
|
154 |
def load_nsfw_detector_model(path="./assets/pretrained_models/open-nsfw.onnx"):
|
@@ -215,7 +212,7 @@ def process(
|
|
215 |
print("\n********** Running on CPU **********\n")
|
216 |
PROVIDER = ["CPUExecutionProvider"]
|
217 |
|
218 |
-
device =
|
219 |
EMPTY_CACHE = lambda: torch.cuda.empty_cache() if device == "cuda" else None
|
220 |
|
221 |
# Reset models to None to reload them with GPU
|
@@ -269,7 +266,7 @@ def process(
|
|
269 |
if face_enhancer_name != "NONE":
|
270 |
if face_enhancer_name not in cv2_interpolations:
|
271 |
yield f"### \n β Loading {face_enhancer_name} model...", *ui_before()
|
272 |
-
FACE_ENHANCER = load_face_enhancer_model(name=face_enhancer_name, device=
|
273 |
else:
|
274 |
FACE_ENHANCER = None
|
275 |
|
@@ -351,7 +348,7 @@ def process(
|
|
351 |
yield "### \n β Face-parsing mask...", *ui_before()
|
352 |
masks = []
|
353 |
count = 0
|
354 |
-
for batch_mask in get_parsed_mask(FACE_PARSER, preds, classes=includes, device=
|
355 |
masks.append(batch_mask)
|
356 |
EMPTY_CACHE()
|
357 |
count += 1
|
@@ -987,5 +984,3 @@ if __name__ == "__main__":
|
|
987 |
interface.queue()
|
988 |
interface.launch()
|
989 |
|
990 |
-
|
991 |
-
#### APP.PY CODE END ###
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import spaces
|
3 |
import cv2
|
|
|
33 |
parser.add_argument("--batch_size", help="Gpu batch size", default=32)
|
34 |
parser.add_argument("--cuda", action="store_true", help="Enable cuda", default=False)
|
35 |
parser.add_argument("--colab", action="store_true", help="Enable colab mode", default=False)
|
36 |
+
parser.add_argument("--device", default="cuda", type=str)
|
37 |
user_args = parser.parse_args()
|
38 |
|
39 |
@spaces.GPU
|
|
|
118 |
print("\n********** Running on CPU **********\n")
|
119 |
PROVIDER = ["CPUExecutionProvider"]
|
120 |
|
121 |
+
device = "cuda" if USE_CUDA else "cpu"
|
122 |
EMPTY_CACHE = lambda: torch.cuda.empty_cache() if device == "cuda" else None
|
123 |
|
124 |
## ------------------------------ LOAD MODELS ------------------------------
|
|
|
145 |
def load_face_parser_model(path="./assets/pretrained_models/79999_iter.pth"):
|
146 |
global FACE_PARSER
|
147 |
if FACE_PARSER is None:
|
148 |
+
FACE_PARSER = init_parsing_model(path, device="cuda")
|
149 |
|
150 |
@spaces.GPU(enable_queue=True)
|
151 |
def load_nsfw_detector_model(path="./assets/pretrained_models/open-nsfw.onnx"):
|
|
|
212 |
print("\n********** Running on CPU **********\n")
|
213 |
PROVIDER = ["CPUExecutionProvider"]
|
214 |
|
215 |
+
device = "cuda" if USE_CUDA else "cpu"
|
216 |
EMPTY_CACHE = lambda: torch.cuda.empty_cache() if device == "cuda" else None
|
217 |
|
218 |
# Reset models to None to reload them with GPU
|
|
|
266 |
if face_enhancer_name != "NONE":
|
267 |
if face_enhancer_name not in cv2_interpolations:
|
268 |
yield f"### \n β Loading {face_enhancer_name} model...", *ui_before()
|
269 |
+
FACE_ENHANCER = load_face_enhancer_model(name=face_enhancer_name, device=device)
|
270 |
else:
|
271 |
FACE_ENHANCER = None
|
272 |
|
|
|
348 |
yield "### \n β Face-parsing mask...", *ui_before()
|
349 |
masks = []
|
350 |
count = 0
|
351 |
+
for batch_mask in get_parsed_mask(FACE_PARSER, preds, classes=includes, device=device, batch_size=BATCH_SIZE, softness=int(mask_soft_iterations)):
|
352 |
masks.append(batch_mask)
|
353 |
EMPTY_CACHE()
|
354 |
count += 1
|
|
|
984 |
interface.queue()
|
985 |
interface.launch()
|
986 |
|
|
|
|