Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,15 +6,7 @@ from transformers import AutoModelForImageSegmentation
|
|
| 6 |
import torch
|
| 7 |
from torchvision import transforms
|
| 8 |
|
| 9 |
-
# GPU
|
| 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 |
-
birefnet.to("cpu") # GPU -> CPU๋ก ๋ณ๊ฒฝ
|
| 17 |
-
|
| 18 |
transform_image = transforms.Compose(
|
| 19 |
[
|
| 20 |
transforms.Resize((1024, 1024)),
|
|
@@ -23,33 +15,42 @@ 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 |
-
|
| 34 |
-
# CPU ํ๊ฒฝ์์ ๋์ํ๋๋ก ์ค์
|
| 35 |
-
|
| 36 |
-
def process(image):
|
| 37 |
image_size = image.size
|
| 38 |
-
input_images = transform_image(image).unsqueeze(0).to("
|
| 39 |
-
#
|
| 40 |
with torch.no_grad():
|
| 41 |
-
preds =
|
| 42 |
pred = preds[0].squeeze()
|
| 43 |
pred_pil = transforms.ToPILImage()(pred)
|
| 44 |
mask = pred_pil.resize(image_size)
|
| 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 |
|
|
@@ -60,7 +61,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 |
+
# ๋ชจ๋ธ ๋ก๋ฉ์ ํจ์ ๋ด๋ก ์ด๋ํ์ฌ GPU ํ ๋น ์ ๋ก๋๋๋๋ก ์ค์
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
transform_image = transforms.Compose(
|
| 11 |
[
|
| 12 |
transforms.Resize((1024, 1024)),
|
|
|
|
| 15 |
]
|
| 16 |
)
|
| 17 |
|
| 18 |
+
@spaces.GPU
|
| 19 |
def fn(image):
|
| 20 |
+
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
| 21 |
+
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
| 22 |
+
)
|
| 23 |
+
birefnet.to("cuda")
|
| 24 |
+
|
| 25 |
im = load_img(image, output_type="pil")
|
| 26 |
im = im.convert("RGB")
|
| 27 |
origin = im.copy()
|
| 28 |
+
processed_image = process(im, birefnet)
|
| 29 |
return (processed_image, origin)
|
| 30 |
|
| 31 |
+
def process(image, model):
|
|
|
|
|
|
|
|
|
|
| 32 |
image_size = image.size
|
| 33 |
+
input_images = transform_image(image).unsqueeze(0).to("cuda")
|
| 34 |
+
# ์์ธก
|
| 35 |
with torch.no_grad():
|
| 36 |
+
preds = model(input_images)[-1].sigmoid().cpu()
|
| 37 |
pred = preds[0].squeeze()
|
| 38 |
pred_pil = transforms.ToPILImage()(pred)
|
| 39 |
mask = pred_pil.resize(image_size)
|
| 40 |
image.putalpha(mask)
|
| 41 |
return image
|
| 42 |
|
| 43 |
+
@spaces.GPU
|
| 44 |
def process_file(f):
|
| 45 |
+
birefnet = AutoModelForImageSegmentation.from_pretrained(
|
| 46 |
+
"ZhengPeng7/BiRefNet", trust_remote_code=True
|
| 47 |
+
)
|
| 48 |
+
birefnet.to("cuda")
|
| 49 |
+
|
| 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, birefnet)
|
| 54 |
transparent.save(name_path)
|
| 55 |
return name_path
|
| 56 |
|
|
|
|
| 61 |
url_input = gr.Textbox(label="Paste an image URL")
|
| 62 |
output_file = gr.File(label="Output PNG File")
|
| 63 |
|
| 64 |
+
# ์์ ์ด๋ฏธ์ง
|
| 65 |
chameleon = load_img("butterfly.jpg", output_type="pil")
|
| 66 |
url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
|
| 67 |
|