Borda / keras-yolo3

A Keras implementation of YOLOv3 (Tensorflow backend) a successor of qqwweee/keras-yolo3
MIT License
31 stars 10 forks source link
classification cnn-keras detection object-detection tensorflow yolo yolov3 yolov3-tiny

Keras: YOLO v3

Build Status Build status CircleCI codecov Codacy Badge CodeFactor license

Introduction

A Keras implementation of YOLOv3 (Tensorflow backend) inspired by allanzelener/YAD2K. This fork is a continuation of qqwweee/keras-yolo3 with some CI and bug fixing since its parent become inactive...

For package installation use of the following commands

pip install git+https://github.com/Borda/keras-yolo3.git
pip install https://github.com/Borda/keras-yolo3/archive/master.zip

or clone/download repository locally and run python setup.py install


Quick Start

For more model and configuration please see YOLO website and darknet repository.

  1. Download YOLOv3 weights from YOLO website.
    wget -O ./model_data/yolo3.weights  \
       https://pjreddie.com/media/files/yolov3.weights  \
       --progress=bar:force:noscroll

    alternatively you can download light version yolov3-tiny.weights

  2. Convert the Darknet YOLO model to a Keras model.
    python3 scripts/convert_weights.py \
        --config_path ./model_data/yolo.cfg \
        --weights_path ./model_data/yolo.weights \
        --output_path ./model_data/yolo.h5
  3. Run YOLO detection.
    python3 scripts/detection.py \
       --path_weights ./model_data/yolo.h5 \
       --path_anchors ./model_data/yolo_anchors.csv \
       --path_classes ./model_data/coco_classes.txt \
       --path_output ./results \
       --path_image ./model_data/bike-car-dog.jpg \
       --path_video person.mp4

    For Full YOLOv3, just do in a similar way, just specify model path and anchor path with --path_weights <model_file> and --path_anchors <anchor_file>.

  4. MultiGPU usage: use --nb_gpu N to use N GPUs. It is passed to the Keras multi_gpu_model().

Training

For training you can use VOC dataset, COCO datset or your own...

  1. Generate your own annotation file and class names file.
    • One row for one image;
    • Row format: image_file_path box1 box2 ... boxN;
    • Box format: x_min,y_min,x_max,y_max,class_id (no space).
    • Run one of following scrips for dataset conversion
      • scripts/annotation_voc.py
      • scripts/annotation_coco.py
      • scripts/annotation_csv.py
        Here is an example:
        path/to/img1.jpg 50,100,150,200,0 30,50,200,120,3
        path/to/img2.jpg 120,300,250,600,2
        ...
  2. Make sure you have run python scripts/convert_weights.py <...>. The file model_data/yolo_weights.h5 is used to load pre-trained weights.
  3. Modify training.py and start training. python training.py. Use your trained weights or checkpoint weights with command line option --model model_file when using yolo_interactive.py. Remember to modify class path or anchor path, with --classes class_file and --anchors anchor_file.

If you want to use original pre-trained weights for YOLOv3:

  1. wget https://pjreddie.com/media/files/darknet53.conv.74
  2. rename it as darknet53.weights
  3. python convert.py -w darknet53.cfg darknet53.weights model_data/darknet53_weights.h5
  4. use model_data/darknet53_weights.h5 in training.py

Some issues to know

  1. The test environment is Python 3.x ; Keras 2.2.0 ; tensorflow 1.14.0
  2. Default anchors are used. If you use your own anchors, probably some changes are needed.
  3. The inference result is not totally the same as Darknet but the difference is small.
  4. Always load pretrained weights and freeze layers in the first stage of training. Or try Darknet training. It's OK if there is a mismatch warning.
  5. The training strategy is for reference only. Adjust it according to your dataset and your goal. and add further strategy if needed.
  6. For speeding up the training process with frozen layers train_bottleneck.py can be used. It will compute the bottleneck features of the frozen model first and then only trains the last layers. This makes training on CPU possible in a reasonable time. See this post for more information on bottleneck features.
  7. Failing while run multi-GPU training, think about porting to TF 2.0.

Nice reading