Meehai's picture
one more
f518336
#!/usr/bin/env python3
import sys
import os
os.environ["STATS_PBAR"] = "1"
os.environ["VRE_LOGLEVEL"] = "0"
import random
from pathlib import Path
sys.path.append(Path.cwd().parent.__str__())
from pprint import pprint
from vre.readers.multitask_dataset import MultiTaskDataset
from vre.representations import Representation, ReprOut
from vre.utils import MemoryData, reorder_dict, lo
import numpy as np
import torch as tr
from media_processing_lib.collage_maker import collage_fn
from media_processing_lib.image import image_add_title, image_write
import matplotlib.pyplot as plt
from datetime import datetime
from dronescapes_representations import get_dronescapes_task_types
def plot_one(data: dict[str, tr.Tensor], title: str, name_to_task: dict[str, Representation],
order: list[str] | None = None) -> np.ndarray:
def vre_plot_fn(rgb: tr.Tensor, x: tr.Tensor, node: Representation) -> np.ndarray:
node.data = ReprOut(rgb.cpu().detach().numpy()[None], MemoryData(x.cpu().detach().numpy()[None]), [0])
return node.make_images()[0]
img_data = {}
keys = np.random.permutation(list(data.keys()))
for k in keys:
start = datetime.now()
img_data[k] = vre_plot_fn(data["rgb"], data[k], name_to_task[k])
print(k, (datetime.now() - start).total_seconds())
img_data = reorder_dict(img_data, order) if order is not None else img_data
titles = [title if len(title) < 40 else f"{title[0:19]}..{title[-19:]}" for title in img_data]
collage = collage_fn(list(img_data.values()), titles=titles, size_px=40)
collage = image_add_title(collage, title, size_px=55, top_padding=110)
return collage
data_path = "../../data/test_set"
stats_path = "../../data/train_set/.task_statistics.npz"
dronescapes_task_types = get_dronescapes_task_types(include_semantics_original=False, include_gt=True, include_ci=False)
task_names = ["rgb", "semantic_mask2former_r50_mapillary_converted", "semantic_mask2former_swin_coco_converted"]
reader = MultiTaskDataset(data_path, task_names=task_names,
task_types=dronescapes_task_types, handle_missing_data="fill_nan",
normalization="min_max", cache_task_stats=True, batch_size_stats=300,
statistics=np.load(stats_path, allow_pickle=True)["arr_0"].item())
print(reader)
print("== Shapes ==")
pprint(reader.data_shape)
print("== Random loaded item ==")
rand_ix = random.randint(0, len(reader) - 1)
# rand_ix = "norway_210821_DJI_0015_full_2774.npz"
data, name = reader[rand_ix] # get a random item
print(name)
collage = plot_one(data, title=name, name_to_task=reader.name_to_task)
print(lo(collage))
# plt.figure(figsize=(20, 10))
# plt.imshow(collage)
image_write(collage, out_path := f"collage_{name[0:-4]}.png")
print(f"Stored at '{out_path}'")