chuangchuangtan / NPR-DeepfakeDetection

148 stars 13 forks source link

How to get the NPR images? #3

Open Phoebe-cap opened 7 months ago

Phoebe-cap commented 7 months ago

Hi, first of all, excellent work! According to my understanding, the input of the training classifier is NPR images. However, do you have the code to get the NPR images? I didn't find it.

chuangchuangtan commented 7 months ago

We use the difference between an image and its resampled version as NPR. NPR = x - self.interpolate(x, 0.5)

Specifically, after one downsampling and one nearest-neighbor upsampling, the 2x2 local pixels will replicate the value of the top-left corner code.

    def interpolate(self, img, factor):
        return F.interpolate(F.interpolate(img, scale_factor=factor, mode='nearest', recompute_scale_factor=True), \
                   scale_factor=1/factor, mode='nearest', recompute_scale_factor=True)

For example

Image:
|  1.1   |  1.2   |  2.1   |  2.2   |
|  1.3   |  1.4   |  2.3   |  2.4   |
|  3.1   |  3.2   |  4.1   |  4.2   |
|  3.3   |  3.4   |  4.3   |  4.4   |

downsampling
|  1.1   |  2.1   | 
|  3.1   |  4.1   | 

nearest-neighbor upsampling  2x
|  1.1   |  1.1   |  2.1   |  2.1   |
|  1.1   |  1.1   |  2.1   |  2.1   |
|  3.1   |  3.1   |  4.1   |  4.1   |
|  3.1   |  3.1   |  4.1   |  4.1   |

Image - upsampling ( downsampling(Image) ):

|  1.1 - 1.1  |  1.2 - 1.1   |  2.1 - 2.1   |  2.2 - 2.1   |
|  1.3 - 1.1  |  1.4 - 1.1   |  2.3 - 2.1   |  2.4 - 2.1   |
|  3.1 - 3.1  |  3.2 - 3.1   |  4.1 - 4.1   |  4.2 - 4.1   |
|  3.3 - 3.1  |  3.4 - 3.1   |  4.3 - 4.1   |  4.4 - 4.1   |
chuangchuangtan commented 7 months ago

Evaluation code

import numpy as np
from torch.nn import functional as F
import torch

image = torch.from_numpy(np.array([[1.1, 1.2, 2.1, 2.2], \
                                   [1.3, 1.4, 2.3, 2.4], \
                                   [3.1, 3.2, 4.1, 4.2], \
                                   [3.3, 3.4, 4.3, 4.4]])).reshape([1, 1, 4, 4])
downsampling_image = F.interpolate(image, scale_factor=0.5, mode='nearest', recompute_scale_factor=True)
upsampling_downsampling_image = F.interpolate(downsampling_image, scale_factor=2, mode='nearest', recompute_scale_factor=True)
Phoebe-cap commented 7 months ago

OK, thank you very much.