Commit
·
a604262
1
Parent(s):
e8c8cc8
added spinners for all tasks
Browse files
app.py
CHANGED
|
@@ -20,33 +20,38 @@ if raw_image is not None:
|
|
| 20 |
image = Image.open(raw_image)
|
| 21 |
image = np.asarray(image)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
with st.spinner('Running inference...'):
|
| 36 |
outputs = model(pixel_values=pixel_values)# logits are of shape (batch_size, num_labels, height/4, width/4)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
# Show image + mask
|
| 51 |
img = np.array(image) * 0.5 + color_seg * 0.5
|
| 52 |
img = img.astype(np.uint8)
|
|
|
|
| 20 |
image = Image.open(raw_image)
|
| 21 |
image = np.asarray(image)
|
| 22 |
|
| 23 |
+
with st.spinner('Loading Model...'):
|
| 24 |
+
feature_extractor = SegformerFeatureExtractor(align=False, reduce_zero_label=False)
|
| 25 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 26 |
+
model = SegformerForSemanticSegmentation.from_pretrained("deep-learning-analytics/segformer_semantic_segmentation",
|
| 27 |
+
ignore_mismatched_sizes=True,
|
| 28 |
+
num_labels=len(id2label), id2label=id2label, label2id=label2id,
|
| 29 |
+
reshape_last_stage=True)
|
| 30 |
+
model = model.to(device)
|
| 31 |
+
model.eval()
|
| 32 |
+
|
| 33 |
+
with st.spinner('Preparing image...'):
|
| 34 |
+
# prepare the image for the model (aligned resize)
|
| 35 |
+
feature_extractor_inference = SegformerFeatureExtractor(do_random_crop=False, do_pad=False)
|
| 36 |
+
pixel_values = feature_extractor_inference(image, return_tensors="pt").pixel_values.to(device)
|
| 37 |
|
| 38 |
with st.spinner('Running inference...'):
|
| 39 |
outputs = model(pixel_values=pixel_values)# logits are of shape (batch_size, num_labels, height/4, width/4)
|
| 40 |
+
|
| 41 |
+
with st.spinner('Postprocessing...'):
|
| 42 |
+
logits = outputs.logits.cpu()
|
| 43 |
+
# First, rescale logits to original image size
|
| 44 |
+
upsampled_logits = nn.functional.interpolate(logits,
|
| 45 |
+
size=image.shape[:-1], # (height, width)
|
| 46 |
+
mode='bilinear',
|
| 47 |
+
align_corners=False)
|
| 48 |
+
# Second, apply argmax on the class dimension
|
| 49 |
+
seg = upsampled_logits.argmax(dim=1)[0]
|
| 50 |
+
color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) # height, width, 3\
|
| 51 |
+
for label, color in enumerate(palette):
|
| 52 |
+
color_seg[seg == label, :] = color
|
| 53 |
+
# Convert to BGR
|
| 54 |
+
color_seg = color_seg[..., ::-1]
|
| 55 |
# Show image + mask
|
| 56 |
img = np.array(image) * 0.5 + color_seg * 0.5
|
| 57 |
img = img.astype(np.uint8)
|