This repository contains the implementation of the YOLOv8 model for object detection as part of the CerviCure project. The model is trained to detect and classify objects in cervical cancer-related datasets. This README provides a step-by-step guide for setting up, training, and running the YOLOv8 model using Google Colab.
- Installation
- Dataset Preparation
- Training the Model
- Validating the Model
- Running Predictions
- Generating Classes Code
- Results Visualization
- Acknowledgements
- Clone the repository:
git clone https://github.com/ArnavGhosh999/CerviCure.git
- Install the required dependencies:
pip install ultralytics
- Check the environment setup for YOLOv8:
import ultralytics ultralytics.checks()
- Mount your Google Drive in Colab to access the dataset:
from google.colab import drive drive.mount('/content/drive')
- Navigate to the project directory:
%cd /content/drive/MyDrive/CerviCure.v2i.yolov8
- Update the
data.yaml
file with the paths to your training and validation datasets.
- Load the YOLOv8 model:
from ultralytics import YOLO model = YOLO('yolov8m.pt')
- Fine-tune the model's training layers:
layer_index = 0 for param in model.parameters(): param.requires_grad = (layer_index == 1) layer_index += 1
- Train the model:
model.train( data='data.yaml', epochs=20, batch=16, imgsz=640, optimizer='Adam', lr0=0.001, weight_decay=0.0005 )
Run model validation to assess performance:
results = model.val()
print(results)
- Make predictions using a trained model:
!yolo task=detect mode=predict model=/content/drive/MyDrive/CerviCure.v2i.yolov8/runs/detect/train8/weights/best.pt source=/content/drive/MyDrive/Cervix save=True
- Use the
predict_and_annotate
function to run predictions on new images and visualize the results:import cv2 import matplotlib.pyplot as plt annotated_image = predict_and_annotate('path_to_image.jpg') plt.imshow(cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)) plt.show()
- Generate Python code for class definitions based on the
names
field in thedata.yaml
file:def generate_classes_code(data_yaml_path): # Function implementation
- Call the function to get class definitions:
data_yaml_path = '/content/drive/MyDrive/CerviCure.v2i.yolov8/data.yaml' classes_code = generate_classes_code(data_yaml_path) print(classes_code)
- Upload images and annotate them using the trained model:
from google.colab import files uploaded = files.upload()
- Display the annotated images:
for filename in uploaded.keys(): annotated_image = predict_and_annotate(filename) if annotated_image is not None: plt.figure(figsize=(12, 8)) plt.imshow(cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)) plt.axis('off') plt.show() else: print(f"Failed to process image: {filename})
- The YOLOv8 model is provided by the Ultralytics team.
- The CerviCure project aims to assist in the detection and classification of cervical cancer-related abnormalities using deep learning techniques.