# Path: datasets/DataKeys.py
# SEGMENTATION_LABELS = "segmentation_labels"
# SEGMENTATION_LABELS_ORIGINAL_SIZE = "segmentation_labels_original_size"
# BBOX_GUIDANCE = "bbox_guidance"
# UNSIGNED_DISTANCE_TRANSFORM_GUIDANCE = "unsigned_distance_transform_guidance"
# SIGNED_DISTANCE_TRANSFORM_GUIDANCE = "signed_distance_transform_guidance"
# LASER_GUIDANCE = "laser_guidance"
# IMAGES = "images"
# RAW_IMAGES = "raw_images"
# INPUTS = "inputs"
# IMAGE_FILENAMES = "image_filenames"
# CLASSES = "classes"
# IDS = "ids"  # bounding box number 1,2,3...  or 0 if no bounding box
# DT_METHOD = "dt_method"
# RAW_SEGMENTATION_LABELS = "raw_segmentation_labels"
# CLICK_MAPS = "clicks_maps"
# NEG_CLICKS = "neg_clicks"
# POS_CLICKS = "pos_clicks"
# OBJ_TAGS = "obj_tags"
# FEATUREMAP_LABELS = "featuremap_labels"
# FEATUREMAP_BOXES = "featuremap_boxes"
# IMAGE_ID = "image_id"
# IS_CROWD = "is_crowd"
# SEGMENTATION_MASK = "segmentation_mask"
# SKIP_EXAMPLE = "skip_example"  # If true, filter the corresponding example from the datastream. Useful, when an example
# RAW_IMAGE_SIZES = "raw_image_sizes"  # h, w
# TIME_OFFSETS = "time_offsets"
# SPACE_OFFSETS = "space_offsets"
# SEGMENTATION_INSTANCE_LABELS = "segmentation_instance_labels"
# N_VALID_IDS = "n_valid_ids"
# 
# Path: datasets/KITTI/segtrack/KITTI_segtrack.py
# class KittiSegtrackDataset(KittiSegtrackDetectionDataset):
#   def __init__(self, config, subset, name=NAME, default_path=DEFAULT_PATH):
#     # batch size here is the number of time steps considered in a chunk
#     # TODO: what do we do at test time?
#     self._batch_size = config.int("batch_size")
#     assert self._batch_size > 1, "use KittiSegtrackDetectionDataset for single image training"
#     super().__init__(config, subset, name, default_path)
# 
#   def read_inputfile_lists(self):
#     seq_ids = self.seq_ids_train if self.subset == "train" else self.seq_ids_val
#     anns = []
#     for seq_id in seq_ids:
#       anns_vid = sorted(glob.glob(self.data_dir + "/instances/" + seq_id + "/*.png"))
#       starting_points = anns_vid[:-(self._batch_size - 1)]
#       anns += starting_points
# 
#     imgs = [x.replace("/instances/", "/images/") for x in anns]
#     return imgs, anns
# 
#   def _batch(self, tfdata, batch_size):
#     return tfdata
# 
#   def load_example(self, input_filenames):
#     examples = []
#     for delta_t in range(self._batch_size):
#       input_filenames_t = [successor_frame_filename(fn, delta_t) for fn in input_filenames]
#       raw_example = self.load_raw_example(*input_filenames_t)
#       examples.append(raw_example)
# 
#     # cf process_raw_example
#     # here we need to do it jointly to synchronize the augmentation (e.g. flipping)
#     examples = [self.postproc_example_initial(example) for example in examples]
#     examples = self.jointly_augment_examples_before_resize(examples)
#     examples = [self.postproc_example_before_resize(example) for example in examples]
#     examples = [self.resize_example(example) for example in examples]
#     examples = self.jointly_augment_examples_after_resize(examples)
#     examples = [self.postproc_example_before_assembly(example) for example in examples]
#     examples = [self.assemble_example(example) for example in examples]
# 
#     # stack everything together
#     examples_stacked = {}
#     for key in examples[0].keys():
#       if key == DataKeys.SKIP_EXAMPLE:
#         stacked = tf.reduce_any([example[key] for example in examples])
#       else:
#         stacked = tf.stack([example[key] for example in examples], axis=0)
#       examples_stacked[key] = stacked
#     return examples_stacked
# 
#   def n_examples_per_epoch(self):
#     n_examples = super().n_examples_per_epoch()
#     if n_examples == len(self.inputfile_lists[0]):
#       return n_examples * self._batch_size
# 
# Path: datasets/Loader.py
# def register_dataset(name, **args):
#   name = name.lower()
# 
#   def _register(dataset):
#     _registered_datasets[name] = (dataset, args)
#     return dataset
#   return _register


import numpy as np
import tensorflow as tf
from datasets import DataKeys
from datasets.KITTI.segtrack.KITTI_segtrack import KittiSegtrackDataset
from datasets.Loader import register_dataset


NAME = "KITTI_segtrack_amodal"


@register_dataset(NAME)
class KittiSegtrackAmodalDataset(KittiSegtrackDataset):
  def __init__(self, config, subset):
    super().__init__(config, subset, NAME)

  def postproc_annotation(self, ann_filename, ann):
    ann = super().postproc_annotation(ann_filename, ann)
    if not isinstance(ann, dict):

# Path: datasets/DataKeys.py
# SEGMENTATION_LABELS = "segmentation_labels"
# SEGMENTATION_LABELS_ORIGINAL_SIZE = "segmentation_labels_original_size"
# BBOX_GUIDANCE = "bbox_guidance"
# UNSIGNED_DISTANCE_TRANSFORM_GUIDANCE = "unsigned_distance_transform_guidance"
# SIGNED_DISTANCE_TRANSFORM_GUIDANCE = "signed_distance_transform_guidance"
# LASER_GUIDANCE = "laser_guidance"
# IMAGES = "images"
# RAW_IMAGES = "raw_images"
# INPUTS = "inputs"
# IMAGE_FILENAMES = "image_filenames"
# CLASSES = "classes"
# IDS = "ids"  # bounding box number 1,2,3...  or 0 if no bounding box
# DT_METHOD = "dt_method"
# RAW_SEGMENTATION_LABELS = "raw_segmentation_labels"
# CLICK_MAPS = "clicks_maps"
# NEG_CLICKS = "neg_clicks"
# POS_CLICKS = "pos_clicks"
# OBJ_TAGS = "obj_tags"
# FEATUREMAP_LABELS = "featuremap_labels"
# FEATUREMAP_BOXES = "featuremap_boxes"
# IMAGE_ID = "image_id"
# IS_CROWD = "is_crowd"
# SEGMENTATION_MASK = "segmentation_mask"
# SKIP_EXAMPLE = "skip_example"  # If true, filter the corresponding example from the datastream. Useful, when an example
# RAW_IMAGE_SIZES = "raw_image_sizes"  # h, w
# TIME_OFFSETS = "time_offsets"
# SPACE_OFFSETS = "space_offsets"
# SEGMENTATION_INSTANCE_LABELS = "segmentation_instance_labels"
# N_VALID_IDS = "n_valid_ids"
# 
# Path: datasets/KITTI/segtrack/KITTI_segtrack.py
# class KittiSegtrackDataset(KittiSegtrackDetectionDataset):
#   def __init__(self, config, subset, name=NAME, default_path=DEFAULT_PATH):
#     # batch size here is the number of time steps considered in a chunk
#     # TODO: what do we do at test time?
#     self._batch_size = config.int("batch_size")
#     assert self._batch_size > 1, "use KittiSegtrackDetectionDataset for single image training"
#     super().__init__(config, subset, name, default_path)
# 
#   def read_inputfile_lists(self):
#     seq_ids = self.seq_ids_train if self.subset == "train" else self.seq_ids_val
#     anns = []
#     for seq_id in seq_ids:
#       anns_vid = sorted(glob.glob(self.data_dir + "/instances/" + seq_id + "/*.png"))
#       starting_points = anns_vid[:-(self._batch_size - 1)]
#       anns += starting_points
# 
#     imgs = [x.replace("/instances/", "/images/") for x in anns]
#     return imgs, anns
# 
#   def _batch(self, tfdata, batch_size):
#     return tfdata
# 
#   def load_example(self, input_filenames):
#     examples = []
#     for delta_t in range(self._batch_size):
#       input_filenames_t = [successor_frame_filename(fn, delta_t) for fn in input_filenames]
#       raw_example = self.load_raw_example(*input_filenames_t)
#       examples.append(raw_example)
# 
#     # cf process_raw_example
#     # here we need to do it jointly to synchronize the augmentation (e.g. flipping)
#     examples = [self.postproc_example_initial(example) for example in examples]
#     examples = self.jointly_augment_examples_before_resize(examples)
#     examples = [self.postproc_example_before_resize(example) for example in examples]
#     examples = [self.resize_example(example) for example in examples]
#     examples = self.jointly_augment_examples_after_resize(examples)
#     examples = [self.postproc_example_before_assembly(example) for example in examples]
#     examples = [self.assemble_example(example) for example in examples]
# 
#     # stack everything together
#     examples_stacked = {}
#     for key in examples[0].keys():
#       if key == DataKeys.SKIP_EXAMPLE:
#         stacked = tf.reduce_any([example[key] for example in examples])
#       else:
#         stacked = tf.stack([example[key] for example in examples], axis=0)
#       examples_stacked[key] = stacked
#     return examples_stacked
# 
#   def n_examples_per_epoch(self):
#     n_examples = super().n_examples_per_epoch()
#     if n_examples == len(self.inputfile_lists[0]):
#       return n_examples * self._batch_size
# 
# Path: datasets/Loader.py
# def register_dataset(name, **args):
#   name = name.lower()
# 
#   def _register(dataset):
#     _registered_datasets[name] = (dataset, args)
#     return dataset
#   return _register


# Path: datasets/KITTI/segtrack/KITTI_segtrack_amodal.py
import numpy as np
import tensorflow as tf
from datasets import DataKeys
from datasets.KITTI.segtrack.KITTI_segtrack import KittiSegtrackDataset
from datasets.Loader import register_dataset



NAME = "KITTI_segtrack_amodal"


@register_dataset(NAME)
class KittiSegtrackAmodalDataset(KittiSegtrackDataset):
  def __init__(self, config, subset):
    super().__init__(config, subset, NAME)

  def postproc_annotation(self, ann_filename, ann):
    ann = super().postproc_annotation(ann_filename, ann)
    if not isinstance(ann, dict):

      ann = {DataKeys.SEGMENTATION_LABELS: ann}