loving zero gpu as much as i love taxes
Browse files
app.py
CHANGED
@@ -7,8 +7,8 @@ from transformers import AutoModelForImageSegmentation
|
|
7 |
from diffusers import FluxFillPipeline
|
8 |
from PIL import Image, ImageOps
|
9 |
|
10 |
-
|
11 |
-
|
12 |
from simple_lama_inpainting import SimpleLama
|
13 |
from contextlib import contextmanager
|
14 |
|
@@ -135,36 +135,35 @@ def rmbg(image=None, url=None):
|
|
135 |
|
136 |
|
137 |
def mask_generation(image=None, d=None):
|
138 |
-
|
139 |
-
#
|
140 |
-
# #
|
141 |
-
#
|
142 |
-
#
|
143 |
-
#
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
# return out
|
168 |
|
169 |
|
170 |
def erase(image=None, mask=None):
|
|
|
7 |
from diffusers import FluxFillPipeline
|
8 |
from PIL import Image, ImageOps
|
9 |
|
10 |
+
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
11 |
+
import numpy as np
|
12 |
from simple_lama_inpainting import SimpleLama
|
13 |
from contextlib import contextmanager
|
14 |
|
|
|
135 |
|
136 |
|
137 |
def mask_generation(image=None, d=None):
|
138 |
+
# use bfloat16 for the entire notebook
|
139 |
+
# torch.autocast("cuda", dtype=torch.bfloat16).__enter__()
|
140 |
+
# # turn on tfloat32 for Ampere GPUs (https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices)
|
141 |
+
# if torch.cuda.get_device_properties(0).major >= 8:
|
142 |
+
# torch.backends.cuda.matmul.allow_tf32 = True
|
143 |
+
# torch.backends.cudnn.allow_tf32 = True
|
144 |
+
d = eval(d) # convert this to dictionary
|
145 |
+
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16):
|
146 |
+
predictor = SAM2ImagePredictor.from_pretrained("facebook/sam2.1-hiera-large")
|
147 |
+
predictor.set_image(image)
|
148 |
+
input_point = np.array(d["input_points"])
|
149 |
+
input_label = np.array(d["input_labels"])
|
150 |
+
masks, scores, logits = predictor.predict(
|
151 |
+
point_coords=input_point,
|
152 |
+
point_labels=input_label,
|
153 |
+
multimask_output=True,
|
154 |
+
)
|
155 |
+
sorted_ind = np.argsort(scores)[::-1]
|
156 |
+
masks = masks[sorted_ind]
|
157 |
+
scores = scores[sorted_ind]
|
158 |
+
logits = logits[sorted_ind]
|
159 |
+
|
160 |
+
out = []
|
161 |
+
for i in range(len(masks)):
|
162 |
+
m = Image.fromarray(masks[i] * 255).convert("L")
|
163 |
+
comp = Image.composite(image, m, m)
|
164 |
+
out.append((comp, f"image {i}"))
|
165 |
+
|
166 |
+
return out
|
|
|
167 |
|
168 |
|
169 |
def erase(image=None, mask=None):
|