Kims12 commited on
Commit
b71c9c4
ยท
verified ยท
1 Parent(s): b09a145

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -6,7 +6,15 @@ from transformers import AutoModelForImageSegmentation
6
  import torch
7
  from torchvision import transforms
8
 
9
- # ๋ชจ๋ธ ๋กœ๋”ฉ์„ ํ•จ์ˆ˜ ๋‚ด๋กœ ์ด๋™ํ•˜์—ฌ GPU ํ• ๋‹น ์‹œ ๋กœ๋“œ๋˜๋„๋ก ์„ค์ •
 
 
 
 
 
 
 
 
10
  transform_image = transforms.Compose(
11
  [
12
  transforms.Resize((1024, 1024)),
@@ -15,42 +23,33 @@ transform_image = transforms.Compose(
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,7 +60,7 @@ image_file_upload = gr.Image(label="Upload an image", type="filepath")
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
 
 
6
  import torch
7
  from torchvision import transforms
8
 
9
+ # GPU ์„ค์ •์„ CPU๋กœ ๋ณ€๊ฒฝ
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
  ]
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("cpu") # GPU -> CPU๋กœ ๋ณ€๊ฒฝ
39
+ # Prediction
40
  with torch.no_grad():
41
+ preds = birefnet(input_images)[-1].sigmoid().cpu()
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
  url_input = gr.Textbox(label="Paste an image URL")
61
  output_file = gr.File(label="Output PNG File")
62
 
63
+ # Example images
64
  chameleon = load_img("butterfly.jpg", output_type="pil")
65
  url_example = "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1229892983-square.jpg"
66