Closed zeinabelsharkawy closed 11 months ago
👋 Hello @zeinabelsharkawy, thank you for your interest in YOLOv8 🚀! We recommend a visit to the YOLOv8 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.
Pip install the ultralytics
package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.
pip install ultralytics
YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
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.
@zeinabelsharkawy i apologize for the inconvenience. The error message you're seeing shows that there is a KeyError: 'CBAM'
, which typically means Python is unable to recognize 'CBAM' in your script. As of now, CBAM (Convolutional Block Attention Module) is not a built-in module in the YOLOv8 implementation. Therefore, to utilize CBAM, you may need to implement this attention mechanism manually within the model architecture.
Make sure you have correctly defined the CBAM module and integrated it correctly within the YOLOv8 model. I recommend checking all relevant code related to CBAM in your script and ensure there are no typographical errors.
Remember to follow Python programming guidelines for defining new classes and functions, and ensure all necessary dependencies for CBAM are installed and properly imported.
I hope these suggestions can help resolve your issue. If the problem persists, please provide more context or code snippets for us to give you more specific guidance. Thank you for your understanding and patience with this process.
thank you for your response. first I check convy.py to add CBAM and add CBAM in task.py then modify yolov8.yaml
after that I run yolo task=detect mode=train model=yolov8n_cbam.yaml data=GRIaug.yaml
from n params module arguments
0 -1 1 464 ultralytics.nn.modules.Conv [3, 16, 3, 2]
1 -1 1 4672 ultralytics.nn.modules.Conv [16, 32, 3, 2]
2 -1 1 7360 ultralytics.nn.modules.C2f [32, 32, 1, True]
3 -1 1 18560 ultralytics.nn.modules.Conv [32, 64, 3, 2]
4 -1 2 49664 ultralytics.nn.modules.C2f [64, 64, 2, True]
5 -1 1 73984 ultralytics.nn.modules.Conv [64, 128, 3, 2]
6 -1 2 197632 ultralytics.nn.modules.C2f [128, 128, 2, True]
7 -1 1 295424 ultralytics.nn.modules.Conv [128, 256, 3, 2]
8 -1 1 460288 ultralytics.nn.modules.C2f [256, 256, 1, True]
9 -1 1 164608 ultralytics.nn.modules.SPPF [256, 256, 5]
10 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
11 [-1, 6] 1 0 ultralytics.nn.modules.Concat [1]
12 -1 1 148224 ultralytics.nn.modules.C2f [384, 128, 1]
Traceback (most recent call last):
File "C:\Users\Eng_Server\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 193, in _run_module_as_main
"main", mod_spec)
File "C:\Users\Eng_Server\AppData\Local\Programs\Python\Python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\Eng_Server\AppData\Local\Programs\Python\Python37\Scripts\yolo.exe__main.py", line 7, in
@zeinabelsharkawy the error appears to be due to the model being unable to find the 'CBAM' in the list of predefined modules. When you define a new module (like 'CBAM'), it will not automatically be recognized, especially if it is not included in the original model architecture or in the torch.nn
library.
You need to make sure that the CBAM module has been appropriately imported and visible to YOLOv8. Honestly, implementing new modules to such established architectures can be quite tricky without a good understanding of YOLOv8's structure.
Please review your code where you are defining the CBAM module, and also where you are trying to use it. In that line of code where it's being imported or used, the CBAM module seems to be not visible or accessible.
Lastly, remember that making such modifications might require changes in other sections of the code too in order to pass the inputs and parameters correctly to the new module and expect outputs in a shape/form that is compatible with the rest of the code. Ensure all of these aspects are thoroughly considered and addressed. Please proceed with these changes and let us know if you encounter further issues.
@glenn-jocher Thank you very much for your help
@zeinabelsharkawy you're welcome! I'm glad I could help. If you have any other questions or run into further issues, please don't hesitate to ask. Happy coding!
My issue is similar to yours. How did you resolve it?
@hegangjian123456 hello, could you please provide a bit more details about the specific issue you're facing? It would help to know what you have tried so far and what the exact error message or behavior you're experiencing is. General steps for debugging include checking your dependencies and versions, making sure your code syntax and logic are correct, and reviewing any modifications you've made to the model carefully. Let's see if we can figure this out together. Looking forward to your reply.
@zeinabelsharkawy hello, this could help you fix the Key Error: 'CBAM'.
In tasks.py:
Line 10: from ultralytics.nn.modules import (AIFI, C1, C2, C3, C3TR, SPP, SPPF, Bottleneck, BottleneckCSP, C2f, C3Ghost, C3x, Classify, Concat, Conv, Conv2, ConvTranspose, Detect, DWConv, DWConvTranspose2d, Focus, GhostBottleneck, GhostConv, HGBlock, HGStem, Pose, RepC3, RepConv, RTDETRDecoder, Segment, CBAM)
Which is done according to the provide task.txt
Line 685: if m in (Classify, Conv, ConvTranspose, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, Focus, BottleneckCSP, C1, C2, C2f, C3, C3TR, C3Ghost, nn.ConvTranspose2d, DWConvTranspose2d, C3x, RepC3, CBAM):
So,
elif m in {CBAM}: c1, c2 = ch[f], args[0] if c2 != nc: c2 = make_divisible(min(c2, max_channels) width, 8) args = [c1, args[1:]]
provided in task.txt does the same ops as in line 685
@zeinabelsharkawy take a look at this implementation of CBAM: Convolutional Block Attention Module. https://arxiv.org/abs/1807.06521
class ChannelAttention(nn.Module): """Channel-attention module https://arxiv.org/abs/1807.06521"""
def __init__(self, f_input, r=16):
"""Initializes the class and sets the basic configurations and instance variables required."""
super(ChannelAttention, self).__init__()
# Apply the reduction ratio r to the input feature F
f_reduced = f_input // r
# Define the AvgPool and MaxPool functions
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.max_pool = nn.AdaptiveMaxPool2d(1)
# Define the Shared MLP
self.shared_MLP = nn.Sequential(
nn.Linear(in_features=f_input, out_features=f_reduced),
nn.LeakyReLU(0.1, inplace=True),
nn.Linear(in_features=f_reduced, out_features=f_input)
)
# Define the sigmoid function σ()
self.act = nn.Sigmoid()
def forward(self, x):
"""
Channel Attention
Mc(F) = σ(MLP(AvgPool(F)) + MLP(MaxPool(F)))
= σ(W1(W0(Fc_avg)) + W1(W0(Fc_max)))
"""
# MLP(AvgPool(F))
mlp_avg_pool_f = self.shared_MLP(self.avg_pool(x).view(x.size(0), -1)).unsqueeze(2).unsqueeze(3)
# MLP(MaxPool(F))
mlp_max_pool_f = self.shared_MLP(self.max_pool(x).view(x.size(0), -1)).unsqueeze(2).unsqueeze(3)
# Mc(F)
return self.act(mlp_avg_pool_f + mlp_max_pool_f)
class SpatialAttention(nn.Module): """Spatial-attention module. https://arxiv.org/abs/1807.06521"""
def __init__(self):
super(SpatialAttention, self).__init__()
self.conv2d = nn.Conv2d(in_channels=2, out_channels=1, kernel_size=7, stride=1, padding=3)
self.act = nn.Sigmoid()
def forward(self, x):
"""
Spatial Attention:
Ms(F) = σ(f7×7([AvgPool(F); MaxPool(F)]))
= σ(f7×7([Fsavg; Fsmax]))
"""
return self.act(self.conv2d(torch.cat([torch.mean(x, 1, keepdim=True), torch.max(x, 1, keepdim=True)[0]], 1)))
class CBAM(nn.Module): """CBAM: Convolutional Block Attention Module. https://arxiv.org/abs/1807.06521"""
def __init__(self, f_input, c2):
super(CBAM, self).__init__()
self.channel_attention = ChannelAttention(f_input)
self.spatial_attention = SpatialAttention()
def forward(self, x):
return self.spatial_attention(self.channel_attention(x) * x) * self.channel_attention(x) * x
@zeinabelsharkawy [-1, 3, CBAM, [256,7]] should be [-1, 3, CBAM, [256]] because the kernel size is set to 7 and the reduction ratio to 16 by default according to the paper. For more insights kindly read the paper. I hope this will hep.
@glenn-jocher hello, recently I had the same issue and I managed to refactor the CBAM in ultralytics, solving this issue. I could open a PR for this.
@jordanesikati this seems like an issue with the Convolutional Block Attention Module (CBAM) integration into YOLOv8. It's likely related to the CBAM module not being recognized by the YOLOv8 model.
When you modify or augment the YOLOv8 model with a new module such as CBAM, you need to ensure that the module is properly imported and accessible to the main YOLOv8 code.
Therefore, double-check where you've defined the CBAM module, and ensure it's imported correctly in the script where it's being used. Also keep in mind that such modifications might need further changes in the codebase for everything to work properly.
I hope this helps you troubleshoot the issue. Let us know if you have any further questions.
@glenn-jocher, incorporating CBAM in tasks.py as illustrated above partially solves the issue(Key Error: 'CBAM'). I managed to get through as described previously.
@jordanesikati, I'm glad to hear that you were able to progress in your task of integrating CBAM with YOLOv8 by modifying the tasks.py file. As you know, adding any new feature or module to an existing model often involves making adjustments to a number of files and areas in the code. Your effort to resolve the KeyError associated with CBAM by adjusting the referenced file shows us about the dynamism of the open-source community.
Keep in mind that fully integrating CBAM into YOLOv8 might require additional modifications in other parts of the codebase, so be sure to thoroughly test your implementation and validate its performance.
Thank you for sharing the way you addressed the issue. Your workaround may assist others in the community who are facing the same problem. If you come across additional issues or have more updates, feel free to share.
@hegangjian123456 hello, could you please provide a bit more details about the specific issue you're facing? It would help to know what you have tried so far and what the exact error message or behavior you're experiencing is. General steps for debugging include checking your dependencies and versions, making sure your code syntax and logic are correct, and reviewing any modifications you've made to the model carefully. Let's see if we can figure this out together. Looking forward to your reply.
Thank you for your response. My issue has been resolved.
@hegangjian123456 i'm glad to hear that your issue has been resolved! If you have any other questions or face any further problems in the future, don't hesitate to reach out. Happy coding!
👋 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 ⭐
@jordanesikati i'm sorry, but I'm unable to access external links, including GitHub pull requests. If you can provide details about the pull request here, I'd be happy to give you a concise response.
@jordanesikati i'm sorry, but I'm unable to access external links, including GitHub pull requests. If you can provide details about the pull request here, I'd be happy to give you a concise response.
Hi @glenn-jocher just a link to a PR for fixing Key Error: 'CBAM'. (https://github.com/ultralytics/ultralytics/pull/6115).
@jordanesikati Thank you for the update on the pull request. Since I can't access the link, I trust that the PR addresses the 'CBAM' KeyError issue you encountered. If you need further assistance or have more questions, feel free to reach out. Good luck with your contribution!
@glenn-jocher Hi! I have encountered the same issue, where I wanted to use the SEAttention on yolov8 and received the error: KeyError: ‘SEAttention’. My initial assumption was that this attention had not been correctly imported. However, I had indeed correctly imported the attention in both tasks.py and init.py files in the project. Despite this, the error: KeyError: ‘SEAttention’ persisted. After some attempts and searched for some information, I discovered that for such issues, going to the path of the virtual environment (for example, my path is: anaconda3/envs/yolov8/lib/site-packages/ultralytics/nn/task.py) and repeating the import of the SEAttention in tasks.py and init.py can solve the issue, and I was able to successfully incorporate the SEAttention for training.Hope this information may help.
Hey there! 🚀 Thanks for sharing your experience with the SEAttention
error in YOLOv8. It's great to hear you've successfully resolved it by ensuring the attention module was imported in both tasks.py
and __init__.py
within your virtual environment's Ultralytics package directory.
For those who might be encountering similar issues, here’s a quick tip based on @LCZCNP’s solution:
SEAttention
, in this case) in ultralytics/nn/tasks.py
and ultralytics/__init__.py
located in your virtual environment, like so:# In your __init__.py and tasks.py
from .attention import SEAttention
It seems tweaking directly in the virtual environment's Ultralytics install can be a handy workaround! Thanks again for sharing, and happy training with enhanced attention models! 🌟
Hi! After reviewing many shared experiences and materials, I think I’ve found the reason for the “Keyerror” when adding attentions.
When setting up the virtual environment, if we use
pip install ultralytics
to configure the virtual environment, the "ultralytics" package will be installed in the virtual environment. If we clone the project from Github for training (let’s assume the cloned project is named "ultralytics-main") and choose the virtual environment we mentioned before as the interpreter. If we import attention in "ultralytics-main/ultralytics/nn/tasks.py" and "ultralytics-main/ultralytics/nn/modules/init.py". In fact, attention is not imported in the "ultralytics" package in the virtual environment. However, the code in the cloned project "ultralytics-main/ultralytics/init.py":
from ultralytics.data.explorer.explorer import Explorer
from ultralytics.models import RTDETR, SAM, YOLO, YOLOWorld
from ultralytics.models.fastsam import FastSAM
from ultralytics.models.nas import NAS
from ultralytics.utils import ASSETS, SETTINGS as settings
from ultralytics.utils.checks import check_yolo as checks
from ultralytics.utils.downloads import download
such files will by default look for the attention code from the "ultralytics" package in the virtual environment, rather than from the "ultralytics-main/ultralytics" where attention was correctly imported. Therefore, this problem occurs, which is why we can resolve the problem by importing the attention in the "ultralytics/nn/tasks.py" and "ultralytics/init.py" located in the virtual environment. Thus, it seems that modifying the way imports are done in such files could also solve this problem, but I haven’t tried this specifically yet, since there are too many files that needs to be edited.
Perhaps we can resolve the problem with another method. When we configure the virtual environment, after installing the torch, we can clone the project first. Then use the code
pip install -e "the path of the project we have just cloned from Github"
to configure the virtual environment. So that the "Editable project location" of the "ultralytics" package would be the project we have just cloned.
@LCZCNP hey there! 👋 It sounds like you've done quite a bit of detective work 🕵️♂️ and uncovered something important about the environment setup when adding custom attentions to YOLOv8. Thanks for sharing your insights!
You're spot on with your observations. When using pip install ultralytics
, Python indeed searches for imports in the installed package rather than the cloned project directory. This can cause the KeyError
if custom modules like attentions are not in the installed ultralytics package but in your cloned directory.
A very practical approach you've mentioned is using pip's editable mode with pip install -e "path/to/cloned/project"
. This effectively tells Python to use the modules from your cloned project directory, ensuring that any custom modules or changes are recognized.
Here's a concise step-by-step based on your suggestion that others might find helpful:
pip install -e .
to install the project in editable mode.This way, your development environment directly references the project's source code, and Python will look there first for any imports, including your custom attention mechanisms.
Thanks for this valuable contribution, and do share if experimenting with the import modifications brings any new findings! Happy coding! 🚀
@hegangjian123456 hello, could you please provide a bit more details about the specific issue you're facing? It would help to know what you have tried so far and what the exact error message or behavior you're experiencing is. General steps for debugging include checking your dependencies and versions, making sure your code syntax and logic are correct, and reviewing any modifications you've made to the model carefully. Let's see if we can figure this out together. Looking forward to your reply.
Thank you for your response. My issue has been resolved.
i have the same issue, but i can't resolve it can you help me please
Search before asking
Question
when I use CBAM attention with YOLOv8, this error appeared. KeyError: 'CBAM' what is the solution?
Additional
No response