训练模型以检测新对象-YoloV3

时间:2019-11-17 20:23:37

标签: python python-3.x tensorflow object-detection yolo

我做了this tutorial来训练我的模型以检测Hololens。

现在,我创建了一个新的“吉他”的Pascal Voc数据集。

我想采用检测电子节拍的实际模型来检测电子节拍和吉他。

在步骤3中,我更改了这些行:

trainer.setDataDirectory(data_directory="hololens")
trainer.setTrainConfig(object_names_array=["hololens"], batch_size=4, num_experiments=100, train_from_pretrained_model="pretrained-yolov3.h5")

收件人:

trainer.setDataDirectory(data_directory="violao")
trainer.setTrainConfig(object_names_array=["hololens", "violao"], batch_size=4, num_experiments=100, train_from_pretrained_model="drive/My Drive/PhoHast/my_model.h5")

“ violao”文件夹结构为:

-violao
--train
---anottations
----<xml_files>
---images
----<images_files>

我在这些图像中的注释是“ violao”。

当我运行此代码时:

from imageai.Detection.Custom import DetectionModelTrainer
trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory="violao")
trainer.setTrainConfig(object_names_array=["hololens", "violao"], batch_size=4, num_experiments=100, train_from_pretrained_model="my_model.h5")
trainer.trainModel()

我收到此错误:

Generating anchor boxes for training images and annotation...
[Errno 21] Is a directory: 'violao/train/annotations/.ipynb_checkpoints'
Ignore this bad annotation: violao/train/annotations/.ipynb_checkpoints
Average IOU for 9 anchors: 0.96
Anchor Boxes generated.
Detection configuration saved in  violao/json/detection_config.json
Some labels have no annotations! Please revise the list of labels in your configuration.
Training on:    None
Training with Batch Size:  4
Number of Experiments:  100
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-2e7a3bd0b6ee> in <module>()
      5 trainer.setDataDirectory(data_directory="violao")
      6 trainer.setTrainConfig(object_names_array=["hololens", "violao"], batch_size=4, num_experiments=100, train_from_pretrained_model="drive/My Drive/PhoHast/PhoHastV1.h5")
----> 7 trainer.trainModel()

1 frames
/usr/local/lib/python3.6/dist-packages/imageai/Detection/Custom/__init__.py in trainModel(self)
    231             shuffle=True,
    232             jitter=0.3,
--> 233             norm=normalize
    234         )
    235 

/usr/local/lib/python3.6/dist-packages/imageai/Detection/Custom/generator.py in __init__(self, instances, anchors, labels, downsample, max_box_per_image, batch_size, min_net_size, max_net_size, shuffle, jitter, norm)
     34         self.net_w              = 416
     35 
---> 36         if shuffle: np.random.shuffle(self.instances)
     37 
     38     def __len__(self):

mtrand.pyx in numpy.random.mtrand.RandomState.shuffle()

TypeError: object of type 'NoneType' has no len()

我在做什么错??

3 个答案:

答案 0 :(得分:1)

我认为关键是这个

Some labels have no annotations! Please revise the list of labels in
your configuration.

通过在训练数据集注释中不存在的 object_names_array 列表中添加新标签,我设法重现了类似的问题,并且出现了这些错误

Some labels have no annotations! Please revise the list of labels in your configuration.
...
Training on:    None
...
TypeError: object of type 'NoneType' has no len()

解决方案

  1. 确保所有 object_names_array 标签都在数据集中,以防“ hololens”和“ violao”。我假设您仅使用带有“ violao”标签图像的数据集,因此ImageAI抱怨即使您也指定也要对“ hololens”进行训练,它也无法找到hololens训练数据(假设您正在训练新模型并且它不知道训练之前有任何标签,例如,如果您只留下[“ violao”],它将不会检测到hololens,因为它对此一无所知。)
  2. data_directory 路径中删除不是 train validation 的文件夹。在添加标签等时,其他会话(最可能是缓存文件夹)生成的训练文件似乎会干扰训练。

如果仍然无法正常工作,请尝试使用给定的预训练yolov3模型https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/pretrained-yolov3.h5来训练模型

答案 1 :(得分:0)

我认为您用双t拼写了错误的前缀

我的问题是我在此处输入文件夹的名称而不是对象

object_names_array=["object name in the Label"]

答案 2 :(得分:0)

试试这个:

trainer.setTrainConfig(object_names_array=[], batch_size=4, num_experiments=200, train_from_pretrained_model="---.h5")

object_names_array=[] 将默认从您的 xml 中选择注释。