Tony Starkus
Tony Starkus

Reputation: 576

Train model to detect new objects - YoloV3

I did this tutorial to train my model to detect hololens.

Now, I created a new Pascal Voc dataset of "guitar".

I want to take my actual model that detect hololens to detect hololens and guitar.

In step 3, I change these lines:

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

to:

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")

The "violao" folder struct is:

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

My annotation in these images is "violao".

When I run this code:

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()

I get this error:

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()

What am I doing wrong??

Upvotes: 1

Views: 1275

Answers (3)

Siddharth Sankhe
Siddharth Sankhe

Reputation: 23

Try this:

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

object_names_array=[] will select by default annotation from your xml.

Upvotes: 2

Sergio Wehbe
Sergio Wehbe

Reputation: 28

I think you misspelled anottations with a double t

My problem was that I entered the name of the folder here and not the object

object_names_array=["object name in the Label"]

Upvotes: 0

Combinacijus
Combinacijus

Reputation: 467

I think key error is this

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

I managed to reproduce similar issue by adding new label in the object_names_array list that does not exist in training dataset annotations and I get these errors

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()

Solution

  1. Make sure all object_names_array labels are in your dataset in your case "hololens" and "violao". I assume you are using dataset only with "violao" labeled images therefore ImageAI complains that it can't find hololens training data even though you specified to train on "hololens" too (assume you are training new model and it doesn't know about any labels before training e.g if you will leave only ["violao"] it won't detect hololens because it knows nothing about it.)
  2. Delete folders from data_directory path that are not train and validation. It seems that generated training files from other sessions (most likely cache folder) might interfere with training when you add labels etc.

If it still doesn't work try training your model from given pretrainged yolov3 model https://github.com/OlafenwaMoses/ImageAI/releases/download/essential-v4/pretrained-yolov3.h5

Upvotes: 1

Related Questions