ultralytics / ultralytics

NEW - YOLOv8 πŸš€ in PyTorch > ONNX > OpenVINO > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
23.76k stars 4.74k forks source link

Error reported when setting model single channel ch: 1 #11247

Open zqstdy opened 2 weeks ago

zqstdy commented 2 weeks ago

Search before asking

YOLOv8 Component

Train

Bug

image

image The model supports channel settings, but single training reports errors. I think this is a bug or an area that needs improvement

8.2.6

Environment

8.2.6

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

github-actions[bot] commented 2 weeks ago

πŸ‘‹ Hello @zqstdy, thank you for your interest in Ultralytics YOLOv8 πŸš€! We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

If this is a πŸ› Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 2 weeks ago

@zqstdy hello! Thanks for reaching out and detailing the issue you encountered with the single-channel input setup. From what you have described and the screenshots, it seems like there might be something specific about the configuration causing this problem.

Could you please share the configuration snippet you used, especially around input resolutions and channel settings? Also, enabling channels for one might require checking the compatibility of subsequent layers or operations specific to single-channel data. Here's a brief example of how you set the channels to 1 in your data configuration file:

# Inside the YAML data configuration file
nc: 1  # number of channels

Furthermore, ensure your preprocessing steps, if any, are converting images to grayscale correctly (assuming you are using grayscale images for a 1-channel model). For now, verifying your data preprocessing steps and the configuration snippet would be helpful to further diagnose this.

Looking forward to your response!

zqstdy commented 2 weeks ago

A grayscale image with a size of 640 * 640 image Then I want to train the model as a single channel grayscale image, rather than a three channel BGR Can we currently achieve such a demand?

glenn-jocher commented 2 weeks ago

Hello! Yes, you can train a YOLOv8 model with single-channel grayscale images. Make sure your dataset images are properly converted to grayscale and that your model configuration is set to handle one channel.

In your data.yaml, set the number of channels like this:

nc: 1  # number of channels

And ensure your preprocessing converts images to grayscale. If using custom data loading or preprocessing scripts, here’s a simple way to convert an image to grayscale with OpenCV:

import cv2
image = cv2.imread('path_to_image')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

This should help you set up training for single-channel images! Let us know if you have more questions. 😊

zqstdy commented 2 weeks ago

First of all, thank you for your reply nc: 1 As far as I know, the number of categories is set, and the number of channels cannot be changed

image

ch: 1 The number of channels can be changed, but training will report an error

image

I hope to use single channel training to improve the inference efficiency of the model

You can do some related testing, but I haven't found any code for single channel processing in the source code yet

glenn-jocher commented 1 week ago

@zqstdy hello! Thank you for the additional information and clarification. Indeed, training YOLOv8 with single-channel images involves some specific setup. The nc: 1 property you've referenced is for adjusting the number of classes, not channels.

To configure your model for one-channel (grayscale) input, you'll need to modify the input layer of your network model to accept one channel. Although the public YOLOv8 repository does not support single-channel training directly through configuration only, this can technically be achieved by altering the model's architecture and preprocessing pipeline.

For the time being, to prepare your images as single-channel inputs, you can adjust your data loading or transformation stages to convert images to grayscale before feeding them into the network. Here's an example of how you might adjust a typical image loading function in PyTorch for grayscale:

from PIL import Image
import torchvision.transforms as transforms

def load_grayscale_image(image_path):
    with Image.open(image_path).convert('L') as img:  # Convert to grayscale
        transform = transforms.Compose([
            transforms.Resize((640, 640)),  # Resize to model input dimensions
            transforms.ToTensor()  # Convert image to tensor
        ])
        return transform(img).unsqueeze(0)  # Add batch dimension

And remember that for modifying the model to accept a single channel across all layers, you may need to dive into the model's definition and adjust the first convolutional layer's input channel parameter.

Unfortunately, if the current YOLOv8 setup or your project constraints don't allow architectural changes, you might have to explore other solutions or models that nativally support single-channel inputs. If you need further assistance adjusting the model, please let me know! 😊

zqstdy commented 1 week ago

I made the changes according to the method on my blog, but it still reported an error and I don't know where it's different https://blog.csdn.net/m0_56276747/article/details/136757921

glenn-jocher commented 1 week ago

@zqstdy hello! Thanks for reaching out and sharing the link to your blog post. It looks like there might be a small discrepancy in your configuration or setup compared to what YOLOv8 expects. Could you please share the specific error message you're seeing? Also, if possible, include a snippet of the code or configuration you changed. This will help identify what might be going wrong more accurately. 😊