Yes, it's possible to use YOLOv5 to detect objects while excluding specific classes, such as 'person'. You can achieve this by specifying which classes you want the model to detect during inference. Below, I'll provide the full code using YOLOv5 in Google Colab with a T4 GPU, which is sufficient for this task. The setup and inference should only take a few minutes.
Contents:
- Setup YOLOv5 in Google Colab
- Load the YOLOv5 Model
- Exclude the 'person' Class During Inference
- Run Inference on an Image
- Process Explanation
- Apply to Other Images
- Estimate Time and GPU Requirements
1. Setup YOLOv5 in Google Colab
First, we'll clone the YOLOv5 repository and install the required dependencies.
# Clone the YOLOv5 repository
!git clone https://github.com/ultralytics/yolov5.git
%cd yolov5
# Install required packages
!pip install -r requirements.txt
Note: Running the above code will take a few minutes as it installs all necessary packages.
2. Load the YOLOv5 Model
We'll import necessary libraries and load the pre-trained YOLOv5 model.
import torch
from IPython.display import Image # For displaying images in the notebook
# Load the YOLOv5s model (small version)
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
3. Exclude the 'person' Class During Inference
YOLOv5 uses the COCO dataset labels by default, where 'person' is class index 0. To exclude it, we'll specify the classes we want to detect, omitting index 0.
# Get all class indices except for 'person' (index 0)
classes_to_detect = list(range(1, len(model.names)))
# For reference, print the class names
print("Classes to detect:", [model.names[i] for i in classes_to_detect])
4. Run Inference on an Image
Now, we'll run inference on an input image, excluding detections of persons.
# Download an example image that contains multiple objects including persons
!wget -O input.jpg https://ultralytics.com/images/zidane.jpg
# Run inference while excluding the 'person' class
results = model('input.jpg', classes=classes_to_detect)
# Save the results
results.save()
# Display the output image
Image(filename='runs/detect/exp/input.jpg', width=600)
Explanation:
classes=classes_to_detect: This parameter tells the model to detect only the specified classes.
- The results are saved in the
runs/detect/exp/ directory by default.
5. Process Explanation
How the Process Works:
- Setup Environment:
- Clone the YOLOv5 repository and install dependencies.
- Load Pre-trained Model:
- Use the
torch.hub.load function to load the model.
- Define Classes to Detect:
- Exclude the 'person' class by creating a list of class indices to detect.
- Run Inference:
- Use the
model object to perform inference on the input image, specifying the classes.
- Save and Display Results:
- The
results.save() method saves the annotated image.
- Display the image using IPython's
Image function.
6. Apply to Other Images
You can apply this process to any other image without redoing the setup.
# Replace 'your_image.jpg' with the path to your image
your_image_path = 'your_image.jpg'
# Run inference on your image
results = model(your_image_path, classes=classes_to_detect)
# Save and display results
results.save()
Image(filename=f'runs/detect/exp/{your_image_path.split("/")[-1]}', width=600)
Note: Ensure that your image is uploaded to the Colab environment or accessible via a URL.
7. Estimate Time and GPU Requirements
- Google Colab T4 GPU: The T4 GPU is sufficient for running YOLOv5 inference and even fine-tuning the model on small datasets.
- Setup Time: Installing dependencies and setting up the environment may take 3-5 minutes.
- Inference Time: Running inference on a single image takes a few seconds.
- Total Time: The entire process, from setup to inference, should take less than 10 minutes.
Scaling to Multiple Images:
- You can process multiple images by passing a list of image paths to the model.
- The processing time will increase linearly with the number of images.
Full Code Block:
Here's the complete code for your convenience.
# Clone the YOLOv5 repository
!git clone https://github.com/ultralytics/yolov5.git
%cd yolov5
# Install required packages
!pip install -r requirements.txt
# Import libraries
import torch
from IPython.display import Image # For displaying images in the notebook
# Load the YOLOv5s model (small version)
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
# Get all class indices except for 'person' (index 0)
classes_to_detect = list(range(1, len(model.names)))
# Print the class names to confirm
print("Classes to detect:", [model.names[i] for i in classes_to_detect])
# Download an example image
!wget -O input.jpg https://ultralytics.com/images/zidane.jpg
# Run inference excluding 'person' class
results = model('input.jpg', classes=classes_to_detect)
# Save and display results
results.save()
Image(filename='runs/detect/exp/input.jpg', width=600)
Conclusion:
By specifying the classes you want to detect during inference, you can effectively exclude certain classes like 'person' from detection using YOLOv5. This method doesn't require retraining the model, and once set up, you can apply it to any number of images without reconfiguring the model.
If you have any more questions or need further assistance, feel free to ask!