david8862 / keras-YOLOv3-model-set

end-to-end YOLOv4/v3/v2 object detection pipeline, implemented on tf.keras with different technologies
MIT License
640 stars 222 forks source link

Metadata TFlite/ Keras to Tensorflow .pb conversion #241

Open VanqCoding opened 1 year ago

VanqCoding commented 1 year ago

Hello David, A post-training-int-quantized tflite tiny yolo3 model is giving me an error "Input tensor has type kTLiteFloat32: it requires specifying NormalizationOptions metadata to preprocess input images" when trying to run it on a Raspberry Pi. Is there a way to generate a metadata for it?

Second issue: I have no success converting my dumped .h5 model to .pb format. I have tried keras_to_tensorflow.py and keras_to_tensorflow_bk.py

2022-12-15 18:32:51,192 - INFO - Saved the graph definition in ascii format at C:\Desktop\keras-YOLOv3-model-set-master.pbtxt Traceback (most recent call last): File "tools/model_converter/keras_to_tensorflow.py", line 176, in <module> main() File "tools/model_converter/keras_to_tensorflow.py", line 172, in main keras_to_tensorflow(args) File "tools/model_converter/keras_to_tensorflow.py", line 136, in keras_to_tensorflow from tensorflow.tools.graph_transforms import TransformGraph ModuleNotFoundError: No module named 'tensorflow.tools.graph_transforms'

Is this a tensorflow version compatibility issue?

david8862 commented 1 year ago

@MrVanq what's your TF version?

VanqCoding commented 1 year ago

@david8862 I am on TF 2.10.1, using a conda env on Windows 11. Training/eval and everything else has been working pretty well so far. here is the full package list:

_tflow_select             2.1.0                       gpu
abseil-cpp                20210324.2           hd77b12b_0
absl-py                   1.3.0            py37haa95532_0
aiohttp                   3.8.1            py37h2bbff1b_1
aiosignal                 1.2.0              pyhd3eb1b0_0
appdirs                   1.4.4                    pypi_0    pypi
arcgispro                 3.0                           0    esri
astor                     0.8.1            py37haa95532_0
astunparse                1.6.3                      py_0
async-timeout             4.0.2            py37haa95532_0
asynctest                 0.13.0                     py_0    conda-forge
attrs                     22.1.0           py37haa95532_0
blas                      1.0                         mkl    conda-forge
blinker                   1.4              py37haa95532_0
bokeh                     2.4.3                    pypi_0    pypi
brotlipy                  0.7.0           py37h2bbff1b_1003
ca-certificates           2022.10.11           haa95532_0
cachetools                4.2.2              pyhd3eb1b0_0
certifi                   2022.9.24        py37haa95532_0
cffi                      1.15.1           py37h2bbff1b_0
charset-normalizer        2.0.4              pyhd3eb1b0_0
click                     8.0.4            py37haa95532_0
colorama                  0.4.5            py37haa95532_0
coloredlogs               15.0.1                   pypi_0    pypi
coremltools               6.1                      pypi_0    pypi
cryptography              38.0.1           py37h21b164f_0
cudatoolkit               11.3.1               h59b6b97_2
cudnn                     8.2.1                cuda11.3_0
cycler                    0.11.0             pyhd8ed1ab_0    conda-forge
cython                    0.29.32          py37hf2a7229_0    conda-forge
dataclasses               0.8                pyh6d0b6a4_7
dm-tree                   0.1.7                    pypi_0    pypi
fftw                      3.3.9                h2bbff1b_1
fire                      0.4.0                    pypi_0    pypi
flatbuffers               2.0.7                    pypi_0    pypi
freetype                  2.10.4               h546665d_1    conda-forge
frozenlist                1.2.0            py37h2bbff1b_0
gast                      0.4.0              pyhd3eb1b0_0
giflib                    5.2.1                h62dcd97_0
google-auth               2.6.0              pyhd3eb1b0_0
google-auth-oauthlib      0.4.1                      py_2    conda-forge
google-pasta              0.2.0              pyhd3eb1b0_0
grpcio                    1.42.0           py37hc60d5dd_0
h5py                      3.7.0            py37h3de5c98_0
hdf5                      1.10.6               h1756f20_1
humanfriendly             10.0                     pypi_0    pypi
icc_rt                    2022.1.0             h6049295_2
icu                       68.1                 h6c2663c_0
idna                      3.4              py37haa95532_0
imagecorruptions          1.1.2                    pypi_0    pypi
imageio                   2.22.4                   pypi_0    pypi
imgaug                    0.4.0                    pypi_0    pypi
importlib-metadata        4.11.3           py37haa95532_0
intel-openmp              2021.4.0          haa95532_3556
jbig                      2.1               h8d14728_2003    conda-forge
jinja2                    3.1.2                    pypi_0    pypi
jpeg                      9e                   h2bbff1b_0
keras                     2.10.0                   pypi_0    pypi
keras-applications        1.0.8                    pypi_0    pypi
keras-preprocessing       1.1.2              pyhd3eb1b0_0
keras2onnx                1.7.0                    pypi_0    pypi
kiwisolver                1.4.4            py37h8c56517_0    conda-forge
lcms2                     2.12                 h2a16943_0    conda-forge
lerc                      2.2.1                h0e60522_0    conda-forge
libclang                  14.0.6                   pypi_0    pypi
libcurl                   7.85.0               h86230a5_0
libdeflate                1.7                  h8ffe710_5    conda-forge
libpng                    1.6.37               h2a8f88b_0
libprotobuf               3.17.2               h23ce68f_1
libssh2                   1.10.0               hcd4344a_0
libtiff                   4.3.0                h0c97f57_1    conda-forge
lz4-c                     1.9.3                h8ffe710_1    conda-forge
markdown                  3.3.4            py37haa95532_0
markupsafe                2.1.1                    pypi_0    pypi
matplotlib                3.4.3           py37_arcgispro_4  [arcgispro]  esri
matplotlib-base           3.4.3            py37h4a79c79_2    conda-forge
mkl                       2021.4.0           haa95532_640
mkl-service               2.4.0            py37h2bbff1b_0
mkl_fft                   1.3.1            py37h277e83a_0
mkl_random                1.2.2            py37hf11a4ad_0
mnn                       1.1.0                    pypi_0    pypi
mpmath                    1.2.1                    pypi_0    pypi
multidict                 6.0.2            py37h2bbff1b_0
networkx                  2.6.3                    pypi_0    pypi
numpy                     1.21.6                   pypi_0    pypi
oauthlib                  3.2.1            py37haa95532_0
olefile                   0.46               pyh9f0ad1d_1    conda-forge
onnx                      1.12.0                   pypi_0    pypi
onnxconverter-common      1.13.0                   pypi_0    pypi
onnxruntime               1.13.1                   pypi_0    pypi
opencv-contrib-python     4.2.0.32                 pypi_0    pypi
opencv-python             4.2.0.32                 pypi_0    pypi
openjpeg                  2.4.0                hb211442_1    conda-forge
openssl                   1.1.1n                        0    esri
opt_einsum                3.3.0              pyhd3eb1b0_1
packaging                 21.3                     pypi_0    pypi
pandas                    1.1.5                    pypi_0    pypi
pillow                    8.3.2            py37hd7d9ad0_0    conda-forge
pip                       22.2.2           py37haa95532_0
protobuf                  3.17.2           py37hd77b12b_0
pyasn1                    0.4.8              pyhd3eb1b0_0
pyasn1-modules            0.2.8                      py_0
pycocotools               2.0.2            py37h5685391_1    esri
pycparser                 2.21               pyhd3eb1b0_0
pyjwt                     2.4.0            py37haa95532_0
pyopenssl                 22.0.0             pyhd3eb1b0_0
pyparsing                 3.0.9              pyhd8ed1ab_0    conda-forge
pyreadline                2.1                      pypi_0    pypi
pysocks                   1.7.1                    py37_1
python                    3.7.0                hea74fb7_0
python-dateutil           2.8.2              pyhd8ed1ab_0    conda-forge
python_abi                3.7                     2_cp37m    conda-forge
pytz                      2022.6                   pypi_0    pypi
pywavelets                1.3.0                    pypi_0    pypi
pyyaml                    6.0                      pypi_0    pypi
requests                  2.28.1           py37haa95532_0
requests-oauthlib         1.3.0                      py_0
rsa                       4.7.2              pyhd3eb1b0_1
scikit-image              0.19.3                   pypi_0    pypi
scikit-learn              0.20.3                   pypi_0    pypi
scipy                     1.7.3            py37h7a0a035_2
seaborn                   0.12.1                   pypi_0    pypi
setuptools                65.5.0           py37haa95532_0
shapely                   1.8.5.post1              pypi_0    pypi
six                       1.16.0             pyhd3eb1b0_1
snappy                    1.1.9                h6c2663c_0
sqlite                    3.39.3               h2bbff1b_0
sympy                     1.10.1                   pypi_0    pypi
tensorboard               2.10.1                   pypi_0    pypi
tensorboard-data-server   0.6.0            py37haa95532_0
tensorboard-plugin-wit    1.8.1            py37haa95532_0
tensorflow                2.10.1                   pypi_0    pypi
tensorflow-addons         0.18.0                   pypi_0    pypi
tensorflow-estimator      2.10.0                   pypi_0    pypi
tensorflow-gpu            2.10.1                   pypi_0    pypi
tensorflow-io-gcs-filesystem 0.27.0                   pypi_0    pypi
tensorflow-model-optimization 0.7.3                    pypi_0    pypi
termcolor                 2.1.0            py37haa95532_0
tf2onnx                   1.13.0                   pypi_0    pypi
tfcoreml                  1.1                      pypi_0    pypi
tidecv                    1.0.1                    pypi_0    pypi
tifffile                  2021.11.2                pypi_0    pypi
tk                        8.6.12               h8ffe710_0    conda-forge
tornado                   6.2              py37hcc03f2d_0    conda-forge
tqdm                      4.64.1                   pypi_0    pypi
typeguard                 2.13.3                   pypi_0    pypi
typing-extensions         4.3.0            py37haa95532_0
typing_extensions         4.3.0            py37haa95532_0
urllib3                   1.26.12          py37haa95532_0
vc                        14.2                 h21ff451_1
vs2015_runtime            14.27.29016          h5e58377_2    esri
werkzeug                  2.0.3              pyhd3eb1b0_0
wheel                     0.35.1             pyhd3eb1b0_0
win_inet_pton             1.1.0            py37haa95532_0
wincertstore              0.2              py37haa95532_2
wrapt                     1.14.1           py37h2bbff1b_0
xz                        5.2.6                h8d14728_0    conda-forge
yarl                      1.8.1            py37h2bbff1b_0
zipp                      3.8.0            py37haa95532_0
zlib                      1.2.13               h8cc25b3_0
zstd                      1.5.0                h6255e5f_0    conda-forge
david8862 commented 1 year ago

@MrVanq I guess you're trying to do pose traning quantize for a model, but I didn't use keras_to_tensorflow.py for that. Maybe you can try post_train_quant_convert.py and it should works on TF 2.10.1

VanqCoding commented 1 year ago

@david8862 post_train_quant_convert.py works and converts my model to a post-training-int-quantized .tflite model which I sadly can't use with python on the RaspberryPi with the tflite_support framework, I found out that it only supports SSD models. So now I am trying to convert the .h5 model to a frozen .pb model so I can atleast run OpenCV cv.dnn.readNet('frozen_model.pb') with it on the RaspberryPi. Is there a way to convert keras to frozen tensorflow? (with post training quantize it would be ofc better but if not I just want to convert it as it is to a frozen tensorflow .pb) Any help is appreciated.

david8862 commented 1 year ago

@david8862 post_train_quant_convert.py works and converts my model to a post-training-int-quantized .tflite model which I sadly can't use with python on the RaspberryPi with the tflite_support framework, I found out that it only supports SSD models. So now I am trying to convert the .h5 model to a frozen .pb model so I can atleast run OpenCV cv.dnn.readNet('frozen_model.pb') with it on the RaspberryPi. Is there a way to convert keras to frozen tensorflow? (with post training quantize it would be ofc better but if not I just want to convert it as it is to a frozen tensorflow .pb) Any help is appreciated.

Sorry but I didn't dig into the quantized pb model convert. The --quantize option in keras_to_tensorflow.py is inherited from the original repo keras_to_tensorflow