|
| 1 | +# Jan Individual Task |
| 2 | +====================== |
| 3 | + |
| 4 | +## Task Overview |
| 5 | +In addition to the overall task, I was assigned the implementation of a multi-layer perceptron model, a dataset loader for a subset of the MNIST dataset, and an accuracy metric. |
| 6 | + |
| 7 | +## Network Implementation In-Depth |
| 8 | +For the network part, I was tasked with making a simple MLP network model for image classification tasks. The model consists of two hidden layers with 100 neurons each followed by a leaky-relu activation. This implementation involves creating a custom class that inherits from the PyTorch `nn.Module` class. This allows our class to have two methods: the `__init__` method and a `forward` method. When we create an instance of the class, we can call the instance like a function, which will run the `forward` method. |
| 9 | + |
| 10 | +The network is initialized with the following parameters: |
| 11 | +* `image_shape` |
| 12 | +* `num_classes` |
| 13 | + |
| 14 | +The `image_shape` argument provides the shape of the input image (channels, height, width) which is used to correctly initialize the input size of the first layer. The `num_classes` argument defines the number of output neurons, corresponding to the number of classes in the dataset. |
| 15 | + |
| 16 | +The forward method in this class processes the input as follows: |
| 17 | +1. Flattens the input image. |
| 18 | +2. Passes the flattened input through the first fully connected layer (`fc1`). |
| 19 | +3. Applies a LeakyReLU activation function. |
| 20 | +4. Passes the result through the second fully connected layer (`fc2`). |
| 21 | +5. Applies another LeakyReLU activation function. |
| 22 | +6. Passes the result through the output layer (`out`). |
| 23 | + |
| 24 | +## MNIST Dataset In-Depth |
| 25 | +For the dataset part, I was tasked with creating a custom dataset class for loading a subset of the MNIST dataset containing digits 0 to 3. This involved creating a class that inherits from the PyTorch `Dataset` class. |
| 26 | + |
| 27 | +The class is initialized with the following parameters: |
| 28 | +* `data_path` |
| 29 | +* `sample_ids` |
| 30 | +* `train` (optional, default is False) |
| 31 | +* `transform` (optional, default is None) |
| 32 | +* `nr_channels` (optional, default is 1) |
| 33 | + |
| 34 | +The `data_path` argument stores the path to the four binary files containing MNIST dataset. The verification of presence of these files and their download, if necessary, is facilitated by the `Downloader`class. The `sample_ids` parameter contains the indices of images and their respective labels that are to be loaded from MNIST dataset. Filtering and random splitting of these indices is performed within the `load_data`function. `train`is a boolean flag indicating whether to load data from training (for training and validation splits) or from testing (test split) part of the MNIST dataset. `transform` is a callable created with `torch.compose()` to be applied on the images. `nr_channels` is not used in this dataset, only included for compatibility with other functions. |
| 35 | + |
| 36 | +The class has two main methods: |
| 37 | +* `__len__`: Returns the number of samples in the dataset. |
| 38 | +* `__getitem__`: Retrieves the image and label at the specified index. |
| 39 | + |
| 40 | +## Accuracy Metric In-Depth |
| 41 | +For the metric part, I was tasked with creating an accuracy metric class. The `Accuracy` class computes the accuracy of a model's predictions. The class is initialized with the following parameters: |
| 42 | +* `num_classes` |
| 43 | +* `macro_averaging` (optional, default is False) |
| 44 | + |
| 45 | +The `num_classes` argument specifies the number of classes in the classification task. The `macro_averaging`argument is a boolean flag specifying whether to compute the accuracy using micro or macro averaging. |
| 46 | + |
| 47 | +The class has the following methods: |
| 48 | +* `forward`: Stores the true and predicted labels computed on a batch level. |
| 49 | +* `_macro_acc`: Computes the macro-averaged accuracy on stored values. |
| 50 | +* `_micro_acc`: Computes the micro-averaged accuracy on stored values. |
| 51 | +* `__returnmetric__`: Returns the computed accuracy based on the averaging method for all stored predictions. |
| 52 | +* `__reset__`: Resets the stored true and predicted labels. |
| 53 | + |
| 54 | +The `forward` method takes the true labels and predicted labels as input and stores them. The `_macro_acc` method computes the macro-average accuracy by averaging the accuracy for each class. The `_micro_acc` method computes the micro-average accuracy by calculating the overall accuracy. The `__returnmetric__` method returns the computed accuracy based on the averaging method. The `__reset__` method resets the stored true and predicted labels to prepare for the next epoch. |
0 commit comments