This project demonstrates how to use the YOLOv10 model for brain tumor detection using MRI images. The model has been trained and fine-tuned on a dataset to detect brain tumors efficiently.
This project uses YOLOv10 for detecting brain tumors from MRI scans. We employ Roboflow for dataset management and Gradio for creating a web-based interface.
- YOLOv10
- Roboflow
- Gradio
- Matplotlib
- OpenCV
-
Install YOLOv10 from the official GitHub repository:
!pip install -q git+https://github.com/THU-MIG/yolov10.git
-
Download the pre-trained YOLOv10n model weights:
!wget -P -q https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10n.pt
-
Install Roboflow for dataset handling:
!pip install -q roboflow
-
Install and set up Roboflow to manage and download the dataset:
from roboflow import Roboflow rf = Roboflow(api_key="2A2n8FdjhCq768GBy1Yw") project = rf.workspace("brain-mri").project("mri-rskcu") version = project.version(3) dataset = version.download("yolov9")
-
Start training the YOLOv10 model with the following command. You can modify parameters such as
epochs
,batch
, andmodel
:!yolo task=detect mode=train epochs=25 batch=32 plots=True \ model=/content/yolov10n.pt \ data=/content/MRI-3/data.yaml
-
Load the trained model:
from ultralytics import YOLOv10 model_path = "/content/runs/detect/train9/weights/best.pt" model = YOLOv10(model_path)
-
Run inference on the dataset with a confidence threshold:
result = model(source="/content/MRI-3/train/images", conf=0.25, save=True)
-
Visualize predictions from multiple images using Matplotlib:
import glob import matplotlib.pyplot as plt import matplotlib.image as mpimg images = glob.glob("/content/runs/detect/predict/*.jpg") images_to_display = images[:10] fig, axes = plt.subplots(2, 5, figsize=(20, 10)) for i, ax in enumerate(axes.flat): if i < len(images_to_display): img = mpimg.imread(images_to_display[i]) ax.imshow(img) ax.axis('off') else: ax.axis('off') plt.tight_layout() plt.show()
-
Visualize predictions on a single image:
result = model.predict(source="/content/MRI-3/valid/images/Tr-gl_0228_jpg.rf.b9ecef834d39f770e41b0585b63bdc1a.jpg", imgsz=640, conf=0.25) annotated_img = result[0].plot() annotated_img[:, :, ::-1]
-
Install Gradio:
!pip install gradio
-
Define the
predict()
function and launch the app:import gradio as gr import cv2 import numpy as np def predict(image): result = model.predict(source=image, imgsz=640, conf=0.25) annotated_img = result[0].plot() annotated_img = annotated_img[:, :, ::-1] return annotated_img app = gr.Interface( fn=predict, inputs=gr.Image(type="numpy", label="Upload an image"), outputs=gr.Image(type="numpy", label="Detect Brain Tumor"), title="Brain Tumor Detection Using YOLOv10", description="Upload an image and the YOLOv10 model will detect and annotate the brain tumor." ) app.launch()
This project is licensed under the MIT License.
Feel free to contribute or suggest improvements!
Note: Replace the images/
paths with the actual paths to the images used in your project. If you don't have these images, you might need to create or capture them based on your project's outputs.