Chapter 29
05. PyTorch Going Modular Exercises
NotebookPython 311 cells
05. PyTorch Going Modular Exercises
Welcome to the 05. PyTorch Going Modular exercise template notebook.
There are several questions in this notebook and it's your goal to answer them by writing Python and PyTorch code.
Note: There may be more than one solution to each of the exercises, don't worry too much about the exact right answer. Try to write some code that works first and then improve it if you can.
Resources and solutions
- These exercises/solutions are based on section 05. PyTorch Going Modular of the Learn PyTorch for Deep Learning course by Zero to Mastery.
Solutions:
Try to complete the code below before looking at these.
- See a live walkthrough of the solutions (errors and all) on YouTube.
- See an example solutions notebook for these exercises on GitHub.
1. Turn the code to get the data (from section 1. Get Data) into a Python script, such as get_data.py.
- When you run the script using
python get_data.pyit should check if the data already exists and skip downloading if it does. - If the data download is successful, you should be able to access the
pizza_steak_sushiimages from thedatadirectory.
In [ ]python · cell 4
python
# YOUR CODE HEREIn [ ]python · cell 5
python
# Example running of get_data.py
!python get_data.py2. Use Python's argparse module to be able to send the train.py custom hyperparameter values for training procedures.
- Add an argument flag for using a different:
- Training/testing directory
- Learning rate
- Batch size
- Number of epochs to train for
- Number of hidden units in the TinyVGG model
- Keep the default values for each of the above arguments as what they already are (as in notebook 05).
- For example, you should be able to run something similar to the following line to train a TinyVGG model with a learning rate of 0.003 and a batch size of 64 for 20 epochs:
python train.py --learning_rate 0.003 batch_size 64 num_epochs 20. - Note: Since
train.pyleverages the other scripts we created in section 05, such as,model_builder.py,utils.pyandengine.py, you'll have to make sure they're available to use too. You can find these in thegoing_modularfolder on the course GitHub.
In [ ]python · cell 7
python
# YOUR CODE HEREIn [ ]python · cell 8
python
# Example running of train.py
!python train.py --num_epochs 5 --batch_size 128 --hidden_units 128 --learning_rate 0.00033. Create a Python script to predict (such as predict.py) on a target image given a file path with a saved model.
- For example, you should be able to run the command
python predict.py some_image.jpegand have a trained PyTorch model predict on the image and return its prediction. - To see example prediction code, check out the predicting on a custom image section in notebook 04.
- You may also have to write code to load in a trained model.
In [ ]python · cell 10
python
# YOUR CODE HEREIn [ ]python · cell 11
python
# Example running of predict.py
!python predict.py --image data/pizza_steak_sushi/test/sushi/175783.jpg