# -*- coding: utf-8 -*-

# BSD 3-Clause License

#

# Copyright (c) 2017

# All rights reserved.

# Copyright 2022 Huawei Technologies Co., Ltd

#

# Redistribution and use in source and binary forms, with or without

# modification, are permitted provided that the following conditions are met:

#

# * Redistributions of source code must retain the above copyright notice, this

#   list of conditions and the following disclaimer.

#

# * Redistributions in binary form must reproduce the above copyright notice,

#   this list of conditions and the following disclaimer in the documentation

#   and/or other materials provided with the distribution.

#

# * Neither the name of the copyright holder nor the names of its

#   contributors may be used to endorse or promote products derived from

#   this software without specific prior written permission.

#

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"

# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE

# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE

# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL

# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR

# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER

# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,

# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# ==========================================================================





# Copyright (c) 2015-present, Facebook, Inc.

# All rights reserved.

import os

import json



from torchvision import datasets, transforms

from torchvision.datasets.folder import ImageFolder, default_loader



from timm.data.constants import IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD

from timm.data import create_transform



from torch.utils.data import Dataset





def build_dataset(is_train, args):

    transform = build_transform(is_train, args)



    if args.data_set == 'IMNET':

        root = os.path.join(args.data_path, 'train' if is_train else 'val')

        dataset = datasets.ImageFolder(root, transform=transform)

        nb_classes = 1000

    else:

        raise NotImplementedError("Support ImageNet only.")



    return dataset, nb_classes





def build_transform(is_train, args):

    resize_im = args.input_size > 32

    if is_train:

        # this should always dispatch to transforms_imagenet_train

        transform = create_transform(

            input_size=args.input_size,

            is_training=True,

            color_jitter=args.color_jitter,

            auto_augment=args.aa,

            interpolation=args.train_interpolation,

            re_prob=args.reprob,

            re_mode=args.remode,

            re_count=args.recount,

        )

        if not resize_im:

            # replace RandomResizedCropAndInterpolation with

            # RandomCrop

            transform.transforms[0] = transforms.RandomCrop(

                args.input_size, padding=4)

        return transform



    t = []

    if resize_im:

        size = int((256 / 224) * args.input_size)

        t.append(

            transforms.Resize(size, interpolation=3),  # to maintain same ratio w.r.t. 224 images

        )

        t.append(transforms.CenterCrop(args.input_size))



    t.append(transforms.ToTensor())

    t.append(transforms.Normalize(IMAGENET_DEFAULT_MEAN, IMAGENET_DEFAULT_STD))

    return transforms.Compose(t)