Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -110,35 +110,67 @@ def show_img(all_imgs, dropdown, bg, alpha_factor):
|
|
| 110 |
###########
|
| 111 |
# Inference
|
| 112 |
|
| 113 |
-
def inference(img
|
| 114 |
-
|
| 115 |
-
with torch.no_grad():
|
| 116 |
-
mask_logits = model(x)[0].cpu().numpy()
|
| 117 |
|
| 118 |
-
|
| 119 |
-
mask_preds = mask_probs > 0.5
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
| 125 |
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
pil_mask = Image.fromarray(img_mask)
|
| 132 |
-
all_images.append(pil_mask)
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
-
mask_pil = Image.fromarray((mask_preds[0]*255).astype(np.uint8))
|
| 141 |
-
return mask_pil, background
|
| 142 |
|
| 143 |
|
| 144 |
title = "Masterarbeit - Bauschadenerkennung"
|
|
|
|
| 110 |
###########
|
| 111 |
# Inference
|
| 112 |
|
| 113 |
+
def inference(img, alpha_factor):
|
| 114 |
+
background = resize_pil(img)
|
|
|
|
|
|
|
| 115 |
|
| 116 |
+
img = process_pil(img)
|
|
|
|
| 117 |
|
| 118 |
+
mask = model(img.unsqueeze(0)) # we need a batch, hence we introduce an extra dimenation at position 0 (unsqueeze)
|
| 119 |
+
mask = mask[0]
|
| 120 |
+
|
| 121 |
+
# Get probability values (logits to probs)
|
| 122 |
+
mask_probs = torch.sigmoid(mask)
|
| 123 |
+
mask_probs = mask_probs.detach().numpy()
|
| 124 |
+
mask_probs.shape
|
| 125 |
|
| 126 |
+
# Make binary mask
|
| 127 |
+
THRESHOLD = 0.5
|
| 128 |
+
mask_preds = mask_probs > THRESHOLD
|
| 129 |
|
| 130 |
+
# All combined
|
| 131 |
+
mask_all = mask_preds.sum(axis=0)
|
| 132 |
+
mask_all = np.expand_dims(mask_all, axis=0)
|
| 133 |
+
mask_all.shape
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
# Concat all combined with normal preds
|
| 136 |
+
mask_preds = np.concatenate((mask_all, mask_preds),axis=0)
|
| 137 |
+
labs = ["ALL"] + target_list
|
| 138 |
+
|
| 139 |
+
fig, axes = plt.subplots(5, 4, figsize = (10,10))
|
| 140 |
+
|
| 141 |
+
# save all mask_preds in all_mask
|
| 142 |
+
all_masks = []
|
| 143 |
+
|
| 144 |
+
for i, ax in enumerate(axes.flat):
|
| 145 |
+
label = labs[i]
|
| 146 |
+
|
| 147 |
+
all_masks.append(mask_preds[i])
|
| 148 |
+
|
| 149 |
+
ax.imshow(mask_preds[i])
|
| 150 |
+
ax.set_title(label)
|
| 151 |
+
|
| 152 |
+
plt.tight_layout()
|
| 153 |
+
|
| 154 |
+
# plt to PIL
|
| 155 |
+
img_buf = io.BytesIO()
|
| 156 |
+
fig.savefig(img_buf, format='png')
|
| 157 |
+
im = Image.open(img_buf)
|
| 158 |
+
|
| 159 |
+
# Saved all masks combined with unvisible xaxis und yaxis and without a white
|
| 160 |
+
# background.
|
| 161 |
+
all_images = []
|
| 162 |
+
for i in range(len(all_masks)):
|
| 163 |
+
plt.figure()
|
| 164 |
+
fig = plt.imshow(all_masks[i])
|
| 165 |
+
plt.axis('off')
|
| 166 |
+
fig.axes.get_xaxis().set_visible(False)
|
| 167 |
+
fig.axes.get_yaxis().set_visible(False)
|
| 168 |
+
img_buf = io.BytesIO()
|
| 169 |
+
plt.savefig(img_buf, bbox_inches='tight', pad_inches = 0, format='png')
|
| 170 |
+
all_images.append(Image.open(img_buf))
|
| 171 |
+
|
| 172 |
+
return im, all_images, background
|
| 173 |
|
|
|
|
|
|
|
| 174 |
|
| 175 |
|
| 176 |
title = "Masterarbeit - Bauschadenerkennung"
|