微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

遍历ModuleList的顺序时,索引超出范围

如何解决遍历ModuleList的顺序时,索引超出范围

我正在编写YOLO神经网络,对它来说还很新。 我有一个用于定义网络的配置文件。它存储为nn.ModuleList,模块存储为nn.Sequential。 这些顺序的示例:

  (0): Sequential(
    (conv_0): Conv2d(3,32,kernel_size=(3,3),stride=(1,1),padding=(1,bias=False)
    (batch_norm_0): Batchnorm2d(32,eps=1e-05,momentum=0.1,affine=True,track_running_stats=True)
    (leaky_0): LeakyReLU(negative_slope=0.1,inplace=True)
  )
  (1): Sequential(
    (conv_1): Conv2d(32,64,stride=(2,2),bias=False)
    (batch_norm_1): Batchnorm2d(64,track_running_stats=True)
    (leaky_1): LeakyReLU(negative_slope=0.1,inplace=True)
  )
  (2): Sequential(
    (conv_2): Conv2d(64,kernel_size=(1,bias=False)
    (batch_norm_2): Batchnorm2d(32,track_running_stats=True)
    (leaky_2): LeakyReLU(negative_slope=0.1,inplace=True)
  )

然后我对此进行一些格式化,以使输出具有正确的形状,并遍历ModuleList的每个模块。

for i,module in enumerate(modules):        
            module_type = (module["type"])
            
            if module_type == "convolutional" or module_type == "upsample":
                x = self.module_list[i](x)
    
            elif module_type == "route":
                layers = module["layers"]
                layers = [int(a) for a in layers]
    
                if (layers[0]) > 0:
                    layers[0] = layers[0] - i
    
                if len(layers) == 1:
                    x = outputs[i + (layers[0])]
    
                else:
                    if (layers[1]) > 0:
                        layers[1] = layers[1] - i
    
                    map1 = outputs[i + layers[0]]
                    map2 = outputs[i + layers[1]]
                    x = torch.cat((map1,map2),1)
                
    
            elif module_type == "shortcut":
                from_ = int(module["from"])
                x = outputs[i-1] + outputs[i+from_]
    
            elif module_type == 'yolo':
                anchors = self.module_list[i][0].anchors
                #Get the input dimensions
                inp_dim = int (self.net_info["height"])
        
                #Get the number of classes
                num_classes = int (module["classes"])
        
                #Transform 
                x = x.data
                x = predict_transform(x,inp_dim,anchors,num_classes,CUDA)
                if not write:              #if no collector has been intialised. 
                    detections = x
                    write = 1
        
                else:       
                    detections = torch.cat((detections,x),1)
        
            outputs[i] = x

但是,当我到达anchors = self.module_list[i][0].anchors行时,出现一个错误提示Index 0 is out of range

我不知道为什么会这样。我试图以不同的方式格式化我的ModuleList的形状。 任何建议将不胜感激。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。