Skip to content

Commit 1c258b4

Browse files
committed
update instructions for exercise 2 and 3
1 parent ce81f36 commit 1c258b4

3 files changed

Lines changed: 19 additions & 22 deletions

File tree

README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Reuse your MNIST digit recognition code. Implement IG as discussed in the lectur
2929
\text{IntegratedGrads}_i(x) = (x_i - x_i') \cdot \frac{1}{m} \sum_{k=1}^m \frac{\partial F (x' + \frac{k}{m} \cdot (x - x'))}{\partial x_i}.
3030
```
3131

32-
F partial xi denotes the gradients with respect to the input color-channels i.
33-
x prime denotes a baseline black image. And x symbolizes an input we are interested in.
34-
Finally, m denotes the number of summation steps from the black baseline image to the interesting input.
32+
$\frac{\partial F}{\partial x_i}$ denotes the gradients with respect to the input color-channels $i$.
33+
$x'$ denotes a baseline black image. And $x$ symbolizes an input we are interested in.
34+
Finally, $m$ denotes the number of summation steps from the black baseline image to the interesting input.
3535

3636
Follow the todos in `./src/mnist_integrated.py` and then run `scripts/integrated_gradients.slurm`.
3737

@@ -66,19 +66,16 @@ The desired outcome is to have a folder called `ffhq_style_gan` in the project d
6666
The `load_folder` function from the `util` module loads both real and fake data.
6767
Code to load the data is already present in the `deepfake_interpretation.py` file.
6868

69-
Compute log-scaled frequency domain representations of samples from both sources via
69+
1. Implement the `transform` function to compute log-scaled frequency domain representations of samples from both sources via
7070

71-
``` math
72-
\mathbf{F}_I = \log_e (| \mathcal{F}_{2d}(\mathbf(I)) | + \epsilon ), \text{ with } \mathbf{I} \in \mathbb{R}^{h,w,c}, \epsilon \approx 0 .
73-
```
74-
75-
Above `h`, `w` and `c` denote image height, width and columns. `Log` denotes the natural logarithm, and bars denote the absolute value. A small epsilon is added for numerical stability.
76-
77-
Use the numpy functions `np.log`, `np.abs`, `np.fft.fft2`. By default, `fft2` transforms the last two axes. The last axis contains the color channels in this case. We are looking to transform the rows and columns.
71+
``` math
72+
\mathbf{F}_I = \log_e (| \mathcal{F}_{2d}(\mathbf(I)) | + \epsilon ), \text{ with } \mathbf{I} \in \mathbb{R}^{h,w,c}, \epsilon \approx 0 .
73+
```
7874

79-
Plot mean spectra for real and fake images as well as their difference over the entire validation or test sets. For that complete the TODOs in `src/deepfake_interpretation.py` and run the script `scripts/train.slurm`.
75+
Above `h`, `w` and `c` denote image height, width and columns. `Log` denotes the natural logarithm, and bars denote the absolute value. A small epsilon is added for numerical stability.
8076

77+
Use the numpy functions `np.log`, `np.abs`, `np.fft.fft2`. By default, `fft2` transforms the last two axes. The last axis contains the color channels in this case. We are looking to transform the rows and columns.
8178

82-
## 3.3 Training and interpreting a linear classifier
83-
Train a linear classifier consisting of a single `nn.Linear`-layer on the log-scaled Fourier coefficients using Torch. Plot the result. What do you see?
79+
2. 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`.
8480

81+
3. `scripts/train.slurm` also trains a linear classifier (consisting of a single `nn.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 to `src/deepfake_interpretation.py` and implement the TODO at the end of the file. What do you see?

src/deepfake_interpretation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def eval_step(net, loss, img, labels):
8787

8888
def transform(image_data):
8989
"""Transform image data."""
90-
# TODO: Implement the function given in the readme
90+
# 3.2.1 TODO: Implement the function given in the readme
9191
return np.zeros_like(image_data)
9292

9393

@@ -249,7 +249,7 @@ def transform(image_data):
249249
plt.colorbar()
250250
plt.savefig("mean_freq_difference.jpg")
251251

252-
# TODO: Visualize the weight array `net.dense.weight`.
252+
# 3.2.3 TODO: Visualize the weight array `net.dense.weight`.
253253
# By reshaping and plotting the weight matrix.
254254

255255
if type(net) is CNN:

src/mnist_integrated.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,20 @@ def integrate_gradients(net, test_images, output_digit, steps_m=300):
115115
g_list = []
116116
for test_image_x in tqdm(test_images, desc="Integrating Gradients"):
117117

118-
# TODO: create a list for the gradients.
118+
# list for the gradients
119119
step_g_list = []
120-
120+
121121
# TODO: create a black reference image via `zeros_like`` .
122-
122+
123123
# TODO: Loop over the integration steps.
124124
for current_step_k in range(steps_m):
125125
pass
126126
# TODO: compute the input to F from equation 5 in the slides.
127-
127+
128128
# TODO: define a forward pass for torch.func.grad
129-
129+
130130
# TODO: use torch.grad to find the gradient with repsect to the input image.
131-
131+
132132
# TODO: append the gradient to your list
133133

134134
# TODO: Return the sum of the of the list elements.

0 commit comments

Comments
 (0)