Skip to content

Image Classifier project implemented using TensorFlow, running with GPU access in a Docker container.

Notifications You must be signed in to change notification settings

edineisantos/Image_Classifier_with_TensorFlow

Repository files navigation

Image Classifier - TensorFlow Project

Project code for Udacity's Intro to Machine Learning with TensorFlow Nanodegree program.

Project Description

This project follows a structured approach to image classification:

  1. Data Loading: Start by importing the image dataset and setting up a processing pipeline.
  2. Model Creation: Construct and train an image classifier using the dataset.
  3. Prediction: Use the trained model to identify flower types from images.

All steps are implemented in Python, providing a clear workflow.

Objectives

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.

Files and Data Description

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.

Folders in the Root Directory:

  • 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.

Dataset

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.

Flower Classifier Predictor: predict.py

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.

Parameters:

  1. image_path (Required):

    • Type: String
    • Description: Specifies the path to the input image that needs to be classified.
  2. model (Required):

    • Type: String
    • Description: Indicates the path to the saved Keras model that will be used for prediction.
  3. --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.
  4. --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.

Usage:

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.

Running the Project

This section provides guidelines for building and running the Docker image, as well as for executing the project's Python scripts.

Building and Running the Docker Image with GPU Access

  1. Preparation: Open your terminal and navigate to the project's root directory, where the Dockerfile is located.

  2. Build the Docker Image:

    docker build -t image_classifier_with_tensorflow .
  3. GPU Configuration: Before running the Docker container with GPU access, ensure that you have the NVIDIA Container Toolkit installed.

  4. 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.

Running Jupyter Notebook

  1. 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.

  2. Navigate to the Notebook: In the Jupyter interface, locate and open the Project_Image_Classifier_Project.ipynb notebook to begin working.

  3. 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.

Running Python Scripts

predict.py

To run the predict.py script:

  1. Navigate to the project's root directory in your terminal.

  2. Execute the script with the following command:

    python predict.py ./test_images/cautleya_spicata.jpg image_classifier_model.h5 --category_names label_map.json

test_predict.py

From the project's root directory:

  1. 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.

About

Image Classifier project implemented using TensorFlow, running with GPU access in a Docker container.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published