text: from __future__ import print_function, absolute_import
  
  import glob
  import re
  from os import path as osp
  import os
  
  
  
  
  
  """Dataset classes"""
  
  
  class Market1501(object):
      """
      Market1501
      Reference:
      Zheng et al. Scalable Person Re-identification: A Benchmark. ICCV 2015.
      URL: http://www.liangzheng.org/Project/project_reid.html
  
      Dataset statistics:
      # identities: 1501 (+1 for background)
      # images: 12936 (train) + 3368 (query) + 15913 (gallery)
      """
      def __init__(self, dataset_dir, mode, root='data'):
          self.dataset_dir = dataset_dir
          self.dataset_dir = osp.join(root, self.dataset_dir)
          self.train_dir = osp.join(self.dataset_dir, 'bounding_box_train')
          self.query_dir = osp.join(self.dataset_dir, 'query')
          self.gallery_dir = osp.join(self.dataset_dir, 'bounding_box_test')
  
          self._check_before_run()
          train_relabel = (mode == 'retrieval')
          train, num_train_pids, num_train_imgs = self._process_dir(self.train_dir, relabel=train_relabel)
          query, num_query_pids, num_query_imgs = self._process_dir(self.query_dir, relabel=False)
          gallery, num_gallery_pids, num_gallery_imgs = self._process_dir(self.gallery_dir, relabel=False)
          num_total_pids = num_train_pids + num_query_pids
          num_total_imgs = num_train_imgs + num_query_imgs + num_gallery_imgs
  
          print("=> Market1501 or CUHK loaded")
          print("Dataset statistics:")
          print("  ------------------------------")
          print("  subset   | # ids | # images")
          print("  ------------------------------")
          print("  train    | {:5d} | {:8d}".format(num_train_pids, num_train_imgs))
          print("  query    | {:5d} | {:8d}".format(num_query_pids, num_query_imgs))
          print("  gallery  | {:5d} | {:8d}".format(num_gallery_pids, num_gallery_imgs))
          print("  ------------------------------")
          print("  total    | {:5d} | {:8d}".format(num_total_pids, num_total_imgs))
          print("  ------------------------------")
  
          self.train = train
          self.query = query
          self.gallery = gallery
  
          self.num_train_pids = num_train_pids
          self.num_query_pids = num_query_pids
          self.num_gallery_pids = num_gallery_pids
  
      def _check_before_run(self):
          """Check if all files are available before going deeper"""
          if not osp.exists(self.dataset_dir):
              raise RuntimeError("'{}' is not available".format(self.dataset_dir))
          if not osp.exists(self.train_dir):
              raise RuntimeError("'{}' is not available".format(self.train_dir))
          if not osp.exists(self.query_dir):
              raise RuntimeError("'{}' is not available".format(self.query_dir))
          if not osp.exists(self.gallery_dir):
              raise RuntimeError("'{}' is not available".format(self.gallery_dir))
  
      def _process_dir(self, dir_path, relabel=False):
          img_names = os.listdir(dir_path)
          img_paths = [os.path.join(dir_path, img_name) for img_name in img_names \
              if img_name.endswith('jpg') or img_name.endswith('png')]
          pattern = re.compile(r'([-\d]+)_c([-\d]+)')
  
          pid_container = set()
          for img_path in img_paths:
              pid, _ = map(int, pattern.search(img_path).groups())
              if pid == -1: continue  # junk images are just ignored
              pid_container.add(pid)
          pid2label = {pid: label for label, pid in enumerate(pid_container)}
  
          dataset = []
          for img_path in img_paths:
              pid, camid = map(int, pattern.search(img_path).groups())
              if pid == -1:
                  continue  # junk images are just ignored
              #assert 0 <= pid <= 1501  # pid == 0 means background
              #assert 1 <= camid <= 6
              camid -= 1  # index starts from 0
              if relabel: pid = pid2label[pid]
              dataset.append((img_path, pid, camid))
  
          num_pids = len(pid_container)
          num_imgs = len(dataset)
          return dataset, num_pids, num_imgs
  
  
  
  class DukeMTMCreID(object):
      """
      DukeMTMC-reID
      Reference:
      1. Ristani et al. Performance Measures and a Data Set for Multi-Target, Multi-Camera Tracking. ECCVW 2016.
      2. Zheng et al. Unlabeled Samples Generated by GAN Improve the Person Re-identification Baseline in vitro. ICCV 2017.
      URL: https://github.com/layumi/DukeMTMC-reID_evaluation
      Dataset statistics:
      # identities: 1404 (train + query)
      # images:16522 (train) + 2228 (query) + 17661 (gallery)
      # cameras: 8
      """
  
  
      def __init__(self, dataset_dir, mode, root='data'):
          self.dataset_dir = dataset_dir
          self.dataset_dir = osp.join(root, self.dataset_dir)
          self.train_dir = osp.join(self.dataset_dir, 'bounding_box_train')
          self.query_dir = osp.join(self.dataset_dir, 'query')
          self.gallery_dir = osp.join(self.dataset_dir, 'bounding_box_test')
  
          self._check_before_run()
          train_relabel = (mode == 'retrieval')
          train, num_train_pids, num_train_imgs = self._process_dir(self.train_dir, relabel=train_relabel)
          query, num_query_pids, num_query_imgs = self._process_dir(self.query_dir, relabel=False)
          gallery, num_gallery_pids, num_gallery_imgs = self._process_dir(self.gallery_dir, relabel=False)
          num_total_pids = num_train_pids + num_query_pids
          num_total_imgs = num_train_imgs + num_query_imgs + num_gallery_imgs
  
          print("=> DukeMTMC loaded")
          print("Dataset statistics:")
          print("  ------------------------------")
          print("  subset   | # ids | # images")
          print("  ------------------------------")
          print("  train    | {:5d} | {:8d}".format(num_train_pids, num_train_imgs))
          print("  query    | {:5d} | {:8d}".format(num_query_pids, num_query_imgs))
          print("  gallery  | {:5d} | {:8d}".format(num_gallery_pids, num_gallery_imgs))
          print("  ------------------------------")
          print("  total    | {:5d} | {:8d}".format(num_total_pids, num_total_imgs))
          print("  ------------------------------")
  
          self.train = train
          self.query = query
          self.gallery = gallery
  
          self.num_train_pids = num_train_pids
          self.num_query_pids = num_query_pids
          self.num_gallery_pids = num_gallery_pids
  
  
      def _check_before_run(self):
          """Check if all files are available before going deeper"""
          if not osp.exists(self.dataset_dir):
              raise RuntimeError("'{}' is not available".format(self.dataset_dir))
          if not osp.exists(self.train_dir):
              raise RuntimeError("'{}' is not available".format(self.train_dir))
          if not osp.exists(self.query_dir):
              raise RuntimeError("'{}' is not available".format(self.query_dir))
          if not osp.exists(self.gallery_dir):
              raise RuntimeError("'{}' is not available".format(self.gallery_dir))
  
      def _process_dir(self, dir_path, relabel=False):
          img_paths = glob.glob(osp.join(dir_path, '*.jpg'))
          pattern = re.compile(r'([-\d]+)_c(\d)')
  
          pid_container = set()
          for img_path in img_paths:
  
              pid, _ = map(int, pattern.search(img_path).groups())
              pid_container.add(pid)
          pid2label = {pid: label for label, pid in enumerate(pid_container)}
  
          dataset = []
          for img_path in img_paths:
              pid, camid = map(int, pattern.search(img_path).groups())
  
              assert 1 <= camid <= 8
              camid -= 1  # index starts from 0
              if relabel: pid = pid2label[pid]
              dataset.append((img_path, pid, camid))
  
  
          num_pids = len(pid_container)
          num_imgs = len(dataset)
  
          return dataset, num_pids, num_imgs
  
  
  
  
  class MSMT17(object):
  
      def __init__(self, dataset_dir, mode, root='data'):
          self.dataset_dir = dataset_dir
          self.dataset_dir = osp.join(root, self.dataset_dir)
          self.train_dir = osp.join(self.dataset_dir, 'bounding_box_train')
          self.query_dir = osp.join(self.dataset_dir, 'query')
          self.gallery_dir = osp.join(self.dataset_dir, 'bounding_box_test')
  
          self._check_before_run()
          train_relabel = (mode == 'retrieval')
          train, num_train_pids, num_train_imgs = self._process_dir(self.train_dir, relabel=train_relabel)
          query, num_query_pids, num_query_imgs = self._process_dir(self.query_dir, relabel=False)
  
          gallery, num_gallery_pids, num_gallery_imgs = self._process_dir(self.gallery_dir, relabel=False)
          num_total_pids = num_train_pids + num_query_pids
          num_total_imgs = num_train_imgs + num_query_imgs + num_gallery_imgs
  
          print("=> MSMT17 loaded")
          print("Dataset statistics:")
          print("  ------------------------------")
          print("  subset   | # ids | # images")
          print("  ------------------------------")
          print("  train    | {:5d} | {:8d}".format(num_train_pids, num_train_imgs))
          print("  query    | {:5d} | {:8d}".format(num_query_pids, num_query_imgs))
          print("  gallery  | {:5d} | {:8d}".format(num_gallery_pids, num_gallery_imgs))
          print("  ------------------------------")
          print("  total    | {:5d} | {:8d}".format(num_total_pids, num_total_imgs))
          print("  ------------------------------")
  
          self.train = train
          self.query = query
          self.gallery = gallery
  
          self.num_train_pids = num_train_pids
          self.num_query_pids = num_query_pids
          self.num_gallery_pids = num_gallery_pids
  
  
      def _check_before_run(self):
          """Check if all files are available before going deeper"""
          if not osp.exists(self.dataset_dir):
              raise RuntimeError("'{}' is not available".format(self.dataset_dir))
          if not osp.exists(self.train_dir):
              raise RuntimeError("'{}' is not available".format(self.train_dir))
          if not osp.exists(self.query_dir):
              raise RuntimeError("'{}' is not available".format(self.query_dir))
          if not osp.exists(self.gallery_dir):
              raise RuntimeError("'{}' is not available".format(self.gallery_dir))
  
      def _process_dir(self, dir_path, relabel=False):
          print(dir_path)
          img_paths = glob.glob(osp.join(dir_path, '*.jpg'))
  
          if dir_path.split('/')[-1] == 'query':
              img_paths = img_paths[:2000]
  
          pid_container = set()
          for img_path in img_paths:
              img_path = img_path.split('/')[-1]
  
              pid = int(img_path.split('_')[0])
              pid_container.add(pid)
          pid2label = {pid: label for label, pid in enumerate(pid_container)}
  
          dataset = []
          for img_path in img_paths:
              img_path = img_path.split('/')[-1]
              pid = int(img_path.split('_')[0])
              camid = int(img_path.split('_')[2])
              assert 1 <= camid <= 15
              camid -= 1  # index starts from 0
              if relabel: pid = pid2label[pid]
              dataset.append((osp.join(dir_path,img_path), pid, camid))
  
  
          num_pids = len(pid_container)
          num_imgs = len(dataset)
  
          return dataset, num_pids, num_imgs
  
  
  
  class Test(object):
  
  
      def __init__(self, dataset_dir, mode, root='data'):
          self.dataset_dir = dataset_dir
          self.dataset_dir = osp.join(root, self.dataset_dir)
          self.train_dir = osp.join(self.dataset_dir, 'bounding_box_train')
          self.query_dir = osp.join(self.dataset_dir, 'query')
          self.gallery_dir = osp.join(self.dataset_dir, 'bounding_box_test')
  
          self._check_before_run()
          train_relabel = (mode == 'retrieval')
          train, num_train_pids, num_train_imgs = self._process_dir(self.train_dir, relabel=train_relabel)
          query, num_query_pids, num_query_imgs = self._process_dir(self.query_dir, relabel=False)
          gallery, num_gallery_pids, num_gallery_imgs = self._process_dir(self.gallery_dir, relabel=False)
          num_total_pids = num_train_pids + num_query_pids
          num_total_imgs = num_train_imgs + num_query_imgs + num_gallery_imgs
  
          print("=> Test loaded")
          print("Dataset statistics:")
          print("  ------------------------------")
          print("  subset   | # ids | # images")
          print("  ------------------------------")
          print("  train    | {:5d} | {:8d}".format(num_train_pids, num_train_imgs))
          print("  query    | {:5d} | {:8d}".format(num_query_pids, num_query_imgs))
          print("  gallery  | {:5d} | {:8d}".format(num_gallery_pids, num_gallery_imgs))
          print("  ------------------------------")
          print("  total    | {:5d} | {:8d}".format(num_total_pids, num_total_imgs))
          print("  ------------------------------")
  
          self.train = train
          self.query = query
          self.gallery = gallery
  
          self.num_train_pids = num_train_pids
          self.num_query_pids = num_query_pids
          self.num_gallery_pids = num_gallery_pids
  
  
      def _check_before_run(self):
          """Check if all files are available before going deeper"""
          if not osp.exists(self.dataset_dir):
              raise RuntimeError("'{}' is not available".format(self.dataset_dir))
          if not osp.exists(self.train_dir):
              raise RuntimeError("'{}' is not available".format(self.train_dir))
          if not osp.exists(self.query_dir):
              raise RuntimeError("'{}' is not available".format(self.query_dir))
          if not osp.exists(self.gallery_dir):
              raise RuntimeError("'{}' is not available".format(self.gallery_dir))
  
      def _process_dir(self, dir_path, relabel=False):
          img_paths = glob.glob(osp.join(dir_path, '*.jpg'))
          pattern = re.compile(r'([-\d]+)_c(\d)')
  
          pid_container = set()
          for img_path in img_paths:
              pid, _ = map(int, pattern.search(img_path).groups())
              pid_container.add(pid)
          pid2label = {pid: label for label, pid in enumerate(pid_container)}
  
          dataset = []
          for img_path in img_paths:
              pid, camid = map(int, pattern.search(img_path).groups())
              assert 0 <= camid <= 1
              #camid -= 1  # index starts from 0
              if relabel: pid = pid2label[pid]
              dataset.append((img_path, pid, camid))
  
  
          num_pids = len(pid_container)
          num_imgs = len(dataset)
  
          return dataset, num_pids, num_imgs
  
  
  
  def init_dataset(name, mode):
      if name == 'market1501':
          return Market1501(name, mode)
      if name == 'cuhk-label':
          return Market1501(name, mode)
      if name == 'cuhk-detected':
          return Market1501(name, mode)
      if name == 'dukemtmc':
          return DukeMTMCreID(name, mode)
      if name == 'msmt17':
          return MSMT17(name, mode)
      if name == 'test':
          return Test(name, mode)
      else:
          raise RuntimeError("the dataset {} is not avaiable".format(name))