Skip to content

No numpy calls should be a precondition #363

@khatchad

Description

@khatchad

The numpy API is not directly compatible with tf.function. The code needs to be restructured first. A first step to deal with this is to make no numpy calls a refactoring precondition. Otherwise, we can get an error after running the refactored code, e.g.:

Traceback (most recent call last):
  File "/home/rk1424/git/Python-Subjects/tensorflow-yolov4-tflite/train.py", line 165, in <module>
    app.run(main)
  File "/home/rk1424/.local/lib/python3.10/site-packages/absl/app.py", line 308, in run
    _run_main(main, args)
  File "/home/rk1424/.local/lib/python3.10/site-packages/absl/app.py", line 254, in _run_main
    sys.exit(main(argv))
  File "/home/rk1424/git/Python-Subjects/tensorflow-yolov4-tflite/train.py", line 156, in main
    train_step(image_data, target)
  File "/home/rk1424/.local/lib/python3.10/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/tmp/__autograph_generated_filest45satl.py", line 48, in tf__train_step
    ag__.converted_call(ag__.ld(tf).print, ('=> STEP %4d/%4d   lr: %.6f   giou_loss: %4.2f   conf_loss: %4.2f   prob_loss: %4.2f   total_loss: %4.2f' % (ag__.ld(global_steps), ag__.ld(total_steps), ag__.converted_call(ag__.ld(optimizer).lr.numpy, (), None, fscope), ag__.ld(giou_loss), ag__.ld(conf_loss), ag__.ld(prob_loss), ag__.ld(total_loss)),), None, fscope)
NotImplementedError: in user code:

    File "/home/rk1424/git/Python-Subjects/tensorflow-yolov4-tflite/train.py", line 99, in train_step  *
        tf.print("=> STEP %4d/%4d   lr: %.6f   giou_loss: %4.2f   conf_loss: %4.2f   "

    NotImplementedError: numpy() is only available when eager execution is enabled.

The code for this example is at;

https://github.com/hunglc007/tensorflow-yolov4-tflite/blob/9f16748aa3f45ff240608da4bd9b1216a29127f5/train.py#L98-L110.

The enclosing function should fail refactoring preconditions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpreconditionsRefactoring preconditions need to be added.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions