This repository provides a simple and efficient way to train and deploy image classification models using ResNet and MobileNet architectures.
- Introduction
- Requirements
- Usage
- Model Architecture
- Preprocessing
- Training
- Evaluation
- Deployment
- Agents
- Functions
- Orchestrator
- Server
- Socket.IO Singleton
- Download Images
- Fix Image Issues
- index.html
- Contributing
- License
This repository provides a basic implementation of image classification using ResNet and MobileNet architectures. The code is designed to be easy to use and modify, making it a great starting point for your own image classification projects.
- Model Selection: The code allows users to choose between MobileNetV2 and ResNet50 as the base model.
- Custom Preprocessing Layer: A custom preprocessing layer is implemented to handle image preprocessing for both MobileNetV2 and ResNet50.GPU
- Optimization: The code is optimized for GPU usage with shared memory optimization and auto-clustering enabled.
- XLA Compilation: The code uses XLA (Accelerated Linear Algebra) compilation to optimize computations.
- K-Fold Cross-Validation: The code implements K-Fold cross-validation to evaluate the model's performance.
- Data Augmentation: The code applies data augmentation techniques to the training data.
- Model Evaluation: The code evaluates the model's performance using metrics such as accuracy, precision, and recall.
- GPU Performance Benchmarking: The code includes a benchmarking function to measure the performance of the GPU with different batch sizes.
- Batch Size Optimization: The code adjusts the batch size based on the GPU's performance to optimize shared memory utilization.
- Speedup Calculation: The code calculates the speedup achieved by using batch processing with different batch sizes.
- Custom Preprocessing Layer: A custom preprocessing layer to handle image preprocessing
- Global Average Pooling Layer: A global average pooling layer to reduce the spatial dimensions of the feature maps
- Dense Layers: One or more dense layers with ReLU activation and dropout
- Final Classification Layer: A final classification layer with softmax activation
- Python 3.8+
- TensorFlow 2.4+
- Keras 2.4+
- NumPy 1.20+
- Matplotlib 3.4+
- SocketIO
- Flask
- DuckDuckGo Search
You can install the required packages using conda or pip, or both (after you clone the repository):
conda create --name --file package-list.txt
pip install -r requirements.txt
git clone https://github.com/your-username/image-classification.git Install the required packages:
conda [create --name ] --file package-list.txt
and/or
pip install -r requirements.txt
- If not using the provided data directory, create a directory that has 2 subfolders: book and vinyl
- Depending on the Usage path, modify the referenced source file(s) to point to the directory where the book and vinyl subfolders are located
- Open the test_orchestrator.py source file
- Modify the configuration values
- Save and close the source file
- python test_orchestrator.py
- python download_images.py (if you want to create your own image set)
- python fix_images_issues.py
- python train.py --model resnet --dir_data data [if using the provided images] (or path/to/parentfolder/of/book/and/vinyl if you downloaded images)
- python app.py
- Open the link to the URL display
- Modify any settings (or keep the default values)
- Make sure all checkboxes are checked
The code provides two model architectures:
ResNet: A pre-trained ResNet model with a custom classification head. MobileNet: A pre-trained MobileNet model with a custom classification head.
The code applies the following preprocessing techniques:
ResNet: Subtracts the ImageNet mean values [123.68, 116.779, 103.939].
MobileNet: Subtracts 127.5 and divides by 127.5.
The code trains the models using the Adam optimizer and categorical cross-entropy loss.
The code evaluates the models using accuracy, precision, and recall metrics.
The code provides a simple way to deploy the trained models using TensorFlow Serving.
To deploy the model, follow these steps:
- Save the model: Save the trained model using orchestrator.save_model().
- Create a TensorFlow Serving instance: Create a TensorFlow Serving instance using the tensorflow_serving package.
- Configure the model: Configure the model to use the saved model and specify the input and output tensors.
- Start the TensorFlow Serving instance: Start the TensorFlow Serving instance using tensorflow_serving.serve().
- Test the deployment: Test the deployment by sending a request to the TensorFlow Serving instance using a tool like curl.
import tensorflow_serving
orchestrator.save_model()
serving_config = tensorflow_serving.ServingConfig( model_config_list=[ tensorflow_serving.ModelConfig( name='image-classification', base_path='/path/to/model' ) ] )
tensorflow_serving.serve(serving_config)
The code provides six agents:
- Agent 1: Download images
- Agent 2: Preprocess images
- Agent 3: Train model
- Agent 4: Predict images
- Agent 5: Create inventory
Agent 1: Download Images
download_images(self, search_query, output_dir) Downloads images for a given search query into a specified output directory.
preprocess_images(self, directory) Validates images in a given directory.
train_model(self, train_dir, model_name, socketio='http://127.0.0.1:5000', folds=3, layers=5, epocs=25, confidence=0.75) Trains a model with data from a given directory.
predict_images(self, image_dir, model_name, confidence, socketio='http://127.0.0.1:5000') Predicts images in a given directory using a trained model.
create_inventory(self, image_dir, output_inventory_file) Creates an inventory from images in a given directory.
The Orchestrator class is responsible for orchestrating the pipeline of agents.
- orchestrate_pipeline(self): Orchestrates the pipeline of agents.
- get_training_results(self): Loads the training results from a file.
- get_prediction_results(self): Loads the prediction results from a file.
The server is a Flask application that provides APIs for submitting jobs and handling socket.io events.
- /submit: Submits a job with the given configuration.
- /api/images: Returns a list of image files in the plots directory.
- /images/: Serves an image file from the plots directory.
- /api/models: Returns a list of the best models trained during each run.
- /api/model-info: Returns a model summary and details of each layer in the model.
The socket_io_singleton.py script provides a singleton Socket.IO instance.
Contributions are welcome! If you'd like to contribute to this repository, please:
- Fork the repository.
- Make your changes.
- Create a pull request.
This repository is licensed under the MIT License. See LICENSE for details.

