Git Product home page Git Product logo

tf2-yolov4's Introduction

YOLOv4

A TensorFlow 2.0 implementation of YOLOv4: Optimal Speed and Accuracy of Object Detection

Pypi Version Python Versions Tensorflow Versions Code style: black Python package Open In Colab

This implementation runs (for now) inference with the original Darknet weights from AlexeyAB. See the roadmap section to see what's next.

Installation

To install this package, you can run:

pip install tf2_yolov4
pip install tensorflow
# Check that tf2_yolov4 is installed properly
python -c "from tf2_yolov4.model import YOLOv4; print(YOLOv4)"

Requirements:

  • MacOs >= 10.15 since tensorflow-addons is not available for older release of MacOs
  • Python >= 3.6
  • Compatible versions between TensorFlow and TensorFlow Addons: check the compatibility matrix

Examples in Colab

Pretrained weights

Our YOLOv4 implementation supports the weights argument similarly to Keras applications. To load a model with pretrained weights, you can simply call:

# Loads Darknet weights trained on COCO
model = YOLOv4(
    input_shape,
    num_classes,
    anchors,
    weights="darknet",
)

If weights are available locally, they will be used. Otherwise, they will be automatically downloaded.

Roadmap

  • Inference
    • CSPDarknet53 backbone with Mish activations
    • SPP Neck
    • YOLOv3 Head
    • Load Darknet Weights
    • Image loading and preprocessing
    • YOLOv3 box postprocessing
    • Handling non-square images
  • Training
    • Training loop with YOLOv3 loss
    • CIoU loss
    • Cross mini-Batch Normalization
    • Self-adversarial Training
    • Mosaic Data Augmentation
    • DropBlock
  • Enhancements
    • Automatic download of pretrained weights (like Keras applications)

References

tf2-yolov4's People

Contributors

antoinetoubhans avatar dependabot[bot] avatar lmontier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tf2-yolov4's Issues

TensorFlow Addons not available in Python 3.8

TF addons is not yet available on Py38, which is making the CI fail. We can either:

  • switch to an installation from github directly, as indicated here
  • wait patiently for addons to support py38
  • remove addons, which implies to recode the Mish activation layer in a less effective way

My 2 cents on this is to wait for Py38 support. We are making a repo that is compatible for TF2.0 and 2.1 so Py38 is not supported entirely anyway. Addons should push a working 3.8 version very soon.

Release 0.1.0 - Inference

What's needed for this first release:

  • Functional inference
  • Tested on imagenet examples
  • Test coverage > 80%
  • Documentation to predict

Custom model w/ AlexeyAB darknet : export fine, load_weights issue

Hello,
I trained a custom model with the c/cuda repo. model is (320,320,3) - 3 classes - custom anchors. I'm able to export the model using the convert_darknet_weights.py ( after modified the INPUT_SHAPE and NUM_CLASSES). However when doing :

import tensorflow as tf

from tf2_yolov4.anchors import YOLOV4_ANCHORS
from tf2_yolov4.model import YOLOv4

model = YOLOv4(
    input_shape=(320, 320, 3),
    anchors=YOLOV4_ANCHORS,
    num_classes=3,
    training=False,
    yolo_max_boxes=100,
    yolo_iou_threshold=0.5,
    yolo_score_threshold=0.5,
)

model.load_weights("....path-to-converted-weights.h5...") 

This is what I get :

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-f6463dea2434> in <module>
----> 1 model.load_weights("./yolov4.h5")

5 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in assert_is_compatible_with(self, other)
   1115     """
   1116     if not self.is_compatible_with(other):
-> 1117       raise ValueError("Shapes %s and %s are incompatible" % (self, other))
   1118 
   1119   def most_specific_compatible_shape(self, other):

ValueError: Shapes (3, 3, 128, 256) and (1024, 512, 3, 3) are incompatible

Better management of __init__ files

I can't do this:

from tf2_yolov4 import YOLOv4

When I do this, it fails:

import tf2_yolov4

yolo_model = tf2_yolov4.model.YOLOv4

I think it's related to our init files being empty. We should add the __all__ export in those init

Training on custom dataset

  • Add YOLO Loss from YOLOv3
  • Convert box annotations to a format adapted to the YOLO loss
  • Load a training set from an annotation file
  • Colab example on Pascal VOC
  • Colab example on custom example

Metrics on official datasets

Now that training loop is nearly done, we need to evaluate how our implementation is doing vs popular datasets (I'm thinking Pascal VOC and COCO) in terms of AP, mAP, ...

Eventually, those metrics could be added into TensorBoard along training.

Error Running colab: "!convert-darknet-weights yolov4.weights -o yolov4.h5"

Hi, I am having error running colab notebook. I tried to run on another instance but it did not work either,

Error raised while converting .weight to .h5:
"!convert-darknet-weights yolov4.weights -o yolov4.h5"

Error:

2020-08-05 10:23:48.397401: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1 2020-08-05 10:23:48.447336: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-08-05 10:23:48.448105: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties: pciBusID: 0000:00:04.0 name: Tesla K80 computeCapability: 3.7 coreClock: 0.8235GHz coreCount: 13 deviceMemorySize: 11.17GiB deviceMemoryBandwidth: 223.96GiB/s 2020-08-05 10:23:48.448420: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1 2020-08-05 10:23:48.720643: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10 2020-08-05 10:23:48.857943: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10 2020-08-05 10:23:48.879975: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10 2020-08-05 10:23:49.188024: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10 2020-08-05 10:23:49.225123: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10 2020-08-05 10:23:49.789556: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7 2020-08-05 10:23:49.789774: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-08-05 10:23:49.790641: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-08-05 10:23:49.791392: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0 2020-08-05 10:23:49.791845: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2020-08-05 10:23:49.799011: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2200000000 Hz 2020-08-05 10:23:49.799288: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x27b0bc0 initialized for platform Host (this does not guarantee that XLA will be used). Devices: 2020-08-05 10:23:49.799322: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host, Default Version 2020-08-05 10:23:49.886345: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-08-05 10:23:49.887205: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x27b0d80 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices: 2020-08-05 10:23:49.887274: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Tesla K80, Compute Capability 3.7 2020-08-05 10:23:49.888592: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-08-05 10:23:49.889296: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties: pciBusID: 0000:00:04.0 name: Tesla K80 computeCapability: 3.7 coreClock: 0.8235GHz coreCount: 13 deviceMemorySize: 11.17GiB deviceMemoryBandwidth: 223.96GiB/s 2020-08-05 10:23:49.889360: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1 2020-08-05 10:23:49.889395: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10 2020-08-05 10:23:49.889444: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10 2020-08-05 10:23:49.889522: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10 2020-08-05 10:23:49.889558: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10 2020-08-05 10:23:49.889587: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10 2020-08-05 10:23:49.889618: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7 2020-08-05 10:23:49.889698: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-08-05 10:23:49.890401: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-08-05 10:23:49.891066: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0 2020-08-05 10:23:49.891133: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1 2020-08-05 10:23:49.897215: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix: 2020-08-05 10:23:49.897263: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] 0 2020-08-05 10:23:49.897305: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0: N 2020-08-05 10:23:49.897485: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-08-05 10:23:49.898322: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:981] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2020-08-05 10:23:49.899023: W tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:39] Overriding allow_growth setting because the TF_FORCE_GPU_ALLOW_GROWTH environment variable is set. Original config value was 0. 2020-08-05 10:23:49.899085: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10691 MB memory) -> physical GPU (device: 0, name: Tesla K80, pci bus id: 0000:00:04.0, compute capability: 3.7) Download original Darknet weights 257196032/261095424 [============================>.] - ETA: 0sConverting original Darknet weights to .h5 format Traceback (most recent call last): File "/usr/local/bin/convert-darknet-weights", line 8, in <module> sys.exit(convert_darknet_weights()) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 829, in __call__ return self.main(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 782, in main rv = self.invoke(ctx) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1066, in invoke return ctx.invoke(self.callback, **ctx.params) File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 610, in invoke return callback(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/tf2_yolov4/tools/convert_darknet_weights.py", line 41, in convert_darknet_weights input_shape=INPUT_SHAPE, num_classes=num_classes, anchors=YOLOV4_ANCHORS File "/usr/local/lib/python3.6/dist-packages/tf2_yolov4/model.py", line 80, in YOLOv4 yolov4.load_weights(weights_path, by_name=True, skip_mismatch=True) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py", line 250, in load_weights return super(Model, self).load_weights(filepath, by_name, skip_mismatch) File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py", line 1227, in load_weights if _is_hdf5_filepath(filepath): File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py", line 1616, in _is_hdf5_filepath return (filepath.endswith('.h5') or filepath.endswith('.keras') or AttributeError: 'PosixPath' object has no attribute 'endswith' ls: cannot access './yolov4.h5': No such file or directory

Error in colab notebook, cannot reshape array of size 687 into shape (32,3,3,3)

In colab notebook YoloV4_Dectection_Example.ipynb, at the cell:

!convert-darknet-weights yolov4.weights -o yolov4.h5
!ls -la ./yolov4.h5

I hit error:

Traceback (most recent call last):
  File "/usr/local/bin/convert-darknet-weights", line 8, in <module>
    sys.exit(convert_darknet_weights())
  File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/tf2_yolov4/tools/convert_darknet_weights.py", line 103, in convert_darknet_weights
    .reshape((filters, input_filters, kernel_size[0], kernel_size[1]))
ValueError: cannot reshape array of size 687 into shape (32,3,3,3)

ls: cannot access './yolov4.h5': No such file or directory

I confirmed yolov4.weights was downloaded successfully. Clearly 'reshape array of size 687 into shape (32,3,3,3)' is not going to succeed

Inference failing with exported tfLite models

The following code results in an error:

import tensorflow as tf
import numpy as np

# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="yolov4.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test the model on random input data.
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
RuntimeError: Encountered unresolved custom op: CombinedNonMaxSuppression.Node number 603 (CombinedNonMaxSuppression) failed to prepare.

An issue is opened in Tensorflow repo to add CombinedNonMaxSuppression op to flex, which will hopefully resolve this :
tensorflow/tensorflow#41012

Can not instanciate model for certain input shapes

How to reproduce

  • python: 3.6
  • tensorflow: 2.2.0
  • tf2_yolov4: 0.0.1
from tf2_yolov4.anchors import YOLOV4_ANCHORS
from tf2_yolov4.model import YOLOv4

YOLOv4(
    input_shape=(400, 820, 3),
    anchors=YOLOV4_ANCHORS,
    num_classes=80,
    training=False,
    yolo_max_boxes=100,
    yolo_iou_threshold=0.5,
    yolo_score_threshold=0.25,
)

I got an error :

ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 25, 51, 256), (None, 24, 50, 256)]

Loading weight file containing 217 layers into a model with 3 layers

Hello Guys! I have trained a Yolov4 model and I got the .weights file back. I am trying to convert the file into .h5 but there is my first Problem.
This is the error message that I get back when trying to convert to .h5. Does anybody know why this might occur?
"ValueError: cannot reshape array of size 4554552 into shape (1024,512,3,3)"

Anyways I found another way to get my file into the .h5 format. But is there the possibility that the 2 different converters are returning different results since I now get this error when trying to create a YOLOv4 Model:
"ValueError: You are trying to load a weight file containing 217 layers into a model with 3 layers."

This is the code that threw the error:

model = YOLOv4( input_shape=(HEIGHT, WIDTH, 3), anchors=YOLOV4_ANCHORS, num_classes=1, training=False, yolo_max_boxes=50, yolo_iou_threshold=0.5, yolo_score_threshold=0.5, )

As you can see I have changed the number of classes but nothing else.

Might the other used converter be the reason for this problem or is there something that I am missing?
Please help me out :D

Pretrained Weights management

For now, it is difficult to generate the h5 file and load it into our model.

This issue should tackle:

  • Automatic download of the AlexeyAB/darknet weights
  • Add a weights="coco" parameter to generate a model with loaded weights already

Import custom model error `ValueError: No such layer: conv2d_32`

Hello! I am trying to import a custom Yolov4 model with 5 classes. I trained the model with the darknet library. The weights are proper because I have processed with them a video already.

I used this code based on the convert_darknet_weights function.

from tf2_yolov4.anchors import YOLOV4_ANCHORS
from tf2_yolov4.model import YOLOv4
from tf2_yolov4.tools.weights import load_darknet_weights_in_yolo
import numpy as np

num_classes = 5
INPUT_SHAPE = (416, 416, 3)
output_weights_path = '/content/yolov4.h5'
_weight_path='/content/drive/My\ Drive/20200722/yolow_backup/yolov4-custom_best.weights'

model = YOLOv4(
        input_shape=INPUT_SHAPE, num_classes=num_classes, anchors=YOLOV4_ANCHORS,
        weights=None,
        training=False
)
model.predict(np.random.random((1, *INPUT_SHAPE)))
model = load_darknet_weights_in_yolo(model, darknet_weights_path=_weight_path)
model.save(output_weights_path)

And I got this error:

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-46-94ab431f8673> in <module>()
     17 
     18 model.predict(np.random.random((1, *INPUT_SHAPE)))
---> 19 model = load_darknet_weights_in_yolo(model, darknet_weights_path=_weight_path)
     20 model.save(output_weights_path)

1 frames

/content/tf2-yolov4/tf2_yolov4/tools/weights.py in load_darknet_weights_in_yolo(yolo_model, darknet_weights_path)
     31     """
     32     sample_conv_weights = (
---> 33         yolo_model.get_layer("CSPDarknet53").get_layer("conv2d_32").get_weights()[0]
     34     )
     35 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/network.py in get_layer(self, name, index)
    561       if layer.name == name:
    562         return layer
--> 563     raise ValueError('No such layer: ' + name)
    564 
    565   @property

ValueError: No such layer: conv2d_32

What causes the error?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.