Inverse discrete Cosine Transformation
| 295 | |
| 296 | |
| 297 | class iDCT8x8(nn.Module): |
| 298 | """Inverse discrete Cosine Transformation |
| 299 | """ |
| 300 | |
| 301 | def __init__(self): |
| 302 | super(iDCT8x8, self).__init__() |
| 303 | alpha = np.array([1. / np.sqrt(2)] + [1] * 7) |
| 304 | self.alpha = nn.Parameter(torch.from_numpy(np.outer(alpha, alpha)).float()) |
| 305 | tensor = np.zeros((8, 8, 8, 8), dtype=np.float32) |
| 306 | for x, y, u, v in itertools.product(range(8), repeat=4): |
| 307 | tensor[x, y, u, v] = np.cos((2 * u + 1) * x * np.pi / 16) * np.cos((2 * v + 1) * y * np.pi / 16) |
| 308 | self.tensor = nn.Parameter(torch.from_numpy(tensor).float()) |
| 309 | |
| 310 | def forward(self, image): |
| 311 | """ |
| 312 | Args: |
| 313 | image(tensor): batch x height x width |
| 314 | |
| 315 | Returns: |
| 316 | Tensor: batch x height x width |
| 317 | """ |
| 318 | image = image * self.alpha |
| 319 | result = 0.25 * torch.tensordot(image, self.tensor, dims=2) + 128 |
| 320 | result.view(image.shape) |
| 321 | return result |
| 322 | |
| 323 | |
| 324 | class BlockMerging(nn.Module): |