Project code for Udacity's Intro to Machine Learning with TensorFlow Nanodegree program.
This project follows a structured approach to image classification:
- Data Loading: Start by importing the image dataset and setting up a processing pipeline.
- Model Creation: Construct and train an image classifier using the dataset.
- Prediction: Use the trained model to identify flower types from images.
All steps are implemented in Python, providing a clear workflow.
This project aims to showcase the following competencies:
- Training, saving, and loading models using Keras and TensorFlow.
- Developing a script that allows predicting the class of an image using the command line.
- Implementing and executing unit tests.
An overview of the folders, files, and data present in the root directory.
- .gitignore: Specifies intentionally untracked files that Git should ignore.
- Dockerfile: Contains instructions for building the Docker image required to run the project.
- Project_Image_Classifier_Project.html: A notebook rendered in HTML where the model in
predict.py
was developed. - Project_Image_Classifier_Project.ipynb: A Jupyter notebook where the model in
predict.py
was developed. - README.md: Provides a project overview and setup instructions.
- image_classifier_model.h5: The saved model file for the image classifier.
- label_map_json: A JSON file mapping labels to their respective classes.
- predict.py: A module to predict the class of an image.
- requirements.txt: A list of dependencies and libraries required to run the project.
- test_predict.py: A module designed to test the functionality in
predict.py
.
- assets: Images used in the Jupyter notebook "Project_Image_Classifier_Project".
- test_images: Images utilized for testing the
predict.py
module. - logs: Logs generated by the
test_predict.py
.
The "oxford_flowers102" dataset, provided by the University of Oxford, contains 102 categories of flowers commonly found in the United Kingdom. Each category consists of between 40 and 258 images, with a total of 8,189 images. The images have large scale, pose, and light variations, making it suitable for flower classification tasks.
The predict.py
script serves as a command-line utility to classify flower images using a trained model. It takes in an image path, the path to a trained Keras model, and other optional parameters to provide the top K
predictions for the flower class. The script outputs the model summary, the probabilities, and the predicted classes for the input image.
-
image_path
(Required):- Type: String
- Description: Specifies the path to the input image that needs to be classified.
-
model
(Required):- Type: String
- Description: Indicates the path to the saved Keras model that will be used for prediction.
-
--top_k
(Optional):- Type: Integer
- Default: 5
- Description: Denotes the number of top predictions to return. For example, if
top_k
is set to 3, the script will return the top 3 predictions for the input image.
-
--category_names
(Optional):- Type: String
- Default: 'label_map.json'
- Description: Path to a JSON file that maps labels to flower names. This allows the script to display the flower name alongside its predicted class.
To execute the script with default parameters, use:
python predict.py [path_to_image] [path_to_model]
For a more customized execution, consider:
python predict.py [path_to_image] [path_to_model] --top_k [desired_top_k] --category_names [path_to_label_json]
Output:
Upon successful execution, the script will display:
- Parameters utilized.
- Summary of the loaded model.
- Top predicted probabilities and their corresponding classes for the input image.
This section provides guidelines for building and running the Docker image, as well as for executing the project's Python scripts.
-
Preparation: Open your terminal and navigate to the project's root directory, where the
Dockerfile
is located. -
Build the Docker Image:
docker build -t image_classifier_with_tensorflow .
-
GPU Configuration: Before running the Docker container with GPU access, ensure that you have the NVIDIA Container Toolkit installed.
-
Run the Docker Container: To start a container with GPU access, use the following command:
docker run --gpus all -it --name image_classifier_container --rm -p 8888:8888 image_classifier_with_tensorflow
Note: The
--name
flag assigns a name to the running container,image_classifier_container
in this case. The--gpus all
flag grants the container access to your GPU. The container initiates a Jupyter server upon startup.
-
Access the Jupyter Interface: After running the container, it will display a URL in the terminal. This URL looks like:
http://127.0.0.1:8888/?token=your_unique_token
Copy and paste this URL into your web browser to access the Jupyter Notebook interface.
-
Navigate to the Notebook: In the Jupyter interface, locate and open the
Project_Image_Classifier_Project.ipynb
notebook to begin working. -
Shutdown: Once finished, shut down the Jupyter server either by clicking the 'Shutdown' button in the Jupyter interface or by pressing
CTRL+C
in the terminal where the Docker container is running.
To run the predict.py
script:
-
Navigate to the project's root directory in your terminal.
-
Execute the script with the following command:
python predict.py ./test_images/cautleya_spicata.jpg image_classifier_model.h5 --category_names label_map.json
From the project's root directory:
- Run the
test_predict.py
script with:python test_predict.py
By following these instructions, you can build the Docker container with GPU support, run Jupyter Notebook, and execute both the predict.py
and test_predict.py
scripts.