Repository navigation
Expand file tree
/
Copy pathdemo.py
More file actions
executable file
·64 lines (56 loc) · 2.04 KB
/
Copy pathdemo.py
File metadata and controls
executable file
·64 lines (56 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import logging
import torch
import torchvision.io
from torch import Tensor
from cups.metrics import PanopticQualitySemanticMatching
from cups.model import (
panoptic_cascade_mask_r_cnn_from_checkpoint,
prediction_to_standard_format,
)
from cups.visualization import plot_panoptic_segmentation_overlay
logging.basicConfig(format="%(message)s")
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
IMAGE_PATH: str = "assets/stuttgart_02_000000_005445_leftImg8bit.png"
DEVICE: str = "cpu"
CHECKPOINT_PATH = "assets/cups.ckpt"
@torch.no_grad()
def main() -> None:
# Init model
model, num_clusters_things, num_clusters_stuffs = panoptic_cascade_mask_r_cnn_from_checkpoint(
path=CHECKPOINT_PATH,
device="cpu",
confidence_threshold=0.5,
)
# Print model
log.info(model)
# Model to device
model = model.to(DEVICE)
# Model to eval mode
model = model.eval()
# Assignments of CUPS checkpoint
assignments: Tensor = torch.tensor(
[7, 4, 2, 4, 2, 6, 2, 0, 5, 8, 0, 2, 9, 10, 3, 8, 1, 2, 0, 0, 0, 0, 11, 13, 18, 15, 14], device=DEVICE
)
# Load image and map pixel-range to [0, 1]
image: Tensor = torchvision.io.read_image(IMAGE_PATH).float() / 255.0
# Image to device
image = image.to(DEVICE)
# Make panoptic prediction
prediction = model([{"image": image.to(DEVICE)}])
# Convert prediction into standard format
prediction: Tensor = prediction_to_standard_format(
prediction[0]["panoptic_seg"],
stuff_classes=tuple(index for index in range(num_clusters_stuffs)),
thing_classes=tuple(index + num_clusters_stuffs for index in range(num_clusters_things)),
)
# Remap semantics for visualization
prediction = PanopticQualitySemanticMatching.map_to_target(
panoptic_segmentation=prediction, assignments=assignments
)
# Plot panoptic prediction
plot_panoptic_segmentation_overlay(
panoptic_segmentation=prediction.cpu(), image=image.cpu(), dataset="cityscapes_19"
)
if __name__ == "__main__":
main()