# Path: data_pipeline/pku_mmd.py
# class PKU_MMD(data.Dataset):
#   MEAN_STD = {
#       'rgb': ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
#       'oflow': (0.5, 1. / 255),
#       'depth': (4084.1213735 / (255 * 256), 1008.31271366 / (255 * 256)),
#       'jjd': (0.53968146, 0.32319776),
#       'jjv': (0, 0.35953656),
#       'jld': (0.15982792, 0.12776225)
#   }
# 
#   def __init__(self,
#                root,
#                mode,
#                evaluation,
#                modalities,
#                step_size,
#                n_frames,
#                downsample,
#                timestep,
#                transforms=None,
#                subsample_rate=0):
#     """PKU_MMD Constructor.
# 
#     Contructs the PKU_MMD dataset.
# 
#     Args:
#       root: dataset root
#       mode: train (0), test (1)
#       evaluation: one of ['cross-subject', 'cross-view']
#       modalities: one of ['rgb', 'oflow', 'depth', 'jjd', 'jjv', 'jld']
#       step_size: step size between clips
#       n_frames: number of frames per clip
#       transforms: transform.
#       subsample_rate: sampling rate
#       downsample: fps /= downsample
#       timestep: number of clips in a sequence.
#     """
#     modalities = utils.unsqueeze(modalities)
#     transforms = utils.unsqueeze(transforms)
# 
#     loaders = {
#         'rgb': rgb_loader,
#         'oflow': oflow_loader,
#         'depth': depth_loader,
#         'jjd': jjd_loader,
#         'jjv': jjv_loader,
#         'jld': jld_loader
#     }
#     self.loaders = loaders
#     self.dataset = make_dataset(
#         root, evaluation, mode, subsample_rate=subsample_rate)
# 
#     self.root = root
#     self.modalities = modalities
# 
#     self.step_size = step_size
#     self.n_frames = n_frames
#     self.downsample = downsample
#     self.timestep = timestep
#     self.all = mode != 0  # True if test mode, return the entire video
#     self.transforms = transforms
# 
#   def __getitem__(self, idx):
#     vid_name, label = self.dataset[idx]
#     # label: action_id, start_frame, end_frame, confidence
#     # -1 because len(oflow) = len(rgb)-1
#     length = len(
#         glob.glob(
#             os.path.join(self.root, rgb_folder_name, vid_name,
#                          '*.' + rgb_pattern.split('.')[1]))) - 1
#     length_ds = length // self.downsample
# 
#     if self.all:
#       # Return entire video
#       starts = np.arange(0, length_ds - self.n_frames + 1,
#                          self.step_size)  # arange: exclusive
#     else:
#       start = random.randint(
#           0, length_ds - ((self.timestep - 1) * self.step_size + self.n_frames))
#       starts = [start + i * self.step_size for i in range(self.timestep)
#                ]  # randint: inclusive
# 
#     frame_ids = []
#     for start in starts:
#       frame_ids_s = np.arange(start, start + self.n_frames,
#                               1) * self.downsample  # arange: exclusive
#       frame_ids.append(frame_ids_s)
#     frame_ids = np.stack(frame_ids)
# 
#     targets = []
#     for frame_ids_s in frame_ids:
#       target = 0
#       max_ratio = 0.5
#       for action_id, start_frame, end_frame, _ in label:
#         overlap = get_overlap([frame_ids_s[0], frame_ids_s[-1] - 1],
#                               [start_frame, end_frame - 1])
#         ratio = overlap / (frame_ids_s[-1] - frame_ids_s[0])
#         if ratio > max_ratio:
#           target = int(action_id)
#       targets.append(target)
#     targets = np.stack(targets)
# 
#     # load raw data
#     inputs = []
#     for modality in self.modalities:
#       vid = self.loaders[modality](self.root, vid_name, frame_ids)
#       inputs.append(vid)
# 
#     # transform
#     if self.transforms is not None:
#       for i, transform in enumerate(self.transforms):
#         if transform is not None:
#           inputs[i] = transform(inputs[i])
# 
#     return inputs, targets, vid_name
# 
#   def __len__(self):
#     return len(self.dataset)


import os
import torch.utils.data as data
import torchvision.transforms as transforms
import third_party.two_stream_pytorch.video_transforms as vtransforms
from data_pipeline.pku_mmd import PKU_MMD
      xform = transforms.Compose([
          vtransforms.RandomSizedCrop(224),
          vtransforms.RandomHorizontalFlip(),
          vtransforms.ToTensor(),
          vtransforms.Normalize(mean, std)
      ])
    elif opt.split == 'train' and modality == 'oflow':
      # Special handling when flipping optical flow
      xform = transforms.Compose([
          vtransforms.RandomSizedCrop(224, True),
          vtransforms.RandomHorizontalFlip(True),
          vtransforms.ToTensor(),
          vtransforms.Normalize(mean, std)
      ])
    elif opt.split != 'train' and modality in CNN_MODALITIES:
      xform = transforms.Compose([
          vtransforms.Scale(256),
          vtransforms.CenterCrop(224),
          vtransforms.ToTensor(),
          vtransforms.Normalize(mean, std)
      ])
    elif modality in GRU_MODALITIES:
      xform = transforms.Compose([vtransforms.SkelNormalize(mean, std)])
    else:
      raise Exception

    xforms.append(xform)

  if opt.dset == 'pku-mmd':
    root = os.path.join(opt.dset_path, opt.dset)

# Path: data_pipeline/pku_mmd.py
# class PKU_MMD(data.Dataset):
#   MEAN_STD = {
#       'rgb': ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
#       'oflow': (0.5, 1. / 255),
#       'depth': (4084.1213735 / (255 * 256), 1008.31271366 / (255 * 256)),
#       'jjd': (0.53968146, 0.32319776),
#       'jjv': (0, 0.35953656),
#       'jld': (0.15982792, 0.12776225)
#   }
# 
#   def __init__(self,
#                root,
#                mode,
#                evaluation,
#                modalities,
#                step_size,
#                n_frames,
#                downsample,
#                timestep,
#                transforms=None,
#                subsample_rate=0):
#     """PKU_MMD Constructor.
# 
#     Contructs the PKU_MMD dataset.
# 
#     Args:
#       root: dataset root
#       mode: train (0), test (1)
#       evaluation: one of ['cross-subject', 'cross-view']
#       modalities: one of ['rgb', 'oflow', 'depth', 'jjd', 'jjv', 'jld']
#       step_size: step size between clips
#       n_frames: number of frames per clip
#       transforms: transform.
#       subsample_rate: sampling rate
#       downsample: fps /= downsample
#       timestep: number of clips in a sequence.
#     """
#     modalities = utils.unsqueeze(modalities)
#     transforms = utils.unsqueeze(transforms)
# 
#     loaders = {
#         'rgb': rgb_loader,
#         'oflow': oflow_loader,
#         'depth': depth_loader,
#         'jjd': jjd_loader,
#         'jjv': jjv_loader,
#         'jld': jld_loader
#     }
#     self.loaders = loaders
#     self.dataset = make_dataset(
#         root, evaluation, mode, subsample_rate=subsample_rate)
# 
#     self.root = root
#     self.modalities = modalities
# 
#     self.step_size = step_size
#     self.n_frames = n_frames
#     self.downsample = downsample
#     self.timestep = timestep
#     self.all = mode != 0  # True if test mode, return the entire video
#     self.transforms = transforms
# 
#   def __getitem__(self, idx):
#     vid_name, label = self.dataset[idx]
#     # label: action_id, start_frame, end_frame, confidence
#     # -1 because len(oflow) = len(rgb)-1
#     length = len(
#         glob.glob(
#             os.path.join(self.root, rgb_folder_name, vid_name,
#                          '*.' + rgb_pattern.split('.')[1]))) - 1
#     length_ds = length // self.downsample
# 
#     if self.all:
#       # Return entire video
#       starts = np.arange(0, length_ds - self.n_frames + 1,
#                          self.step_size)  # arange: exclusive
#     else:
#       start = random.randint(
#           0, length_ds - ((self.timestep - 1) * self.step_size + self.n_frames))
#       starts = [start + i * self.step_size for i in range(self.timestep)
#                ]  # randint: inclusive
# 
#     frame_ids = []
#     for start in starts:
#       frame_ids_s = np.arange(start, start + self.n_frames,
#                               1) * self.downsample  # arange: exclusive
#       frame_ids.append(frame_ids_s)
#     frame_ids = np.stack(frame_ids)
# 
#     targets = []
#     for frame_ids_s in frame_ids:
#       target = 0
#       max_ratio = 0.5
#       for action_id, start_frame, end_frame, _ in label:
#         overlap = get_overlap([frame_ids_s[0], frame_ids_s[-1] - 1],
#                               [start_frame, end_frame - 1])
#         ratio = overlap / (frame_ids_s[-1] - frame_ids_s[0])
#         if ratio > max_ratio:
#           target = int(action_id)
#       targets.append(target)
#     targets = np.stack(targets)
# 
#     # load raw data
#     inputs = []
#     for modality in self.modalities:
#       vid = self.loaders[modality](self.root, vid_name, frame_ids)
#       inputs.append(vid)
# 
#     # transform
#     if self.transforms is not None:
#       for i, transform in enumerate(self.transforms):
#         if transform is not None:
#           inputs[i] = transform(inputs[i])
# 
#     return inputs, targets, vid_name
# 
#   def __len__(self):
#     return len(self.dataset)


# Path: detection/get_dataloader.py
import os
import torch.utils.data as data
import torchvision.transforms as transforms
import third_party.two_stream_pytorch.video_transforms as vtransforms
from data_pipeline.pku_mmd import PKU_MMD

      xform = transforms.Compose([
          vtransforms.RandomSizedCrop(224),
          vtransforms.RandomHorizontalFlip(),
          vtransforms.ToTensor(),
          vtransforms.Normalize(mean, std)
      ])
    elif opt.split == 'train' and modality == 'oflow':
      # Special handling when flipping optical flow
      xform = transforms.Compose([
          vtransforms.RandomSizedCrop(224, True),
          vtransforms.RandomHorizontalFlip(True),
          vtransforms.ToTensor(),
          vtransforms.Normalize(mean, std)
      ])
    elif opt.split != 'train' and modality in CNN_MODALITIES:
      xform = transforms.Compose([
          vtransforms.Scale(256),
          vtransforms.CenterCrop(224),
          vtransforms.ToTensor(),
          vtransforms.Normalize(mean, std)
      ])
    elif modality in GRU_MODALITIES:
      xform = transforms.Compose([vtransforms.SkelNormalize(mean, std)])
    else:
      raise Exception

    xforms.append(xform)

  if opt.dset == 'pku-mmd':
    root = os.path.join(opt.dset_path, opt.dset)

    dset = PKU_MMD(root, idx_t, 'cross-subject', opt.modalities, opt.step_size,