Spaces:
Sleeping
Sleeping
Update segment.py
Browse files- segment.py +13 -5
segment.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
|
| 2 |
from transformers import AutoImageProcessor, Mask2FormerForUniversalSegmentation
|
| 3 |
from PIL import Image
|
| 4 |
-
import spaces
|
| 5 |
import torch
|
| 6 |
from collections import defaultdict
|
|
|
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
from matplotlib import cm
|
| 9 |
import matplotlib.patches as mpatches
|
|
@@ -14,6 +14,15 @@ import matplotlib
|
|
| 14 |
import gradio as gr
|
| 15 |
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def load_image(image_path, left=0, right=0, top=0, bottom=0, size = 512):
|
| 18 |
if type(image_path) is str:
|
| 19 |
image = np.array(Image.open(image_path))[:, :, :3]
|
|
@@ -94,17 +103,16 @@ def draw_panoptic_segmentation(segmentation, segments_info,save_folder=None, nos
|
|
| 94 |
# plt.savefig(os.path.join(save_folder, 'mask_clear.png'), dpi=500)
|
| 95 |
ax.legend(handles=handles)
|
| 96 |
plt.savefig(os.path.join(save_folder, 'seg_init.png'), dpi=500 )
|
|
|
|
| 97 |
print("; ".join(label_list))
|
| 98 |
return mask_np_list,label_list
|
| 99 |
|
| 100 |
|
| 101 |
-
@spaces.GPU(duration=10)
|
| 102 |
def run_segmentation(image, name="example_tmp", size = 512, noseg=False):
|
| 103 |
|
| 104 |
base_folder_path = "."
|
| 105 |
|
| 106 |
-
processor =
|
| 107 |
-
model = Mask2FormerForUniversalSegmentation.from_pretrained("facebook/mask2former-swin-base-coco-panoptic")
|
| 108 |
|
| 109 |
|
| 110 |
# input_folder = os.path.join(base_folder_path, name )
|
|
@@ -117,7 +125,7 @@ def run_segmentation(image, name="example_tmp", size = 512, noseg=False):
|
|
| 117 |
os.makedirs(name, exist_ok=True)
|
| 118 |
#image.save(os.path.join(name,"img_{}.png".format(size)))
|
| 119 |
inputs = processor(image, return_tensors="pt")
|
| 120 |
-
with torch.
|
| 121 |
outputs = model(**inputs)
|
| 122 |
|
| 123 |
panoptic_segmentation = processor.post_process_panoptic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
|
|
|
|
| 1 |
|
| 2 |
from transformers import AutoImageProcessor, Mask2FormerForUniversalSegmentation
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
import torch
|
| 5 |
from collections import defaultdict
|
| 6 |
+
from functools import lru_cache
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
from matplotlib import cm
|
| 9 |
import matplotlib.patches as mpatches
|
|
|
|
| 14 |
import gradio as gr
|
| 15 |
|
| 16 |
|
| 17 |
+
@lru_cache(maxsize=1)
|
| 18 |
+
def get_segmentation_model():
|
| 19 |
+
model_id = "facebook/mask2former-swin-base-coco-panoptic"
|
| 20 |
+
processor = AutoImageProcessor.from_pretrained(model_id)
|
| 21 |
+
model = Mask2FormerForUniversalSegmentation.from_pretrained(model_id)
|
| 22 |
+
model.eval()
|
| 23 |
+
return processor, model
|
| 24 |
+
|
| 25 |
+
|
| 26 |
def load_image(image_path, left=0, right=0, top=0, bottom=0, size = 512):
|
| 27 |
if type(image_path) is str:
|
| 28 |
image = np.array(Image.open(image_path))[:, :, :3]
|
|
|
|
| 103 |
# plt.savefig(os.path.join(save_folder, 'mask_clear.png'), dpi=500)
|
| 104 |
ax.legend(handles=handles)
|
| 105 |
plt.savefig(os.path.join(save_folder, 'seg_init.png'), dpi=500 )
|
| 106 |
+
plt.close(fig)
|
| 107 |
print("; ".join(label_list))
|
| 108 |
return mask_np_list,label_list
|
| 109 |
|
| 110 |
|
|
|
|
| 111 |
def run_segmentation(image, name="example_tmp", size = 512, noseg=False):
|
| 112 |
|
| 113 |
base_folder_path = "."
|
| 114 |
|
| 115 |
+
processor, model = get_segmentation_model()
|
|
|
|
| 116 |
|
| 117 |
|
| 118 |
# input_folder = os.path.join(base_folder_path, name )
|
|
|
|
| 125 |
os.makedirs(name, exist_ok=True)
|
| 126 |
#image.save(os.path.join(name,"img_{}.png".format(size)))
|
| 127 |
inputs = processor(image, return_tensors="pt")
|
| 128 |
+
with torch.inference_mode():
|
| 129 |
outputs = model(**inputs)
|
| 130 |
|
| 131 |
panoptic_segmentation = processor.post_process_panoptic_segmentation(outputs, target_sizes=[image.size[::-1]])[0]
|