Home>

I'm thinking of using yolov3 to learn my own data and detect objects.

I prepared my own data and annotated it with labelimg.
After that, I tried to train and output the detection result, but an error occurred and no result was obtained.

I searched a lot, but I couldn't find a solution ...

The cause is probably this part of the error below.
raise ValueError ("Shapes% s and% s are incompatible"% (self, other))
ValueError: Shapes (255,) and (42,) are incompatible

Traceback (most recent call last):
  File "detect_video.py", line 90, in<module>  app.run (main)
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ absl \ app.py", line 303, in run
    _run_main (main, args)
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ absl \ app.py", line 251, in _run_main
    sys.exit (main (argv))
  File "detect_video.py", line 35, in main
    yolo.load_weights (FLAGS.weights)
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ tensorflow_core \ python \ keras \ engine \ training.py", line 234, in load_weights
    return super (Model, self) .load_weights (filepath, by_name, skip_mismatch)
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ tensorflow_core \ python \ keras \ engine \ network.py", line 1193, in load_weights
    status = self._trackable_saver.restore (filepath)
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ tensorflow_core \ python \ training \ tracking \ util.py", line 1283, in restorecheckpoint = checkpoint, proto_id = 0) .restore (self._graph_view.root)
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ tensorflow_core \ python \ training \ tracking \ base.py", line 209, in restore
    restore_ops = trackable._restore_from_checkpoint_position (self) # pylint: disable = protected-access
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ tensorflow_core \ python \ training \ tracking \ base.py", line 908, in _restore_from_checkpoint_position
    tensor_saveables, python_saveables))
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ tensorflow_core \ python \ training \ tracking \ util.py", line 289, in restore_saveables
    validated_saveables) .restore (self.save_path_tensor)
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ tensorflow_core \ python \ training \ saving \ functional_saver.py", line 255, in restore
    restore_ops.update (saver.restore (file_prefix))
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ tensorflow_core \ python \ training \ saving \ functional_saver.py", line 102, in restore
    restored_tensors, restored_shapes = None)
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ tensorflow_core \ python \ training \ saving \ saveable_object_util.py", line 116, in restore
    self.handle_op, self._var_shape, restored_tensor)
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ tensorflow_core \ python \ ops \ resource_variable_ops.py", line 297, in shape_safe_assign_variable_handle
    shape.assert_is_compatible_with (value_tensor.shape)
  File "C: \ ProgramData \ Anaconda3 \ envs \ yolov3-tf2-gpu \ lib \ site-packages \ tensorflow_core \ python \ framework \ tensor_shape.py", line 1110, in assert_is_compatible_with
    raise ValueError ("Shapes% s and% s are incompatible"% (self, other))
ValueError: Shapes (255,) and (42,) are incompatible
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer
W1120 16: 47: 36.936927 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer.iter
W1120 16: 47: 36.936927 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer.iter
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer.beta_1W1120 16: 47: 36.936927 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer.beta_1
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer.beta_2
W1120 16: 47: 36.936927 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer.beta_2
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer.decay
W1120 16: 47: 36.936927 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer.decay
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer.learning_rate
W1120 16: 47: 36.936927 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer.learning_rate
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer's state'm' for (root) .layer_with_weights-1.layer_with_weights-0.kernel
W1120 16: 47: 36.936927 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer's state'm' for (root) .layer_with_weights-1.layer_with_weights-0.kernel
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer's state'm' for (root) .layer_with_weights-1.layer_with_weights-1.gamma
W1120 16: 47: 36.936927 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer's state'm' for (root) .layer_with_weights-1.layer_with_weights-1.gamma
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer's state'm' for (root) .layer_with_weights-1.layer_with_weights-1.beta
W1120 16: 47: 36.936927 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer's state'm' for (root) .layer_with_weights-1.layer_with_weights-1.beta
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer's state'm' for (root) .layer_with_weights-1.layer_with_weights-2.kernel
W1120 16: 47: 36.936927 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer's state'm' for (root) .layer_with_weights-1.layer_with_weights-2.kernel
・
・
・
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer's state'v' for (root) .layer_with_weights-6.layer_with_weights-1.beta
W1120 16: 47: 36.952536 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer's state'v' for (root) .layer_with_weights-6.layer_with_weights-1.beta
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer's state'v' for (root) .layer_with_weights-6.layer_with_weights-2.kernel
W1120 16: 47: 36.952536 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer's state'v' for (root) .layer_with_weights-6.layer_with_weights-2.kernel
WARNING: tensorflow: Unresolved object in checkpoint: (root) .optimizer's state'v' for (root) .layer_with_weights-6.layer_with_weights-2.biasW1120 16: 47: 36.952536 32388 util.py: 144] Unresolved object in checkpoint: (root) .optimizer's state'v' for (root) .layer_with_weights-6.layer_with_weights-2.bias
WARNING: tensorflow: A checkpoint was restored (eg tf.train.Checkpoint.restore or tf.keras.Model.load_weights) but not all checkpointed values ​​were used. See above for specific issues. Use expect_partial () on the load status object, eg tf.train.Checkpoint.restore (...). expect_partial (), to silence these warnings, or use assert_consumed () to make the check explicit. See https://www.tensorflow.org/guide/checkpoint#loading_mechanics for details.
W1120 16: 47: 36.952536 32388 util.py: 152] A checkpoint was restored (eg tf.train.Checkpoint.restore or tf.keras.Model.load_weights) but not all checkpointed values ​​were used. See above for specific issues. Use expect_partial () on the load status object, eg tf.train.Checkpoint.restore (...). expect_partial (), to silence these warnings, or use assert_consumed () to make the check explicit. See https: // www. tensorflow.org/guide/checkpoint#loading_mechanics for details.
Corresponding source code

The original source code has been dropped from the following.
https://github.com/zzh8829/yolov3-tf2

What I tried

Prepare your own data and annotate

Create a tfrecord file with the following command (train and val)
python tools/voc2012.py --data_dir ./VOCdevkit/VOC2012 --split train --output_file ./data/voc2012_train_traffic.tfrecord --classes ./data/traffic_voc2012.names
python tools/voc2012.py --data_dir ./VOCdevkit/VOC2012 --split val --output_file ./data/voc2012_val_traffic.tfrecord --classes ./data/traffic_voc2012.names

Learn the model with the following command (class is trying to make 9 models)
python train.py --dataset ./data/voc2012_train_traffic.tfrecord --val_dataset ./data/voc2012_val_traffic.tfrecord --classes ./data/traffic_voc2012.names --num_classes 9 --mode fit --transfer darknet --batch_size 2 --epochs 5 --weights ./checkpoints/yolov3.tf --weights_num_classes 80

Object detection using the trained model (trained data is yolov3_train_5.tf) (Error occurred here)
python detect_video.py --video NNF_201103-120904.AVI --classes ./data/traffic_voc2012.names --weights ./checkpoints/yolov3_train_5.tf num_classes 80 --output ./output.avi

Supplementary information (FW/tool version, etc.)

python uses 3.7 and the model is yolov3 available in Tensorflow.
In addition, the environment uses Anaconda, and I proceeded with reference to the following site.
https://rightcode.co.jp/blog/information-technology/learn-yolov3-image-windows10-environment-construction
https://rightcode.co.jp/blog/information-technology/learn-yolov3-image-windows10-object-detection
https://qiita.com/plseal/items/f493c67b2e810f2f876e

  • Answer # 1

    The cause was that I forgot to add --to the option of the execution command.

    python detect_video.py --video NNF_201103-120904.AVI --classes ./data/traffic_voc2012.names --weights ./checkpoints/yolov3_train_5.tf-num_classes 80 --output ./output.avi

    I apologize to you for a fuss over.