hhk7734 / tensorflow-yolov4

YOLOv4 Implemented in Tensorflow 2.
MIT License
136 stars 75 forks source link

Error while yolo.make_model() #72

Closed ayanjava closed 3 years ago

ayanjava commented 3 years ago
python -m pip show yolov4
Name: yolov4
Version: 3.2.0
Summary: YOLOv4: Optimal Speed and Accuracy of Object Detection
Home-page: https://github.com/hhk7734/tensorflow-yolov4
Author: Hyeonki Hong
Author-email: hhk7734@gmail.com
License: MIT
Location: c:\programdata\anaconda3\lib\site-packages
Requires: numpy
Required-by:

Code:

from yolov4.tf import YOLOv4

yolo = YOLOv4()
# yolo = YOLOv4(tiny=True)

yolo.classes = "coco.names"
yolo.input_size = (640, 480)

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

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

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

Error:

library cudnn64_8.dll
2021-03-02 21:08:09.901846: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1862] Adding visible gpu devices: 0
Call tf.config.experimental.set_memory_growth(GPU0, True)
Traceback (most recent call last):
  File "C:\Users\E\Documents\GitHub\cgan\yolo4.py", line 9, in <module>
    yolo.make_model()
  File "C:\ProgramData\Anaconda3\lib\site-packages\yolov4\tf\__init__.py", line 58, in make_model
    _input = Input(self.config.net.input_shape)
  File "C:\ProgramData\Anaconda3\lib\site-packages\yolov4\common\config.py", line 132, in __getattr__
    return self._metalayers[metalayer]
KeyError: 'net'

Could you please help me to resolve this issue?

hhk7734 commented 3 years ago

your test script is for v2.x.x.

the code below is for v3.

import cv2

from yolov4.tf import YOLOv4

yolo = YOLOv4()

yolo.config.parse_names("coco.names")
yolo.config.parse_cfg("yolov4-tiny.cfg")

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

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

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

yolo.inference(
    "/dev/video0",
    is_image=False,
    cv_apiPreference=cv2.CAP_V4L2,
    cv_frame_size=(640, 480),
    cv_fourcc="YUYV",
)

https://wiki.loliot.net/docs/lang/python/libraries/yolov4/python-yolov4-about#tensorflow

ayanjava commented 3 years ago

@hhk7734 Thank you so much:), Now it working and getting memory error ( I have 8 GB GPU) for this code.

 I tensorflow/stream_executor/cuda/cuda_driver.cc:789] failed to allocate 3.99G (4280207104 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
YOLOv4: Inference is finished
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 461.40       Driver Version: 461.40       CUDA Version: 11.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  GeForce RTX 206... WDDM  | 00000000:01:00.0 Off |                  N/A |
|  0%   40C    P8    28W / 215W |    161MiB /  8192MiB |      0%      Default |
|                               |                      |                  N/A
hhk7734 commented 3 years ago

This is the result when I ran yolov4-tiny.

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|

...

|    0   N/A  N/A     19436      C   ...ow-yolov4/venv/bin/python     4173MiB |
+-----------------------------------------------------------------------------+
ayanjava commented 3 years ago

@hhk7734 Thank you :). It's running only one Inference on same time.

First one is running, when I comment only video one running.

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

yolo.inference(media_path="video\Car.mp4", is_image=False)
I tensorflow/stream_executor/cuda/cuda_driver.cc:789] failed to allocate 3.99G (4280207104 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory
time: 4093.83 ms
YOLOv4: Inference is finished
Traceback (most recent call last):
  File "yolo4.py", line 17, in <module>
    yolo.inference(media_path="kite.jpg")
  File "C:\ProgramData\Anaconda3\lib\site-packages\yolov4\common\base_class.py", line 219, in inference
    while cv2.waitKey(10) & 0xFF != ord("q"):
hhk7734 commented 3 years ago

Try this.

import tensorflow as tf

gpus = tf.config.experimental.list_physical_devices("GPU")
if gpus:
    # Restrict TensorFlow to only allocate 1GB of memory on the first GPU
    try:
        tf.config.experimental.set_virtual_device_configuration(
            gpus[0],
            [
                tf.config.experimental.VirtualDeviceConfiguration(
                    memory_limit=1024 # <-----
                )
            ],
        )
        logical_gpus = tf.config.experimental.list_logical_devices("GPU")
        print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
    except RuntimeError as e:
        # Virtual devices must be set before GPUs have been initialized
        print(e)
ayanjava commented 3 years ago

@hhk7734 Thank you :). No luck with this code.

ayanjava commented 3 years ago

Code:

import cv2
import os
os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices'
import tensorflow as tf
from yolov4.tf import YOLOv4

gpus = tf.config.list_physical_devices('GPU')
if gpus:
  try:
    # Currently, memory growth needs to be the same across GPUs
    for gpu in gpus:
      tf.config.experimental.set_memory_growth(gpu, True)
    logical_gpus = tf.config.experimental.list_logical_devices('GPU')
    print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
  except RuntimeError as e:
    # Memory growth must be set before GPUs have been initialized
    print(e)

yolo = YOLOv4()

yolo.config.parse_names("coco.names")
yolo.config.parse_cfg("yolov4.cfg")

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

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

# yolo.inference(media_path="video\Car.mp4", is_image=False)

# yolo.inference(
#     "/dev/video0",
#     is_image=False,
#     cv_apiPreference=cv2.CAP_V4L2,
#     cv_frame_size=(640, 480),
#     cv_fourcc="YUYV",
# )

Now I'm getting below error: yolo.make_model()

tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): GeForce RTX 2060 SUPER, Compute Capability 7.5
1 Physical GPUs, 1 Logical GPUs
WARNING:tensorflow:AutoGraph could not transform <bound method YOLOv4Model.call of <yolov4.tf.model.YOLOv4Model object at 0x0000017B0EF02550>> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: module 'gast' has no attribute 'Index'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
WARNING:tensorflow:AutoGraph could not transform <function YoloLayer.__init__.<locals>._coords_0 at 0x0000017B15783820> and will run it as-is.
Please report this to the TensorFlow team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output.
Cause: module 'gast' has no attribute 'Index'
To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
Traceback (most recent call last):
  File "yolo4.py", line 29, in <module>
    yolo.make_model()
  File "C:\Users\E\.conda\envs\P38T23\lib\site-packages\yolov4\tf\__init__.py", line 60, in make_model
    self._model(_input)
  File "C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\base_layer.py", line 951, in __call__
    return self._functional_construction_call(inputs, args, kwargs,
  File "C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1090, in _functional_construction_call
    outputs = self._keras_tensor_symbolic_call(
  File "C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\base_layer.py", line 822, in _keras_tensor_symbolic_call
    return self._infer_output_signature(inputs, args, kwargs, input_masks)
  File "C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\engine\base_layer.py", line 863, in _infer_output_signature
    outputs = call_fn(inputs, *args, **kwargs)
  File "C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\autograph\impl\api.py", line 670, in wrapper
    raise e.ag_error_metadata.to_exception(e)
tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: in user code:

    C:\Users\E\.conda\envs\P38T23\lib\site-packages\yolov4\tf\layers\yolo_layer.py:135 call  *
        return self._yolo_function(x, training)
    C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\def_function.py:828 __call__  **
        result = self._call(*args, **kwds)
    C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\def_function.py:871 _call
        self._initialize(args, kwds, add_initializers_to=initializers)
    C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\def_function.py:725 _initialize
        self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
    C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\function.py:2969 _get_concrete_function_internal_garbage_collected
        graph_function, _ = self._maybe_define_function(args, kwargs)
    C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\function.py:3361 _maybe_define_function
        graph_function = self._create_graph_function(args, kwargs)
    C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\function.py:3196 _create_graph_function
        func_graph_module.func_graph_from_py_func(
    C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\framework\func_graph.py:990 func_graph_from_py_func
        func_outputs = python_func(*func_args, **func_kwargs)
    C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\eager\def_function.py:634 wrapped_fn
        out = weak_wrapped_fn().__wrapped__(*args, **kwds)
    C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\framework\func_graph.py:966 wrapper
        return autograph.converted_call(
    C:\Users\E\.conda\envs\P38T23\lib\site-packages\yolov4\tf\layers\yolo_layer.py:59 _coords_0  **
        if scale_x_y == 1.0:
    C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\framework\ops.py:885 __bool__
        self._disallow_bool_casting()
    C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\framework\ops.py:488 _disallow_bool_casting
        self._disallow_when_autograph_enabled(
    C:\Users\E\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\framework\ops.py:474 _disallow_when_autograph_enabled
        raise errors.OperatorNotAllowedInGraphError(

    OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature.
hhk7734 commented 3 years ago

https://www.tensorflow.org/xla#enable_xla_for_tensorflow_models

Modify @tf.function to @tf.function(jit_compile=True) and reinstall manually. Then try again.

I don't know exactly what XLA is, so it seems difficult to give detailed help.

ayanjava commented 3 years ago

@hhk7734 Thank you, XLS issue fixed after adding below.

os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices'

Error:

 File "yolo4.py", line 29, in <module>
    yolo.make_model()
  File "C:\Users\E\.conda\envs\P38T23\lib\site-packages\yolov4\tf\__init__.py", line 60, in make_model
    self._model(_input)

I will try to fix. Thank you so much for your help :)