|
"""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 |
|
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 = { |
|
"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), |
|
} |
|
|