Downsamping with Gaussian kernel used in the DUF official code. Args: x (Tensor): Frames to be downsampled, with shape (b, t, c, h, w). kernel_size (int): Kernel size. Default: 13. scale (int): Downsampling factor. Supported scale: (2, 3, 4). Default: 4.
(x, kernel_size=13, scale=4)
| 330 | |
| 331 | |
| 332 | def duf_downsample(x, kernel_size=13, scale=4): |
| 333 | """Downsamping with Gaussian kernel used in the DUF official code. |
| 334 | |
| 335 | Args: |
| 336 | x (Tensor): Frames to be downsampled, with shape (b, t, c, h, w). |
| 337 | kernel_size (int): Kernel size. Default: 13. |
| 338 | scale (int): Downsampling factor. Supported scale: (2, 3, 4). |
| 339 | Default: 4. |
| 340 | |
| 341 | Returns: |
| 342 | Tensor: DUF downsampled frames. |
| 343 | """ |
| 344 | assert scale in (2, 3, 4), f'Only support scale (2, 3, 4), but got {scale}.' |
| 345 | |
| 346 | squeeze_flag = False |
| 347 | if x.ndim == 4: |
| 348 | squeeze_flag = True |
| 349 | x = x.unsqueeze(0) |
| 350 | b, t, c, h, w = x.size() |
| 351 | x = x.view(-1, 1, h, w) |
| 352 | pad_w, pad_h = kernel_size // 2 + scale * 2, kernel_size // 2 + scale * 2 |
| 353 | x = F.pad(x, (pad_w, pad_w, pad_h, pad_h), 'reflect') |
| 354 | |
| 355 | gaussian_filter = generate_gaussian_kernel(kernel_size, 0.4 * scale) |
| 356 | gaussian_filter = torch.from_numpy(gaussian_filter).type_as(x).unsqueeze(0).unsqueeze(0) |
| 357 | x = F.conv2d(x, gaussian_filter, stride=scale) |
| 358 | x = x[:, :, 2:-2, 2:-2] |
| 359 | x = x.view(b, t, c, x.size(2), x.size(3)) |
| 360 | if squeeze_flag: |
| 361 | x = x.squeeze(0) |
| 362 | return x |