Skip to content

Commit f2daf0a

Browse files
committed
update README for clarity; make TODOs
1 parent cb8c7fe commit f2daf0a

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
### Convolutional Neural network Exercise
22

33
### Task 1 - Find Waldo
4-
In this first task use cross-correlation to find Waldo in the image below:
4+
In this first task we will use cross-correlation to find Waldo in the image below:
55
![where_is_waldo](./data/waldo/waldo_space.jpg)
66

77
[ Image source: https://rare-gallery.com ]
@@ -14,16 +14,18 @@ for an image matrix I and a kernel matrix $K$ of shape $(M \times N)$. To find w
1414

1515
![waldo](./data/waldo/waldo_small.jpg)
1616

17+
By sliding the waldo-kernel over the image and computing the cross-correlation at each position we can find the position where waldo is located by looking for the maximum value in the resulting matrix.
18+
1719
#### Task 1.1 - Direct convolution
1820
Navigate to the `src/custom_conv.py` module.
19-
- Start in `my_conv_direct` and implement the convolution following the equation above. Test your function with vscode tests or `nox -s test`.
20-
- Go to `src/waldo.py` and make sure that `my_conv_direct` is used for convolution. This script finds waldo in the image using your convolution function. Execute it with `python ./src/waldo.py` in your terminal.
21-
- If your code passes the pytest but is too slow to find waldo feel free to use `scipy.signal.correlate2d` in `src/waldo.py` instead of your convolution function.
21+
1. Start in `my_conv_direct` and implement the convolution following the equation above. Test your function with vscode tests or `nox -s test`.
22+
2. Go to `src/waldo.py` and make sure that `my_conv_direct` is used for convolution. This script finds waldo in the image using your convolution function. Execute it with `python ./src/waldo.py` in your terminal.
23+
3. If your code passes the pytest but is too slow to find waldo feel free to use `scipy.signal.correlate2d` in `src/waldo.py` instead of your convolution function.
2224

2325
#### Task 1.2 (Optional)
2426
Navigate to the `src/custom_conv.py` module.
25-
The function `my_conv` implements a fast version of the convolution operation above using a flattend kernel. We learned about this fast version in the lecture. Have a look at the slides again and then implement `get_indices` to make `my_conv` work. It should return
26-
- A matrix of indices following the flattend convolution rule from the lecture, e.g. for a $(2\times 2)$ kernel and a $(3\times 3)$ image it should return the index transformation
27+
The function `my_conv` implements a fast version of the convolution operation above using a flattened kernel. We learned about this fast version in the lecture. Have a look at the slides again and then implement `get_indices` to make `my_conv` work. It should return
28+
- A matrix of indices following the flattened convolution rule from the lecture, e.g. for a $(2\times 2)$ kernel and a $(3\times 3)$ image it should return the index transformation
2729

2830
$$
2931
\begin{pmatrix}
@@ -43,6 +45,11 @@ $$
4345
$$o=(i-k)+1$$
4446
where $i$ denotes the input size and $k$ the kernel size.
4547

48+
If you need help, follow the hints below:
49+
- First create a list of starting indices for each row in the output. These are the upper left corners of each kernel application.
50+
- Then create a list of offsets within the kernel. These are the indices that need to be added to each starting index to get the full set of indices for each kernel application.
51+
- Finally use broadcasting to add the two lists together and get the final index matrix.
52+
4653
You can test the function with `nox -s test` by importing `my_conv` in `tests/test_conv.py` and changing `my_conv_direct` to `my_conv` in the test function. Make sure that `src/waldo.py` now uses `my_conv` for convolution and run the script again.
4754

4855

@@ -51,7 +58,7 @@ You can test the function with `nox -s test` by importing `my_conv` in `tests/te
5158

5259
![mnist](./figures/mnist.png)
5360

54-
Open `src/mnist.py` and implement MNIST digit recognition with `CNN` in `torch`
61+
Open `src/mnist.py` and implement MNIST digit recognition with `CNN` in `torch`. Go through the TODOs in the file. You will see that a lot of the code is really similar or the same as in yesterday's exercise.
5562

5663
- *Reuse* your code from yesterday.
5764
- Reuse yesterday's `Net` class, add convolutional layers and pooling. `torch.nn.Conv2d` and `torch.nn.MaxPool2d` will help you.

src/custom_conv.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def get_indices(image: torch.Tensor, kernel: torch.Tensor) -> tuple:
1818
image_rows, image_cols = image.shape
1919
kernel_rows, kernel_cols = kernel.shape
2020

21-
# TODO: Implement me
21+
# 1.2 TODO: Implement me
2222
idx_list = None
2323
corr_rows = None
24-
corr_cols = None
24+
corr_cols = None
2525
return idx_list, corr_rows, corr_cols
2626

2727

@@ -45,5 +45,5 @@ def my_conv_direct(image: torch.Tensor, kernel: torch.Tensor) -> torch.Tensor:
4545
image_rows, image_cols = image.shape
4646
kernel_rows, kernel_cols = kernel.shape
4747
corr = []
48-
# TODO: Implement direct convolution.
49-
return torch.tensor([0.])
48+
# 1.1.1 TODO: Implement direct convolution.
49+
return torch.tensor([0.0])

src/waldo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,18 @@
2626
plt.imshow(problem_image)
2727
plt.show()
2828

29+
# Normalizing images such that they have mean 0 and variance 1.
2930
mean = np.mean(problem_image)
3031
std = np.std(problem_image)
3132
problem_image = (problem_image - mean) / std
3233
waldo = (waldo - mean) / std
3334

3435
# Too slow does not work.
36+
# 1.1.2 TODO: Use your own convolution function to find waldo.
3537
# conv_res = my_conv_direct(problem_image, waldo)
3638

3739
# Built in function very fast.
40+
# 1.1.3 TODO: Use scipy's correlate2d function to find waldo.
3841
# conv_res = correlate2d(problem_image, waldo, mode="valid", boundary="fill")
3942

4043
# Selfmade ok but too costly in terms of memory.

0 commit comments

Comments
 (0)