Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 of 2 tasks
zqstdy opened this issue May 4, 2024 · 9 comments
Closed
1 of 2 tasks

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

zqstdy opened this issue May 4, 2024 · 9 comments
Labels
bug Something isn't working Stale

Comments

@zqstdy
Copy link

zqstdy commented May 4, 2024

Search before asking

  • I have searched the YOLOv8 issues and found no similar bug report.

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?

  • Yes I'd like to help by submitting a PR!
@zqstdy zqstdy added the bug Something isn't working label May 4, 2024
Copy link

github-actions bot commented May 4, 2024

👋 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
Copy link
Member

@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
Copy link
Author

zqstdy commented May 4, 2024

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
Copy link
Member

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
Copy link
Author

zqstdy commented May 5, 2024

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
Copy link
Member

@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
Copy link
Author

zqstdy commented May 5, 2024

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
Copy link
Member

@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. 😊

Copy link

github-actions bot commented Jun 6, 2024

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐

@github-actions github-actions bot added the Stale label Jun 6, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale
Projects
None yet
Development

No branches or pull requests

2 participants