Views
No views yet
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/mit-b4",
num_labels=2,
id2label=id2label,
label2id=label2id, )
1
2from torch import nn
3import numpy as np
4import matplotlib.pyplot as plt
5
6# Transforms
7_transform = A.Compose([
8 A.Resize(height = 512, width=512),
9 ToTensorV2(),
10])
11
12
13trans_image = _transform(image=np.array(image))
14outputs = model(trans_image['image'].float().unsqueeze(0))
15logits = outputs.logits.cpu()
16print(logits.shape)
17
18
19# First, rescale logits to original image size
20upsampled_logits = nn.functional.interpolate(logits,
21 size=image.size[::-1], # (height, width)
22 mode='bilinear',
23 align_corners=False)
24
25
26seg = upsampled_logits.argmax(dim=1)[0]
27color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8) # height, width, 3
28palette = np.array([[0, 0, 0],[255, 255, 255]])
29for label, color in enumerate(palette):
30 color_seg[seg == label, :] = color
31# Convert to BGR
32color_seg = color_seg[..., ::-1]
33