Spaces:
taesiri
/
Running on Zero

taesiri commited on
Commit
70c6b79
1 Parent(s): b82985a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -12
app.py CHANGED
@@ -1,20 +1,21 @@
1
- from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation
2
  import gradio as gr
3
  from PIL import Image
4
  import torch
5
  import matplotlib.pyplot as plt
6
  import cv2
7
- import torch
8
  import numpy as np
 
 
9
 
10
  processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
11
- model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined")
12
 
13
-
14
- def process_image(image, prompt, threhsold, alpha_value, draw_rectangles):
15
  inputs = processor(
16
- text=prompt, images=image, padding="max_length", return_tensors="pt"
17
  )
 
18
 
19
  # predict
20
  with torch.no_grad():
@@ -22,7 +23,7 @@ def process_image(image, prompt, threhsold, alpha_value, draw_rectangles):
22
  preds = outputs.logits
23
 
24
  pred = torch.sigmoid(preds)
25
- mat = pred.cpu().numpy()
26
  mask = Image.fromarray(np.uint8(mat * 255), "L")
27
  mask = mask.convert("RGB")
28
  mask = mask.resize(image.size)
@@ -34,9 +35,9 @@ def process_image(image, prompt, threhsold, alpha_value, draw_rectangles):
34
  mask = (mask - mask_min) / (mask_max - mask_min)
35
 
36
  # threshold the mask
37
- bmask = mask > threhsold
38
  # zero out values below the threshold
39
- mask[mask < threhsold] = 0
40
 
41
  fig, ax = plt.subplots()
42
  ax.imshow(image)
@@ -62,12 +63,10 @@ def process_image(image, prompt, threhsold, alpha_value, draw_rectangles):
62
 
63
  return fig, mask, output_image
64
 
65
-
66
  title = "Interactive demo: zero-shot image segmentation with CLIPSeg"
67
  description = "Demo for using CLIPSeg, a CLIP-based model for zero- and one-shot image segmentation. To use it, simply upload an image and add a text to mask (identify in the image), or use one of the examples below and click 'submit'. Results will show up in a few seconds."
68
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2112.10003'>CLIPSeg: Image Segmentation Using Text and Image Prompts</a> | <a href='https://huggingface.co/docs/transformers/main/en/model_doc/clipseg'>HuggingFace docs</a></p>"
69
 
70
-
71
  with gr.Blocks() as demo:
72
  gr.Markdown("# CLIPSeg: Image Segmentation Using Text and Image Prompts")
73
  gr.Markdown(article)
@@ -119,4 +118,4 @@ with gr.Blocks() as demo:
119
  ],
120
  )
121
 
122
- demo.launch()
 
 
1
  import gradio as gr
2
  from PIL import Image
3
  import torch
4
  import matplotlib.pyplot as plt
5
  import cv2
 
6
  import numpy as np
7
+ import spaces
8
+ from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation
9
 
10
  processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
11
+ model = CLIPSegForImageSegmentation.from_pretrained("CIDAS/clipseg-rd64-refined").cuda()
12
 
13
+ @spaces.GPU
14
+ def process_image(image, prompt, threshold, alpha_value, draw_rectangles):
15
  inputs = processor(
16
+ text=prompt, images=image, return_tensors="pt"
17
  )
18
+ inputs = {k: v.cuda() for k, v in inputs.items()}
19
 
20
  # predict
21
  with torch.no_grad():
 
23
  preds = outputs.logits
24
 
25
  pred = torch.sigmoid(preds)
26
+ mat = pred.squeeze().cpu().numpy()
27
  mask = Image.fromarray(np.uint8(mat * 255), "L")
28
  mask = mask.convert("RGB")
29
  mask = mask.resize(image.size)
 
35
  mask = (mask - mask_min) / (mask_max - mask_min)
36
 
37
  # threshold the mask
38
+ bmask = mask > threshold
39
  # zero out values below the threshold
40
+ mask[mask < threshold] = 0
41
 
42
  fig, ax = plt.subplots()
43
  ax.imshow(image)
 
63
 
64
  return fig, mask, output_image
65
 
 
66
  title = "Interactive demo: zero-shot image segmentation with CLIPSeg"
67
  description = "Demo for using CLIPSeg, a CLIP-based model for zero- and one-shot image segmentation. To use it, simply upload an image and add a text to mask (identify in the image), or use one of the examples below and click 'submit'. Results will show up in a few seconds."
68
  article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2112.10003'>CLIPSeg: Image Segmentation Using Text and Image Prompts</a> | <a href='https://huggingface.co/docs/transformers/main/en/model_doc/clipseg'>HuggingFace docs</a></p>"
69
 
 
70
  with gr.Blocks() as demo:
71
  gr.Markdown("# CLIPSeg: Image Segmentation Using Text and Image Prompts")
72
  gr.Markdown(article)
 
118
  ],
119
  )
120
 
121
+ demo.launch(share=True)