Git Product home page Git Product logo

megvision's People

Contributors

qsingle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

fweigao

megvision's Issues

Unaligned behavior of AdaptiveAvgPool2d with original psp implementation

In MegEngine, AdaptiveAvgPool is adpted from AvgPool by automatically determine kernel_size and stride. However, torch's implementation of AdaptiveAvgPool are highly diferent which uses diferent kernel_size and stride when sliding window.
I am not familiar with cpp and cuda, so there I implement a python version:

import megengine.module as M
import megengine.functional as F
from typing import Union, Tuple


class AdaptiveAvgPooling2D(M.Module):
    """ 
        use python to implement AdaptiveAvgPool2D in pytorch
    """

    def __init__(
        self,
        oshp: Union[int, Tuple[int, int]],
    ):
        super(AdaptiveAvgPooling2D, self).__init__()
        if isinstance(oshp, int):
            oshp = (oshp, oshp)
        self.oshp = oshp

    def _cal_kernel_size(self, ishp):
        kh = (ishp[0] + self.oshp[0] - 1) // self.oshp[0]
        kw = (ishp[1] + self.oshp[1] - 1) // self.oshp[1]
        return (kh, kw)

    @staticmethod
    def _zip(*x):
        element_length = len(x[0])
        for i in x:
            assert element_length == len(i)
        out = []
        total_length = len(x)
        for i in range(element_length):
            temp = []
            for j in range(total_length):
                temp.append(x[j][i])
            out.append(temp)
        return out

    def _get_points(self, input_size, kernel_size):
        start_points_h = (F.arange(
            self.oshp[0], dtype='float32') * (input_size[0] / self.oshp[0])).astype('int32')
        end_points_h = F.ceil(((F.arange(
            self.oshp[0], dtype='float32') + 1) * (input_size[0] / self.oshp[0]))).astype('int32')
        start_points_w = (F.arange(
            self.oshp[1], dtype='float32') * input_size[1] / self.oshp[1]).astype('int32')
        end_points_w = F.ceil(((F.arange(
            self.oshp[1], dtype='float32') + 1) * (input_size[1] / self.oshp[1]))).astype('int32')
        return self._zip(start_points_h, end_points_h), self._zip(start_points_w, end_points_w)

    def _get_windows(self, inp, coords, kernel_size):
        windows = []
        a = 0
        for h_s, h_e in coords[0]:
            for w_s, w_e in coords[1]:
                windows.append(F.mean(inp[:, :, h_s: h_e, w_s: w_e], axis=(2, 3)))
        windows = F.stack(windows, -1)
        return windows

    def forward(self, inputs):
        assert inputs.ndim == 4, "Currently only support 4D input"
        ishp = inputs.shape[-2:]
        kernel_size = self._cal_kernel_size(ishp)
        point_h, point_w = self._get_points(ishp, kernel_size)
        windows = self._get_windows(inputs, (point_h, point_w), kernel_size)
        return windows.reshape(*windows.shape[:2], *self.oshp)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.