Seite 1 von 1

Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Samstag 7. Dezember 2019, 18:30
von Felix92
Huhu und zwar verzweifele ich gerade daran ein Tensorflow frozen_graph.pb in eine .tflite zu konvertieren unter Tensorflow 2.0.
Vlt. kann mir ja hier jmd. helfen der damit schon Erfahrung hat und erklären wo mein Fehler liegt :)

Hier der Graph und die Map:
https://gofile.io/?c=YjsCuY

Gegeben:
frozen_graph.pb
label_map.pbtxt
PS: Retrain ist leider nicht möglich

input_tensor:

Code: Alles auswählen

Tensor("image_tensor:0", shape=(None, None, None, 3), dtype=uint8)
Skript zur Konvertierung:

Code: Alles auswählen

import tensorflow as tf

graph_def_file = "traffic_graph.pb"
input_arrays = ["image_tensor"] 
output_arrays = ['num_detections', 'detection_boxes', 'detection_scores', 'detection_classes']

converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph(
        graph_def_file, input_arrays, output_arrays, input_shapes={"image_tensor":[None, 800, 600, 3]})    #wäre aktuelle Webcam input [None, None, None, 3] mag er nicht
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
Fehler:

Code: Alles auswählen

2019-12-07 18:07:36.188665: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before Removing unused ops: 4129 operators, 6953 arrays (0 quantized)
2019-12-07 18:07:36.870121: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] After Removing unused ops pass 1: 4081 operators, 6862 arrays (0 quantized)
2019-12-07 18:07:37.941125: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before general graph transformations: 4081 operators, 6862 arrays (0 quantized)
2019-12-07 18:07:38.160704: F tensorflow/lite/toco/graph_transformations/resolve_constant_slice.cc:59] Check failed: dim_size >= 1 (0 vs. 1)
Fatal Python error: Aborted

Current thread 0x00000dd4 (most recent call first):
  File "D:\Programme\Anaconda\lib\site-packages\tensorflow_core\lite\toco\python\toco_from_protos.py", line 52 in execute
  File "D:\Programme\Anaconda\lib\site-packages\absl\app.py", line 250 in _run_main
  File "D:\Programme\Anaconda\lib\site-packages\absl\app.py", line 299 in run
  File "D:\Programme\Anaconda\lib\site-packages\tensorflow_core\python\platform\app.py", line 40 in run
  File "D:\Programme\Anaconda\lib\site-packages\tensorflow_core\lite\toco\python\toco_from_protos.py", line 89 in main
  File "D:\Programme\Anaconda\Scripts\toco_from_protos-script.py", line 10 in <module>

Weitere Frage:
Ich versuche schon die ganze Zeit TF-gpu (Anaconda) unter Windows zu nutzen allerdings scheitere ich immer an diesem Fehler:

Code: Alles auswählen

2019-06-28 00:04:13.171528: W tensorflow/stream_executor/cuda/redzone_allocator.cc:294] Internal: Invoking ptxas not supported on Windows Relying on driver to perform ptx compilation 2019-06-28 00:04:13.198421: W tensorflow/stream_executor/cuda/redzone_allocator.cc:294] Internal: Invoking ptxas not supported on Windows Relying on driver to perform ptx compilation 2019-06-28 00:04:13.207294: W tensorflow/stream_executor/cuda/redzone_allocator.cc:294] Internal: Invoking ptxas not supported on Windows Relying on driver to perform ptx compilation
Github und StackOverflow gibt leider auch nichts brauchbares her :(
Versuche:
Treiber downgrade
Anaconda komplett neu installiert

Ideen/Erfahrungen ?

Vielen Dank für eure Hilfe !
MfG Felix
Tensorflow-gpu über pip und CUDA direkt von NVIDIA

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Montag 9. Dezember 2019, 08:13
von Felix92
Keiner ? :(

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Montag 9. Dezember 2019, 08:47
von Jankie
Kenne mich damit leider nicht aus, habe aber gesehen es gibt eine Möglichkeit über die Kommandozeile, vielleicht klappt das ja:

https://www.tensorflow.org/lite/convert ... e_examples

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Montag 9. Dezember 2019, 10:38
von Felix92
Danke @Jankie das habe ich auch schon probiert funktioniert leider nicht :/

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Montag 9. Dezember 2019, 19:48
von Felix92
Kein TF-Guru hier ? ^^
Oder jmd. mit Erfahrung (TF2.0) der mir den Graphen in .tflite konvertieren könnte ?
visualisierten Graph hätte ich auch noch im Angebot zwecks in/outpu :)

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Dienstag 10. Dezember 2019, 10:21
von Felix92
Hier mal noch der Error vom TFLiteConverter Skript:
Auch hier habe ich sämtliche möglichen Varianten probiert.
In models liegt:
traffic_graph.pb
traffic_map.pbtxt

Namen in saved_model ändern bringt auch nichts.

Code: Alles auswählen

tflite_convert --saved_model_dir=C:\Users\felix\Desktop\traffic_helper\object_detection_API\models --output_file=C:\Users\felix\Desktop\traffic_helper\object_detection_API\models\converted.tflite

Code: Alles auswählen

2019-12-10 10:15:32.581572: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
Traceback (most recent call last):
  File "D:\Programme\Anaconda\Scripts\tflite_convert-script.py", line 10, in <module>
    sys.exit(main())
  File "D:\Programme\Anaconda\lib\site-packages\tensorflow_core\lite\python\tflite_convert.py", line 515, in main
    app.run(main=run_main, argv=sys.argv[:1])
  File "D:\Programme\Anaconda\lib\site-packages\tensorflow_core\python\platform\app.py", line 40, in run
    _run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
  File "D:\Programme\Anaconda\lib\site-packages\absl\app.py", line 299, in run
    _run_main(main, args)
  File "D:\Programme\Anaconda\lib\site-packages\absl\app.py", line 250, in _run_main
    sys.exit(main(argv))
  File "D:\Programme\Anaconda\lib\site-packages\tensorflow_core\lite\python\tflite_convert.py", line 502, in run_main
    _convert_tf2_model(tflite_flags)
  File "D:\Programme\Anaconda\lib\site-packages\tensorflow_core\lite\python\tflite_convert.py", line 215, in _convert_tf2_model
    converter = lite.TFLiteConverterV2.from_saved_model(flags.saved_model_dir)
  File "D:\Programme\Anaconda\lib\site-packages\tensorflow_core\lite\python\lite.py", line 359, in from_saved_model
    saved_model = _load(saved_model_dir, tags)
  File "D:\Programme\Anaconda\lib\site-packages\tensorflow_core\python\saved_model\load.py", line 517, in load
    return load_internal(export_dir, tags)
  File "D:\Programme\Anaconda\lib\site-packages\tensorflow_core\python\saved_model\load.py", line 526, in load_internal
    saved_model_proto = loader_impl.parse_saved_model(export_dir)
  File "D:\Programme\Anaconda\lib\site-packages\tensorflow_core\python\saved_model\loader_impl.py", line 83, in parse_saved_model
    constants.SAVED_MODEL_FILENAME_PB))
OSError: SavedModel file does not exist at: C:\Users\felix\Desktop\traffic_helper\object_detection_API\models/{saved_model.pbtxt|saved_model.pb}

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Dienstag 10. Dezember 2019, 10:56
von sparrow
Was ist denn die Ausgabe, wenn du in der Kommandozeile

Code: Alles auswählen

dir C:\Users\felix\Desktop\traffic_helper\object_detection_API\models
eingibst?

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Dienstag 10. Dezember 2019, 11:24
von Felix92

Code: Alles auswählen

(base) C:\Users\felix>dir C:\Users\felix\Desktop\traffic_helper\object_detection_API\models
 Volume in Laufwerk C: hat keine Bezeichnung.
 Volumeseriennummer: C45F-A812

 Verzeichnis von C:\Users\felix\Desktop\traffic_helper\object_detection_API\models

07.12.2019  18:38    <DIR>          .
07.12.2019  18:38    <DIR>          ..
10.12.2019  10:06               729 convert_to_tflite.py
06.12.2019  10:19        81.966.379 traffic_graph.pb
06.12.2019  10:19             3.654 traffic_map.pbtxt
               3 Datei(en),     81.970.762 Bytes
               2 Verzeichnis(se), 26.445.381.632 Bytes frei

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Dienstag 10. Dezember 2019, 12:09
von __blackjack__
Da wird ja anscheinend versucht etwas mit dem Namen "{saved_model.pbtxt|saved_model.pb}" zu öffnen, was ein sehr komischer Name ist und eher wie ein Platzhalter aussieht für den tatsächlichen Namen. Kann es sein, dass Du den Namen *angeben* musst?

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Dienstag 10. Dezember 2019, 12:56
von Felix92
Das hab ich auch schon probiert also ...models/traffic_graph.pb
der help von --saved_model_dir sagt full path to saved_model directory

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Dienstag 10. Dezember 2019, 13:23
von __blackjack__
@Felix92: Ja da natürlich nicht, das heisst ja ``--saved_model_dir``. Da fehlt noch eine andere Option die sagt wie die Datei in dem Verzeichnis heisst. Woher sollte das Skript das sonst auch wissen‽ Schau doch mal in die Jankie verlinkte Seite mit Beispielen wie man das Skript verwendet. Da sieht man welche Option das ist.

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Dienstag 10. Dezember 2019, 13:28
von sparrow
Ohne jetzt eine Ahnung von dem Programm zu haben, würde ich die .pbtxt und die .pb mal umbenennen in saved_model.pbtxt und saved_model.pb

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Dienstag 10. Dezember 2019, 13:45
von Felix92

Code: Alles auswählen

Guide
Converter command line reference
This page describes how to use the TensorFlow Lite converter using the command line tool. The preferred approach for conversion is using the Python API.

Note: This only contains documentation on the command line tool in TensorFlow 2. Documentation on using the command line tool in TensorFlow 1 is available on GitHub (reference, example).
High-level overview
The TensorFlow Lite Converter has a command line tool tflite_convert which supports basic models. Use the TFLiteConverter Python API for any conversions involving quantization or any additional parameters (e.g. signatures in SavedModels or custom objects in Keras models).

Usage
The following flags specify the input and output files.

--output_file. Type: string. Specifies the full path of the output file.
--saved_model_dir. Type: string. Specifies the full path to the directory containing the SavedModel generated in 1.X or 2.X.
--keras_model_file. Type: string. Specifies the full path of the HDF5 file containing the tf.keras model generated in 1.X or 2.X.
The following is an example usage.

tflite_convert \
  --saved_model_dir=/tmp/mobilenet_saved_model \
  --output_file=/tmp/mobilenet.tflite

In addition to the input and output flags, the converter contains the following flag.

--enable_v1_converter. Type: bool. Enables user to enable the 1.X command line flags instead of the 2.X flags. The 1.X command line flags are specified here.
Direkt von TF2.0 docu der flag graph_def_file wird nicht mehr unterstützt also müsste es eigentlich erkannt werden ...

Re: Tensorflow 2.0 convert frozen_graph.pb to .tflite

Verfasst: Dienstag 10. Dezember 2019, 13:46
von Felix92
@sparrow die Idee hatte ich auch schon ... ohne Erfolg ^^