Photo by Tara Winstead from Pexels
To be able to try the code you will need to
- Download the
flowers.zip
file - Extract it to the root of the project as
flowers/
- Install the dependencies Using Anaconda (recommended)
conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch
conda install -c anaconda pillow
The flowers.zip
has 3 folders that contain our flower images
train
this is our training datavalid
this is the data used for evaluating our classifier accuracy during trainingtest
used for sanity checking
The cat_to_name.json
has the mappings between the flower ids and their actual names
Now it is time to do fun stuff!
-
--arch
: (optional) 'Set the CCN Model architecture to use'vgg16
alexnet
(default)
-
--save_dir
: (optional) 'Set the folder that will be used to save the checkpoints' the default ischeckpoints
-
--learning_rate
: (optional) 'Set the learning rate' the default is0.001
-
--hidden_units
: (optional) 'Set the number of hidden units in the classifier hidden layer' the default is1024
-
--epochs
: (optional) 'Set the number of training epochs' default is1
-
--gpu
: (optional) 'Train the model on gpu' this requires that you have a CUDA supported GPU!
The train.py
script requires you to:
- pass as a first argument the folder that 'contains' your images
- after that you can pass in any order the above arguments as well to fine tune the training process 😸
If you have CUDA compatible gpu
python train.py flowers --epochs=15 --gpu
Otherwise
python train.py flowers --epochs=15
This will start the training process for each epoch the tool will train the classifier and will evaluate the classifier accuracy.
When the training is completed the tool will save a checkpoint in checkpoints/alexnet_checkpoint.pth
We will need this for making predictions later!
Now it is time to use our classifier!
-
--category_names
: (optional) 'Path to the category names JSON, this is used to map category IDs to their labels' the default iscat_to_name.json
-
--top_k
: (optional) 'The number of top predictions to be displayed' the default is5
-
--hidden_units
: (optional) 'Set the number of hidden units in the classifier hidden layer' the default is1024
-
--gpu
: (optional) 'Make prediction using the gpu' this requires that you have a CUDA supported GPU!
The predict.py
script requires you to:
- pass as a first argument the path to your image
- pass as a seccond argument the checkpoint path to be used
- after that you can pass in any order the above arguments as well to fine tune the prediction process 😸
If you have CUDA compatible gpu
python predict.py flowers/test/10/image_07090.jpg checkpoints/alexnet_checkpoint.pth --gpu
Otherwise
python predict.py flowers/test/10/image_07090.jpg checkpoints/alexnet_checkpoint.pth
This will start the prediction process and you will get a list of the top predictions for the flowers/test/10/image_07090.jpg
!
You have trained an image classifier and used it to make predictions 👏 !!!