"""
Copyright 2020 The OneFlow Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import math
import time

import pandas as pd

from oneflow.compatible import single_client as flow


class COCODataLoadConfig(object):
    def __init__(self):
        self.annotation_file = (
            "/dataset/mscoco_2017/annotations/instances_train2017.json"
        )
        self.image_dir = "/dataset/mscoco_2017/train2017"
        self.shuffle_after_epoch = True
        self.stride_partition = False
        self.batch_size = 2
        self.target_size = 800
        self.max_size = 1333
        self.image_align_size = 32
        self.image_normal_std = (1.0, 1.0, 1.0)
        self.image_normal_mean = (102.9801, 115.9465, 122.7717)
        self.max_num_objs = 512


def roundup(x, align):
    return int(math.ceil(x / float(align)) * align)


def coco_data_load(cfg, machine_id, nrank):
    with flow.scope.placement("cpu", "{}:0-{}".format(machine_id, nrank - 1)):
        (
            image,
            image_id,
            image_size,
            bbox,
            label,
            segm_poly,
            segm_poly_index,
        ) = flow.data.coco_reader(
            annotation_file=cfg.annotation_file,
            image_dir=cfg.image_dir,
            batch_size=cfg.batch_size,
            shuffle=cfg.shuffle_after_epoch,
            stride_partition=cfg.stride_partition,
            name="coco_reader",
        )
        image = flow.image.decode(image, dtype=flow.float)
        aligned_target_size = roundup(cfg.target_size, cfg.image_align_size)
        aligned_max_size = roundup(cfg.max_size, cfg.image_align_size)
        (image, new_size, scale) = flow.image.target_resize(
            image, target_size=aligned_target_size, max_size=aligned_max_size
        )
        bbox = flow.detection.object_bbox_scale(bbox, scale)
        segm_poly = flow.detection.object_segmentation_polygon_scale(segm_poly, scale)
        flip_code = flow.random.coin_flip(cfg.batch_size)
        image = flow.image.flip(image, flip_code)
        bbox = flow.detection.object_bbox_flip(bbox, new_size, flip_code)
        segm_poly = flow.detection.object_segmentation_polygon_flip(
            segm_poly, new_size, flip_code
        )
        image = flow.image.normalize(image, cfg.image_normal_std, cfg.image_normal_mean)
        image = flow.image.batch_align(
            image,
            shape=(aligned_target_size, aligned_max_size, 3),
            dtype=flow.float,
            alignment=cfg.image_align_size,
        )
        gt_bbox = flow.tensor_buffer_to_list_of_tensors(
            bbox, (cfg.max_num_objs, 4), flow.float, True
        )
        gt_label = flow.tensor_buffer_to_list_of_tensors(
            label, (cfg.max_num_objs,), flow.int32, True
        )
        segm_mask = flow.detection.object_segmentation_polygon_to_mask(
            segm_poly, segm_poly_index, new_size
        )
        gt_mask = flow.tensor_buffer_to_list_of_tensors(
            segm_mask,
            (cfg.max_num_objs, aligned_target_size, aligned_max_size),
            flow.int8,
            True,
        )
        return {
            "image": image,
            "image_size": new_size,
            "gt_bbox": list(gt_bbox),
            "gt_label": list(gt_label),
            "gt_mask": list(gt_mask),
        }


def _make_data_load_fn():
    flow.clear_default_session()
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_distribute_strategy(flow.scope.consistent_view())
    cfg = COCODataLoadConfig()

    @flow.global_function(func_config)
    def data_load_fn():
        return coco_data_load(cfg, 0, 1)

    return data_load_fn


def _benchmark(iter_num, drop_first_iters, verbose=False):
    flow.env.init()
    data_loader = _make_data_load_fn()
    s = pd.Series([], name="time_elapsed", dtype="float32")
    timestamp = time.perf_counter()
    for i in range(iter_num):
        dict = data_loader().get()
        image = dict["image"]
        image_size = dict["image_size"]
        gt_bbox = dict["gt_bbox"]
        gt_label = dict["gt_label"]
        gt_mask = dict["gt_mask"]
        cur = time.perf_counter()
        s[i] = cur - timestamp
        timestamp = cur
        if verbose:
            print("==== iter {} ====".format(i))
            print(
                "image: {}\n".format(image.numpy_list()[0].shape), image.numpy_list()[0]
            )
            print(
                "image_size: {}\n".format(image_size.numpy().shape), image_size.numpy()
            )
            print("gt_bbox:\n", [x.numpy_list()[0] for x in gt_bbox])
            print("gt_label:\n", [x.numpy_list()[0] for x in gt_label])
            print("gt_mask:\n", [x.numpy_list()[0] for x in gt_mask])
    print(
        "mean of time elapsed of {} iters (dropped {} first iters): {}".format(
            iter_num, drop_first_iters, s[drop_first_iters:].mean()
        )
    )
    s.to_csv("coco_data_benchmark.csv", header=True)


if __name__ == "__main__":
    _benchmark(500, 10)
