Welcome to the world of object detection! In this blog post, we’ll explore how you can harness the power of YOLO (You Only Look Once) v4 and v3 to create a CPU-based inference API. This technology will allow you to detect objects without needing a GPU, making it accessible for everyone, whether you’re using Windows or Linux. Ready to get started? Let’s go!
What You Need to Get Started
Before diving in, make sure you have the following prerequisites:
- Operating Systems:
- Ubuntu 16.04 or 18.04
- Windows 10 Pro/Enterprise
- Docker Installed:
- Check your Docker installation using the command: docker –version
Installing Docker
To install Docker, follow these steps:
For Ubuntu Users
chmod +x install_prerequisites.sh
source install_prerequisites.sh
For Windows Users
Follow the link to install Docker on Windows. Note: Open the Docker Desktop menu and adjust resources under Settings > Advanced.
Building the Docker Image
Now it’s time to build your project. From the project’s root directory, run the following command:
sudo docker build -t yolov4_inference_api_cpu -f .docker/Dockerfile .
Behind a Proxy
If you’re behind a proxy, use this command:
sudo docker build --build-arg http_proxy= --build-arg https_proxy= -t yolov4_inference_api_cpu -f .docker/Dockerfile .
Running the Docker Container
You can deploy the API using either Docker or Docker Swarm. If you’re simply running the API, use Docker without Swarm.
Using Docker
Run the API with this command:
sudo docker run -it -v $(pwd)/models:/models -v $(pwd)/models_hash:/models_hash -p :7770 yolov4_inference_api_cpu
Using Docker Swarm
Refer to the Docker Swarm documentation for commands.
Understanding the API Endpoints
Your API comes with various endpoints, but let’s visualize it first with an analogy:
Imagine you have a restaurant where customers can order different types of dishes. Each dish represents a specific function of the API.
- **Load (GET)**: This is like the menu where customers see all the available dishes (models) along with their ingredients (hashed values).
- **Detect (POST)**: When a customer places an order for a specific dish (requests inference), they receive their meal with all the details (bounding boxes) about what they ordered.
- **Get Labels (POST)**: This function is akin to providing customers with a list of ingredients (model labels) that make up a dish.
Endpoints Summary
You can explore the API through the following endpoints:
- **load (GET)**: Loads all models.
- **detect (POST)**: Performs inference on a specified model.
- **get_labels (POST)**: Returns labels of the specified model.
Configuring Your Models
The models folder contains subfolders for each model. Inside each folder, you need:
- Cfg file
- Weights file
- Names file
- Config.json
Customize the configuration according to your needs. For example, you can choose between yolov4_opencv_cpu_detection and yolov3_opencv_cpu_detection.
Troubleshooting
If you run into any issues while setting up or running your API, here are some tips:
- Double-check if Docker is installed correctly by running docker –version.
- Ensure that the appropriate ports are open and not already in use.
- Consult the logs for any error messages by running docker logs
. - If you’re having difficulty with image predictions, verify that your model files are correctly placed in their respective folders.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.