File size: 4,907 Bytes
61740cb 630cdf5 61740cb f755e76 61740cb 630cdf5 f755e76 630cdf5 f755e76 630cdf5 61740cb f755e76 120d732 f755e76 120d732 f755e76 120d732 9fe8f32 120d732 f755e76 61740cb 630cdf5 61740cb 120d732 61740cb f755e76 630cdf5 868a71f 630cdf5 f755e76 61740cb 61fd3f3 61740cb f755e76 630cdf5 f755e76 630cdf5 61740cb 630cdf5 61740cb 6a58a50 61740cb 630cdf5 61740cb 630cdf5 61740cb f755e76 |
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
"""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, hsv2rgb
from overrides import overrides
from matplotlib.cm import hot # pylint: disable=no-name-in-module
from .multitask_dataset import NpzRepresentation
from torch.nn import functional as F
class ColorRepresentation(NpzRepresentation):
def __init__(self, name: str, n_channels: int):
super().__init__(name)
self._n_channels = n_channels
@overrides
def load_from_disk(self, path: Path) -> tr.Tensor:
res = super().load_from_disk(path)
return res.float() / 255
@overrides
def save_to_disk(self, data: tr.Tensor, path: Path):
return super().save_to_disk((data * 255).byte(), path)
@property
@overrides
def n_channels(self) -> int:
return self._n_channels
class HSVRepresentation(ColorRepresentation):
@overrides
def load_from_disk(self, path: Path) -> tr.Tensor:
rgb = NpzRepresentation.load_from_disk(self, path)
return tr.from_numpy(rgb2hsv(rgb)).float()
@overrides
def save_to_disk(self, data: tr.Tensor, path: Path):
rgb = tr.from_numpy(hsv2rgb(data) * 255).byte()
NpzRepresentation.save_to_disk(rgb, path)
class EdgesRepresentation(NpzRepresentation):
@overrides
def load_from_disk(self, path: Path) -> tr.Tensor:
res = super().load_from_disk(path).float()
assert len(res.shape) == 3 and res.shape[-1] == 1
return res
@overrides
def plot_fn(self, x: tr.Tensor) -> np.ndarray:
return (x.detach().repeat(1, 1, 3) * 255).cpu().numpy().astype(np.uint8)
@property
@overrides
def n_channels(self) -> int:
return 1
class DepthRepresentation(NpzRepresentation):
"""DepthRepresentation. Implements depth task-specific stuff, like hotmap."""
def __init__(self, *args, min_depth: float, max_depth: float, **kwargs):
super().__init__(*args, **kwargs)
assert 0 <= min_depth < max_depth, (min_depth, max_depth)
self.min_depth = min_depth
self.max_depth = max_depth
@overrides
def plot_fn(self, x: tr.Tensor) -> np.ndarray:
x = x.detach().clip(0, 1).squeeze().cpu().numpy()
y: np.ndarray = hot(x)[..., 0:3] * 255
return y.astype(np.uint8)
@overrides
def load_from_disk(self, path: Path) -> tr.Tensor:
res = super().load_from_disk(path).squeeze().unsqueeze(-1)
res = res.float().clip(self.min_depth, self.max_depth)
res = (res - self.min_depth) / (self.max_depth - self.min_depth)
return res
@property
@overrides
def n_channels(self) -> int:
return 1
class NormalsRepresentation(NpzRepresentation):
@property
@overrides
def n_channels(self) -> int:
return 3
class OpticalFlowRepresentation(NpzRepresentation):
"""OpticalFlowRepresentation. Implements depth task-specific stuff, like using flow_vis."""
@overrides
def plot_fn(self, x: tr.Tensor) -> np.ndarray:
return flow_vis.flow_to_color(x.squeeze().nan_to_num(0).detach().cpu().numpy())
@overrides
def load_from_disk(self, path: Path) -> tr.Tensor:
return super().load_from_disk(path).float()
@property
@overrides
def n_channels(self) -> int:
return 2
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):
super().__init__(*args, **kwargs)
self.classes = list(range(classes)) if isinstance(classes, int) else classes
self.n_classes = len(self.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 = x.squeeze().nan_to_num(0).detach().argmax(-1).cpu().numpy()
new_images = np.zeros((*x.shape, 3), dtype=np.uint8)
for i in range(self.n_classes):
new_images[x == i] = self.color_map[i]
return new_images
@property
@overrides
def n_channels(self) -> int:
return self.n_classes
|