Nymbo commited on
Commit
4dd158f
·
verified ·
1 Parent(s): 9f2c373

Gradio 6.29, py3.12, CPU torch; PNG output, no_grad, theme to launch()

Browse files
Files changed (3) hide show
  1. README.md +2 -1
  2. app.py +80 -83
  3. requirements.txt +1 -7
README.md CHANGED
@@ -4,7 +4,8 @@ emoji: 💻
4
  colorFrom: red
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 4.29.0
 
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
4
  colorFrom: red
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 6.29.0
8
+ python_version: "3.12"
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
app.py CHANGED
@@ -1,83 +1,80 @@
1
- import numpy as np
2
- import torch
3
- import torch.nn.functional as F
4
- import gradio as gr
5
- from ormbg import ORMBG
6
- from PIL import Image
7
-
8
-
9
- model_path = "ormbg.pth"
10
-
11
- net = ORMBG()
12
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
- net.to(device)
14
-
15
- if torch.cuda.is_available():
16
- net.load_state_dict(torch.load(model_path))
17
- net = net.cuda()
18
- else:
19
- net.load_state_dict(torch.load(model_path, map_location="cpu"))
20
- net.eval()
21
-
22
-
23
- def resize_image(image):
24
- image = image.convert("RGB")
25
- model_input_size = (1024, 1024)
26
- image = image.resize(model_input_size, Image.BILINEAR)
27
- return image
28
-
29
-
30
- def inference(image):
31
-
32
- # prepare input
33
- orig_image = Image.fromarray(image)
34
- w, h = orig_image.size
35
- image = resize_image(orig_image)
36
- im_np = np.array(image)
37
- im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2, 0, 1)
38
- im_tensor = torch.unsqueeze(im_tensor, 0)
39
- im_tensor = torch.divide(im_tensor, 255.0)
40
- if torch.cuda.is_available():
41
- im_tensor = im_tensor.cuda()
42
-
43
- # inference
44
- result = net(im_tensor)
45
- # post process
46
- result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode="bilinear"), 0)
47
- ma = torch.max(result)
48
- mi = torch.min(result)
49
- result = (result - mi) / (ma - mi)
50
- # image to pil
51
- im_array = (result * 255).cpu().data.numpy().astype(np.uint8)
52
- pil_im = Image.fromarray(np.squeeze(im_array))
53
- # paste the mask on the original image
54
- new_im = Image.new("RGBA", pil_im.size, (0, 0, 0, 0))
55
- new_im.paste(orig_image, mask=pil_im)
56
-
57
- return new_im
58
-
59
-
60
- gr.Markdown("## Open Remove Background Model (ormbg)")
61
- gr.HTML(
62
- """
63
- <p style="margin-bottom: 10px; font-size: 94%">
64
- This is a demo for Open Remove Background Model (ormbg) that using
65
- <a href="https://huggingface.co/schirrmacher/ormbg" target="_blank">Open Remove Background Model (ormbg) model</a> as backbone.
66
- </p>
67
- """
68
- )
69
- title = "Transparent Background (Fast)"
70
-
71
- examples = ["./example1.png", "./example2.png", "./example3.png"]
72
-
73
- demo = gr.Interface(
74
- fn=inference,
75
- inputs="image",
76
- outputs="image",
77
- examples=examples,
78
- title=title,
79
- theme="Nymbo/Nymbo_Theme"
80
- )
81
-
82
- if __name__ == "__main__":
83
- demo.launch(share=False)
 
1
+ import numpy as np
2
+ import torch
3
+ import torch.nn.functional as F
4
+ import gradio as gr
5
+ from ormbg import ORMBG
6
+ from PIL import Image
7
+
8
+
9
+ model_path = "ormbg.pth"
10
+
11
+ net = ORMBG()
12
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
+ net.to(device)
14
+
15
+ if torch.cuda.is_available():
16
+ net.load_state_dict(torch.load(model_path))
17
+ net = net.cuda()
18
+ else:
19
+ net.load_state_dict(torch.load(model_path, map_location="cpu"))
20
+ net.eval()
21
+
22
+
23
+ def resize_image(image):
24
+ image = image.convert("RGB")
25
+ model_input_size = (1024, 1024)
26
+ image = image.resize(model_input_size, Image.BILINEAR)
27
+ return image
28
+
29
+
30
+ def inference(image):
31
+
32
+ # prepare input
33
+ if image is None:
34
+ raise gr.Error("Please upload an image.")
35
+ orig_image = Image.fromarray(image).convert("RGB")
36
+ w, h = orig_image.size
37
+ image = resize_image(orig_image)
38
+ im_np = np.array(image)
39
+ im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2, 0, 1)
40
+ im_tensor = torch.unsqueeze(im_tensor, 0)
41
+ im_tensor = torch.divide(im_tensor, 255.0)
42
+ if torch.cuda.is_available():
43
+ im_tensor = im_tensor.cuda()
44
+
45
+ # inference
46
+ with torch.no_grad():
47
+ result = net(im_tensor)
48
+ # post process
49
+ result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode="bilinear"), 0)
50
+ ma = torch.max(result)
51
+ mi = torch.min(result)
52
+ result = (result - mi) / (ma - mi)
53
+ # image to pil
54
+ im_array = (result * 255).cpu().data.numpy().astype(np.uint8)
55
+ pil_im = Image.fromarray(np.squeeze(im_array))
56
+ # paste the mask on the original image
57
+ new_im = Image.new("RGBA", pil_im.size, (0, 0, 0, 0))
58
+ new_im.paste(orig_image, mask=pil_im)
59
+
60
+ return new_im
61
+
62
+
63
+ title = "Transparent Background (Fast)"
64
+
65
+ examples = ["./example1.png", "./example2.png", "./example3.png"]
66
+
67
+ demo = gr.Interface(
68
+ fn=inference,
69
+ inputs="image",
70
+ outputs=gr.Image(type="pil", format="png"),
71
+ examples=examples,
72
+ title=title,
73
+ description=(
74
+ "This is a demo for Open Remove Background Model (ormbg) that using "
75
+ '<a href="https://huggingface.co/schirrmacher/ormbg" target="_blank">Open Remove Background Model (ormbg) model</a> as backbone.'
76
+ ),
77
+ )
78
+
79
+ if __name__ == "__main__":
80
+ demo.launch(share=False, theme="Nymbo/Nymbo_Theme")
 
 
 
requirements.txt CHANGED
@@ -1,10 +1,4 @@
1
- gradio
2
- gradio_imageslider
3
  torch
4
- torchvision
5
- scikit-image
6
  pillow
7
  numpy
8
- typing
9
- gitpython
10
- huggingface_hub
 
1
+ --extra-index-url https://download.pytorch.org/whl/cpu
 
2
  torch
 
 
3
  pillow
4
  numpy