deep-learning-analytics commited on
Commit
a604262
1 Parent(s): e8c8cc8

added spinners for all tasks

Browse files
Files changed (1) hide show
  1. app.py +29 -24
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
- feature_extractor = SegformerFeatureExtractor(align=False, reduce_zero_label=False)
24
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
25
- model = SegformerForSemanticSegmentation.from_pretrained("deep-learning-analytics/segformer_semantic_segmentation",
26
- ignore_mismatched_sizes=True,
27
- num_labels=len(id2label), id2label=id2label, label2id=label2id,
28
- reshape_last_stage=True)
29
- model = model.to(device)
30
- # prepare the image for the model (aligned resize)
31
- feature_extractor_inference = SegformerFeatureExtractor(do_random_crop=False, do_pad=False)
32
- pixel_values = feature_extractor_inference(image, return_tensors="pt").pixel_values.to(device)
33
- model.eval()
 
 
 
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
- logits = outputs.logits.cpu()
38
- # First, rescale logits to original image size
39
- upsampled_logits = nn.functional.interpolate(logits,
40
- size=image.shape[:-1], # (height, width)
41
- mode='bilinear',
42
- align_corners=False)
43
- # Second, apply argmax on the class dimension
44
- seg = upsampled_logits.argmax(dim=1)[0]
45
- color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) # height, width, 3\
46
- for label, color in enumerate(palette):
47
- color_seg[seg == label, :] = color
48
- # Convert to BGR
49
- color_seg = color_seg[..., ::-1]
 
 
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)