#   Copyright (c) 2018 PaddlePaddle 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.

from __future__ import print_function

import unittest
import numpy as np
import math
import sys
import paddle.compat as cpt
from op_test import OpTest


class TestROIPoolOp(OpTest):
    def set_data(self):
        self.init_test_case()
        self.make_rois()
        self.calc_roi_pool()

        self.inputs = {'X': self.x, 'ROIs': (self.rois[:, 1:5], self.rois_lod)}

        self.attrs = {
            'spatial_scale': self.spatial_scale,
            'pooled_height': self.pooled_height,
            'pooled_width': self.pooled_width
        }

        self.outputs = {'Out': self.outs, 'Argmax': self.argmaxes}

    def init_test_case(self):
        self.batch_size = 3
        self.channels = 3
        self.height = 6
        self.width = 4

        # n, c, h, w
        self.x_dim = (self.batch_size, self.channels, self.height, self.width)

        self.spatial_scale = 1.0 / 4.0
        self.pooled_height = 2
        self.pooled_width = 2

        self.x = np.random.random(self.x_dim).astype('float32')

    def calc_roi_pool(self):
        out_data = np.zeros((self.rois_num, self.channels, self.pooled_height,
                             self.pooled_width))
        argmax_data = np.zeros((self.rois_num, self.channels,
                                self.pooled_height, self.pooled_width))

        for i in range(self.rois_num):
            roi = self.rois[i]
            roi_batch_id = roi[0]
            roi_start_w = int(cpt.round(roi[1] * self.spatial_scale))
            roi_start_h = int(cpt.round(roi[2] * self.spatial_scale))
            roi_end_w = int(cpt.round(roi[3] * self.spatial_scale))
            roi_end_h = int(cpt.round(roi[4] * self.spatial_scale))

            roi_height = int(max(roi_end_h - roi_start_h + 1, 1))
            roi_width = int(max(roi_end_w - roi_start_w + 1, 1))

            x_i = self.x[roi_batch_id]

            bin_size_h = float(roi_height) / float(self.pooled_height)
            bin_size_w = float(roi_width) / float(self.pooled_width)

            for c in range(self.channels):
                for ph in range(self.pooled_height):
                    for pw in range(self.pooled_width):
                        hstart = int(math.floor(ph * bin_size_h))
                        wstart = int(math.floor(pw * bin_size_w))
                        hend = int(math.ceil((ph + 1) * bin_size_h))
                        wend = int(math.ceil((pw + 1) * bin_size_w))

                        hstart = min(max(hstart + roi_start_h, 0), self.height)
                        hend = min(max(hend + roi_start_h, 0), self.height)
                        wstart = min(max(wstart + roi_start_w, 0), self.width)
                        wend = min(max(wend + roi_start_w, 0), self.width)

                        is_empty = (hend <= hstart) or (wend <= wstart)
                        if is_empty:
                            out_data[i, c, ph, pw] = 0
                        else:
                            out_data[i, c, ph, pw] = -sys.float_info.max

                        argmax_data[i, c, ph, pw] = -1

                        for h in range(hstart, hend):
                            for w in range(wstart, wend):
                                if x_i[c, h, w] > out_data[i, c, ph, pw]:
                                    out_data[i, c, ph, pw] = x_i[c, h, w]
                                    argmax_data[i, c, ph,
                                                pw] = h * self.width + w

        self.outs = out_data.astype('float32')
        self.argmaxes = argmax_data.astype('int64')

    def make_rois(self):
        rois = []
        self.rois_lod = [[]]
        for bno in range(self.batch_size):
            self.rois_lod[0].append(bno + 1)
            for i in range(bno + 1):
                x1 = np.random.random_integers(
                    0, self.width // self.spatial_scale - self.pooled_width)
                y1 = np.random.random_integers(
                    0, self.height // self.spatial_scale - self.pooled_height)

                x2 = np.random.random_integers(x1 + self.pooled_width,
                                               self.width // self.spatial_scale)
                y2 = np.random.random_integers(
                    y1 + self.pooled_height, self.height // self.spatial_scale)

                roi = [bno, x1, y1, x2, y2]
                rois.append(roi)
        self.rois_num = len(rois)
        self.rois = np.array(rois).astype("int64")

    def setUp(self):
        self.op_type = "roi_pool"
        self.set_data()

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(['X'], 'Out')


if __name__ == '__main__':
    unittest.main()

[((142, 4, 142, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((57, 19, 58, 48), 'numpy.zeros', 'np.zeros', ({(57, 28, 58, 47): '(self.rois_num, self.channels, self.pooled_height, self.pooled_width)'}, {}), '((self.rois_num, self.channels, self.pooled_height, self.pooled_width))', True, 'import numpy as np\n'), ((59, 22, 60, 71), 'numpy.zeros', 'np.zeros', ({(59, 31, 60, 70): '(self.rois_num, self.channels, self.pooled_height, self.pooled_width)'}, {}), '((self.rois_num, self.channels, self.pooled_height, self.pooled_width))', True, 'import numpy as np\n'), ((54, 17, 54, 45), 'numpy.random.random', 'np.random.random', ({(54, 34, 54, 44): 'self.x_dim'}, {}), '(self.x_dim)', True, 'import numpy as np\n'), ((65, 30, 65, 68), 'paddle.compat.round', 'cpt.round', ({(65, 40, 65, 67): 'roi[1] * self.spatial_scale'}, {}), '(roi[1] * self.spatial_scale)', True, 'import paddle.compat as cpt\n'), ((66, 30, 66, 68), 'paddle.compat.round', 'cpt.round', ({(66, 40, 66, 67): 'roi[2] * self.spatial_scale'}, {}), '(roi[2] * self.spatial_scale)', True, 'import paddle.compat as cpt\n'), ((67, 28, 67, 66), 'paddle.compat.round', 'cpt.round', ({(67, 38, 67, 65): 'roi[3] * self.spatial_scale'}, {}), '(roi[3] * self.spatial_scale)', True, 'import paddle.compat as cpt\n'), ((68, 28, 68, 66), 'paddle.compat.round', 'cpt.round', ({(68, 38, 68, 65): 'roi[4] * self.spatial_scale'}, {}), '(roi[4] * self.spatial_scale)', True, 'import paddle.compat as cpt\n'), ((115, 21, 116, 76), 'numpy.random.random_integers', 'np.random.random_integers', ({(116, 20, 116, 21): '0', (116, 23, 116, 75): 'self.width // self.spatial_scale - self.pooled_width'}, {}), '(0, self.width // self.spatial_scale - self.\n    pooled_width)', True, 'import numpy as np\n'), ((117, 21, 118, 78), 'numpy.random.random_integers', 'np.random.random_integers', ({(118, 20, 118, 21): '0', (118, 23, 118, 77): 'self.height // self.spatial_scale - self.pooled_height'}, {}), '(0, self.height // self.spatial_scale - self.\n    pooled_height)', True, 'import numpy as np\n'), ((120, 21, 121, 80), 'numpy.random.random_integers', 'np.random.random_integers', ({(120, 47, 120, 69): 'x1 + self.pooled_width', (121, 47, 121, 79): 'self.width // self.spatial_scale'}, {}), '(x1 + self.pooled_width, self.width // self.\n    spatial_scale)', True, 'import numpy as np\n'), ((122, 21, 123, 79), 'numpy.random.random_integers', 'np.random.random_integers', ({(123, 20, 123, 43): 'y1 + self.pooled_height', (123, 45, 123, 78): 'self.height // self.spatial_scale'}, {}), '(y1 + self.pooled_height, self.height // self.\n    spatial_scale)', True, 'import numpy as np\n'), ((128, 20, 128, 34), 'numpy.array', 'np.array', ({(128, 29, 128, 33): 'rois'}, {}), '(rois)', True, 'import numpy as np\n'), ((81, 37, 81, 64), 'math.floor', 'math.floor', ({(81, 48, 81, 63): 'ph * bin_size_h'}, {}), '(ph * bin_size_h)', False, 'import math\n'), ((82, 37, 82, 64), 'math.floor', 'math.floor', ({(82, 48, 82, 63): 'pw * bin_size_w'}, {}), '(pw * bin_size_w)', False, 'import math\n'), ((83, 35, 83, 67), 'math.ceil', 'math.ceil', ({(83, 45, 83, 66): '(ph + 1) * bin_size_h'}, {}), '((ph + 1) * bin_size_h)', False, 'import math\n'), ((84, 35, 84, 67), 'math.ceil', 'math.ceil', ({(84, 45, 84, 66): '(pw + 1) * bin_size_w'}, {}), '((pw + 1) * bin_size_w)', False, 'import math\n')]