Open src/input_opt.py. In this exercise we will turn the network optimization problem around. Instead of updating weights to minimize loss, we will keep the weights fixed and update the input image to maximize the activation of a specific output neuron.
The network ./data/weights.pth contains network weights pre-trained on MNIST. We want to generate an image
Mathematically, we want to maximize:
where
- Complete
forward_pass: Implement the function to return the scalar output of the target neuron.
The gradients are computed using torch.func.grad. Start with a network input image [1, 1, 28, 28].
-
Write an optimization loop to iteratively update the input image
$\mathbf{x}$ based on the computed gradients. -
Compare and visualize the results of starting with a random noise image versus starting with a image filled with ones.
Reuse your MNIST digit recognition code. Implement IG as discussed in the lecture. Recall the equation
Follow the todos in ./src/mnist_integrated.py and then run scripts/integrated_gradients.slurm.
In this exercise we will consider 128 by 128-pixel fake images from StyleGAN and pictures of real people from the Flickr-Faces-HQ dataset.
Flickr-Faces-HQ images depict real people, such as the person below:
Generative adversarial networks allow the generation of fake images at scale. Does the picture below seem real?
How can we identify the fake? Given that modern neural networks can generate hundreds of fake images per second can we create a classifier to automate the process?
- Move to the
datafolder in your terminal. Download ffhq_style_gan.zip on bender using the commandIfgdown https://drive.google.com/uc?id=1MOHKuEVqURfCKAN9dwp1o2tuR19OTQCFgdownis not installed, typepip install gdownand then try again. - Type
export UNZIP_DISABLE_ZIPBOMB_DETECTION=TRUEto make unzipping big archives possible. - Extract the image pairs here by executing
unzip ffhq_style_gan.zipin the terminal.
The desired outcome is to have a folder called ffhq_style_gan in the project data-folder.
The load_folder function from the util module loads both real and fake data.
Code to load the data is already present in the deepfake_interpretation.py file.
-
Implement the
transformfunction to compute log-scaled frequency domain representations of samples from both sources via$$\mathbf{F}_I = \log_e (| \mathcal{F}_{2d}(\mathbf(I)) | + \epsilon ), \text{ with } \mathbf{I} \in \mathbb{R}^{h,w,c}, \epsilon \approx 0 .$$ Above
h,wandcdenote image height, width and columns.Logdenotes the natural logarithm, and bars denote the absolute value. A small epsilon is added for numerical stability.Use the numpy functions
np.log,np.abs,np.fft.fft2. By default,fft2transforms the last two axes. The last axis contains the color channels in this case. We are looking to transform the rows and columns. -
Plot mean spectra for real and fake images as well as their difference over the entire validation or test sets. For that run the script
scripts/train.slurm. -
scripts/train.slurmalso trains a linear classifier (consisting of a singlenn.Linear-layer) to distinguish real from fake images on the log-scaled Fourier coefficients. We want to visualize the weights of the trained classifier. For that go tosrc/deepfake_interpretation.pyand implement the TODO at the end of the file. What do you see?

