Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,14 +6,11 @@ from transformers import AutoModelForImageSegmentation
|
|
6 |
import torch
|
7 |
from torchvision import transforms
|
8 |
|
9 |
-
#
|
10 |
-
# GPU ์ค์ ์ ์ญ์ ํ๊ฑฐ๋ "cuda"๋ฅผ "cpu"๋ก ๋ณ๊ฒฝ
|
11 |
-
# torch.set_float32_matmul_precision("high")๋ CPU์์ ํ์ ์์.
|
12 |
-
|
13 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
14 |
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
15 |
)
|
16 |
-
|
17 |
|
18 |
transform_image = transforms.Compose(
|
19 |
[
|
@@ -23,19 +20,21 @@ transform_image = transforms.Compose(
|
|
23 |
]
|
24 |
)
|
25 |
|
|
|
26 |
def fn(image):
|
|
|
|
|
27 |
im = load_img(image, output_type="pil")
|
28 |
im = im.convert("RGB")
|
29 |
origin = im.copy()
|
30 |
processed_image = process(im)
|
|
|
|
|
31 |
return (processed_image, origin)
|
32 |
|
33 |
-
# @spaces.GPU ๋ฐ์ฝ๋ ์ดํฐ ์ ๊ฑฐ
|
34 |
-
# CPU ํ๊ฒฝ์์ ๋์ํ๋๋ก ์ค์
|
35 |
-
|
36 |
def process(image):
|
37 |
image_size = image.size
|
38 |
-
input_images = transform_image(image).unsqueeze(0).to("
|
39 |
# Prediction
|
40 |
with torch.no_grad():
|
41 |
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
@@ -45,12 +44,15 @@ def process(image):
|
|
45 |
image.putalpha(mask)
|
46 |
return image
|
47 |
|
|
|
48 |
def process_file(f):
|
|
|
49 |
name_path = f.rsplit(".", 1)[0] + ".png"
|
50 |
im = load_img(f, output_type="pil")
|
51 |
im = im.convert("RGB")
|
52 |
transparent = process(im)
|
53 |
transparent.save(name_path)
|
|
|
54 |
return name_path
|
55 |
|
56 |
slider1 = ImageSlider(label="Processed Image", type="pil")
|
@@ -60,7 +62,7 @@ image_file_upload = gr.Image(label="Upload an image", type="filepath")
|
|
60 |
url_input = gr.Textbox(label="Paste an image URL")
|
61 |
output_file = gr.File(label="Output PNG File")
|
62 |
|
63 |
-
#
|
64 |
chameleon = load_img("butterfly.jpg", output_type="pil")
|
65 |
url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
|
66 |
|
|
|
6 |
import torch
|
7 |
from torchvision import transforms
|
8 |
|
9 |
+
# ๋ชจ๋ธ์ ์ ์ญ์ผ๋ก ๋ก๋ (๊ธฐ๋ณธ์ ์ผ๋ก CPU์ ๋ก๋)
|
|
|
|
|
|
|
10 |
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
11 |
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
12 |
)
|
13 |
+
# GPU ํ๊ฒฝ์์๋ง ๋ชจ๋ธ์ GPU๋ก ์ด๋
|
14 |
|
15 |
transform_image = transforms.Compose(
|
16 |
[
|
|
|
20 |
]
|
21 |
)
|
22 |
|
23 |
+
@spaces.GPU
|
24 |
def fn(image):
|
25 |
+
# GPU ํ ๋น ์ ๋ชจ๋ธ์ CUDA๋ก ์ด๋
|
26 |
+
birefnet.to("cuda")
|
27 |
im = load_img(image, output_type="pil")
|
28 |
im = im.convert("RGB")
|
29 |
origin = im.copy()
|
30 |
processed_image = process(im)
|
31 |
+
# ์์
์๋ฃ ํ ๋ชจ๋ธ์ CPU๋ก ์ด๋
|
32 |
+
birefnet.to("cpu")
|
33 |
return (processed_image, origin)
|
34 |
|
|
|
|
|
|
|
35 |
def process(image):
|
36 |
image_size = image.size
|
37 |
+
input_images = transform_image(image).unsqueeze(0).to("cuda")
|
38 |
# Prediction
|
39 |
with torch.no_grad():
|
40 |
preds = birefnet(input_images)[-1].sigmoid().cpu()
|
|
|
44 |
image.putalpha(mask)
|
45 |
return image
|
46 |
|
47 |
+
@spaces.GPU
|
48 |
def process_file(f):
|
49 |
+
birefnet.to("cuda")
|
50 |
name_path = f.rsplit(".", 1)[0] + ".png"
|
51 |
im = load_img(f, output_type="pil")
|
52 |
im = im.convert("RGB")
|
53 |
transparent = process(im)
|
54 |
transparent.save(name_path)
|
55 |
+
birefnet.to("cpu")
|
56 |
return name_path
|
57 |
|
58 |
slider1 = ImageSlider(label="Processed Image", type="pil")
|
|
|
62 |
url_input = gr.Textbox(label="Paste an image URL")
|
63 |
output_file = gr.File(label="Output PNG File")
|
64 |
|
65 |
+
# ์์ ์ด๋ฏธ์ง
|
66 |
chameleon = load_img("butterfly.jpg", output_type="pil")
|
67 |
url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
|
68 |
|