Skip to content

doraak47/tensorflow-yolov4

 
 

Repository files navigation

license pypi language

tensorflow-yolov4

python3 -m pip install yolov4

YOLOv4 Implemented in Tensorflow 2.

Download Weights

Dependencies

python3 -m pip install -U pip setuptools wheel
python3 -m pip install numpy

Install OpenCV (cv2)

Tensorflow 2

python3 -m pip install tensorflow

TFlite

Ref: https://www.tensorflow.org/lite/guide/python

Objective

  • Train and predict using TensorFlow 2 only
  • Run yolov4-tiny-relu on Coral board(TPU).
  • Update Docs
  • Optimize model and operations

Performance

performance

performance-tiny

Help

>>> from yolov4.tf import YOLOv4
>>> help(YOLOv4)

Inference

tensorflow

from yolov4.tf import YOLOv4

yolo = YOLOv4()

yolo.classes = "coco.names"

yolo.make_model()
yolo.load_weights("yolov4.weights", weights_type="yolo")

yolo.inference(media_path="kite.jpg")

yolo.inference(media_path="road.mp4", is_image=False)

Object detection test jupyter notebook

from yolov4.tf import YOLOv4

yolo = YOLOv4(tiny=True)

yolo.classes = "coco.names"

yolo.make_model()
yolo.load_weights("yolov4-tiny.weights", weights_type="yolo")

yolo.inference(media_path="kite.jpg")

yolo.inference(media_path="road.mp4", is_image=False)

tensorflow lite

from yolov4.tf import YOLOv4

yolo = YOLOv4()

yolo.classes = "coco.names"

yolo.make_model()
yolo.load_weights("yolov4.weights", weights_type="yolo")

yolo.save_as_tflite("yolov4.tflite")
from yolov4.tflite import YOLOv4

yolo = YOLOv4()

yolo.classes = "coco.names"

yolo.load_tflite("yolov4.tflite")

yolo.inference("kite.jpg")

Training

from tensorflow.keras import callbacks, optimizers
from yolov4.tf import SaveWeightsCallback, YOLOv4

yolo = YOLOv4(tiny=True)

yolo.classes = "coco.names"
yolo.input_size = 608
yolo.batch_size = 32
yolo.subdivision = 16

yolo.make_model()
yolo.load_weights("yolov4-tiny.conv.29", weights_type="yolo")

train_data_set = yolo.load_dataset("train2017.txt")
val_data_set = yolo.load_dataset("val2017.txt")
# data_set = yolo.load_dataset("darknet/data/train.txt", dataset_type="yolo")

lr = 1e-4
epochs = 80000

optimizer = optimizers.Adam(learning_rate=lr)
yolo.compile(optimizer=optimizer, loss_iou_type="ciou")


def lr_scheduler(epoch):
    if epoch < 1000:
        return (epoch / 1000) * lr
    elif epoch < int(epochs * 0.8):
        return lr
    elif epoch < int(epochs * 0.9):
        return lr * 0.1
    else:
        return lr * 0.01


yolo.fit(
    train_data_set,
    epochs=epochs,
    callbacks=[
        callbacks.LearningRateScheduler(lr_scheduler),
        callbacks.TerminateOnNaN(),
        callbacks.TensorBoard(
            log_dir="/content/drive/My Drive/Hard_Soft/NN/logs",
        ),
        SaveWeightsCallback(
            yolo=yolo, weights_type="yolo", epoch_per_save=1000
        ),
    ],
    validation_data=val_data_set,
    validation_steps=100,
    validation_freq=100,
)

Custom training on Colab jupyter notebook

About

YOLOv4 Implemented in Tensorflow 2.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Jupyter Notebook 81.3%
  • Python 18.7%