欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-26 09:09:46
训练YOLOV3报错TypeError: not all arguments converted during string formatting问题描述pytorch下yolov3训练自己的数据集,报错:Traceback (most recent call last): File "train.py", line 431, in train(hyp) # train normally File "train.py", line 91, in trai...

训练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))

本文地址:https://blog.csdn.net/Pippy03/article/details/107105994