Python torch.nn.functional.conv2d() Examples

The following are 30 code examples of torch.nn.functional.conv2d(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch.nn.functional , or try the search function .
Example #1
Source File: __init__.py    From iSeeBetter with MIT License 6 votes vote down vote up
def _ssim(img1, img2, window, window_size, channel, size_average=True):
    mu1 = F.conv2d(img1, window, padding=window_size // 2, groups=channel)
    mu2 = F.conv2d(img2, window, padding=window_size // 2, groups=channel)

    mu1_sq = mu1.pow(2)
    mu2_sq = mu2.pow(2)
    mu1_mu2 = mu1 * mu2

    sigma1_sq = F.conv2d(img1 * img1, window, padding=window_size // 2, groups=channel) - mu1_sq
    sigma2_sq = F.conv2d(img2 * img2, window, padding=window_size // 2, groups=channel) - mu2_sq
    sigma12 = F.conv2d(img1 * img2, window, padding=window_size // 2, groups=channel) - mu1_mu2

    C1 = 0.01 ** 2
    C2 = 0.03 ** 2

    ssim_map = ((2 * mu1_mu2 + C1) * (2 * sigma12 + C2)) / ((mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2))

    if size_average:
        return ssim_map.mean()
    else:
        return ssim_map.mean(1).mean(1).mean(1) 
Example #2
Source File: layers.py    From variance-networks with Apache License 2.0 6 votes vote down vote up
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
                 padding=0, dilation=1, bias=True):
        super(ConvVarianceBe, self).__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = (kernel_size, kernel_size)
        self.stride = stride
        self.padding = padding
        self.dilation = dilation
        self.groups = 1
        self.probs = torch.ones([out_channels, in_channels, *self.kernel_size]).cuda() * 0.5
        self.W = Parameter(torch.Tensor(out_channels, in_channels, *self.kernel_size))
        if bias:
            self.bias = Parameter(torch.Tensor(1, out_channels, 1, 1))
        else:
            self.register_parameter('bias', None)
        self.op_bias = lambda input, kernel: F.conv2d(input, kernel, self.bias, self.stride, self.padding, self.dilation, self.groups)
        self.op_nobias = lambda input, kernel: F.conv2d(input, kernel, None, self.stride, self.padding, self.dilation, self.groups)
        self.reset_parameters() 
Example #3
Source File: layers.py    From variance-networks with Apache License 2.0 6 votes vote down vote up
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
                 padding=0, dilation=1, bias=True):
        super(ConvVarianceUnif, self).__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = (kernel_size, kernel_size)
        self.stride = stride
        self.padding = padding
        self.dilation = dilation
        self.groups = 1
        self.W = Parameter(torch.Tensor(out_channels, in_channels, *self.kernel_size))
        if bias:
            self.bias = Parameter(torch.Tensor(1, out_channels, 1, 1))
        else:
            self.register_parameter('bias', None)
        self.op_bias = lambda input, kernel: F.conv2d(input, kernel, self.bias, self.stride, self.padding, self.dilation, self.groups)
        self.op_nobias = lambda input, kernel: F.conv2d(input, kernel, None, self.stride, self.padding, self.dilation, self.groups)
        self.reset_parameters() 
Example #4
Source File: layers.py    From variance-networks with Apache License 2.0 6 votes vote down vote up
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
                 padding=0, dilation=1, bias=True):
        super(ConvVariance, self).__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.kernel_size = (kernel_size, kernel_size)
        self.stride = stride
        self.padding = padding
        self.dilation = dilation
        self.groups = 1
        self.sigma = Parameter(torch.Tensor(
            out_channels, in_channels, *self.kernel_size))
        if bias:
            self.bias = Parameter(torch.Tensor(1, out_channels, 1, 1))
        else:
            self.register_parameter('bias', None)
        self.op_bias = lambda input, kernel: F.conv2d(input, kernel, self.bias, self.stride, self.padding, self.dilation, self.groups)
        self.op_nobias = lambda input, kernel: F.conv2d(input, kernel, None, self.stride, self.padding, self.dilation, self.groups)
        self.reset_parameters()
        self.zero_mean = False
        self.permute_sigma = False 
Example #5
Source File: SSIM.py    From DSMnet with Apache License 2.0 6 votes vote down vote up
def _ssim1(img1, img2, window, window_size, channel):
    mu1 = F.conv2d(img1, window, padding = window_size//2, groups = channel)
    mu2 = F.conv2d(img2, window, padding = window_size//2, groups = channel)

    mu1_sq = mu1.pow(2)
    mu2_sq = mu2.pow(2)
    mu1_mu2 = mu1*mu2

    sigma1_sq = F.conv2d(img1*img1, window, padding = window_size//2, groups = channel) - mu1_sq
    sigma2_sq = F.conv2d(img2*img2, window, padding = window_size//2, groups = channel) - mu2_sq
    sigma12 = F.conv2d(img1*img2, window, padding = window_size//2, groups = channel) - mu1_mu2

    scale = 1
    C1 = (scale * 0.01)**2
    C2 = (scale * 0.03)**2

    ssim_map = ((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*(sigma1_sq + sigma2_sq + C2))
    return ssim_map 
Example #6
Source File: SSIM.py    From DSMnet with Apache License 2.0 6 votes vote down vote up
def _ssim0(img1, img2, window, window_size, channel):
    mu1 = F.conv2d(img1, window, padding = window_size//2, groups = channel)
    mu2 = F.conv2d(img2, window, padding = window_size//2, groups = channel)

    mu1_sq = mu1.pow(2)
    mu2_sq = mu2.pow(2)
    mu1_mu2 = mu1*mu2

    dimg1 = (img1 - mu1)
    dimg2 = (img2 - mu2)
    sigma1_sq = F.conv2d(dimg1*dimg1, window, padding = window_size//2, groups = channel)
    sigma2_sq = F.conv2d(dimg2*dimg2, window, padding = window_size//2, groups = channel)
    sigma12 = F.conv2d(dimg1*dimg2, window, padding = window_size//2, groups = channel)

    scale = 1
    C1 = (scale * 0.01)**2
    C2 = (scale * 0.03)**2

    ssim_map = ((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*(sigma1_sq + sigma2_sq + C2))
    return ssim_map 
Example #7
Source File: SSIM0.py    From DSMnet with Apache License 2.0 6 votes vote down vote up
def _ssim1(img1, img2, window, window_size, channel):
    mu1 = F.conv2d(img1, window, padding = window_size//2, groups = channel)
    mu2 = F.conv2d(img2, window, padding = window_size//2, groups = channel)

    mu1_sq = mu1.pow(2)
    mu2_sq = mu2.pow(2)
    mu1_mu2 = mu1*mu2

    sigma1_sq = F.conv2d(img1*img1, window, padding = window_size//2, groups = channel) - mu1_sq
    sigma2_sq = F.conv2d(img2*img2, window, padding = window_size//2, groups = channel) - mu2_sq
    sigma12 = F.conv2d(img1*img2, window, padding = window_size//2, groups = channel) - mu1_mu2

    scale = 1
    C1 = (scale * 0.01)**2
    C2 = (scale * 0.03)**2

    ssim_map = ((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*(sigma1_sq + sigma2_sq + C2))
    return ssim_map 
Example #8
Source File: SSIM0.py    From DSMnet with Apache License 2.0 6 votes vote down vote up
def _ssim0(img1, img2, window, window_size, channel):
    mu1 = F.conv2d(img1, window, padding = window_size//2, groups = channel)
    mu2 = F.conv2d(img2, window, padding = window_size//2, groups = channel)

    mu1_sq = mu1.pow(2)
    mu2_sq = mu2.pow(2)
    mu1_mu2 = mu1*mu2

    dimg1 = (img1 - mu1)
    dimg2 = (img2 - mu2)
    sigma1_sq = F.conv2d(dimg1*dimg1, window, padding = window_size//2, groups = channel)
    sigma2_sq = F.conv2d(dimg2*dimg2, window, padding = window_size//2, groups = channel)
    sigma12 = F.conv2d(dimg1*dimg2, window, padding = window_size//2, groups = channel)

    scale = 1
    C1 = (scale * 0.01)**2
    C2 = (scale * 0.03)**2

    ssim_map = ((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*(sigma1_sq + sigma2_sq + C2))
    return ssim_map 
Example #9
Source File: loss_ssim.py    From KAIR with MIT License 6 votes vote down vote up
def _ssim(img1, img2, window, window_size, channel, size_average=True):
    mu1 = F.conv2d(img1, window, padding=window_size//2, groups=channel)
    mu2 = F.conv2d(img2, window, padding=window_size//2, groups=channel)

    mu1_sq = mu1.pow(2)
    mu2_sq = mu2.pow(2)
    mu1_mu2 = mu1*mu2

    sigma1_sq = F.conv2d(img1*img1, window, padding=window_size//2, groups=channel) - mu1_sq
    sigma2_sq = F.conv2d(img2*img2, window, padding=window_size//2, groups=channel) - mu2_sq
    sigma12 = F.conv2d(img1*img2, window, padding=window_size//2, groups=channel) - mu1_mu2

    C1 = 0.01**2
    C2 = 0.03**2

    ssim_map = ((2*mu1_mu2 + C1)*(2*sigma12 + C2))/((mu1_sq + mu2_sq + C1)*(sigma1_sq + sigma2_sq + C2))
    if size_average:
        return ssim_map.mean()
    else:
        return ssim_map.mean(1).mean(1).mean(1) 
Example #10
Source File: vcnn.py    From Versatile-Filters with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def forward(self, x):
    x_list = []
    s_num = self.s_num
    ch_ratio = (1+self.delta/self.g)
    ch_len = self.in_channels - self.delta
    for s in range(s_num):
        for start in range(0, self.delta+1, self.g):
            weight1 = self.weight[:, :ch_len, s:self.kernel_size[0]-s, s:self.kernel_size[0]-s]
            if self.padding[0]-s < 0:
                h = x.size(2)
                x1 = x[:,start:start+ch_len,s:h-s,s:h-s]
                padding1 = _pair(0)
            else:
                x1 = x[:,start:start+ch_len,:,:]
                padding1 = _pair(self.padding[0]-s)
            x_list.append(F.conv2d(x1, weight1, self.bias[int(self.out_channels*(s*ch_ratio+start)/s_num/ch_ratio):int(self.out_channels*(s*ch_ratio+start+1)/s_num/ch_ratio)], self.stride,
                      padding1, self.dilation, self.groups))
    x = torch.cat(x_list, 1)
    return x 
Example #11
Source File: image_gradient.py    From pde-surrogate with MIT License 6 votes vote down vote up
def grad_v(self, image, filter_size=3):
        image_height = image.shape[-2]
        if filter_size == 3:
            replicate_pad = 1
            kernel = self.HSOBEL_WEIGHTS_3x3
        elif filter_size == 5:
            replicate_pad = 2
            kernel = self.HSOBEL_WEIGHTS_5x5
        image = F.pad(image, _quadruple(replicate_pad), mode='replicate')
        grad = F.conv2d(image, kernel, stride=1, padding=0, 
            bias=None) * image_height
        # modify the boundary based on forward & backward finite difference
        if self.correct:
            return torch.matmul(self.modifier.t(), grad)
        else:
            return grad 
Example #12
Source File: image_gradient.py    From pde-surrogate with MIT License 6 votes vote down vote up
def __init__(self, imsize, correct=True, device='cpu'):
        # conv2d is cross-correlation, need to transpose the kernel here
        self.HSOBEL_WEIGHTS_3x3 = torch.FloatTensor(
            np.array([[-1, -2, -1],
                     [ 0, 0, 0],
                     [1, 2, 1]]) / 8.0).unsqueeze(0).unsqueeze(0).to(device)

        self.VSOBEL_WEIGHTS_3x3 = self.HSOBEL_WEIGHTS_3x3.transpose(-1, -2)

        self.VSOBEL_WEIGHTS_5x5 = torch.FloatTensor(
                    np.array([[-5, -4, 0, 4, 5],
                                [-8, -10, 0, 10, 8],
                                [-10, -20, 0, 20, 10],
                                [-8, -10, 0, 10, 8],
                                [-5, -4, 0, 4, 5]]) / 240.).unsqueeze(0).unsqueeze(0).to(device)
        self.HSOBEL_WEIGHTS_5x5 = self.VSOBEL_WEIGHTS_5x5.transpose(-1, -2)

        modifier = np.eye(imsize)
        modifier[0:2, 0] = np.array([4, -1])
        modifier[-2:, -1] = np.array([-1, 4])
        self.modifier = torch.FloatTensor(modifier).to(device)
        self.correct = correct 
Example #13
Source File: qconv.py    From fairseq with MIT License 6 votes vote down vote up
def _conv_forward(self, input, weight):
        if self.padding_mode != "zeros":
            return F.conv2d(
                F.pad(input, self._padding_repeated_twice, mode=self.padding_mode),
                weight,
                self.bias,
                self.stride,
                _pair(0),
                self.dilation,
                self.groups,
            )
        return F.conv2d(
            input,
            weight,
            self.bias,
            self.stride,
            self.padding,
            self.dilation,
            self.groups,
        ) 
Example #14
Source File: losses.py    From centerpose with MIT License 5 votes vote down vote up
def forward(self, output_weight , seg_feat , ind , target ):

        B,C,H,W = seg_feat.size()
        weight = _transpose_and_gather_feat(output_weight, ind)
        seg_feat = seg_feat.view([-1,H,W]).unsqueeze(0)
        weight = weight.view([-1,C]).unsqueeze(-1).unsqueeze(-1)
        pred_seg = F.conv2d(seg_feat, weight,groups = B)
        pred_seg=pred_seg.view([B,-1,H,W])
        loss = (lovasz_hinge(pred_seg, target, 255) + lovasz_hinge(-pred_seg,1-target,-254))/2
        return loss 
Example #15
Source File: losses.py    From centerpose with MIT License 5 votes vote down vote up
def dfl_ssim(img1, img2, mask, window_size=11, val_range=1, gauss='original'):
    # Value range can be different from 255. Other common ranges are 1 (sigmoid) and 2 (tanh).
    # padd = window_size//2
    padd = 0
    (batch, channel, height, width) = img1.size()
    img1, img2 = torch.mul(img1, mask), torch.mul(img2, mask)

    real_size = min(window_size, height, width)
    window = create_window(real_size, gauss=gauss).to(img1.device)

    # 2019.05.07.
    c1 = (0.01 * val_range) ** 2
    c2 = (0.03 * val_range) ** 2

    mu1 = F.conv2d(img1, window, padding=padd, groups=channel)
    mu2 = F.conv2d(img2, window, padding=padd, groups=channel)

    num0 = mu1 * mu2 * 2.0
    mu1_sq = mu1.pow(2)
    mu2_sq = mu2.pow(2)
    den0 = mu1_sq + mu2_sq

    luminance = (num0 + c1) / (den0 + c1)

    num1 = F.conv2d(img1 * img2, window, padding=padd, groups=channel) * 2.0
    den1 = F.conv2d(img1 * img1 + img2 * img2, window, padding=padd, groups=channel)
    cs = (num1 - num0 + c2) / (den1 - den0 + c2)
    ssim_val = torch.mean(luminance * cs, dim=(-3, -2))

    return torch.mean((1.0 - ssim_val) / 2.0)


# Classes to re-use window 
Example #16
Source File: utils.py    From EfficientDet.Pytorch with MIT License 5 votes vote down vote up
def forward(self, x):
        x = self.static_padding(x)
        x = F.conv2d(x, self.weight, self.bias, self.stride,
                     self.padding, self.dilation, self.groups)
        return x 
Example #17
Source File: glow_msc.py    From pde-surrogate with MIT License 5 votes vote down vote up
def __init__(self, in_channels):
        super(LatentEncoder, self).__init__()
        self.conv2d = Conv2dZeros(in_channels, in_channels * 2) 
Example #18
Source File: glow_msc.py    From pde-surrogate with MIT License 5 votes vote down vote up
def reverse(self, x):
        # z --> x
        logdet = self.log_s.sum() * x.shape[2] * x.shape[3]
        if self.train_sampling:
            # reverse path is used for training, do not take inverse here
            weight = self.weight()
            logdet = -logdet
        else:
            weight = self.inv_weight()
        kernel = weight.view(*self.w_shape, 1, 1)
        return F.conv2d(x, kernel), logdet 
Example #19
Source File: glow_msc.py    From pde-surrogate with MIT License 5 votes vote down vote up
def forward(self, x):
        # x --> z
        logdet = self.log_s.sum() * x.shape[2] * x.shape[3]
        if self.train_sampling:
            # reverse path is used for training, take matrix inverse here
            weight = self.inv_weight()
            logdet = -logdet
        else:
            weight = self.weight()

        kernel = weight.view(*self.w_shape, 1, 1)
        return F.conv2d(x, kernel), logdet 
Example #20
Source File: glow_msc.py    From pde-surrogate with MIT License 5 votes vote down vote up
def reverse(self, z):
        # z --> x
        if self.train_sampling:
            W = self.weight
        else:
            W = torch.inverse(self.weight.double()).float()  
        logdet = self.log_determinant(z, W)
        kernel = W.view(*self.w_shape, 1, 1)
        # negative logdet, since we are still computing p(x|z)
        return F.conv2d(z, kernel), -logdet 
Example #21
Source File: glow_msc.py    From pde-surrogate with MIT License 5 votes vote down vote up
def forward(self, x):
        # x --> z
        # torch.slogdet() is not stable
        if self.train_sampling:
            W = torch.inverse(self.weight.double()).float()  
        else:
            W = self.weight
        logdet = self.log_determinant(x, W)        
        kernel = W.view(*self.w_shape, 1, 1)
        return F.conv2d(x, kernel), logdet 
Example #22
Source File: module.py    From EfficientDet.Pytorch with MIT License 5 votes vote down vote up
def conv_ws_2d(input,
               weight,
               bias=None,
               stride=1,
               padding=0,
               dilation=1,
               groups=1,
               eps=1e-5):
    c_in = weight.size(0)
    weight_flat = weight.view(c_in, -1)
    mean = weight_flat.mean(dim=1, keepdim=True).view(c_in, 1, 1, 1)
    std = weight_flat.std(dim=1, keepdim=True).view(c_in, 1, 1, 1)
    weight = (weight - mean) / (std + eps)
    return F.conv2d(input, weight, bias, stride, padding, dilation, groups) 
Example #23
Source File: image_gradient.py    From pde-surrogate with MIT License 5 votes vote down vote up
def grad_h(self, image, filter_size=5):
        # horizontal derivative
        image_width = image.shape[-1]
        if filter_size == 3:
            replicate_pad = 1
            kernel = self.kernel_h_3x3
        elif filter_size == 5:
            replicate_pad = 2
            kernel = self.kernel_h_5x5
        elif filter_size == 7:
            replicate_pad = 3
            kernel = self.kernel_h_7x7
        image = F.pad(image, _quadruple(replicate_pad), mode='replicate')
        return F.conv2d(image, kernel, stride=1, padding=0, 
            bias=None) * image_width 
Example #24
Source File: conv2d_layers.py    From gen-efficientnet-pytorch with Apache License 2.0 5 votes vote down vote up
def conv2d_same(
        x, weight: torch.Tensor, bias: Optional[torch.Tensor] = None, stride: Tuple[int, int] = (1, 1),
        padding: Tuple[int, int] = (0, 0), dilation: Tuple[int, int] = (1, 1), groups: int = 1):
    ih, iw = x.size()[-2:]
    kh, kw = weight.size()[-2:]
    pad_h = _calc_same_pad(ih, kh, stride[0], dilation[0])
    pad_w = _calc_same_pad(iw, kw, stride[1], dilation[1])
    if pad_h > 0 or pad_w > 0:
        x = F.pad(x, [pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2])
    return F.conv2d(x, weight, bias, stride, (0, 0), dilation, groups) 
Example #25
Source File: image_gradient.py    From pde-surrogate with MIT License 5 votes vote down vote up
def grad_h(self, image, filter_size=3):
        """Get image gradient along horizontal direction, or x axis.
        Option to do replicate padding for image before convolution. This is mainly
        for estimate the du/dy, enforcing Neumann boundary condition.

        Args:
            image (Tensor): (1, 1, H, W)
            replicate_pad (None, int, 4-tuple): if 4-tuple, (padLeft, padRight, padTop, 
                padBottom)
        """
        image_width = image.shape[-1]

        if filter_size == 3:
            replicate_pad = 1
            kernel = self.VSOBEL_WEIGHTS_3x3
        elif filter_size == 5:
            replicate_pad = 2
            kernel = self.VSOBEL_WEIGHTS_5x5
        image = F.pad(image, _quadruple(replicate_pad), mode='replicate')
        grad = F.conv2d(image, kernel, stride=1, padding=0, bias=None) * image_width
        # modify the boundary based on forward & backward finite difference (three points)
        # forward [-3, 4, -1], backward [3, -4, 1]
        if self.correct:
            return torch.matmul(grad, self.modifier)
        else:
            return grad 
Example #26
Source File: conv2d_layers.py    From gen-efficientnet-pytorch with Apache License 2.0 5 votes vote down vote up
def forward(self, x):
        input_size = x.size()[-2:]
        if self.pad is None:
            pad_arg = _same_pad_arg(input_size, self.weight.size()[-2:], self.stride, self.dilation)
            self.pad = nn.ZeroPad2d(pad_arg)
            self.pad_input_size = input_size
        else:
            assert self.pad_input_size == input_size

        x = self.pad(x)
        return F.conv2d(
            x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups) 
Example #27
Source File: stylegan2.py    From StyleGAN2_PyTorch with MIT License 5 votes vote down vote up
def forward(self, x):
        y = x.clone()
        y = y.reshape([-1, x.shape[2], x.shape[3], 1])  # N C H W ---> N*C H W 1

        inC, inH, inW = x.shape[1:]
        # step 1: upfirdn2d

        # 1) Upsample
        y = torch.reshape(y, (-1, inH, 1, inW, 1, 1))
        y = F.pad(y, (0, 0, self.factor - 1, 0, 0, 0, self.factor - 1, 0, 0, 0, 0, 0))
        y = torch.reshape(y, (-1, 1, inH * self.factor, inW * self.factor))

        # 2) Pad (crop if negative).
        y = F.pad(y, (0, 0,
                      max(self.pady0, 0), max(self.pady1, 0),
                      max(self.padx0, 0), max(self.padx1, 0),
                      0, 0
                      ))
        y = y[:,
            max(-self.pady0, 0): y.shape[1] - max(-self.pady1, 0),
            max(-self.padx0, 0): y.shape[2] - max(-self.padx1, 0),
            :]

        # 3) Convolve with filter.
        y = y.permute(0, 3, 1, 2)  # N*C H W 1 --> N*C 1 H W
        y = y.reshape(-1, 1, inH * self.factor + self.pady0 + self.pady1, inW * self.factor + self.padx0 + self.padx1)
        y = F.conv2d(y, self.k)
        y = y.view(-1, 1,
                   inH * self.factor + self.pady0 + self.pady1 - self.kernelH + 1,
                   inW * self.factor + self.padx0 + self.padx1 - self.kernelW + 1)

        # 4) Downsample (throw away pixels).
        if inH * self.factor != y.shape[1]:
            y = F.interpolate(y, size=(inH * self.factor, inW * self.factor), mode='bilinear')
        y = y.permute(0, 2, 3, 1)
        y = y.reshape(-1, inC, inH * self.factor, inW * self.factor)

        return y 
Example #28
Source File: conv2d_layers.py    From gen-efficientnet-pytorch with Apache License 2.0 5 votes vote down vote up
def forward(self, x, routing_weights):
        B, C, H, W = x.shape
        weight = torch.matmul(routing_weights, self.weight)
        new_weight_shape = (B * self.out_channels, self.in_channels // self.groups) + self.kernel_size
        weight = weight.view(new_weight_shape)
        bias = None
        if self.bias is not None:
            bias = torch.matmul(routing_weights, self.bias)
            bias = bias.view(B * self.out_channels)
        # move batch elements with channels so each batch element can be efficiently convolved with separate kernel
        x = x.view(1, B * C, H, W)
        if self.dynamic_padding:
            out = conv2d_same(
                x, weight, bias, stride=self.stride, padding=self.padding,
                dilation=self.dilation, groups=self.groups * B)
        else:
            out = F.conv2d(
                x, weight, bias, stride=self.stride, padding=self.padding,
                dilation=self.dilation, groups=self.groups * B)
        out = out.permute([1, 0, 2, 3]).view(B, self.out_channels, out.shape[-2], out.shape[-1])

        # Literal port (from TF definition)
        # x = torch.split(x, 1, 0)
        # weight = torch.split(weight, 1, 0)
        # if self.bias is not None:
        #     bias = torch.matmul(routing_weights, self.bias)
        #     bias = torch.split(bias, 1, 0)
        # else:
        #     bias = [None] * B
        # out = []
        # for xi, wi, bi in zip(x, weight, bias):
        #     wi = wi.view(*self.weight_shape)
        #     if bi is not None:
        #         bi = bi.view(*self.bias_shape)
        #     out.append(self.conv_fn(
        #         xi, wi, bi, stride=self.stride, padding=self.padding,
        #         dilation=self.dilation, groups=self.groups))
        # out = torch.cat(out, 0)
        return out 
Example #29
Source File: conv_ws.py    From mmdetection with Apache License 2.0 5 votes vote down vote up
def conv_ws_2d(input,
               weight,
               bias=None,
               stride=1,
               padding=0,
               dilation=1,
               groups=1,
               eps=1e-5):
    c_in = weight.size(0)
    weight_flat = weight.view(c_in, -1)
    mean = weight_flat.mean(dim=1, keepdim=True).view(c_in, 1, 1, 1)
    std = weight_flat.std(dim=1, keepdim=True).view(c_in, 1, 1, 1)
    weight = (weight - mean) / (std + eps)
    return F.conv2d(input, weight, bias, stride, padding, dilation, groups) 
Example #30
Source File: utils.py    From EfficientDet.Pytorch with MIT License 5 votes vote down vote up
def forward(self, x):
        ih, iw = x.size()[-2:]
        kh, kw = self.weight.size()[-2:]
        sh, sw = self.stride
        oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
        pad_h = max((oh - 1) * self.stride[0] +
                    (kh - 1) * self.dilation[0] + 1 - ih, 0)
        pad_w = max((ow - 1) * self.stride[1] +
                    (kw - 1) * self.dilation[1] + 1 - iw, 0)
        if pad_h > 0 or pad_w > 0:
            x = F.pad(x, [pad_w // 2, pad_w - pad_w //
                          2, pad_h // 2, pad_h - pad_h // 2])
        return F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)