Numpy Tutorial
Numpy Tutorial
DL Tutors Team
Exercise 0
April 19, 2023
Numpy Tutorial
Numpy Tutorial
Introduction
The purpose of this exercise is to refresh your knowledge of Python and NumPy. We will use
numpy functions to create different types of patterns, and implement an image generator class
to load and to synthetically augment the data using rigid transformations. If you have never
programmed with Python before, please make yourself familiar with the language first, e.g., at
https://docs.python.org/3/tutorial/.
For all exercise parts, unit tests are available in the file NumpyTests.py. These tests will
help you to assess whether your implementation is correct or not. Note that these unit tests
also serve as an extension of slides and description. Many IDEs (e.g., PyCharm) offer the
option to run these unittests directly, or you can run via the command line:
python NumpyTests.py
1
Deep Learning Exercises [DL E]
DL Tutors Team
Exercise 0
April 19, 2023
Numpy Tutorial
Task:
• Create a file “pattern.py” and implement the classes Checker and Circle in this file.
Note that we do not provide any skeleton here. Also create a file “main.py”, which
imports all other classes.
Hints:
init () is the constructor of the class. Following functions from the cheat sheet might be
useful: np.tile(), np.arange(), np.zeros(), np.ones(), np.concatenate() and
np.expand dims()
2
Deep Learning Exercises [DL E]
DL Tutors Team
Exercise 0
April 19, 2023
Numpy Tutorial
1.2 Checkerboard
The first pattern to implement is a checkerboard pattern in the class Checkers with adaptable
tile size and resolution. You might want to start with a fixed tile size and adapt later on. For
simplicity we assume that the resolution is divisible by the tile size without remainder.
Task:
• Implement the constructor. It receives two arguments: an integer resolution that de-
fines the number of pixels in each dimension, and an integer tile size that defines the
number of pixel an individual tile has in each dimension. Store the arguments as instance
variables. Create an additional instance variable output that can store the pattern.
• Implement the method draw() which creates the checkerboard pattern as a numpy array.
The tile in the top left corner should be black. In order to avoid truncated checkerboard
patterns, make sure your code only allows values for resolution that are evenly dividable
by 2· tile size. Store the pattern in the instance variable output and return a copy.
Helpful functions for that can be found on the Deep Learning Cheatsheet provided.
• Implement the method show() which shows the checkerboard pattern with for example
plt.imshow(). If you want to display a grayscale image you can use cmap = gray as
a parameter for this function.
• Verify your implementation visually by creating an object of this class in your main script
and calling the object’s functions.
Hint: Try to build your checkerboard out of simpler constituents. Think about how tile size
and resolution must relate to each other in order to get a valid checkerboard pattern.
3
Deep Learning Exercises [DL E]
DL Tutors Team
Exercise 0
April 19, 2023
Numpy Tutorial
1.3 Circle
The second pattern to implement is a binary circle with a given radius at a specified position
in the image. Note that we expect you to use numpy operations to draw this pattern. We do
not accept submissions which draw a circle with a single library function call.
Task:
• Implement the constructor. It receives three arguments: An integer resolution, an
integer radius that describes the radius of the circle, and a tuple position that contains
the x- and y-coordinate of the circle center in the image.
• Implement the method draw() which creates a binary image of a circle as a numpy array.
Store the pattern in the instance variable output and return a copy.
• Implement the method show() which shows the circle with for example plt.imshow().
• Verify your implementation visually by creating an object of this class in your main script
and calling the object’s functions.
Hints:
Think of a formula describing the circle with respect to pixel coordinates. Make yourself fa-
miliar with np.meshgrid.
4
Deep Learning Exercises [DL E]
DL Tutors Team
Exercise 0
April 19, 2023
Numpy Tutorial
Task:
• Implement the constructor. It receives one parameter: an integer resolution.
• Implement the method draw() which creates the spectrum in Fig. 3 as a numpy array.
Remember that RGB images have 3 channels and that a spectrum consists of rising
values across a specific dimension. For each color channel, the intensity minimum and
maximum should be 0.0 and 1.0, respectively. Store the pattern in the instance variable
output and return a copy. Hint: Particularly take a look into the corners and their
color, to figure out the underlying distribution of the channels.
• Implement the method show() which shows the RGB spectrum with for example
plt.imshow().
• Verify your implementation visually by creating an object of this class in your main script
and calling the object’s functions.
5
Deep Learning Exercises [DL E]
DL Tutors Team
Exercise 0
April 19, 2023
Numpy Tutorial
One of the most important tasks for machine learning is adequate pre-processing and data
handling. In the following, we will implement a class that is able to read in a set of images,
their associated class labels (stored as a JSON file), and generate batches (subsets of the data)
that can be used for training of a neural network.
• The labels in the JSON file are stored as a dictionary, where the key represents the
corresponding filename of the images as a string (e.g. the key ’15’ corresponds to the
image 15.npy) and the value of each key stands for the respective class label encoded as
integer. (0 = ’airplane’; 1 = ’automobile’; 2 = ’bird’; 3 = ’cat’; 4 = ’deer’; 5 = ’dog’; 6
= ’frog’; 7 = ’horse’; 8 = ’ship’; 9 = ’truck’ )
• Provide the method next(), which returns one batch of the provided dataset as a tuple
(images, labels), where images represents a batch of images and labels an array with the
corresponding labels, when called. Each image of your data set should be included only
once in those batches until the end of one epoch. One epoch describes a run through the
whole data set.
• Note: Sometimes the images fed into a neural network are first resized. Therefore, a
resizing option should be included within the next() method. Do not confuse resizing
with reshaping! Resizing usually involves interpolation of data, reshaping is the simple
reordering of data. It is allowed to use a library function for resizing (Hint: Have a look
into skimage.transform.resize).
• Note: Make sure all your batches have the same size. If the last batch is smaller than
the others, complete that batch by reusing images from the beginning of your training
data set.
6
Deep Learning Exercises [DL E]
DL Tutors Team
Exercise 0
April 19, 2023
Numpy Tutorial
• Implement a method current epoch() which returns an integer of the current epoch.
This number should be updated in the next() when we start to iterate through the data
set (again) from the beginning.
• Implement a method class name(int label), which returns the class name that corre-
sponds to the integer label in the argument int label.
• Implement a method show() which generates a batch using next() and plots it. Use
class name() to obtain the titles for the image plots, as shown in Fig. 4.
• Verify your implementation visually by creating an object of this class in your main script
and calling show().
• Verify the correct handling in next() by calling the unit tests with TestGen.
Hints:
init () is the constructor of the class as previously.
Make sure to handle the data type correctly when you visualize the images (see documentation
of plt.imshow).
Check out the methods provided in np.random for data augmentation.
Have a look at plt.add subplot to simplify the plot generation.
7
Deep Learning Exercises [DL E]
DL Tutors Team
Exercise 0
April 19, 2023
Numpy Tutorial
8
Deep Learning Exercises [DL E]
DL Tutors Team
Exercise 0
April 19, 2023
Numpy Tutorial
Task:
Debug your implementation until every test in the suite passes. You can run all tests by
providing no commandline parameter. To run the unittests you can either execute them with
python in the terminal or with the dedicated unittest environment of PyCharm. We recommend
the latter one, as it provides a better overview of all tests. For the automated computation of
the bonus points achieved in one exercise, run the unittests with the bonus flag in a terminal,
with
to check out the manual. For dispatching your folder run e.g.
1
https://www.jetbrains.com/help/pycharm/creating-and-editing-run-debug-configurations.html