在我自己训练yolov5模型时报错这个AttributeError: 'Detect' object has no attribute 'nm'
该错误定位到的代码为 raise AttributeError(f"'{type(self).name}' object has no attribute '{name}'")

在我自己训练yolov5模型时报错这个AttributeError: 'Detect' object has no attribute 'nm'
该错误定位到的代码为 raise AttributeError(f"'{type(self).name}' object has no attribute '{name}'")

以下回复参考:皆我百晓生、券券喵儿等免费微信小程序作答:
在您提供的代码中,detect.py文件中的Module类定义了nm属性,但没有正确导入相应的模块(m)。这可能导致AttributeError: 'Detect' object has no attribute 'nm'错误。
要解决这个问题,您可以将m替换为您想要使用的模块名。例如,在您的代码中:
from module import m
然后在__getattr__方法中检查当前对象是否是m实例,并返回其属性值。
修复后的代码示例:
import torch.nn as nn
from module import m
class Module(nn.Module):
def __init__(self):
super(Module, self).__init__()
@property
def nm(self):
return m.nm
# 假设您已经创建了一个名为'm'的模块实例
model = m()
# 调用`__getattr__`方法获取`nm`属性
try:
attr_name = 'nm'
attr_value = getattr(model, attr_name)
except AttributeError:
print("AttributeError: 'Detect' object has no attribute '{}'".format(attr_name))
else:
print("Attribute found: {}".format(attr_value))
请注意,这里假设m是一个可以访问nm属性的对象。如果您有其他类型的模块或者不同的数据结构,请相应调整上述代码以适应实际情况。