【问题记录】raise IndexError(‘index {} is out of range‘.format(idx)) index 0 is out of range

灰太狼 2022-11-12 02:34 324阅读 0赞

先记录一些语法:
X[:, m:n]即取矩阵X的所有行中的的第m到n-1列数据,含左不含右。
X[0,:]就是取矩阵X的第0行的所有元素,X[1,:]取矩阵X的第一行的所有元素。

今天训练模型输入python3 train.py —model_def config/yolov3-custom.cfg —data_config config/custom.data
第一次出现这个错误,记录解决方案:
查了看到有人说什么加上except:continue;大概跟我出的错不一样,总之我没法按照这样解决问题,先截图一下我出问题的界面:
File “train.py”, line 62, in
model = Darknet(opt.model_def).to(device)
File “/hexinyi/PyTorch-YOLOv3/models.py”, line 240, in init
self.yolo_layers = [layer[0] for layer in self.module_list if isinstance(layer[0], YOLOLayer)]
File “/hexinyi/PyTorch-YOLOv3/models.py”, line 240, in
self.yolo_layers = [layer[0] for layer in self.module_list if isinstance(layer[0], YOLOLayer)]
File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/container.py”, line 68, in getitem
return self._get_item_by_idx(self._modules.values(), idx)
File “/usr/local/lib/python3.6/dist-packages/torch/nn/modules/container.py”, line 60, in _get_item_by_idx
raise IndexError(‘index {} is out of range’.format(idx))
IndexError: index 0 is out of range
在这里插入图片描述
首先出现这个问题是在我新增了数据集的图片并且增加了种类数目之后(classes numbers)
然后我检查我的train.txt和label等txt文件是否写错了或者下标溢出了,但是都没有发现错误,
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

然后我检查自己的config文件,怀疑可能是配置的问题,因为class的数目发生修改之后yolo层的config也需要修改:
在这里插入图片描述

然后发现我确实有写错,之前以为只有一层yolo,然后仔细看才发现有两层yolo:

在这里插入图片描述
然后每一个yolo之前的[convolutional]都需要修改其中的filters=21(因为是两类,3*7)
[yolo]的classes 修改为 2(原来是1) 改完之后 bash create_custom_model.sh,生成一个新的yolov3-custom.cfg,
其中cfg中有的要改的地方还没发生改变就手动修改过来,然后会弹出一个窗口说file changed,选择overwrite。

  1. [net]
  2. # Testing
  3. batch=1
  4. subdivisions=1
  5. # Training
  6. # batch=64
  7. # subdivisions=2
  8. width=416
  9. height=416
  10. channels=3
  11. momentum=0.9
  12. decay=0.0005
  13. angle=0
  14. saturation = 1.5
  15. exposure = 1.5
  16. hue=.1
  17. learning_rate=0.001
  18. burn_in=1000
  19. max_batches = 500200
  20. policy=steps
  21. steps=400000,450000
  22. scales=.1,.1
  23. [convolutional]
  24. batch_normalize=1
  25. filters=16
  26. size=3
  27. stride=1
  28. pad=1
  29. activation=leaky
  30. [maxpool]
  31. size=2
  32. stride=2
  33. [convolutional]
  34. batch_normalize=1
  35. filters=32
  36. size=3
  37. stride=1
  38. pad=1
  39. activation=leaky
  40. [maxpool]
  41. size=2
  42. stride=2
  43. [convolutional]
  44. batch_normalize=1
  45. filters=64
  46. size=3
  47. stride=1
  48. pad=1
  49. activation=leaky
  50. [maxpool]
  51. size=2
  52. stride=2
  53. [convolutional]
  54. batch_normalize=1
  55. filters=128
  56. size=3
  57. stride=1
  58. pad=1
  59. activation=leaky
  60. [maxpool]
  61. size=2
  62. stride=2
  63. [convolutional]
  64. batch_normalize=1
  65. filters=256
  66. size=3
  67. stride=1
  68. pad=1
  69. activation=leaky
  70. [maxpool]
  71. size=2
  72. stride=2
  73. [convolutional]
  74. batch_normalize=1
  75. filters=512
  76. size=3
  77. stride=1
  78. pad=1
  79. activation=leaky
  80. [maxpool]
  81. size=2
  82. stride=1
  83. [convolutional]
  84. batch_normalize=1
  85. filters=1024
  86. size=3
  87. stride=1
  88. pad=1
  89. activation=leaky
  90. [convolutional]
  91. batch_normalize=1
  92. filters=256
  93. size=1
  94. stride=1
  95. pad=1
  96. activation=leaky
  97. [convolutional]
  98. batch_normalize=1
  99. filters=512
  100. size=3
  101. stride=1
  102. pad=1
  103. activation=leaky
  104. [convolutional] #这里要修改filters
  105. size=1
  106. stride=1
  107. pad=1
  108. filters=21
  109. activation=linear
  110. [yolo] #注意这里
  111. mask = 3,4,5
  112. anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
  113. classes=2
  114. num=6
  115. jitter=.3
  116. ignore_thresh = .7
  117. truth_thresh = 1
  118. random=1
  119. [route]
  120. layers = -4
  121. [convolutional]
  122. batch_normalize=1
  123. filters=128
  124. size=1
  125. stride=1
  126. pad=1
  127. activation=leaky
  128. [upsample]
  129. stride=2
  130. [route]
  131. layers = -1, 8
  132. [convolutional]
  133. batch_normalize=1
  134. filters=256
  135. size=3
  136. stride=1
  137. pad=1
  138. activation=leaky
  139. [convolutional] #修改filter
  140. size=1
  141. stride=1
  142. pad=1
  143. filters=21
  144. activation=linear
  145. [yolo] #这里要修改classes
  146. mask = 0,1,2
  147. anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319
  148. classes=2
  149. num=6
  150. jitter=.3
  151. ignore_thresh = .7
  152. truth_thresh = 1
  153. random=1

再次运行输入python3 train.py —model_def config/yolov3-custom.cfg —data_config config/custom.data
成功了:
在这里插入图片描述
另外发现刚开始的epoch的mAP都非常低,到后来逐渐增高。

在这里插入图片描述
在第二十多epoch的时候已经达到百分之七十。
然后总结就是修改数据集后一定要注意cfg文件是否要修改。

发表评论

表情:
评论列表 (有 0 条评论,324人围观)

还没有评论,来说两句吧...

相关阅读