Kims12 commited on
Commit
878b1ca
ยท
verified ยท
1 Parent(s): 93086f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -6,14 +6,11 @@ from transformers import AutoModelForImageSegmentation
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
  [
@@ -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("cpu") # GPU -> CPU๋กœ ๋ณ€๊ฒฝ
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
- # 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
 
 
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