(inputs, kernel, strides=None, name=None)
avgpool
avgpool
avgpool
def avgpool(inputs, kernel, strides=None, name=None):
    """
        avgpool
    """
    if strides is None:
        strides = (1, 1)
    padding_pixel = (0, 0, 0, 0)
    padding_pixel += calc_padding(inputs.shape[2], strides[0], 1, kernel[0])
    padding_pixel += calc_padding(inputs.shape[3], strides[1], 1, kernel[1])

    if padding_pixel[4] == padding_pixel[5] and padding_pixel[
            6] == padding_pixel[7]:
        # same padding pixel num on all sides.
        return fluid.layers.pool2d(
            inputs,
            kernel,
            'avg',
            strides,
            pool_padding=(padding_pixel[4], padding_pixel[6]),
            ceil_mode=False)
    elif padding_pixel[4] + 1 == padding_pixel[5] \
            and padding_pixel[6] + 1 == padding_pixel[7] \
            and strides == (1, 1):
        # different padding size: first pad then crop.
        x = fluid.layers.pool2d(
            inputs,
            kernel,
            'avg',
            strides,
            pool_padding=(padding_pixel[5], padding_pixel[7]),
            ceil_mode=False)
        x_shape = x.shape
        return fluid.layers.crop(
            x,
            shape=(-1, x_shape[1], x_shape[2] - 1, x_shape[3] - 1),
            offsets=(0, 0, 1, 1), name=name)
    else:
        # not support. use padding-zero and pool2d.
        print("Warning: use zero-padding in avgpool")
        outputs = fluid.layers.pad(inputs, padding_pixel, 0)
        return fluid.layers.pool2d(
                outputs, kernel, 'avg', strides, pool_padding=0,
                ceil_mode=False, name=name)
def avgpool ( inputs , kernel , strides = None , name = None ) : if strides is None : strides = ( 1 , 1 ) padding_pixel = ( 0 , 0 , 0 , 0 ) padding_pixel += calc_padding ( inputs . shape [ 2 ] , strides [ 0 ] , 1 , kernel [ 0 ] ) padding_pixel += calc_padding ( inputs . shape [ 3 ] , strides [ 1 ] , 1 , kernel [ 1 ] ) if padding_pixel [ 4 ] == padding_pixel [ 5 ] and padding_pixel [ 6 ] == padding_pixel [ 7 ] : # same padding pixel num on all sides. return fluid . layers . pool2d ( inputs , kernel , 'avg' , strides , pool_padding = ( padding_pixel [ 4 ] , padding_pixel [ 6 ] ) , ceil_mode = False ) elif padding_pixel [ 4 ] + 1 == padding_pixel [ 5 ] and padding_pixel [ 6 ] + 1 == padding_pixel [ 7 ] and strides == ( 1 , 1 ) : # different padding size: first pad then crop. x = fluid . layers . pool2d ( inputs , kernel , 'avg' , strides , pool_padding = ( padding_pixel [ 5 ] , padding_pixel [ 7 ] ) , ceil_mode = False ) x_shape = x . shape return fluid . layers . crop ( x , shape = ( - 1 , x_shape [ 1 ] , x_shape [ 2 ] - 1 , x_shape [ 3 ] - 1 ) , offsets = ( 0 , 0 , 1 , 1 ) , name = name ) else : # not support. use padding-zero and pool2d. print ( "Warning: use zero-padding in avgpool" ) outputs = fluid . layers . pad ( inputs , padding_pixel , 0 ) return fluid . layers . pool2d ( outputs , kernel , 'avg' , strides , pool_padding = 0 , ceil_mode = False , name = name )