Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,14 +30,29 @@ def detect_using_clip(image,prompts=[],threshould=0.4):
|
|
| 30 |
)
|
| 31 |
with torch.no_grad(): # Use 'torch.no_grad()' to disable gradient computation
|
| 32 |
outputs = model(**inputs)
|
| 33 |
-
preds = outputs.logits.unsqueeze(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def visualize_images(image,predicted_images,brightness=15,contrast=1.8):
|
| 43 |
alpha = 0.7
|
|
@@ -50,27 +65,28 @@ def visualize_images(image,predicted_images,brightness=15,contrast=1.8):
|
|
| 50 |
return cv2.convertScaleAbs(resize_image_copy, alpha=contrast, beta=brightness)
|
| 51 |
|
| 52 |
def shot(alpha,beta,image,labels_text):
|
|
|
|
| 53 |
if "," in labels_text:
|
| 54 |
prompts = labels_text.split(',')
|
| 55 |
else:
|
| 56 |
prompts = [labels_text]
|
| 57 |
-
|
| 58 |
prompts = list(map(lambda x: x.strip(),prompts))
|
| 59 |
|
| 60 |
mask_labels = [f"{prompt}_{i}" for i,prompt in enumerate(prompts)]
|
| 61 |
cmap = plt.cm.tab20(np.arange(len(mask_labels)))[..., :-1]
|
| 62 |
|
| 63 |
-
resize_image = cv2.resize(image,(352,352))
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
return category_image
|
| 69 |
|
| 70 |
iface = gr.Interface(fn=shot,
|
| 71 |
inputs = [
|
| 72 |
gr.Slider(0.1, 1, value=0.4, step=0.1 , label="alpha", info="Choose between 0.1 to 1"),
|
| 73 |
-
gr.Slider(0.1, 1, value=
|
| 74 |
"image",
|
| 75 |
"text"
|
| 76 |
],
|
|
|
|
| 30 |
)
|
| 31 |
with torch.no_grad(): # Use 'torch.no_grad()' to disable gradient computation
|
| 32 |
outputs = model(**inputs)
|
| 33 |
+
#preds = outputs.logits.unsqueeze(1)
|
| 34 |
+
preds = nn.functional.interpolate(
|
| 35 |
+
outputs.logits.unsqueeze(1),
|
| 36 |
+
size=(test_image.shape[0], test_image.shape[1]),
|
| 37 |
+
mode="bilinear"
|
| 38 |
+
)
|
| 39 |
+
threshold = 0.1
|
| 40 |
|
| 41 |
+
flat_preds = torch.sigmoid(preds.squeeze()).reshape((preds.shape[0], -1))
|
| 42 |
+
|
| 43 |
+
# Initialize a dummy "unlabeled" mask with the threshold
|
| 44 |
+
flat_preds_with_treshold = torch.full((preds.shape[0] + 1, flat_preds.shape[-1]), threshold)
|
| 45 |
+
flat_preds_with_treshold[1:preds.shape[0]+1,:] = flat_preds
|
| 46 |
+
|
| 47 |
+
# Get the top mask index for each pixel
|
| 48 |
+
inds = torch.topk(flat_preds_with_treshold, 1, dim=0).indices.reshape((preds.shape[-2], preds.shape[-1]))
|
| 49 |
+
predicted_masks = []
|
| 50 |
+
|
| 51 |
+
for i in range(1, len(prompts)+1):
|
| 52 |
+
mask = np.where(inds==i,255,0)
|
| 53 |
+
predicted_masks.append(mask)
|
| 54 |
+
|
| 55 |
+
return predicted_masks
|
| 56 |
|
| 57 |
def visualize_images(image,predicted_images,brightness=15,contrast=1.8):
|
| 58 |
alpha = 0.7
|
|
|
|
| 65 |
return cv2.convertScaleAbs(resize_image_copy, alpha=contrast, beta=brightness)
|
| 66 |
|
| 67 |
def shot(alpha,beta,image,labels_text):
|
| 68 |
+
print(labels_text)
|
| 69 |
if "," in labels_text:
|
| 70 |
prompts = labels_text.split(',')
|
| 71 |
else:
|
| 72 |
prompts = [labels_text]
|
| 73 |
+
print(prompts)
|
| 74 |
prompts = list(map(lambda x: x.strip(),prompts))
|
| 75 |
|
| 76 |
mask_labels = [f"{prompt}_{i}" for i,prompt in enumerate(prompts)]
|
| 77 |
cmap = plt.cm.tab20(np.arange(len(mask_labels)))[..., :-1]
|
| 78 |
|
|
|
|
| 79 |
|
| 80 |
+
predicted_masks = detect_using_clip(image,prompts=prompts)
|
| 81 |
+
bool_masks = [predicted_mask.astype('bool') for predicted_mask in predicted_masks]
|
| 82 |
+
category_image = overlay_masks(resize_image,np.stack(bool_masks,-1),labels=mask_labels,colors=cmap,alpha=alpha,beta=beta)
|
| 83 |
|
| 84 |
return category_image
|
| 85 |
|
| 86 |
iface = gr.Interface(fn=shot,
|
| 87 |
inputs = [
|
| 88 |
gr.Slider(0.1, 1, value=0.4, step=0.1 , label="alpha", info="Choose between 0.1 to 1"),
|
| 89 |
+
gr.Slider(0.1, 1, value=0.7, step=0.1, label="beta", info="Choose between 0.1 to 1"),
|
| 90 |
"image",
|
| 91 |
"text"
|
| 92 |
],
|