File size: 4,605 Bytes
61740cb 630cdf5 61740cb 0094b94 61740cb 630cdf5 85a37ac 5e6866e 0094b94 f755e76 0094b94 f755e76 630cdf5 0094b94 f755e76 120d732 0094b94 f755e76 61740cb 0094b94 61740cb 120d732 0094b94 120d732 61740cb f755e76 0094b94 f755e76 61740cb 0094b94 61740cb f755e76 0094b94 630cdf5 61740cb 0094b94 61740cb 630cdf5 61740cb 6a58a50 61740cb 630cdf5 61740cb 0094b94 61740cb 0094b94 61740cb f755e76 5e6866e 0094b94 5e6866e 0094b94 5e6866e 0094b94 5e6866e |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
"""Dronescapes representations -- adds various loading/writing/image showing capabilities to dronescapes tasks"""
from __future__ import annotations
from pathlib import Path
import numpy as np
import torch as tr
import flow_vis
from skimage.color import rgb2hsv
from overrides import overrides
from matplotlib.cm import hot # pylint: disable=no-name-in-module
from torch.nn import functional as F
try:
from npz_representation import NpzRepresentation
except ImportError:
from .npz_representation import NpzRepresentation
class RGBRepresentation(NpzRepresentation):
def __init__(self, name: str):
super().__init__(name, n_channels=3)
class HSVRepresentation(RGBRepresentation):
@overrides
def load_from_disk(self, path: Path) -> tr.Tensor:
rgb = super().load_from_disk(path)
return tr.from_numpy(rgb2hsv(rgb)).float()
class EdgesRepresentation(NpzRepresentation):
def __init__(self, name: str):
super().__init__(name, n_channels=1)
class DepthRepresentation(NpzRepresentation):
"""DepthRepresentation. Implements depth task-specific stuff, like hotmap."""
def __init__(self, name: str):
super().__init__(name, n_channels=1)
@overrides
def plot_fn(self, x: tr.Tensor) -> np.ndarray:
x = x.detach().clip(0, 1).squeeze().cpu().numpy()
_min, _max = np.percentile(x, [5, 95])
x = np.nan_to_num((x - _min) / (_max - _min), False, 0, 0, 0).clip(0, 1)
y: np.ndarray = hot(x)[..., 0:3] * 255
return y.astype(np.uint8)
class NormalsRepresentation(NpzRepresentation):
def __init__(self, name: str):
super().__init__(name, n_channels=3)
class OpticalFlowRepresentation(NpzRepresentation):
"""OpticalFlowRepresentation. Implements depth task-specific stuff, like using flow_vis."""
def __init__(self, name: str):
super().__init__(name, n_channels=2)
@overrides
def plot_fn(self, x: tr.Tensor) -> np.ndarray:
_min, _max = x.min(0)[0].min(0)[0], x.max(0)[0].max(0)[0]
x = ((x - _min) / (_max - _min)).nan_to_num(0, 0, 0).detach().cpu().numpy()
return flow_vis.flow_to_color(x)
class SemanticRepresentation(NpzRepresentation):
"""SemanticRepresentation. Implements depth task-specific stuff, like using flow_vis."""
def __init__(self, *args, classes: int | list[str], color_map: list[tuple[int, int, int]], **kwargs):
self.n_classes = len(list(range(classes)) if isinstance(classes, int) else classes)
super().__init__(*args, **kwargs, n_channels=self.n_classes)
self.classes = list(range(classes)) if isinstance(classes, int) else classes
self.color_map = color_map
assert len(color_map) == self.n_classes and self.n_classes > 1, (color_map, self.n_classes)
@overrides
def load_from_disk(self, path: Path) -> tr.Tensor:
res = super().load_from_disk(path)
if len(res.shape) == 3:
assert res.shape[-1] == self.n_classes, f"Expected {self.n_classes} (HxWxC), got {res.shape[-1]}"
res = res.argmax(-1)
assert len(res.shape) == 2, f"Only argmaxed data supported, got: {res.shape}"
res = F.one_hot(res.long(), num_classes=self.n_classes).float()
return res
@overrides
def plot_fn(self, x: tr.Tensor) -> np.ndarray:
x_argmax = x.squeeze().nan_to_num(0).detach().argmax(-1).cpu().numpy()
new_images = np.zeros((*x_argmax.shape, 3), dtype=np.uint8)
for i in range(self.n_classes):
new_images[x_argmax == i] = self.color_map[i]
return new_images
_color_map = [[0, 255, 0], [0, 127, 0], [255, 255, 0], [255, 255, 255],
[255, 0, 0], [0, 0, 255], [0, 255, 255], [127, 127, 63]]
_m2f_name = "semantic_mask2former_swin_mapillary_converted"
dronescapes_task_types = { # some pre-baked representations
"rgb": RGBRepresentation("rgb"),
"hsv": HSVRepresentation("hsv"),
"edges_dexined": EdgesRepresentation("edges_dexined"),
"edges_gb": EdgesRepresentation("edges_gb"),
"depth_dpt": DepthRepresentation("depth_dpt"),
"depth_sfm_manual202204": DepthRepresentation("depth_sfm_manual202204"),
"depth_ufo": DepthRepresentation("depth_ufo"),
"normals_sfm_manual202204": NormalsRepresentation("normals_sfm_manual202204"),
"opticalflow_rife": OpticalFlowRepresentation("opticalflow_rife"),
"semantic_segprop8": SemanticRepresentation("semantic_segprop8", classes=8, color_map=_color_map),
_m2f_name: SemanticRepresentation(_m2f_name, classes=8, color_map=_color_map),
"softseg_gb": NpzRepresentation("softseg_gb", 3),
}
|