GithubHelp home page GithubHelp logo

Comments (12)

sergenerbay avatar sergenerbay commented on July 19, 2024 1

Hello,
If you are asking how I solved the first problem.
[-1,1, CBAM, [1024]]

Because I used the CBAM module in my structure, I defined the kernel_size inside it and solved the first problem.

class CBAM(nn.Module):
    """Convolutional Block Attention Module."""

    def __init__(self, c1, kernel_size=7):
        """Initialize CBAM with given input channel (c1) and kernel size."""
        super().__init__()
        self.channel_attention = ChannelAttention(c1)
        **kernel_size=7**
        self.spatial_attention = SpatialAttention(kernel_size)

    def forward(self, x):
        """Applies the forward pass through C1 module."""
        return self.spatial_attention(self.channel_attention(x))

from ultralytics.

glenn-jocher avatar glenn-jocher commented on July 19, 2024 1

Hello @sergenerbay,

Thank you for sharing your solution! It's great to see that you were able to resolve the issue by defining the kernel_size inside the CBAM module. This is indeed a crucial step to ensure the module works correctly within the YOLOv8 architecture.

For others encountering similar issues, here's a quick summary of the solution:

  1. Define the Kernel Size: Ensure that the kernel_size is defined within the CBAM class.
  2. Modify the YAML Configuration: Make sure your YAML configuration correctly references the CBAM module.

Here's the updated CBAM class for reference:

class CBAM(nn.Module):
    """Convolutional Block Attention Module."""

    def __init__(self, c1, kernel_size=7):
        """Initialize CBAM with given input channel (c1) and kernel size."""
        super().__init__()
        self.channel_attention = ChannelAttention(c1)
        self.spatial_attention = SpatialAttention(kernel_size)

    def forward(self, x):
        """Applies the forward pass through C1 module."""
        return self.spatial_attention(self.channel_attention(x))

Additionally, ensure your YAML configuration includes the CBAM module correctly:

# YOLOv8.0n backbone
backbone:
  # [from, repeats, module, args]
  - [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
  - [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
  - [-1, 3, C2f, [128, True]]
  - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
  - [-1, 6, C2f, [256, True]]
  - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
  - [-1, 6, C2f, [512, True]]
  - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
  - [-1, 3, C2f, [1024, True]]
  - [-1, 1, CBAM, [1024]]
  - [-1, 1, SPPF, [1024, 5]] # 9

If anyone else is facing similar issues, please ensure you are using the latest versions of torch and ultralytics. If the problem persists, providing a minimum reproducible example would be very helpful for further investigation. You can find more details on creating a reproducible example here.

Feel free to reach out if you have any more questions or need further assistance. Happy coding! 🚀

from ultralytics.

github-actions avatar github-actions commented on July 19, 2024

👋 Hello @sergenerbay, 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.

from ultralytics.

glenn-jocher avatar glenn-jocher commented on July 19, 2024

@sergenerbay hello,

Thank you for reaching out and providing detailed information about your issue with integrating the CBAM module into the YOLOv8 configuration. It seems like there might be a compatibility or configuration issue with how the CBAM module is integrated into your model architecture.

To better assist you, could you please provide the specific error message or behavior you're encountering when you add the CBAM module? Knowing the exact error will help in diagnosing the issue more effectively.

In the meantime, ensure that all module dependencies for CBAM are correctly installed and that the module's input and output dimensions align with your model's architecture at the point of integration.

Looking forward to your response to assist you further!

from ultralytics.

sergenerbay avatar sergenerbay commented on July 19, 2024

Hello @glenn-jocher
I solved the kernel size problem, but ı getting other problem.
Also, I using CBAM module in ultralytics library.

conv.py
class ChannelAttention(nn.Module):
    """Channel-attention module https://github.com/open-mmlab/mmdetection/tree/v3.0.0rc1/configs/rtmdet."""

    def __init__(self, channels: int) -> None:
        """Initializes the class and sets the basic configurations and instance variables required."""
        super().__init__()
        self.pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Conv2d(channels, channels, 1, 1, 0, bias=True)
        self.act = nn.Sigmoid()

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Applies forward pass using activation on convolutions of the input, optionally using batch normalization."""
        return x * self.act(self.fc(self.pool(x)))


class SpatialAttention(nn.Module):
    """Spatial-attention module."""

    def __init__(self, kernel_size=7):
        """Initialize Spatial-attention module with kernel size argument."""
        super().__init__()
        print(kernel_size)
        assert kernel_size in {3, 7}, "kernel size must be 3 or 7"
        padding = 3 if kernel_size == 7 else 1
        self.cv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False)
        self.act = nn.Sigmoid()

    def forward(self, x):
        """Apply channel and spatial attention on input for feature recalibration."""
        return x * self.act(self.cv1(torch.cat([torch.mean(x, 1, keepdim=True), torch.max(x, 1, keepdim=True)[0]], 1)))


class CBAM(nn.Module):
    """Convolutional Block Attention Module."""

    def __init__(self, c1, kernel_size=7):
        """Initialize CBAM with given input channel (c1) and kernel size."""
        super().__init__()
        self.channel_attention = ChannelAttention(c1)
        kernel_size=7
        self.spatial_attention = SpatialAttention(kernel_size)

    def forward(self, x):
        """Applies the forward pass through C1 module."""
        return self.spatial_attention(self.channel_attention(x))

I am getting the problem below.

Traceback (most recent call last):
File "/home/sergen/v10/denemeee.py", line 2, in
model = YOLO("yolov8m-cbam.yaml")
File "/home/sergen/.local/lib/python3.10/site-packages/ultralytics/models/yolo/model.py", line 23, in init
super().init(model=model, task=task, verbose=verbose)
File "/home/sergen/.local/lib/python3.10/site-packages/ultralytics/engine/model.py", line 150, in init
self._new(model, task=task, verbose=verbose)
File "/home/sergen/.local/lib/python3.10/site-packages/ultralytics/engine/model.py", line 219, in _new
self.model = (model or self._smart_load("model"))(cfg_dict, verbose=verbose and RANK == -1) # build model
File "/home/sergen/.local/lib/python3.10/site-packages/ultralytics/nn/tasks.py", line 288, in init
self.model, self.save = parse_model(deepcopy(self.yaml), ch=ch, verbose=verbose) # model, savelist
File "/home/sergen/.local/lib/python3.10/site-packages/ultralytics/nn/tasks.py", line 921, in parse_model
args.append([ch[x] for x in f])
File "/home/sergen/.local/lib/python3.10/site-packages/ultralytics/nn/tasks.py", line 921, in
args.append([ch[x] for x in f])
IndexError: list index out of range

from ultralytics.

sergenerbay avatar sergenerbay commented on July 19, 2024

I solved my problem :))I had to make the code change carefully. The problem was in the YAML file; I did not set compatible values in the detection layer.

from ultralytics.

glenn-jocher avatar glenn-jocher commented on July 19, 2024

Hello @sergenerbay,

Great to hear that you've resolved the issue! 🎉 Careful adjustments in the YAML configuration can indeed make a significant difference. If you have any more questions or run into further issues, feel free to reach out. Happy coding with YOLOv8!

from ultralytics.

sergenerbay avatar sergenerbay commented on July 19, 2024

I actually want to input two images (thermal and RGB) in the next step. I want to pass each of these inputs through separate backbones and then fuse the information. Is this possible?

from ultralytics.

glenn-jocher avatar glenn-jocher commented on July 19, 2024

Hello,

Yes, it's definitely possible to input two different types of images (thermal and RGB) and process them through separate backbones before fusing the information. You'll need to modify your model architecture to handle dual inputs and ensure that the features extracted from both backbones are compatible for fusion.

You might consider using a two-stream network where each stream processes one type of input. Afterward, you can merge these streams using various fusion techniques such as concatenation, element-wise addition, or more complex operations depending on your application's requirements.

If you need specific guidance on how to implement this in YOLOv8, feel free to ask!

from ultralytics.

kashinou avatar kashinou commented on July 19, 2024

@sergenerbay

Hello,

I have encountered the same error and have not been able to resolve it. So I would like to know what changes you have made to solve the problem.

from ultralytics.

kashinou avatar kashinou commented on July 19, 2024

Thank you for comments.

I solved the problem by defining the kernel size in the structure as you have shown me. Thank you very much.

from ultralytics.

glenn-jocher avatar glenn-jocher commented on July 19, 2024

Hello @kashinou,

I'm glad to hear that you were able to resolve the issue by defining the kernel size in the CBAM structure! 🎉 It's always rewarding to see solutions come together.

If you have any further questions or run into any other issues, feel free to reach out. We're here to help! Also, if you have any interesting results or insights from your work with YOLOv8 and CBAM, we'd love to hear about them. Sharing your experiences can be incredibly valuable to the community.

Happy coding and best of luck with your project! 🚀

from ultralytics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.