示例#1
0
 def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True):
     kernel_size = _pair(kernel_size)
     stride = _pair(stride)
     padding = _pair(padding)
     dilation = _pair(dilation)
     super(SNConv2d, self).__init__(
         in_channels, out_channels, kernel_size, stride, padding, dilation,
         False, _pair(0), groups, bias)
示例#2
0
def avg_pool2d(g, input, kernel_size, stride, padding, ceil_mode, count_include_pad):
    if ceil_mode:
        return _unimplemented("avg_pool2d", "ceil_mode")
    if not stride:
        stride = kernel_size
    # TODO: What about count_include_pad?!
    return g.op("AveragePool", input,
                kernel_shape_i=_pair(kernel_size),
                strides_i=_pair(stride),
                pads_i=_pair(padding) * 2)
    def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True,
                 cuda=False, init_weight=None, init_bias=None, clip_var=None):
        kernel_size = utils._pair(kernel_size)
        stride = utils._pair(stride)
        padding = utils._pair(padding)
        dilation = utils._pair(dilation)

        super(Conv2dGroupNJ, self).__init__(
            in_channels, out_channels, kernel_size, stride, padding, dilation,
            False, utils._pair(0), groups, bias, init_weight, init_bias, cuda, clip_var)
示例#4
0
def max_pool2d(g, input, kernel_size, stride, padding, dilation, ceil_mode):
    if ceil_mode:
        return _unimplemented("max_pool2d", "ceil_mode")
    if set(_pair(dilation)) != {1}:
        return _unimplemented("max_pool2d", "dilation")
    if not stride:
        stride = kernel_size
    r = g.op("MaxPool", input,
             kernel_shape_i=_pair(kernel_size),
             pads_i=_pair(padding) * 2,
             strides_i=_pair(stride))
    return r, None
示例#5
0
 def forward(ctx, input, roi, output_size, spatial_scale):
     ctx.output_size = _pair(output_size)
     ctx.spatial_scale = spatial_scale
     ctx.input_shape = input.size()
     output, argmax = _C.roi_pool_forward(input, roi, spatial_scale,
                                          output_size[0], output_size[1])
     ctx.save_for_backward(input, roi, argmax)
     return output
示例#6
0
 def forward(ctx, input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False):
     ctx.kernel_size = _pair(kernel_size)
     ctx.stride = _pair(stride if stride is not None else kernel_size)
     ctx.padding = _pair(padding)
     ctx.dilation = _pair(dilation)
     ctx.ceil_mode = ceil_mode
     backend = type2backend[type(input)]
     indices, output = input.new().long(), input.new()
     backend.SpatialDilatedMaxPooling_updateOutput(backend.library_state,
                                                   input, output, indices,
                                                   ctx.kernel_size[1], ctx.kernel_size[0],
                                                   ctx.stride[1], ctx.stride[0],
                                                   ctx.padding[1], ctx.padding[0],
                                                   ctx.dilation[1], ctx.dilation[0],
                                                   ctx.ceil_mode)
     ctx.save_for_backward(input, indices)
     ctx.mark_non_differentiable(indices)
     return output, indices
示例#7
0
 def forward(ctx, input, roi, output_size, spatial_scale, sampling_ratio):
     ctx.save_for_backward(roi)
     ctx.output_size = _pair(output_size)
     ctx.spatial_scale = spatial_scale
     ctx.sampling_ratio = sampling_ratio
     ctx.input_shape = input.size()
     output = _C.roi_align_forward(
         input, roi, spatial_scale, output_size[0], output_size[1], sampling_ratio
     )
     return output
示例#8
0
 def forward(ctx, input, kernel_size, stride=None, padding=0,
             ceil_mode=False, count_include_pad=True):
     ctx.kernel_size = _pair(kernel_size)
     ctx.stride = _pair(stride if stride is not None else kernel_size)
     ctx.padding = _pair(padding)
     ctx.ceil_mode = ceil_mode
     ctx.count_include_pad = count_include_pad
     backend = type2backend[type(input)]
     output = input.new()
     # can avoid this with cudnn
     ctx.save_for_backward(input)
     backend.SpatialAveragePooling_updateOutput(
         backend.library_state,
         input, output,
         ctx.kernel_size[1], ctx.kernel_size[0],
         ctx.stride[1], ctx.stride[0],
         ctx.padding[1], ctx.padding[0],
         ctx.ceil_mode, ctx.count_include_pad)
     return output
示例#9
0
 def forward(ctx, input, output_size):
     ctx.output_size = _pair(output_size)
     backend = type2backend[type(input)]
     output = input.new()
     ctx.save_for_backward(input)
     backend.SpatialAdaptiveAveragePooling_updateOutput(
         backend.library_state,
         input, output,
         ctx.output_size[1], ctx.output_size[0])
     return output
示例#10
0
 def forward(ctx, input, output_size):
     ctx.output_size = list(_pair(output_size))
     for i, s in enumerate(ctx.output_size):
         ctx.output_size[i] = ctx.output_size[i] or input.size(i + 2)
     ctx.output_size = tuple(ctx.output_size)
     backend = type2backend[type(input)]
     indices, output = input.new().long(), input.new()
     backend.SpatialAdaptiveMaxPooling_updateOutput(backend.library_state,
                                                    input, output, indices,
                                                    ctx.output_size[1], ctx.output_size[0])
     ctx.save_for_backward(input, indices)
     ctx.mark_non_differentiable(indices)
     return output, indices
示例#11
0
    def forward(ctx, input, output_size):
        ctx.output_size = list(_pair(output_size))
        for i, s in enumerate(ctx.output_size):
            ctx.output_size[i] = ctx.output_size[i] or input.size(i + 2)
        ctx.output_size = tuple(ctx.output_size)

        backend = type2backend[type(input)]
        output = input.new()
        ctx.save_for_backward(input)
        backend.SpatialAdaptiveAveragePooling_updateOutput(
            backend.library_state,
            input, output,
            ctx.output_size[1], ctx.output_size[0])
        return output
示例#12
0
 def forward(ctx, input, output_size, return_indices=False):
     ctx.output_size = _pair(output_size)
     ctx.return_indices = return_indices
     backend = type2backend[type(input)]
     indices, output = input.new().long(), input.new()
     backend.SpatialAdaptiveMaxPooling_updateOutput(backend.library_state,
                                                    input, output, indices,
                                                    ctx.output_size[1], ctx.output_size[0])
     if ctx.return_indices:
         ctx.save_for_backward(input, indices)
         ctx.mark_non_differentiable(indices)
         return output, indices
     else:
         ctx.save_for_backward(input)
         ctx.indices = indices
         return output
示例#13
0
def deform_conv2d(input,
                  offset,
                  weight,
                  bias=None,
                  stride=(1, 1),
                  padding=(0, 0),
                  dilation=(1, 1)):
    # type: (Tensor, Tensor, Tensor, Optional[Tensor], Tuple[int, int], Tuple[int, int], Tuple[int, int]) -> Tensor
    """
    Performs Deformable Convolution, described in Deformable Convolutional Networks

    Arguments:
        input (Tensor[batch_size, in_channels, in_height, in_width]): input tensor
        offset (Tensor[batch_size, 2 * offset_groups * kernel_height * kernel_width,
            out_height, out_width]): offsets to be applied for each position in the
            convolution kernel.
        weight (Tensor[out_channels, in_channels // groups, kernel_height, kernel_width]):
            convolution weights, split into groups of size (in_channels // groups)
        bias (Tensor[out_channels]): optional bias of shape (out_channels,). Default: None
        stride (int or Tuple[int, int]): distance between convolution centers. Default: 1
        padding (int or Tuple[int, int]): height/width of padding of zeroes around
            each image. Default: 0
        dilation (int or Tuple[int, int]): the spacing between kernel elements. Default: 1

    Returns:
        output (Tensor[batch_sz, out_channels, out_h, out_w]): result of convolution


    Examples::
        >>> input = torch.rand(1, 3, 10, 10)
        >>> kh, kw = 3, 3
        >>> weight = torch.rand(5, 3, kh, kw)
        >>> # offset should have the same spatial size as the output
        >>> # of the convolution. In this case, for an input of 10, stride of 1
        >>> # and kernel size of 3, without padding, the output size is 8
        >>> offset = torch.rand(5, 2 * kh * kw, 8, 8)
        >>> out = deform_conv2d(input, offset, weight)
        >>> print(out.shape)
        >>> # returns
        >>>  torch.Size([1, 5, 8, 8])
    """

    out_channels = weight.shape[0]
    if bias is None:
        bias = torch.zeros(out_channels,
                           device=input.device,
                           dtype=input.dtype)

    stride_h, stride_w = _pair(stride)
    pad_h, pad_w = _pair(padding)
    dil_h, dil_w = _pair(dilation)
    weights_h, weights_w = weight.shape[-2:]
    _, n_in_channels, in_h, in_w = input.shape

    n_offset_grps = offset.shape[1] // (2 * weights_h * weights_w)
    n_weight_grps = n_in_channels // weight.shape[1]

    if n_offset_grps == 0:
        raise RuntimeError(
            "the shape of the offset tensor at dimension 1 is not valid. It should "
            "be a multiple of 2 * weight.size[2] * weight.size[3].\n"
            "Got offset.shape[1]={}, while 2 * weight.size[2] * weight.size[3]={}"
            .format(offset.shape[1], 2 * weights_h * weights_w))

    return torch.ops.torchvision.deform_conv2d(input, weight, offset, bias,
                                               stride_h, stride_w, pad_h,
                                               pad_w, dil_h, dil_w,
                                               n_weight_grps, n_offset_grps)
示例#14
0
    def __init__(self,
                 num_convs=4,
                 roi_feat_size=14,
                 in_channels=256,
                 conv_kernel_size=3,
                 conv_out_channels=256,
                 upsample_method='deconv',
                 upsample_ratio=2,
                 num_upsamples=1,
                 num_classes=81,
                 class_agnostic=False,
                 upsample_cfg=None,
                 conv_cfg=None,
                 norm_cfg=None,
                 loss_mask=dict(type='CrossEntropyLoss',
                                use_mask=True,
                                loss_weight=1.0)):
        super(FCNMaskHead, self).__init__()
        if upsample_method not in [
                None, 'deconv', 'nearest', 'bilinear', 'carafe'
        ]:
            raise ValueError(
                'Invalid upsample method {}, accepted methods '
                'are "deconv", "nearest", "bilinear", "carafe"'.format(
                    upsample_method))
        if upsample_method == 'carafe':
            assert upsample_cfg is not None
        self.upsample_cfg = upsample_cfg
        self.num_convs = num_convs
        # WARN: roi_feat_size is reserved and not used
        self.roi_feat_size = _pair(roi_feat_size)
        self.in_channels = in_channels
        self.conv_kernel_size = conv_kernel_size
        self.conv_out_channels = conv_out_channels
        self.upsample_method = upsample_method
        self.upsample_ratio = upsample_ratio
        self.num_classes = num_classes
        self.class_agnostic = class_agnostic
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.fp16_enabled = False
        self.loss_mask = build_loss(loss_mask)
        self.num_upsamples = num_upsamples

        self.convs = nn.ModuleList()
        for i in range(self.num_convs):
            in_channels = (self.in_channels
                           if i == 0 else self.conv_out_channels)
            padding = (self.conv_kernel_size - 1) // 2
            self.convs.append(
                ConvModule(in_channels,
                           self.conv_out_channels,
                           self.conv_kernel_size,
                           padding=padding,
                           conv_cfg=conv_cfg,
                           norm_cfg=norm_cfg))
        upsample_in_channels = (self.conv_out_channels
                                if self.num_convs > 0 else in_channels)
        if self.upsample_method is None:
            self.upsample = None
        elif self.upsample_method == 'deconv':
            self.upsample = nn.ModuleList()
            for i in range(self.num_upsamples):
                # upsample_in_channels = (
                #     self.upsample_in_channels if i == 0 else self.conv_out_channels)
                self.upsample.append(
                    nn.ConvTranspose2d(upsample_in_channels,
                                       self.conv_out_channels,
                                       self.upsample_ratio,
                                       stride=self.upsample_ratio))
        elif self.upsample_method == 'carafe':
            self.upsample = CARAFEPack(upsample_in_channels,
                                       self.upsample_ratio,
                                       **self.upsample_cfg)
        else:
            self.upsample = nn.ModuleList()
            for i in range(self.num_upsamples):
                self.upsample.append(
                    nn.Upsample(scale_factor=self.upsample_ratio,
                                mode=self.upsample_method))

        out_channels = 1 if self.class_agnostic else self.num_classes
        logits_in_channel = (self.conv_out_channels if self.upsample_method
                             == 'deconv' else upsample_in_channels)
        self.conv_logits = nn.Conv2d(logits_in_channel, out_channels, 1)
        self.relu = nn.ReLU(inplace=True)
        self.debug_imgs = None
示例#15
0
def conv2d_config(in_shape, out_shape, kernel_size, stride=None):
    """
    Given desired input and output tensor shapes and convolution kernel size,
    returns configurations that can be used to construct an appropriate 2D
    convolution operation satisfying the desired properties.

    Args:
        in_shape: shape of the input tensor. May be either [batch, channel, height, width]
                  or [channel, height, width]
        out_shape: shape of the output tensor. May be either [batch, channel, height, width]
                   or [channel, height, width]
        kernel_size: shape of the kernel. May be an integer or a pair tuple
        stride: (OPTIONAL) desired stride to be used. If not provided, optimal stride size
                will be computed and returned to minimize the necessary amount of padding
                or stripping.

    Returns:
        A tuple (stride, padding, output_padding, padded_shape, conv_type, padding_type).
        stride: optimial stride size to be used. If stride was passed in, no change is made.
        padding: padding to be applied to each edge
        output_padding: if operation is transpose convolution, supplies output_padding that's
            necessary. Otherwise, this is None.
        conv_type: the required type of convolution. It is either "NORMAL" or "TRANSPOSE"
        padding_type: string to indicate the type of padding. Either "VALID" or "SAME".

    """
    in_shape = np.array(in_shape[-3:])
    out_shape = np.array(out_shape[-3:])
    kern_shape = np.array(kernel_size)

    # determine the kind of convolution to use
    if np.all(in_shape[-2:] >= out_shape[-2:]):
        conv_type = "NORMAL"
    elif np.all(in_shape[-2:] <= out_shape[-2:]):
        conv_type = "TRANSPOSE"
        in_shape, out_shape = out_shape, in_shape
    else:
        raise ValueError('Input shape dimensions must be both >= OR <= the output shape dimensions')

    if stride is None:
        stride = np.ceil((in_shape[-2:] - kern_shape + 1) / (out_shape[-2:] - 1)).astype(np.int)
    else:
        stride = np.array(_pair(stride))
    stride[stride <= 0] = 1
    padding = (out_shape[-2:] - 1) * stride + kern_shape - in_shape[-2:]

    if np.all(np.ceil(in_shape[-2:] / stride) == out_shape[-2:]):
        padding_type = 'SAME'
    else:
        padding_type = 'VALID'

    # get padded input shape
    in_shape[-2:] = in_shape[-2:] + padding.astype(np.int)
    padded_shape = tuple(in_shape.tolist())
    if conv_type == "TRANSPOSE":
        output_padding = tuple((padding % 2 != 0).astype(np.int).tolist())
    else:
        output_padding = None

    padding = tuple(np.ceil(padding / 2).astype(np.int).tolist())
    stride = tuple(stride.tolist())

    return stride, padding, output_padding, \
           padded_shape, conv_type, padding_type
示例#16
0
文件: __init__.py 项目: ptillet/isaac
 def __init__(self, kernel_size, stride=1, padding=0):
     super(AvgPool2d, self).__init__(_pair(kernel_size), 'avg', _pair(stride), _pair(padding))
示例#17
0
    def __init__(self,
                 num_convs=4,
                 roi_feat_size=14,
                 in_channels=256,
                 conv_kernel_size=3,
                 conv_out_channels=256,
                 num_classes=81,
                 class_agnostic=False,
                 upsample_cfg=dict(type='deconv', scale_factor=2),
                 conv_cfg=None,
                 norm_cfg=None,
                 loss_mask=dict(type='CrossEntropyLoss',
                                use_mask=True,
                                loss_weight=1.0)):
        super(FCNMaskHead, self).__init__()
        self.upsample_cfg = upsample_cfg.copy()
        if self.upsample_cfg['type'] not in [
                None, 'deconv', 'nearest', 'bilinear', 'carafe'
        ]:
            raise ValueError(
                'Invalid upsample method {}, accepted methods '
                'are "deconv", "nearest", "bilinear", "carafe"'.format(
                    self.upsample_cfg['type']))
        self.num_convs = num_convs
        # WARN: roi_feat_size is reserved and not used
        self.roi_feat_size = _pair(roi_feat_size)
        self.in_channels = in_channels
        self.conv_kernel_size = conv_kernel_size
        self.conv_out_channels = conv_out_channels
        self.upsample_method = self.upsample_cfg.get('type')
        self.scale_factor = self.upsample_cfg.pop('scale_factor')
        self.num_classes = num_classes
        self.class_agnostic = class_agnostic
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.fp16_enabled = False
        self.loss_mask = build_loss(loss_mask)

        self.convs = nn.ModuleList()
        for i in range(self.num_convs):
            in_channels = (self.in_channels
                           if i == 0 else self.conv_out_channels)
            padding = (self.conv_kernel_size - 1) // 2
            self.convs.append(
                ConvModule(in_channels,
                           self.conv_out_channels,
                           self.conv_kernel_size,
                           padding=padding,
                           conv_cfg=conv_cfg,
                           norm_cfg=norm_cfg))
        upsample_in_channels = (self.conv_out_channels
                                if self.num_convs > 0 else in_channels)
        upsample_cfg_ = self.upsample_cfg.copy()
        if self.upsample_method is None:
            self.upsample = None
        elif self.upsample_method == 'deconv':
            upsample_cfg_.update(in_channels=upsample_in_channels,
                                 out_channels=self.conv_out_channels,
                                 kernel_size=self.scale_factor,
                                 stride=self.scale_factor)
        elif self.upsample_method == 'carafe':
            upsample_cfg_.update(channels=upsample_in_channels,
                                 scale_factor=self.scale_factor)
        else:
            # suppress warnings
            align_corners = (None
                             if self.upsample_method == 'nearest' else False)
            upsample_cfg_.update(scale_factor=self.scale_factor,
                                 mode=self.upsample_method,
                                 align_corners=align_corners)
        self.upsample = build_upsample_layer(upsample_cfg_)

        out_channels = 1 if self.class_agnostic else self.num_classes
        logits_in_channel = (self.conv_out_channels if self.upsample_method
                             == 'deconv' else upsample_in_channels)
        self.conv_logits = nn.Conv2d(logits_in_channel, out_channels, 1)
        self.relu = nn.ReLU(inplace=True)
        self.debug_imgs = None
示例#18
0
    def __init__(self,
                 with_avg_pool=False,
                 with_cls=True,
                 with_reg=True,
                 roi_feat_size=7,
                 in_channels=[256, 512, 1024],
                 anchors=[[10, 13], [16, 30], [33, 23], [30, 61], [62, 45],
                          [59, 119], [116, 90], [156, 198], [373, 326]],
                 num_classes=80,
                 input_size=416,
                 target_means=[0., 0., 0., 0.],
                 target_stds=[0.1, 0.1, 0.2, 0.2],
                 reg_class_agnostic=False,
                 loss_cls=dict(type='CrossEntropyLoss',
                               use_sigmoid=False,
                               loss_weight=1.0),
                 loss_bbox=dict(type='SmoothL1Loss', beta=1.0,
                                loss_weight=1.0)):
        super(YoloHead, self).__init__()
        assert with_cls or with_reg
        self.with_avg_pool = with_avg_pool
        self.with_cls = with_cls
        self.with_reg = with_reg
        self.roi_feat_size = _pair(roi_feat_size)
        self.roi_feat_area = self.roi_feat_size[0] * self.roi_feat_size[1]
        self.in_channels = in_channels
        self.num_classes = num_classes
        self.target_means = target_means
        self.target_stds = target_stds
        self.reg_class_agnostic = reg_class_agnostic
        self.fp16_enabled = False

        self.input_size = input_size
        self.grid_size = [
            int(input_size / 8),
            int(input_size / 16),
            int(input_size / 32)
        ]
        self.anchors = anchors
        in_channels = self.in_channels
        self.head_layers = []

        for i in range(len(in_channels)):
            numIn = int(in_channels[i])
            # if numIn != 1024:
            #     numIn = numIn  + int(numIn / 2)
            numOut = int(numIn / 2)
            numOut2 = 3 * (self.num_classes - 1 + 5)

            if numIn != 1024:
                up = nn.Sequential(
                    DarknetConv2D_BN_Leaky(numIn,
                                           numOut,
                                           ksize=1,
                                           stride=1,
                                           padding=0),
                    Upsample(scale_factor=2))
                last = LastLayer(numIn + numOut, numOut, numOut2)
                up_name = 'up{}'.format(i + 1)
                self.add_module(up_name, up)
                last_name = 'last{}'.format(i + 1)
                self.add_module(last_name, last)
                self.head_layers.append([up_name, last_name])
            else:
                last = LastLayer(numIn, numOut, numOut2)
                last_name = 'last{}'.format(i + 1)
                self.add_module(last_name, last)
                self.head_layers.append(last_name)
def max_pool2d(input, kernel_size: Vector, stride: Vector, padding: Vector, dilation: Vector, ceil_mode: bool):
    if len(_pair(stride)) == 0:
        stride = kernel_size
    return torch.ops.torch_ipex.max_pool2d(input, _pair(kernel_size), _pair(stride), _pair(padding), _pair(dilation), ceil_mode)
示例#20
0
"""Split-Attention"""
示例#21
0
 def __init__(self, kernel_size=3, stride=1, padding=0, same=False):
     super(MedianPool2d, self).__init__()
     self.k = _pair(kernel_size)
     self.stride = _pair(stride)
     self.padding = _quadruple(padding)  # convert to l, r, t, b
     self.same = same
示例#22
0
    def __init__(self,
                 num_convs=4,
                 roi_feat_size=14,
                 in_channels=256,
                 conv_kernel_size=3,
                 conv_out_channels=256,
                 upsample_method='deconv',
                 upsample_ratio=2,
                 num_classes=81,
                 class_agnostic=False,
                 conv_cfg=None,
                 norm_cfg=None,
                 loss_mask=dict(type='CrossEntropyLoss',
                                use_mask=True,
                                loss_weight=1.0)):
        super(FCNMaskHead, self).__init__()
        if upsample_method not in [None, 'deconv', 'nearest', 'bilinear']:
            raise ValueError(
                'Invalid upsample method {}, accepted methods '
                'are "deconv", "nearest", "bilinear"'.format(upsample_method))
        self.num_convs = num_convs
        # WARN: roi_feat_size is reserved and not used
        self.roi_feat_size = _pair(roi_feat_size)
        self.in_channels = in_channels
        self.conv_kernel_size = conv_kernel_size
        self.conv_out_channels = conv_out_channels
        self.upsample_method = upsample_method
        self.upsample_ratio = upsample_ratio
        self.num_classes = num_classes
        self.class_agnostic = class_agnostic
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.fp16_enabled = False
        self.loss_mask = build_loss(loss_mask)

        self.convs = nn.ModuleList()
        for i in range(self.num_convs):
            in_channels = (self.in_channels
                           if i == 0 else self.conv_out_channels)
            padding = (self.conv_kernel_size - 1) // 2
            self.convs.append(
                ConvModule(in_channels,
                           self.conv_out_channels,
                           self.conv_kernel_size,
                           padding=padding,
                           conv_cfg=conv_cfg,
                           norm_cfg=norm_cfg))
        upsample_in_channels = (self.conv_out_channels
                                if self.num_convs > 0 else in_channels)
        if self.upsample_method is None:
            self.upsample = None
        elif self.upsample_method == 'deconv':
            self.upsample = nn.ConvTranspose2d(upsample_in_channels,
                                               self.conv_out_channels,
                                               self.upsample_ratio,
                                               stride=self.upsample_ratio)
        else:
            self.upsample = nn.Upsample(scale_factor=self.upsample_ratio,
                                        mode=self.upsample_method)

        out_channels = 1 if self.class_agnostic else self.num_classes
        logits_in_channel = (self.conv_out_channels if self.upsample_method
                             == 'deconv' else upsample_in_channels)
        # each binary mask is corresponding to a class (exclude background) in origin paper.
        # but in most implement, background is included even the 0-th dim is discharged.
        self.conv_logits = nn.Conv2d(logits_in_channel, out_channels, 1)
        self.relu = nn.ReLU(inplace=True)
        self.debug_imgs = None
示例#23
0
def plot_locally_connected_weights(
    weights: torch.Tensor,
    n_filters: int,
    kernel_size: Union[int, Tuple[int, int]],
    conv_size: Union[int, Tuple[int, int]],
    locations: torch.Tensor,
    input_sqrt: Union[int, Tuple[int, int]],
    wmin: float = 0.0,
    wmax: float = 1.0,
    im: Optional[AxesImage] = None,
    lines: bool = True,
    figsize: Tuple[int, int] = (5, 5),
    cmap: str = "hot_r",
) -> AxesImage:
    # language=rst
    """
    Plot a connection weight matrix of a :code:`Connection` with `locally connected
    structure <http://yann.lecun.com/exdb/publis/pdf/gregor-nips-11.pdf>_.

    :param weights: Weight matrix of Conv2dConnection object.
    :param n_filters: No. of convolution kernels in use.
    :param kernel_size: Side length(s) of 2D convolution kernels.
    :param conv_size: Side length(s) of 2D convolution population.
    :param locations: Indices of input receptive fields for convolution population
        neurons.
    :param input_sqrt: Side length(s) of 2D input data.
    :param wmin: Minimum allowed weight value.
    :param wmax: Maximum allowed weight value.
    :param im: Used for re-drawing the weights plot.
    :param lines: Whether or not to draw horizontal and vertical lines separating input
        regions.
    :param figsize: Horizontal, vertical figure size in inches.
    :param cmap: Matplotlib colormap.
    :return: Used for re-drawing the weights plot.
    """
    kernel_size = _pair(kernel_size)
    conv_size = _pair(conv_size)
    input_sqrt = _pair(input_sqrt)

    reshaped = reshape_locally_connected_weights(
        weights, n_filters, kernel_size, conv_size, locations, input_sqrt
    )
    n_sqrt = int(np.ceil(np.sqrt(n_filters)))

    if not im:
        fig, ax = plt.subplots(figsize=figsize)

        im = ax.imshow(reshaped.cpu(), cmap=cmap, vmin=wmin, vmax=wmax)
        div = make_axes_locatable(ax)
        cax = div.append_axes("right", size="5%", pad=0.05)

        if lines:
            for i in range(
                n_sqrt * kernel_size[0],
                n_sqrt * conv_size[0] * kernel_size[0],
                n_sqrt * kernel_size[0],
            ):
                ax.axhline(i - 0.5, color="g", linestyle="--")

            for i in range(
                n_sqrt * kernel_size[1],
                n_sqrt * conv_size[1] * kernel_size[1],
                n_sqrt * kernel_size[1],
            ):
                ax.axvline(i - 0.5, color="g", linestyle="--")

        ax.set_xticks(())
        ax.set_yticks(())
        ax.set_aspect("auto")

        plt.colorbar(im, cax=cax)
        fig.tight_layout()
    else:
        im.set_data(reshaped)

    return im
示例#24
0
    def __init__(self, in_channels, out_channels, kernel_size=1, stride=1,
                 padding=0, dilation=1, groups=1, bias=False):
        """

        :param in_channels:
        :param out_channels:
        :param kernel_size:
        :param stride:
        :param padding:
        :param dilation:
        :param groups:
        :param bias:
        """

        kernel_size = _pair(kernel_size)
        stride = _pair(stride)
        padding = _pair(padding)
        dilation = _pair(dilation)
        super(HighFrequencyGatedSpatialConv2d, self).__init__(
            in_channels, out_channels, kernel_size, stride, padding, dilation,
            False, _pair(0), groups, bias)

        self._gate_conv = nn.Sequential(
            mynn.Norm2d(in_channels+1),
            nn.Conv2d(in_channels+1, in_channels+1, 1),
            nn.ReLU(), 
            nn.Conv2d(in_channels+1, 1, 1),
            mynn.Norm2d(1),
            nn.Sigmoid()
        )

        kernel_size = 7
        sigma = 3

        x_cord = torch.arange(kernel_size).float()
        x_grid = x_cord.repeat(kernel_size).view(kernel_size, kernel_size).float()
        y_grid = x_grid.t().float()
        xy_grid = torch.stack([x_grid, y_grid], dim=-1).float()

        mean = (kernel_size - 1)/2.
        variance = sigma**2.
        gaussian_kernel = (1./(2.*math.pi*variance)) *\
                          torch.exp(
                              -torch.sum((xy_grid - mean)**2., dim=-1) /\
                              (2*variance)
                          )

        gaussian_kernel = gaussian_kernel / torch.sum(gaussian_kernel)

        gaussian_kernel = gaussian_kernel.view(1, 1, kernel_size, kernel_size)
        gaussian_kernel = gaussian_kernel.repeat(in_channels, 1, 1, 1)

        self.gaussian_filter = nn.Conv2d(in_channels=in_channels, out_channels=in_channels, padding=3,
                                         kernel_size=kernel_size, groups=in_channels, bias=False)

        self.gaussian_filter.weight.data = gaussian_kernel
        self.gaussian_filter.weight.requires_grad = False

        self.cw = nn.Conv2d(in_channels * 2, in_channels, 1)
 
        self.procdog = nn.Sequential(
            nn.Conv2d(in_channels, in_channels, 1),
            mynn.Norm2d(in_channels),
            nn.Sigmoid()
        )
示例#25
0
def _ann_to_snn_helper(prev, current, node_type, **kwargs):
    # language=rst
    """
    Helper function for main ``ann_to_snn`` method.

    :param prev: Previous PyTorch module in artificial neural network.
    :param current: Current PyTorch module in artificial neural network.
    :return: Spiking neural network layer and connection corresponding to ``prev`` and ``current`` PyTorch modules.
    """
    if isinstance(current, nn.Linear):
        layer = node_type(n=current.out_features,
                          reset=0,
                          thresh=1,
                          refrac=0,
                          **kwargs)
        bias = current.bias if current.bias is not None else torch.zeros(
            layer.n)
        connection = topology.Connection(source=prev,
                                         target=layer,
                                         w=current.weight.t(),
                                         b=bias)

    elif isinstance(current, nn.Conv2d):
        input_height, input_width = prev.shape[2], prev.shape[3]
        out_channels, output_height, output_width = (
            current.out_channels,
            prev.shape[2],
            prev.shape[3],
        )

        width = (input_height - current.kernel_size[0] +
                 2 * current.padding[0]) / current.stride[0] + 1
        height = (input_width - current.kernel_size[1] +
                  2 * current.padding[1]) / current.stride[1] + 1
        shape = (1, out_channels, int(width), int(height))

        layer = node_type(shape=shape, reset=0, thresh=1, refrac=0, **kwargs)
        bias = current.bias if current.bias is not None else torch.zeros(
            layer.shape[1])
        connection = topology.Conv2dConnection(
            source=prev,
            target=layer,
            kernel_size=current.kernel_size,
            stride=current.stride,
            padding=current.padding,
            dilation=current.dilation,
            w=current.weight,
            b=bias,
        )

    elif isinstance(current, nn.MaxPool2d):
        input_height, input_width = prev.shape[2], prev.shape[3]
        current.kernel_size = _pair(current.kernel_size)
        current.padding = _pair(current.padding)
        current.stride = _pair(current.stride)

        width = (input_height - current.kernel_size[0] +
                 2 * current.padding[0]) / current.stride[0] + 1
        height = (input_width - current.kernel_size[1] +
                  2 * current.padding[1]) / current.stride[1] + 1
        shape = (1, prev.shape[1], int(width), int(height))

        layer = PassThroughNodes(shape=shape)
        connection = topology.MaxPool2dConnection(
            source=prev,
            target=layer,
            kernel_size=current.kernel_size,
            stride=current.stride,
            padding=current.padding,
            dilation=current.dilation,
            decay=1,
        )

    elif isinstance(current, Permute):
        layer = PassThroughNodes(shape=[
            prev.shape[current.dims[0]],
            prev.shape[current.dims[1]],
            prev.shape[current.dims[2]],
            prev.shape[current.dims[3]],
        ])

        connection = PermuteConnection(source=prev,
                                       target=layer,
                                       dims=current.dims)

    elif isinstance(current, nn.ConstantPad2d):
        layer = PassThroughNodes(shape=[
            prev.shape[0],
            prev.shape[1],
            current.padding[0] + current.padding[1] + prev.shape[2],
            current.padding[2] + current.padding[3] + prev.shape[3],
        ])

        connection = ConstantPad2dConnection(source=prev,
                                             target=layer,
                                             padding=current.padding)

    else:
        return None, None

    return layer, connection
示例#26
0
 def __init__(self, crop_size):
     self.crop_size = _pair(crop_size)
     if not mmcv.is_tuple_of(self.crop_size, int):
         raise TypeError(f'Crop_size must be int or tuple of int, '
                         f'but got {type(crop_size)}')
示例#27
0
 def __init__(self, kernel_size, stride=None, padding=0):
     super(MaxUnpool2d, self).__init__()
     self.kernel_size = _pair(kernel_size)
     self.stride = _pair(stride or kernel_size)
     self.padding = _pair(padding)
示例#28
0
    def __init__(self,
                 strides,
                 ratios,
                 min_sizes=None,
                 max_sizes=None,
                 basesize_ratio_range=(0.15, 0.9),
                 input_size=300,
                 scale_major=True):
        assert len(strides) == len(ratios)
        assert not (min_sizes is None) ^ (max_sizes is None)
        self.strides = [_pair(stride) for stride in strides]
        self.centers = [(stride[0] / 2., stride[1] / 2.)
                        for stride in self.strides]

        if min_sizes is None and max_sizes is None:
            # use hard code to generate SSD anchors
            self.input_size = input_size
            assert mmcv.is_tuple_of(basesize_ratio_range, float)
            self.basesize_ratio_range = basesize_ratio_range
            # calculate anchor ratios and sizes
            min_ratio, max_ratio = basesize_ratio_range
            min_ratio = int(min_ratio * 100)
            max_ratio = int(max_ratio * 100)
            step = int(np.floor(max_ratio - min_ratio) / (self.num_levels - 2))
            min_sizes = []
            max_sizes = []
            for ratio in range(int(min_ratio), int(max_ratio) + 1, step):
                min_sizes.append(int(self.input_size * ratio / 100))
                max_sizes.append(int(self.input_size * (ratio + step) / 100))
            if self.input_size == 300:
                if basesize_ratio_range[0] == 0.15:  # SSD300 COCO
                    min_sizes.insert(0, int(self.input_size * 7 / 100))
                    max_sizes.insert(0, int(self.input_size * 15 / 100))
                elif basesize_ratio_range[0] == 0.2:  # SSD300 VOC
                    min_sizes.insert(0, int(self.input_size * 10 / 100))
                    max_sizes.insert(0, int(self.input_size * 20 / 100))
                else:
                    raise ValueError(
                        'basesize_ratio_range[0] should be either 0.15'
                        'or 0.2 when input_size is 300, got '
                        f'{basesize_ratio_range[0]}.')
            elif self.input_size == 512:
                if basesize_ratio_range[0] == 0.1:  # SSD512 COCO
                    min_sizes.insert(0, int(self.input_size * 4 / 100))
                    max_sizes.insert(0, int(self.input_size * 10 / 100))
                elif basesize_ratio_range[0] == 0.15:  # SSD512 VOC
                    min_sizes.insert(0, int(self.input_size * 7 / 100))
                    max_sizes.insert(0, int(self.input_size * 15 / 100))
                else:
                    raise ValueError(
                        'When not setting min_sizes and max_sizes,'
                        'basesize_ratio_range[0] should be either 0.1'
                        'or 0.15 when input_size is 512, got'
                        f' {basesize_ratio_range[0]}.')
            else:
                raise ValueError(
                    'Only support 300 or 512 in SSDAnchorGenerator when '
                    'not setting min_sizes and max_sizes, '
                    f'got {self.input_size}.')

        assert len(min_sizes) == len(max_sizes) == len(strides)

        anchor_ratios = []
        anchor_scales = []
        for k in range(len(self.strides)):
            scales = [1., np.sqrt(max_sizes[k] / min_sizes[k])]
            anchor_ratio = [1.]
            for r in ratios[k]:
                anchor_ratio += [1 / r, r]  # 4 or 6 ratio
            anchor_ratios.append(torch.Tensor(anchor_ratio))
            anchor_scales.append(torch.Tensor(scales))

        self.base_sizes = min_sizes
        self.scales = anchor_scales
        self.ratios = anchor_ratios
        self.scale_major = scale_major
        self.center_offset = 0
        self.base_anchors = self.gen_base_anchors()
示例#29
0
 def __init__(self,
              in_channels,
              channels,
              kernel_size,
              stride=(1, 1),
              padding=(0, 0),
              dilation=(1, 1),
              groups=1,
              bias=True,
              radix=2,
              reduction_factor=4,
              rectify=False,
              rectify_avg=False,
              norm_layer=None,
              dropblock_prob=0.0,
              **kwargs):
     super(SplAtConv2d, self).__init__()
     padding = _pair(padding)
     self.rectify = rectify and (padding[0] > 0 or padding[1] > 0)
     self.rectify_avg = rectify_avg
     inter_channels = max(in_channels * radix // reduction_factor, 32)
     self.radix = radix
     self.cardinality = groups
     self.channels = channels
     self.dropblock_prob = dropblock_prob
     if self.rectify:
         from rfconv import RFConv2d
         self.conv = RFConv2d(in_channels,
                              channels * radix,
                              kernel_size,
                              stride,
                              padding,
                              dilation,
                              groups=groups * radix,
                              bias=bias,
                              average_mode=rectify_avg,
                              **kwargs)
     else:
         self.conv = Conv2d(in_channels,
                            channels * radix,
                            kernel_size,
                            stride,
                            padding,
                            dilation,
                            groups=groups * radix,
                            bias=bias,
                            **kwargs)
     self.use_bn = norm_layer is not None
     if self.use_bn:
         self.bn0 = norm_layer(channels * radix)
     self.relu = ReLU(inplace=True)
     self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality)
     if self.use_bn:
         self.bn1 = norm_layer(inter_channels)
     self.fc2 = Conv2d(inter_channels,
                       channels * radix,
                       1,
                       groups=self.cardinality)
     if dropblock_prob > 0.0:
         self.dropblock = DropBlock2D(dropblock_prob, 3)
     self.rsoftmax = rSoftMax(radix, groups)
示例#30
0
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 in_length,
                 out_length,
                 stride=1,
                 padding=0,
                 dilation=1,
                 share_weight=True,
                 routing_type='k_means',
                 num_iterations=3,
                 bias=True,
                 **kwargs):
        super(CapsuleConv2d, self).__init__()
        if in_channels % in_length != 0:
            raise ValueError(
                'Expected in_channels must be divisible by in_length.')
        if out_channels % out_length != 0:
            raise ValueError(
                'Expected out_channels must be divisible by out_length.')
        if num_iterations < 1:
            raise ValueError(
                'num_iterations has to be greater than 0, but got {}.'.format(
                    num_iterations))

        kernel_size = _pair(kernel_size)
        stride = _pair(stride)
        padding = _pair(padding)
        dilation = _pair(dilation)

        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = kernel_size
        self.in_length = in_length
        self.out_length = out_length
        self.stride = stride
        self.padding = padding
        self.dilation = dilation
        self.share_weight = share_weight
        self.routing_type = routing_type
        self.num_iterations = num_iterations
        self.kwargs = kwargs

        if self.share_weight:
            self.weight = Parameter(
                torch.Tensor(out_channels // out_length, out_length, in_length,
                             *kernel_size))
        else:
            self.weight = Parameter(
                torch.Tensor(out_channels // out_length,
                             in_channels // in_length, out_length, in_length,
                             *kernel_size))
        if bias:
            self.bias = Parameter(
                torch.Tensor(out_channels // out_length, out_length))
            nn.init.zeros_(self.bias)
        else:
            self.bias = None

        nn.init.xavier_uniform_(self.weight)
示例#31
0
def conv2d(input,
           weight,
           bias,
           stride=1,
           padding=0,
           dilation=1,
           groups=1,
           padding_mode='zeros',
           scale=1.0,
           zero_point=0,
           dtype=torch.quint8):
    r"""
    Applies a 2D convolution over a quantized 2D input composed of several input
    planes.

    See :class:`~torch.nn.quantized.Conv2d` for details and output shape.

    Args:
        input: quantized input tensor of shape :math:`(\text{minibatch} , \text{in\_channels} , iH , iW)`
        weight: quantized filters of shape :math:`(\text{out\_channels} , \frac{\text{in\_channels}}{\text{groups}} , kH , kW)`
        bias: **non-quantized** bias tensor of shape :math:`(\text{out\_channels})`. The tensor type must be `torch.float`.
        stride: the stride of the convolving kernel. Can be a single number or a
          tuple `(sH, sW)`. Default: 1
        padding: implicit paddings on both sides of the input. Can be a
          single number or a tuple `(padH, padW)`. Default: 0
        dilation: the spacing between kernel elements. Can be a single number or
          a tuple `(dH, dW)`. Default: 1
        groups: split input into groups, :math:`\text{in\_channels}` should be divisible by the
          number of groups. Default: 1
        padding_mode: the padding mode to use. Only "zeros" is supported for quantized convolution at the moment. Default: "zeros"
        scale: quantization scale for the output. Default: 1.0
        zero_point: quantization zero_point for the output. Default: 0
        dtype: quantization data type to use. Default: ``torch.quint8``

    Examples::

        >>> from torch.nn.quantized import functional as qF
        >>> filters = torch.randn(8, 4, 3, 3, dtype=torch.float)
        >>> inputs = torch.randn(1, 4, 5, 5, dtype=torch.float)
        >>> bias = torch.randn(8, dtype=torch.float)
        >>>
        >>> scale, zero_point = 1.0, 0
        >>> dtype_inputs = torch.quint8
        >>> dtype_filters = torch.qint8
        >>>
        >>> q_filters = torch.quantize_per_tensor(filters, scale, zero_point, dtype_filters)
        >>> q_inputs = torch.quantize_per_tensor(inputs, scale, zero_point, dtype_inputs)
        >>> qF.conv2d(q_inputs, q_filters, bias, padding=1, scale=scale, zero_point=zero_point)
    """  # noqa: E501
    if padding_mode != 'zeros':
        raise NotImplementedError("Only zero-padding is supported!")
    if input.dtype != torch.quint8:
        raise NotImplementedError(
            "Only torch.quint8 is supported for activation tensor!")
    if weight.dtype != torch.qint8:
        raise NotImplementedError(
            "Only torch.qint8 is supported for weight tensor!")
    if input.ndim != 4:
        raise ValueError("Input shape must be `(N, C, H, W)`!")
    stride = _pair(stride)
    padding = _pair(padding)
    dilation = _pair(dilation)

    packed_params = torch.ops.quantized.conv2d_prepack(weight, bias, stride,
                                                       padding, dilation,
                                                       groups)
    return torch.ops.quantized.conv2d(input, packed_params, scale, zero_point)
示例#32
0
    def __init__(self, output_size, spatial_scale=1.0):
        super(RoIPool, self).__init__()

        self.output_size = _pair(output_size)
        self.spatial_scale = float(spatial_scale)
示例#33
0
文件: __init__.py 项目: ptillet/isaac
 def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, upsample=1, groups=1, bias=True, activation = 'linear', alpha = 0., residual = None):
     super(Conv2d, self).__init__(4, in_channels, out_channels, _pair(kernel_size), _pair(stride), _pair(padding), _pair(dilation), _pair(upsample), groups, bias, activation, alpha, residual)
示例#34
0
    def forward(ctx, input1, input2, kernel_size, stride, padding,
                dilation) -> torch.Tensor:
        """

:param ctx:
:type ctx:
:param input1:
:type input1:
:param input2:
:type input2:
:param kernel_size:
:type kernel_size:
:param stride:
:type stride:
:param padding:
:type padding:
:param dilation:
:type dilation:
:return:
:rtype:
"""
        kernel_size, stride, padding, dilation = (
            _pair(kernel_size),
            _pair(stride),
            _pair(padding),
            _pair(dilation),
        )
        ctx.kernel_size, ctx.stride, ctx.padding, ctx.dilation = (
            kernel_size,
            stride,
            padding,
            dilation,
        )
        assert input1.dim() == 4 and input1.is_cuda
        batch_size, input_channels, input_height, input_width = input1.size()
        output_height = int((input_height + 2 * padding[0] -
                             (dilation[0] *
                              (kernel_size[0] - 1) + 1)) / stride[0] + 1)
        output_width = int((input_width + 2 * padding[1] -
                            (dilation[1] *
                             (kernel_size[1] - 1) + 1)) / stride[1] + 1)
        output = input1.new(
            batch_size,
            input_channels,
            kernel_size[0] * kernel_size[1],
            output_height * output_width,
        )
        n = output.numel() // output.shape[2]
        with torch.cuda.device_of(input1):
            f = load_kernel(
                "subtraction2_refpad_forward_kernel",
                _subtraction2_refpad_forward_kernel,
                Dtype=get_dtype_str(input1),
                nthreads=n,
                num=batch_size,
                input_channels=input_channels,
                bottom_height=input_height,
                bottom_width=input_width,
                top_height=output_height,
                top_width=output_width,
                kernel_h=kernel_size[0],
                kernel_w=kernel_size[1],
                stride_h=stride[0],
                stride_w=stride[1],
                dilation_h=dilation[0],
                dilation_w=dilation[1],
                pad_h=padding[0],
                pad_w=padding[1],
            )
            f(
                block=(CUDA_NUM_THREADS, 1, 1),
                grid=(GET_BLOCKS(n), 1, 1),
                args=[input1.data_ptr(),
                      input2.data_ptr(),
                      output.data_ptr()],
                stream=Stream(ptr=torch.cuda.current_stream().cuda_stream),
            )
        ctx.save_for_backward(input1, input2)
        return output
示例#35
0
    def forward(ctx, input, weight, kernel_size, stride, padding, dilation):
        """

        :param ctx:
        :type ctx:
        :param input:
        :type input:
        :param weight:
        :type weight:
        :param kernel_size:
        :type kernel_size:
        :param stride:
        :type stride:
        :param padding:
        :type padding:
        :param dilation:
        :type dilation:
        :return:
        :rtype:"""
        kernel_size, stride, padding, dilation = (
            _pair(kernel_size),
            _pair(stride),
            _pair(padding),
            _pair(dilation),
        )
        ctx.kernel_size, ctx.stride, ctx.padding, ctx.dilation = (
            kernel_size,
            stride,
            padding,
            dilation,
        )
        assert input.dim() == 4 and input.is_cuda and weight.is_cuda
        batch_size, input_channels, input_height, input_width = input.size()
        _, weight_channels, weight_height, weight_width = weight.size()
        output_height = int((input_height + 2 * padding[0] -
                             (dilation[0] *
                              (kernel_size[0] - 1) + 1)) / stride[0] + 1)
        output_width = int((input_width + 2 * padding[1] -
                            (dilation[1] *
                             (kernel_size[1] - 1) + 1)) / stride[1] + 1)
        assert output_height * output_width == weight_width
        output = input.new(batch_size, input_channels, output_height,
                           output_width)
        n = output.numel()
        with torch.cuda.device_of(input):
            f = load_kernel(
                "aggregation_refpad_forward_kernel",
                _aggregation_refpad_forward_kernel,
                Dtype=get_dtype_str(input),
                nthreads=n,
                num=batch_size,
                input_channels=input_channels,
                weight_channels=weight_channels,
                bottom_height=input_height,
                bottom_width=input_width,
                top_height=output_height,
                top_width=output_width,
                kernel_h=kernel_size[0],
                kernel_w=kernel_size[1],
                stride_h=stride[0],
                stride_w=stride[1],
                dilation_h=dilation[0],
                dilation_w=dilation[1],
                pad_h=padding[0],
                pad_w=padding[1],
            )
            f(
                block=(CUDA_NUM_THREADS, 1, 1),
                grid=(get_blocks_(n), 1, 1),
                args=[input.data_ptr(),
                      weight.data_ptr(),
                      output.data_ptr()],
                stream=Stream(ptr=torch.cuda.current_stream().cuda_stream),
            )
        ctx.save_for_backward(input, weight)
        return output
示例#36
0
    def __init__(
        self,
        source: Nodes,
        target: Nodes,
        kernel_size: Union[int, Tuple[int, int]],
        stride: Union[int, Tuple[int, int]] = 1,
        padding: Union[int, Tuple[int, int]] = 0,
        dilation: Union[int, Tuple[int, int]] = 1,
        nu: Optional[Union[float, Sequence[float]]] = None,
        reduction: Optional[callable] = None,
        weight_decay: float = 0.0,
        **kwargs
    ) -> None:
        # language=rst
        """
        Instantiates a ``Conv2dConnection`` object.

        :param source: A layer of nodes from which the connection originates.
        :param target: A layer of nodes to which the connection connects.
        :param kernel_size: Horizontal and vertical size of convolutional kernels.
        :param stride: Horizontal and vertical stride for convolution.
        :param padding: Horizontal and vertical padding for convolution.
        :param dilation: Horizontal and vertical dilation for convolution.
        :param nu: Learning rate for both pre- and post-synaptic events.
        :param reduction: Method for reducing parameter updates along the minibatch
            dimension.
        :param weight_decay: Constant multiple to decay weights by on each iteration.

        Keyword arguments:

        :param LearningRule update_rule: Modifies connection parameters according to
            some rule.
        :param torch.Tensor w: Strengths of synapses.
        :param torch.Tensor b: Target population bias.
        :param float wmin: Minimum allowed value on the connection weights.
        :param float wmax: Maximum allowed value on the connection weights.
        :param float norm: Total weight per target neuron normalization constant.
        """
        super().__init__(source, target, nu, reduction, weight_decay, **kwargs)

        self.kernel_size = _pair(kernel_size)
        self.stride = _pair(stride)
        self.padding = _pair(padding)
        self.dilation = _pair(dilation)

        self.in_channels, input_height, input_width = (
            source.shape[0],
            source.shape[1],
            source.shape[2],
        )
        self.out_channels, output_height, output_width = (
            target.shape[0],
            target.shape[1],
            target.shape[2],
        )

        width = (
            input_height - self.kernel_size[0] + 2 * self.padding[0]
        ) / self.stride[0] + 1
        height = (
            input_width - self.kernel_size[1] + 2 * self.padding[1]
        ) / self.stride[1] + 1
        shape = (self.in_channels, self.out_channels, int(width), int(height))

        error = (
            "Target dimensionality must be (out_channels, ?,"
            "(input_height - filter_height + 2 * padding_height) / stride_height + 1,"
            "(input_width - filter_width + 2 * padding_width) / stride_width + 1"
        )
        
        assert (
            target.shape[0] == shape[1]
            and target.shape[1] == shape[2]
            and target.shape[2] == shape[3]
        ), error

        w = kwargs.get("w", None)
        if w is None:
            if self.wmin == -np.inf or self.wmax == np.inf:
                w = torch.clamp(
                    torch.rand(self.out_channels, self.in_channels, *self.kernel_size),
                    self.wmin,
                    self.wmax,
                )
            else:
                w = (self.wmax - self.wmin) * torch.rand(
                    self.out_channels, self.in_channels, *self.kernel_size
                )
                w += self.wmin
        else:
            if self.wmin != -np.inf or self.wmax != np.inf:
                w = torch.clamp(w, self.wmin, self.wmax)

        self.w = Parameter(w, requires_grad=False)
        self.b = Parameter(
            kwargs.get("b", torch.zeros(self.out_channels)), requires_grad=False
        )
示例#37
0
    def __init__(self,
                 source: Nodes,
                 target: Nodes,
                 kernel_size: Union[int, Tuple[int, int]],
                 stride: Union[int, Tuple[int, int]] = 1,
                 padding: Union[int, Tuple[int, int]] = 0,
                 dilation: Union[int, Tuple[int, int]] = 1,
                 nu: Optional[Union[float, Tuple[float, float]]] = None,
                 **kwargs) -> None:
        # language=rst
        """
        Instantiates a ``Conv2dConnection`` object.

        :param source: A layer of nodes from which the connection originates.
        :param target: A layer of nodes to which the connection connects.
        :param kernel_size: Horizontal and vertical size of convolutional kernels.
        :param stride: Horizontal and vertical stride for convolution.
        :param padding: Horizontal and vertical padding for convolution.
        :param dilation: Horizontal and vertical dilation for convolution.
        :param nu: Learning rate for both pre- and post-synaptic events.
        :param nu_pre: Learning rate for pre-synaptic events.
        :param nu_post: Learning rate for post-synpatic events.

        Keyword arguments:

        :param function update_rule: Modifies connection parameters according to some rule.
        :param torch.Tensor w: Strengths of synapses.
        :param float wmin: Minimum allowed value on the connection weights.
        :param float wmax: Maximum allowed value on the connection weights.
        :param float norm: Total weight per target neuron normalization constant.
        """
        super().__init__(source, target, nu, **kwargs)

        self.kernel_size = _pair(kernel_size)
        self.stride = _pair(stride)
        self.padding = _pair(padding)
        self.dilation = _pair(dilation)

        assert source.shape[0] == target.shape[
            0], 'Minibatch size not equal across source and target'

        minibatch = source.shape[0]
        self.in_channels, input_height, input_width = source.shape[
            1], source.shape[2], source.shape[3]
        self.out_channels, output_height, output_width = target.shape[
            1], target.shape[2], target.shape[3]

        error = 'Target dimensionality must be (minibatch, out_channels, \
                        (input_height - filter_height + 2 * padding_height) / stride_height + 1, \
                        (input_width - filter_width + 2 * padding_width) / stride_width + 1'

        width = (input_height - self.kernel_size[0] +
                 2 * self.padding[0]) / self.stride[0] + 1
        height = (input_width - self.kernel_size[1] +
                  2 * self.padding[1]) / self.stride[1] + 1
        shape = (minibatch, self.out_channels, width, height)

        assert tuple(target.shape) == shape, error

        self.w = kwargs.get(
            'w',
            torch.rand(self.out_channels, self.in_channels, *self.kernel_size))
        self.w = torch.clamp(self.w, self.wmin, self.wmax)
示例#38
0
    def __init__(
        self,
        source: Nodes,
        target: Nodes,
        kernel_size: Union[int, Tuple[int, int]],
        stride: Union[int, Tuple[int, int]],
        n_filters: int,
        nu: Optional[Union[float, Sequence[float]]] = None,
        reduction: Optional[callable] = None,
        weight_decay: float = 0.0,
        **kwargs
    ) -> None:
        # language=rst
        """
        Instantiates a ``LocalConnection`` object. Source population should be
        two-dimensional.

        Neurons in the post-synaptic population are ordered by receptive field; that is,
        if there are ``n_conv`` neurons in each post-synaptic patch, then the first
        ``n_conv`` neurons in the post-synaptic population correspond to the first
        receptive field, the second ``n_conv`` to the second receptive field, and so on.

        :param source: A layer of nodes from which the connection originates.
        :param target: A layer of nodes to which the connection connects.
        :param kernel_size: Horizontal and vertical size of convolutional kernels.
        :param stride: Horizontal and vertical stride for convolution.
        :param n_filters: Number of locally connected filters per pre-synaptic region.
        :param nu: Learning rate for both pre- and post-synaptic events.
        :param reduction: Method for reducing parameter updates along the minibatch
            dimension.
        :param weight_decay: Constant multiple to decay weights by on each iteration.

        Keyword arguments:

        :param LearningRule update_rule: Modifies connection parameters according to
            some rule.
        :param torch.Tensor w: Strengths of synapses.
        :param torch.Tensor b: Target population bias.
        :param float wmin: Minimum allowed value on the connection weights.
        :param float wmax: Maximum allowed value on the connection weights.
        :param float norm: Total weight per target neuron normalization constant.
        :param Tuple[int, int] input_shape: Shape of input population if it's not
            ``[sqrt, sqrt]``.
        """
        super().__init__(source, target, nu, reduction, weight_decay, **kwargs)

        kernel_size = _pair(kernel_size)
        stride = _pair(stride)

        self.kernel_size = kernel_size
        self.stride = stride
        self.n_filters = n_filters

        shape = kwargs.get("input_shape", None)
        if shape is None:
            sqrt = int(np.sqrt(source.n))
            shape = _pair(sqrt)

        if kernel_size == shape:
            conv_size = [1, 1]
        else:
            conv_size = (
                int((shape[0] - kernel_size[0]) / stride[0]) + 1,
                int((shape[1] - kernel_size[1]) / stride[1]) + 1,
            )

        self.conv_size = conv_size

        conv_prod = int(np.prod(conv_size))
        kernel_prod = int(np.prod(kernel_size))

        assert (
            target.n == n_filters * conv_prod
        ), "Target layer size must be n_filters * (kernel_size ** 2)."

        locations = torch.zeros(
            kernel_size[0], kernel_size[1], conv_size[0], conv_size[1]
        ).long()
        for c1 in range(conv_size[0]):
            for c2 in range(conv_size[1]):
                for k1 in range(kernel_size[0]):
                    for k2 in range(kernel_size[1]):
                        location = (
                            c1 * stride[0] * shape[1]
                            + c2 * stride[1]
                            + k1 * shape[0]
                            + k2
                        )
                        locations[k1, k2, c1, c2] = location

        self.register_buffer("locations", locations.view(kernel_prod, conv_prod))
        w = kwargs.get("w", None)

        if w is None:
            w = torch.zeros(source.n, target.n)
            for f in range(n_filters):
                for c in range(conv_prod):
                    for k in range(kernel_prod):
                        if self.wmin == -np.inf or self.wmax == np.inf:
                            w[self.locations[k, c], f * conv_prod + c] = np.clip(
                                np.random.rand(), self.wmin, self.wmax
                            )
                        else:
                            w[
                                self.locations[k, c], f * conv_prod + c
                            ] = self.wmin + np.random.rand() * (self.wmax - self.wmin)
        else:
            if self.wmin != -np.inf or self.wmax != np.inf:
                w = torch.clamp(w, self.wmin, self.wmax)

        self.w = Parameter(w, requires_grad=False)

        self.register_buffer("mask", self.w == 0)

        self.b = Parameter(kwargs.get("b", torch.zeros(target.n)), requires_grad=False)

        if self.norm is not None:
            self.norm *= kernel_prod
示例#39
0
    def __init__(self,
                 source: Nodes,
                 target: Nodes,
                 kernel_size: Union[int, Tuple[int, int]],
                 stride: Union[int, Tuple[int, int]],
                 n_filters: int,
                 nu: Optional[Union[float, Tuple[float, float]]] = None,
                 weight_decay: float = 0.0,
                 **kwargs) -> None:
        # language=rst
        """
        Instantiates a ``LocallyConnectedConnection`` object. Source population should be two-dimensional.

        :param source: A layer of nodes from which the connection originates.
        :param target: A layer of nodes to which the connection connects.
        :param kernel_size: Horizontal and vertical size of convolutional kernels.
        :param stride: Horizontal and vertical stride for convolution.
        :param n_filters: Number of locally connected filters per pre-synaptic region.
        :param nu: Learning rate for both pre- and post-synaptic events.
        :param weight_decay: Constant multiple to decay weights by on each iteration.

        Keyword arguments:

        :param function update_rule: Modifies connection parameters according to some rule.
        :param torch.Tensor w: Strengths of synapses.
        :param float wmin: Minimum allowed value on the connection weights.
        :param float wmax: Maximum allowed value on the connection weights.
        :param float norm: Total weight per target neuron normalization constant.
        :param Tuple[int, int] input_shape: Shape of input population if it's not ``[sqrt, sqrt]``.
        """
        super().__init__(source, target, nu, weight_decay, **kwargs)

        kernel_size = _pair(kernel_size)
        stride = _pair(stride)

        self.kernel_size = kernel_size
        self.stride = stride
        self.n_filters = n_filters

        shape = kwargs.get('input_shape', None)
        if shape is None:
            sqrt = int(np.sqrt(source.n))
            shape = _pair(sqrt)

        if kernel_size == shape:
            conv_size = [1, 1]
        else:
            conv_size = (int((shape[0] - kernel_size[0]) / stride[0]) + 1,
                         int((shape[1] - kernel_size[1]) / stride[1]) + 1)

        self.conv_size = conv_size

        conv_prod = int(np.prod(conv_size))
        kernel_prod = int(np.prod(kernel_size))

        assert target.n == n_filters * conv_prod, 'Target layer size must be n_filters * (kernel_size ** 2).'

        locations = torch.zeros(kernel_size[0], kernel_size[1], conv_size[0],
                                conv_size[1]).long()
        for c1 in range(conv_size[0]):
            for c2 in range(conv_size[1]):
                for k1 in range(kernel_size[0]):
                    for k2 in range(kernel_size[1]):
                        location = c1 * stride[0] * shape[1] + c2 * stride[
                            1] + k1 * shape[0] + k2
                        locations[k1, k2, c1, c2] = location

        self.locations = locations.view(kernel_prod, conv_prod)
        self.w = kwargs.get('w', None)

        if self.w is None:
            self.w = torch.zeros(source.n, target.n)
            for f in range(n_filters):
                for c in range(conv_prod):
                    for k in range(kernel_prod):
                        if self.wmin == -np.inf or self.wmax == np.inf:
                            self.w[self.locations[k, c],
                                   f * conv_prod + c] = np.random.rand()
                        else:
                            self.w[self.locations[k, c], f * conv_prod + c] = \
                                self.wmin + np.random.rand() * (self.wmax - self.wmin)
        else:
            if self.wmin is not None and self.wmax is not None:
                self.w = torch.clamp(self.w, self.wmin, self.wmax)

        self.mask = self.w == 0

        if self.norm is not None:
            self.norm *= kernel_prod
示例#40
0
    def __init__(self, n_inpt: int, input_shape: List[int], kernel_size: Union[int, Tuple[int, int]],
                 stride: Union[int, Tuple[int, int]], n_filters: int, inh: float = 25.0, dt: float = 1.0,
                 nu_pre: float = 1e-4, nu_post: float = 1e-2, theta_plus: float = 0.05, theta_decay: float = 1e-7,
                 wmin: float = 0.0, wmax: float = 1.0, norm: float = 0.2) -> None:
        # language=rst
        """
        Constructor for class ``LocallyConnectedNetwork``. Uses ``DiehlAndCookNodes`` to avoid multiple spikes per
        timestep in the output layer population.

        :param n_inpt: Number of input neurons. Matches the 1D size of the input data.
        :param input_shape: Two-dimensional shape of input population.
        :param kernel_size: Size of input windows. Integer or two-tuple of integers.
        :param stride: Length of horizontal, vertical stride across input space. Integer or two-tuple of integers.
        :param n_filters: Number of locally connected filters per input region. Integer or two-tuple of integers.
        :param inh: Strength of synapse weights from output layer back onto itself.
        :param dt: Simulation time step.
        :param nu_pre: Pre-synaptic learning rate.
        :param nu_post: Post-synaptic learning rate.
        :param wmin: Minimum allowed weight on ``Input`` to ``DiehlAndCookNodes`` synapses.
        :param wmax: Maximum allowed weight on ``Input`` to ``DiehlAndCookNodes`` synapses.
        :param theta_plus: On-spike increment of ``DiehlAndCookNodes`` membrane threshold potential.
        :param theta_decay: Time constant of ``DiehlAndCookNodes`` threshold potential decay.
        :param norm: ``Input`` to ``DiehlAndCookNodes`` layer connection weights normalization constant.
        """
        super().__init__(dt=dt)

        kernel_size = _pair(kernel_size)
        stride = _pair(stride)

        self.n_inpt = n_inpt
        self.input_shape = input_shape
        self.kernel_size = kernel_size
        self.stride = stride
        self.n_filters = n_filters
        self.inh = inh
        self.dt = dt
        self.theta_plus = theta_plus
        self.theta_decay = theta_decay
        self.wmin = wmin
        self.wmax = wmax
        self.norm = norm

        if kernel_size == input_shape:
            conv_size = [1, 1]
        else:
            conv_size = (int((input_shape[0] - kernel_size[0]) / stride[0]) + 1,
                         int((input_shape[1] - kernel_size[1]) / stride[1]) + 1)

        input_layer = Input(n=self.n_inpt, traces=True, trace_tc=5e-2)
        output_layer = DiehlAndCookNodes(
            n=self.n_filters * conv_size[0] * conv_size[1], traces=True, rest=-65.0, reset=-60.0,
            thresh=-52.0, refrac=5, decay=1e-2, trace_tc=5e-2, theta_plus=theta_plus, theta_decay=theta_decay
        )
        input_output_conn = LocallyConnectedConnection(
            input_layer, output_layer, kernel_size=kernel_size, stride=stride, n_filters=n_filters,
            nu=(nu_pre, nu_post), update_rule=PostPre, wmin=wmin, wmax=wmax, norm=norm, input_shape=input_shape
        )

        w = torch.zeros(n_filters, *conv_size, n_filters, *conv_size)
        for fltr1 in range(n_filters):
            for fltr2 in range(n_filters):
                if fltr1 != fltr2:
                    for i in range(conv_size[0]):
                        for j in range(conv_size[1]):
                            w[fltr1, i, j, fltr2, i, j] = -inh

        recurrent_conn = Connection(output_layer, output_layer, w=w)

        self.add_layer(input_layer, name='X')
        self.add_layer(output_layer, name='Y')
        self.add_connection(input_output_conn, source='X', target='Y')
        self.add_connection(recurrent_conn, source='Y', target='Y')
示例#41
0
    def forward(
        ctx,
        input,
        offset,
        weight,
        stride=1,
        padding=0,
        dilation=1,
        groups=1,
        deformable_groups=1,
        im2col_step=64,
    ):
        if input is not None and input.dim() != 4:
            raise ValueError(
                "Expected 4D tensor as input, got {}D tensor instead.".format(
                    input.dim()))
        ctx.stride = _pair(stride)
        ctx.padding = _pair(padding)
        ctx.dilation = _pair(dilation)
        ctx.groups = groups
        ctx.deformable_groups = deformable_groups
        ctx.im2col_step = im2col_step

        ctx.save_for_backward(input, offset, weight)

        output = input.new_empty(
            _DeformConv._output_size(input, weight, ctx.padding, ctx.dilation,
                                     ctx.stride))

        ctx.bufs_ = [input.new_empty(0), input.new_empty(0)]  # columns, ones

        if not input.is_cuda:
            if deformable_groups != 1:
                raise NotImplementedError(
                    "Deformable Conv with deformable_groups != 1 is not supported on CPUs!"
                )
            return deform_conv2d(input,
                                 offset,
                                 weight,
                                 stride=stride,
                                 padding=padding,
                                 dilation=dilation)
        else:
            cur_im2col_step = _DeformConv._cal_im2col_step(
                input.shape[0], ctx.im2col_step)
            assert (input.shape[0] %
                    cur_im2col_step) == 0, "im2col step must divide batchsize"

            _C.deform_conv_forward(
                input,
                weight,
                offset,
                output,
                ctx.bufs_[0],
                ctx.bufs_[1],
                weight.size(3),
                weight.size(2),
                ctx.stride[1],
                ctx.stride[0],
                ctx.padding[1],
                ctx.padding[0],
                ctx.dilation[1],
                ctx.dilation[0],
                ctx.groups,
                ctx.deformable_groups,
                cur_im2col_step,
            )
        return output
 def __init__(self, kernel_size=2, stride=2, padding=0, same=False):
     super(QuaternionMaxAmpPool2d, self).__init__()
     self.k = _pair(kernel_size)
     self.stride = _pair(stride)
     self.padding = _quadruple(padding)  # convert to l, r, t, b
     self.same = same
def adaptive_avg_pool2d(input, output_size: Vector):
    return torch.ops.torch_ipex.adaptive_avg_pool2d(input, _pair(output_size))
示例#44
0
    def forward(ctx,
                input,
                offset,
                weight,
                stride=1,
                padding=0,
                dilation=1,
                groups=1,
                deform_groups=1,
                bias=False,
                im2col_step=32):
        if input is not None and input.dim() != 4:
            raise ValueError(
                f'Expected 4D tensor as input, got {input.dim()}D tensor \
                  instead.')
        assert bias is False, 'Only support bias is False.'
        ctx.stride = _pair(stride)
        ctx.padding = _pair(padding)
        ctx.dilation = _pair(dilation)
        ctx.groups = groups
        ctx.deform_groups = deform_groups
        ctx.im2col_step = im2col_step

        # When pytorch version >= 1.6.0, amp is adopted for fp16 mode;
        # amp won't cast the type of model (float32), but "offset" is cast
        # to float16 by nn.Conv2d automatically, leading to the type
        # mismatch with input (when it is float32) or weight.
        # The flag for whether to use fp16 or amp is the type of "offset",
        # we cast weight and input to temporarily support fp16 and amp
        # whatever the pytorch version is.
        input = input.type_as(offset)
        weight = weight.type_as(input)
        ctx.save_for_backward(input, offset, weight)

        output = input.new_empty(
            DeformConv2dFunction._output_size(ctx, input, weight))

        ctx.bufs_ = [input.new_empty(0), input.new_empty(0)]  # columns, ones

        cur_im2col_step = min(ctx.im2col_step, input.size(0))
        assert (input.size(0) % cur_im2col_step
                ) == 0, 'batch size must be divisible by im2col_step'
        ext_module.deform_conv_forward(
            input,
            weight,
            offset,
            output,
            ctx.bufs_[0],
            ctx.bufs_[1],
            kW=weight.size(3),
            kH=weight.size(2),
            dW=ctx.stride[1],
            dH=ctx.stride[0],
            padW=ctx.padding[1],
            padH=ctx.padding[0],
            dilationW=ctx.dilation[1],
            dilationH=ctx.dilation[0],
            group=ctx.groups,
            deformable_group=ctx.deform_groups,
            im2col_step=cur_im2col_step)
        return output