예제 #1
0
 def pool_param(self, type='MAX', kernel_size=2, stride=2, pad=None, global_pooling=False):
     pool_param = pb.PoolingParameter()
     pool_param.pool = pool_param.PoolMethod.Value(type)
     if global_pooling:
         pool_param.global_pooling = True
     else:
         pool_param.kernel_size = pair_process(kernel_size)
         pool_param.stride = pair_process(stride)
         if pad:
             pool_param.pad = pad
     self.param.pooling_param.CopyFrom(pool_param)
예제 #2
0
def pooling_layer(name, bottom, top, mode, ph, pw, sh, sw, kh, kw):
    param = cp.PoolingParameter(
        pool=0 if mode == "MAX" else 1,
        pad_h=ph,
        pad_w=pw,
        stride_h=sh,
        stride_w=sw,
        kernel_h=kh,
        kernel_w=kw,
    )
    return cp.LayerParameter(bottom=bottom,
                             top=top,
                             name=name,
                             type="Pooling",
                             pooling_param=param)
예제 #3
0
    def pool_param(self, type='MAX', kernel_size=2, stride=2, pad=None):
        pool_param = pb.PoolingParameter()
        pool_param.pool = pool_param.PoolMethod.Value(type)
        if len(pair_process(kernel_size)) > 1:
            pool_param.kernel_h = kernel_size[0]
            pool_param.kernel_w = kernel_size[1]
        else:
            pool_param.kernel_size = kernel_size
        if len(pair_process(stride)) > 1:
            pool_param.stride_h = stride[0]
            pool_param.stride_w = stride[1]
        else:
            pool_param.stride = stride

        if pad:
            pool_param.pad = pad
        self.param.pooling_param.CopyFrom(pool_param)
예제 #4
0
 def pool_param(self,
                type='MAX',
                kernel_size=2,
                stride=2,
                pad=None,
                ceil_mode=True):
     pool_param = pb.PoolingParameter()
     pool_param.pool = pool_param.PoolMethod.Value(type)
     pool_param.kernel_size = pair_process(kernel_size)
     pool_param.stride = pair_process(stride)
     #pool_param.ceil_mode=ceil_mode
     if pad:
         if isinstance(pad, tuple):
             pool_param.pad_h = pad[0]
             pool_param.pad_w = pad[1]
         else:
             pool_param.pad = pad
     self.param.pooling_param.CopyFrom(pool_param)
예제 #5
0
def _pooling2d(opr, context):
    # assert opr.mode in [
    # 	  "MAX",
    # 	  "AVERAGE",
    # 	  "AVERAGE_COUNT_EXCLUDE_PADDING",
    # ], "Pooling op doesn't support mode {}, you can implement it in _pooling2d".format(
    # 	  opr.mode
    # )
    def _unexpand(x):
        if isinstance(x, Sequence):
            return x[0], x[1]
        elif isinstance(x, int):
            return x, x
        else:
            raise TypeError(
                "get error type! got {} expect int or tuple[int,..]".format(
                    type(x)))

    if not isinstance(opr, AdaptiveAvgPool2dOpr):
        if opr.mode == "AVERAGE_COUNT_EXCLUDE_PADDING":
            logger.warning(
                "Caffe average pooling layer doesn't support 'COUNT_EXCLUDE_PADDING', you'd better set pooling mode to 'AVERAGE'"
            )

        ph, pw = _unexpand(opr.padding)
        sh, sw = _unexpand(opr.stride)
        kh, kw = _unexpand(opr.kernel_size)
        assert not None in opr.inp_tensors[0].shape[2:4]

        ih, iw = opr.inp_tensors[0].shape[2:4]
        nh = ceil((ph * 2 + ih - kh + sh) / sh)
        nw = ceil((pw * 2 + iw - kw + sw) / sw)
        if ph > 0 and (nh - 1) * sh >= ih + ph:
            nh = nh - 1
        if pw > 0 and (nw - 1) * sw >= iw + pw:
            nw = nw - 1
    elif isinstance(opr, AdaptiveAvgPool2dOpr):
        oh, ow = _unexpand(opr.out_shape)
        ih, iw = list(opr.inp_tensors[0].shape)[-2:]
        ph, pw = 0, 0
        sh, sw = ih // oh, iw // ow
        kh, kw = ih - (oh - 1) * sh, iw - (ow - 1) * sw

    if hasattr(opr, "mode"):
        pool_mode = 0 if opr.mode == "MAX" else 1
    else:
        pool_mode = 1

    param = cp.PoolingParameter(
        pool=pool_mode,
        pad_h=ph,
        pad_w=pw,
        stride_h=sh,
        stride_w=sw,
        kernel_h=kh,
        kernel_w=kw,
    )

    bottom = [context.get_blob_name(opr.inp_tensors[0])]
    top = [context.set_blob_name(opr.out_tensors[0], opr.out_tensors[0].name)]
    context.add_layer(
        cp.LayerParameter(
            name=opr.out_tensors[0].name,
            type="Pooling",
            bottom=bottom,
            top=top,
            pooling_param=param,
        ))

    if not isinstance(opr, AdaptiveAvgPool2dOpr):
        if (nh - 1) * sh + kh > ph * 2 + ih:
            logger.warning("Add extra 'slice layer' after Pooling Opr %s",
                           opr.out_tensors[0].name)
            param = cp.SliceParameter(axis=2, slice_point=[nh - 1])
            bottom = top[:1]
            name = opr.out_tensors[0].name + context.gen_name
            top = [name, context.gen_name]
            context.add_layer(
                cp.LayerParameter(name=name,
                                  type="Slice",
                                  bottom=bottom,
                                  top=top,
                                  slice_param=param))
            context.add_layer(silence_blob(top[1]))
        if (nw - 1) * sw + kw > pw * 2 + iw:
            logger.warning("Add extra 'slice layer' after Pooling Opr %s",
                           opr.out_tensors[0].name)
            param = cp.SliceParameter(axis=3, slice_point=[nw - 1])
            bottom = top[:1]
            name = opr.out_tensors[0].name + context.gen_name
            top = [name, context.gen_name]
            context.add_layer(
                cp.LayerParameter(name=name,
                                  type="Slice",
                                  bottom=bottom,
                                  top=top,
                                  slice_param=param))
            context.add_layer(silence_blob(top[1]))
    context.reset_blob_name(opr.out_tensors[0], top[0])