# Copyright (c) Facebook, Inc. and its affiliates.

# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import concurrent.futures
import numpy as np
import os
from PIL import Image
import torchvision

from datasets import base
from platforms.platform import get_platform


def _get_samples(root, y_name, y_num):
    y_dir = os.path.join(root, y_name)
    if not get_platform().isdir(y_dir): return []
    output = [(os.path.join(y_dir, f), y_num) for f in get_platform().listdir(y_dir) if f.lower().endswith('jpeg')]
    return output


class Dataset(base.ImageDataset):
    """ImageNet"""

    def __init__(self, loc: str, image_transforms):
        # Load the data.
        classes = sorted(get_platform().listdir(loc))
        samples = []

        if get_platform().num_workers > 0:
            executor = concurrent.futures.ThreadPoolExecutor(max_workers=get_platform().num_workers)
            futures = [executor.submit(_get_samples, loc, y_name, y_num) for y_num, y_name in enumerate(classes)]
            for d in concurrent.futures.wait(futures)[0]: samples += d.result()
        else:
            for y_num, y_name in enumerate(classes):
                samples += _get_samples(loc, y_name, y_num)

        examples, labels = zip(*samples)
        super(Dataset, self).__init__(
            np.array(examples), np.array(labels), image_transforms,
            [torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])

    @staticmethod
    def num_train_examples(): return 1281167

    @staticmethod
    def num_test_examples(): return 50000

    @staticmethod
    def num_classes(): return 1000

    @staticmethod
    def _augment_transforms():
        return [
            torchvision.transforms.RandomResizedCrop(224, scale=(0.1, 1.0), ratio=(0.8, 1.25)),
            torchvision.transforms.RandomHorizontalFlip()
        ]

    @staticmethod
    def _transforms():
        return [torchvision.transforms.Resize(256), torchvision.transforms.CenterCrop(224)]

    @staticmethod
    def get_train_set(use_augmentation, resize):
        transforms = Dataset._augment_transforms() if use_augmentation else Dataset._transforms()
        return Dataset(os.path.join(get_platform().imagenet_root, 'train'), transforms)

    @staticmethod
    def get_test_set(resize):
        return Dataset(os.path.join(get_platform().imagenet_root, 'val'), Dataset._transforms())

    @staticmethod
    def example_to_image(example):
        with get_platform().open(example, 'rb') as fp:
            return Image.open(fp).convert('RGB')


DataLoader = base.DataLoader

[((17, 12, 17, 38), 'os.path.join', 'os.path.join', ({(17, 25, 17, 29): 'root', (17, 31, 17, 37): 'y_name'}, {}), '(root, y_name)', False, 'import os\n'), ((19, 15, 19, 37), 'os.path.join', 'os.path.join', ({(19, 28, 19, 33): 'y_dir', (19, 35, 19, 36): 'f'}, {}), '(y_dir, f)', False, 'import os\n'), ((41, 12, 41, 30), 'numpy.array', 'np.array', ({(41, 21, 41, 29): 'examples'}, {}), '(examples)', True, 'import numpy as np\n'), ((41, 32, 41, 48), 'numpy.array', 'np.array', ({(41, 41, 41, 47): 'labels'}, {}), '(labels)', True, 'import numpy as np\n'), ((56, 12, 56, 94), 'torchvision.transforms.RandomResizedCrop', 'torchvision.transforms.RandomResizedCrop', (), '', False, 'import torchvision\n'), ((57, 12, 57, 57), 'torchvision.transforms.RandomHorizontalFlip', 'torchvision.transforms.RandomHorizontalFlip', ({}, {}), '()', False, 'import torchvision\n'), ((62, 16, 62, 50), 'torchvision.transforms.Resize', 'torchvision.transforms.Resize', ({(62, 46, 62, 49): '(256)'}, {}), '(256)', False, 'import torchvision\n'), ((62, 52, 62, 90), 'torchvision.transforms.CenterCrop', 'torchvision.transforms.CenterCrop', ({(62, 86, 62, 89): '(224)'}, {}), '(224)', False, 'import torchvision\n'), ((18, 11, 18, 25), 'platforms.platform.get_platform', 'get_platform', ({}, {}), '()', False, 'from platforms.platform import get_platform\n'), ((31, 11, 31, 25), 'platforms.platform.get_platform', 'get_platform', ({}, {}), '()', False, 'from platforms.platform import get_platform\n'), ((42, 13, 42, 91), 'torchvision.transforms.Normalize', 'torchvision.transforms.Normalize', ({(42, 46, 42, 67): '[0.485, 0.456, 0.406]', (42, 69, 42, 90): '[0.229, 0.224, 0.225]'}, {}), '([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])', False, 'import torchvision\n'), ((19, 55, 19, 69), 'platforms.platform.get_platform', 'get_platform', ({}, {}), '()', False, 'from platforms.platform import get_platform\n'), ((28, 25, 28, 39), 'platforms.platform.get_platform', 'get_platform', ({}, {}), '()', False, 'from platforms.platform import get_platform\n'), ((67, 36, 67, 50), 'platforms.platform.get_platform', 'get_platform', ({}, {}), '()', False, 'from platforms.platform import get_platform\n'), ((71, 36, 71, 50), 'platforms.platform.get_platform', 'get_platform', ({}, {}), '()', False, 'from platforms.platform import get_platform\n'), ((75, 13, 75, 27), 'platforms.platform.get_platform', 'get_platform', ({}, {}), '()', False, 'from platforms.platform import get_platform\n'), ((76, 19, 76, 33), 'PIL.Image.open', 'Image.open', ({(76, 30, 76, 32): 'fp'}, {}), '(fp)', False, 'from PIL import Image\n'), ((32, 73, 32, 87), 'platforms.platform.get_platform', 'get_platform', ({}, {}), '()', False, 'from platforms.platform import get_platform\n')]