训练YOLOV3报错TypeError: not all arguments converted during string formatting

训练YOLOV3报错TypeError: not all arguments converted during string formatting

问题描述

pytorch下yolov3训练自己的数据集,报错:

Traceback (most recent call last):
  File "train.py", line 431, in <module>
    train(hyp)  # train normally
  File "train.py", line 91, in train
    model = Darknet(cfg).to(device)
  File "C:\Users\pippy\Desktop\yolov3-master\models.py", line 226, in __init__
    self.module_list, self.routs = create_modules(self.module_defs, img_size, cfg)
  File "C:\Users\pippy\Desktop\yolov3-master\models.py", line 33, in create_modules
    bias=not bn))
  File "C:\Users\pippy\AppData\Roaming\Python\Python37\site-packages\torch\nn\modules\conv.py", line 332, in __init__
    False, _pair(0), groups, bias, padding_mode)
  File "C:\Users\pippy\AppData\Roaming\Python\Python37\site-packages\torch\nn\modules\conv.py", line 24, in __init__
    if out_channels % groups != 0:
TypeError: not all arguments converted during string formatting

报错原因是求余符号两边得是数字(if out_channels % groups != 0),不知道别人为什么没有出现这个问题,原程序models.py中也确实没有int我们的groups,所以抱着试一试的心态添加了int,发现竟然可行,models.py中的修改部分如下(大约在源程序21-33行的位置):

 if mdef['type'] == 'convolutional':
            bn = int(mdef['batch_normalize'])
            filters = int(mdef['filters'])
            k = int(mdef['size'])  # kernel size
            stride = int(mdef['stride']) if 'stride' in mdef else (mdef['stride_y'], mdef['stride_x'])
            if isinstance(k, int):  # single-size conv
                modules.add_module('Conv2d',nn.Conv2d(in_channels=output_filters[-1],
                                                       out_channels=filters,
                                                       kernel_size=k,
                                                       stride=stride,
                                                       padding=k // 2 if mdef['pad'] else 0,
                                                       groups=int(mdef['groups']) if 'groups' in mdef else 1,
                                                       bias=not bn))
### 解决 `TypeError: not all arguments converted during string formatting` 错误 当遇到此错误时,通常是因为字符串格式化过程中提供的参数数量与预期不符。具体来说,在使用 `%` 进行字符串插值时,如果指定的占位符数目和实际传递给它的变量数目不匹配,则会触发该异常。 #### 原因分析 这种类型的错误经常发生在如下场景中: - 使用百分号风格(`%`) 的字符串格式化方式时,传入了过多或过少的位置参数[^1]。 例如: ```python print("%s %d" % ("hello")) # 缺少一个参数 ``` 上述代码试图用两个占位符来替换单个输入项,因此会导致错误。 #### 正确做法 为了修正这个问题,可以采取以下几种方法之一: ##### 方法一:确保提供足够的参数 确认所提供的参数正好满足所有的占位符需求。对于上面的例子,应该像这样修改它: ```python print("%s %d" % ("hello", 123)) # 提供了两个参数 ``` ##### 方法二:采用更现代的方式——f-string 或 `.format()` 函数 这两种替代方案不仅更加直观易读,而且不容易犯错。比如利用 f-string 来重写之前的例子: ```python greeting = "hello" number = 123 formatted_string = f"{greeting} {number}" print(formatted_string) ``` 或者通过调用`.format()`函数实现相同效果: ```python formatted_string = "{} {}".format(greeting, number) print(formatted_string) ``` 这些改进后的版本能够有效防止由于参数数量不对而导致的类型错误。 #### YOLO 示例中的应用 假设在一个YOLO项目里有这样一个情况,想要打印检测到的目标名称及其置信度分数,但是遇到了类似的错误提示。那么可以通过调整为f-string或其他安全的形式来进行修复: ```python object_name = "person" confidence_score = 0.9578 # 不推荐的做法,容易引发错误 incorrect_formatting = "%s %.2f" % (object_name,) # 推荐的方法,使用f-string correct_formatting_fstring = f"{object_name} {confidence_score:.2f}" # 或者使用.format() correct_formatting_dot_format = "{} {:.2f}".format(object_name, confidence_score) print(correct_formatting_fstring) # 输出 person 0.96 ```
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值