# 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/DetectionDataset.py
# class MapillaryLikeDetectionFileListDataset(DetectionFileListDataset):
#   def __init__(self, config, dataset_name, subset, default_path, num_classes, n_max_detections,
#                class_ids_with_instances, id_divisor, crowd_id=None):
#     super().__init__(config, dataset_name, subset, default_path, num_classes)
#     self._n_max_detections = n_max_detections
#     self._class_ids_with_instances = class_ids_with_instances
#     self._id_divisor = id_divisor
#     self._crowd_id = crowd_id
# 
#     self._preload_dataset = config.bool("preload_dataset", False)
#     if self._preload_dataset:
#       print("Preloading ENTIRE!!! dataset into memory!!!", file=log.v1)
#       imgs, anns = self.read_inputfile_lists()
#       self.imgs_preload_dict = {img_filename: np.array(Image.open(img_filename), dtype=np.float32) / 255.0
#                                 for img_filename in imgs}
#       self.anns_preload_dict = {ann_filename: np.array(Image.open(ann_filename)) for ann_filename in anns}
#       self._load_ann_np = self._anns_preload_dict_lookup
#     else:
#       self._load_ann_np = partial(load_instance_seg_annotation_np, n_max_detections=self._n_max_detections,
#                                   class_ids_with_instances=self._class_ids_with_instances, id_divisor=self._id_divisor,
#                                   crowd_id=self._crowd_id)
# 
#   def _anns_preload_dict_lookup(self, ann_filename):
#     ann_filename = ann_filename.decode("utf-8")
#     ann = self.anns_preload_dict[ann_filename]
#     return load_instance_seg_annotation_from_img_array_np(ann, n_max_detections=self._n_max_detections,
#                                                           class_ids_with_instances=self._class_ids_with_instances, id_divisor=self._id_divisor,
#                                                           crowd_id=self._crowd_id)
# 
#   def _imgs_preload_dict_lookup(self, img_filename):
#     img_filename = img_filename.decode("utf-8")
#     img = self.imgs_preload_dict[img_filename]
#     return img
# 
#   def load_image(self, img_filename):
#     if self._preload_dataset:
#       img = tf.py_func(self._imgs_preload_dict_lookup, [img_filename], tf.float32, name="imgs_preload_dict_lookup_np")
#       img.set_shape((None, None, 3))
#     else:
#       img = super(MapillaryLikeDetectionFileListDataset, self).load_image(img_filename)
#     return img
# 
#   def load_annotation(self, img, img_filename, annotation_filename):
#     bboxes, ids, classes, is_crowd, mask = tf.py_func(self._load_ann_np, [annotation_filename],
#                                                       [tf.float32, tf.int32, tf.int32, tf.int32, tf.uint8],
#                                                       name="postproc_ann_np")
#     bboxes.set_shape((self._n_max_detections, 4))
#     ids.set_shape((self._n_max_detections,))
#     classes.set_shape((self._n_max_detections,))
#     is_crowd.set_shape((self._n_max_detections,))
#     mask.set_shape((None, None, self._n_max_detections))
# 
#     return_dict = {DataKeys.BBOXES_y0x0y1x1: bboxes, DataKeys.CLASSES: classes, DataKeys.IDS: ids,
#                    DataKeys.IS_CROWD: is_crowd}
#     if self.add_masks:
#       return_dict[DataKeys.SEGMENTATION_MASK] = mask
#     return_dict = self.postproc_annotation(annotation_filename, return_dict)
#     return return_dict
# 
# Path: datasets/util/Util.py
# def username():
#   return os.environ["USER"]


from datasets.Loader import register_dataset
from datasets.DetectionDataset import MapillaryLikeDetectionFileListDataset
from datasets.util.Util import username

NAME = "Mapillary_detection"
DATA_LIST_PATH = "datasets/Mapillary/"
DEFAULT_PATH = "/fastwork/" + username() + "/mywork/data/mapillary_quarter/"
N_MAX_DETECTIONS = 300
ID_DIVISOR = 256
CLASS_IDS_WITH_INSTANCES = [0, 1, 8, 19, 20, 21, 22, 23, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 44, 45, 46, 47,
                            48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62]
NUM_CLASSES = len(CLASS_IDS_WITH_INSTANCES) + 1


@register_dataset(NAME)

# 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/DetectionDataset.py
# class MapillaryLikeDetectionFileListDataset(DetectionFileListDataset):
#   def __init__(self, config, dataset_name, subset, default_path, num_classes, n_max_detections,
#                class_ids_with_instances, id_divisor, crowd_id=None):
#     super().__init__(config, dataset_name, subset, default_path, num_classes)
#     self._n_max_detections = n_max_detections
#     self._class_ids_with_instances = class_ids_with_instances
#     self._id_divisor = id_divisor
#     self._crowd_id = crowd_id
# 
#     self._preload_dataset = config.bool("preload_dataset", False)
#     if self._preload_dataset:
#       print("Preloading ENTIRE!!! dataset into memory!!!", file=log.v1)
#       imgs, anns = self.read_inputfile_lists()
#       self.imgs_preload_dict = {img_filename: np.array(Image.open(img_filename), dtype=np.float32) / 255.0
#                                 for img_filename in imgs}
#       self.anns_preload_dict = {ann_filename: np.array(Image.open(ann_filename)) for ann_filename in anns}
#       self._load_ann_np = self._anns_preload_dict_lookup
#     else:
#       self._load_ann_np = partial(load_instance_seg_annotation_np, n_max_detections=self._n_max_detections,
#                                   class_ids_with_instances=self._class_ids_with_instances, id_divisor=self._id_divisor,
#                                   crowd_id=self._crowd_id)
# 
#   def _anns_preload_dict_lookup(self, ann_filename):
#     ann_filename = ann_filename.decode("utf-8")
#     ann = self.anns_preload_dict[ann_filename]
#     return load_instance_seg_annotation_from_img_array_np(ann, n_max_detections=self._n_max_detections,
#                                                           class_ids_with_instances=self._class_ids_with_instances, id_divisor=self._id_divisor,
#                                                           crowd_id=self._crowd_id)
# 
#   def _imgs_preload_dict_lookup(self, img_filename):
#     img_filename = img_filename.decode("utf-8")
#     img = self.imgs_preload_dict[img_filename]
#     return img
# 
#   def load_image(self, img_filename):
#     if self._preload_dataset:
#       img = tf.py_func(self._imgs_preload_dict_lookup, [img_filename], tf.float32, name="imgs_preload_dict_lookup_np")
#       img.set_shape((None, None, 3))
#     else:
#       img = super(MapillaryLikeDetectionFileListDataset, self).load_image(img_filename)
#     return img
# 
#   def load_annotation(self, img, img_filename, annotation_filename):
#     bboxes, ids, classes, is_crowd, mask = tf.py_func(self._load_ann_np, [annotation_filename],
#                                                       [tf.float32, tf.int32, tf.int32, tf.int32, tf.uint8],
#                                                       name="postproc_ann_np")
#     bboxes.set_shape((self._n_max_detections, 4))
#     ids.set_shape((self._n_max_detections,))
#     classes.set_shape((self._n_max_detections,))
#     is_crowd.set_shape((self._n_max_detections,))
#     mask.set_shape((None, None, self._n_max_detections))
# 
#     return_dict = {DataKeys.BBOXES_y0x0y1x1: bboxes, DataKeys.CLASSES: classes, DataKeys.IDS: ids,
#                    DataKeys.IS_CROWD: is_crowd}
#     if self.add_masks:
#       return_dict[DataKeys.SEGMENTATION_MASK] = mask
#     return_dict = self.postproc_annotation(annotation_filename, return_dict)
#     return return_dict
# 
# Path: datasets/util/Util.py
# def username():
#   return os.environ["USER"]


# Path: datasets/Mapillary/Mapillary_detection.py
from datasets.Loader import register_dataset
from datasets.DetectionDataset import MapillaryLikeDetectionFileListDataset
from datasets.util.Util import username


NAME = "Mapillary_detection"
DATA_LIST_PATH = "datasets/Mapillary/"
DEFAULT_PATH = "/fastwork/" + username() + "/mywork/data/mapillary_quarter/"
N_MAX_DETECTIONS = 300
ID_DIVISOR = 256
CLASS_IDS_WITH_INSTANCES = [0, 1, 8, 19, 20, 21, 22, 23, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 44, 45, 46, 47,
                            48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62]
NUM_CLASSES = len(CLASS_IDS_WITH_INSTANCES) + 1


@register_dataset(NAME)

class MapillaryDetectionDataset(MapillaryLikeDetectionFileListDataset):