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.
(x, scale=4)
| 150 | |
| 151 | |
| 152 | def DUF_downsample(x, scale=4): |
| 153 | """Downsamping with Gaussian kernel used in the DUF official code |
| 154 | |
| 155 | Args: |
| 156 | x (Tensor, [B, T, C, H, W]): frames to be downsampled. |
| 157 | scale (int): downsampling factor: 2 | 3 | 4. |
| 158 | """ |
| 159 | |
| 160 | assert scale in [2, 3, 4], 'Scale [{}] is not supported'.format(scale) |
| 161 | |
| 162 | def gkern(kernlen=13, nsig=1.6): |
| 163 | import scipy.ndimage.filters as fi |
| 164 | inp = np.zeros((kernlen, kernlen)) |
| 165 | # set element at the middle to one, a dirac delta |
| 166 | inp[kernlen // 2, kernlen // 2] = 1 |
| 167 | # gaussian-smooth the dirac, resulting in a gaussian filter mask |
| 168 | return fi.gaussian_filter(inp, nsig) |
| 169 | |
| 170 | B, T, C, H, W = x.size() |
| 171 | x = x.view(-1, 1, H, W) |
| 172 | pad_w, pad_h = 6 + scale * 2, 6 + scale * 2 # 6 is the pad of the gaussian filter |
| 173 | r_h, r_w = 0, 0 |
| 174 | if scale == 3: |
| 175 | r_h = 3 - (H % 3) |
| 176 | r_w = 3 - (W % 3) |
| 177 | x = F.pad(x, [pad_w, pad_w + r_w, pad_h, pad_h + r_h], 'reflect') |
| 178 | |
| 179 | gaussian_filter = torch.from_numpy(gkern(13, 0.4 * scale)).type_as(x).unsqueeze(0).unsqueeze(0) |
| 180 | x = F.conv2d(x, gaussian_filter, stride=scale) |
| 181 | x = x[:, :, 2:-2, 2:-2] |
| 182 | x = x.view(B, T, C, x.size(2), x.size(3)) |
| 183 | return x |
| 184 | |
| 185 | |
| 186 | def single_forward(model, inp): |