megvii-research/DCLS-SR

deg_utils.py 问题

Opened this issue · 0 comments

感谢您的工作,我在阅读代码时遇到了问题:pad_w, pad_h = 6 + scale * 2, 6 + scale * 2. pad时为什么要加上scale * 2,我认为x = x[:, :, 2:-2, 2:-2]操作 将scale * 2pad带来的像素点完全裁掉了,在pad时加scale * 2起不到任何作用,期望您的解答。

def DUF_downsample(x, scale=4):
"""Downsamping with Gaussian kernel used in the DUF official code

Args:
    x (Tensor, [B, T, C, H, W]): frames to be downsampled.
    scale (int): downsampling factor: 2 | 3 | 4.
"""

assert scale in [2, 3, 4], "Scale [{}] is not supported".format(scale)

  def gkern(kernlen=13, nsig=1.6):
      import scipy.ndimage.filters as fi
  
      inp = np.zeros((kernlen, kernlen))
      # set element at the middle to one, a dirac delta
      inp[kernlen // 2, kernlen // 2] = 1
      # gaussian-smooth the dirac, resulting in a gaussian filter mask
      return fi.gaussian_filter(inp, nsig)
  
  B, T, C, H, W = x.size()
  x = x.view(-1, 1, H, W)
  pad_w, pad_h = 6 + scale * 2, 6 + scale * 2  # 6 is the pad of the gaussian filter
  r_h, r_w = 0, 0
  if scale == 3:
      r_h = 3 - (H % 3)
      r_w = 3 - (W % 3)
  x = F.pad(x, [pad_w, pad_w + r_w, pad_h, pad_h + r_h], "reflect")
  
  gaussian_filter = (
      torch.from_numpy(gkern(13, 0.4 * scale)).type_as(x).unsqueeze(0).unsqueeze(0)
  )
  x = F.conv2d(x, gaussian_filter, stride=scale)
  x = x[:, :, 2:-2, 2:-2]
  x = x.view(B, T, C, x.size(2), x.size(3))
  return x