遍历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 (将#修改为@)