This repository extends the functionalities of the ultralytics library by adding support for grayscale images. This functionality has been tested for detection and classification only. Its use for pose and segmentation haven't been tested yet lol.
To use grayscale image functionality in YOLO, replace the default ultralytics package with the modified version from this repository.
Using CLI
Go to the python packages or anaconda env libraries:
cd /path/to/python/packagesRemove the ultralytics package and clone this repo:
rm -r ultralytics
git clone git@github.com:Mauricio-Gonzalez-Ortiz/ultralytics.gitExtract the ultralytics folder:
find "ultralytics" -mindepth 1 -path "ultralytics/ultralytics" -prune -o -exec rm -rf {} +
mv ultralytics/ultralytics/* ultralytics/
rm -r ultralytics/ultralyticsEditing your own package
Follow the steps in this medium article
To train a model in python you just need to specify a configuration flag for the number of channels "ch=1"
Python Example
from ultralytics import YOLO
model = YOLO("yolov8.pt")
model.train(data="yaml_file.yaml", ..., ch=1)CLI Example
yolo detect/classify train data=yaml_file_path/folder_path ... ch=1For prediction, it is recommended to specify the same "ch=1" flag
Python Example
from ultralytics import YOLO
import cv2
model = YOLO("path/to/your/model.pt")
img = cv2.imread("path/to/your/image", cv2.IMREAD_GRAYSCALE)
results = model(img, ch=1)CLI Example
Currently, prediction on grayscale images using the CLI doesn't work.
These changes also allow the user to export a model in a different format in grayscale:
CLI Example
yolo export model="/path/to/your/model.pt" format="engine" ch=1 If any problems arise when loading a different model type, specify the type of task
from ultralytics import YOLO
import cv2
model = YOLO("path/to/your/model.engine", task="classify/detect")
img = cv2.imread("path/to/your/image", cv2.IMREAD_GRAYSCALE)
results = model(img, ch=1)