|
| 1 | +--- |
| 2 | +title: Getting started: importing TensorFlow models into SameDiff |
| 3 | +short_title: Model import |
| 4 | +description: importing TensorFlow models into SameDiff |
| 5 | +category: SameDiff |
| 6 | +weight: 3 |
| 7 | +--- |
| 8 | + |
| 9 | +# Getting started: importing TensorFlow models into SameDiff |
| 10 | + |
| 11 | +## What models can be imported into SameDiff |
| 12 | + |
| 13 | +Currently SameDiff supports the import of TensorFlow frozen graphs through the various SameDiff.importFrozenTF methods. |
| 14 | +TensorFlow documentation on frozen models can be found [here](https://www.TensorFlow.org/guide/saved_model#the_savedmodel_format_on_disk). |
| 15 | + |
| 16 | + import org.nd4j.autodiff.SameDiff.SameDiff; |
| 17 | + |
| 18 | + SameDiff sd = SameDiff.importFrozenTF(modelFile); |
| 19 | + |
| 20 | + ## Finding the model input/outputs and running inference |
| 21 | + |
| 22 | + After you import the TensorFlow model there are 2 ways to find the inputs and outputs. The first method is to look at the output of |
| 23 | + |
| 24 | + sd.summary(); |
| 25 | + |
| 26 | + Where the input variables are the output of no ops, and the output variables are the input of no ops. Another way to find the inputs is |
| 27 | + |
| 28 | + List<String> inputs = sd.inputs(); |
| 29 | + |
| 30 | + To run inference use: |
| 31 | + |
| 32 | + INDArray out = sd.batchOutput() |
| 33 | + .input(inputs, inputArray) |
| 34 | + .output(outputs) |
| 35 | + .execSingle(); |
| 36 | + |
| 37 | +For multiple outputs, use `exec()` instead of `execSingle()`, to return a `Map<String,INDArray>` of outputs instead. |
| 38 | +Alternatively, you can use methods such as `SameDiff.output(Map<String, INDArray> placeholders, String... outputs)` to get the same output. |
| 39 | + |
| 40 | +## Import Validation. |
| 41 | +We have a TensorFlow graph analyzing utility which will report any missing operations (operations that still need to be implemented) [here](https://github.com/eclipse/deeplearning4j/blob/master/nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/imports/TensorFlow/TensorFlowImportValidator.java) |
| 42 | + |
| 43 | +## Advanced: Node Skipping and Import Overrides |
| 44 | +It is possible to remove nodes from the network. For example TensorFlow 1.x models can have hard coded dropout layers. |
| 45 | +See the [BERT Graph test](https://github.com/eclipse/deeplearning4j/blob/master/nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/imports/TFGraphs/BERTGraphTest.java#L114-L150) for an example. |
| 46 | + |
| 47 | +## List of models known to work with SameDiff. |
| 48 | + |
| 49 | +- [PorV-RNN](https://deeplearning4jblob.blob.core.windows.net/testresources/PorV-RNN_frozenmodel.pb) |
| 50 | +- [alexnet](https://deeplearning4jblob.blob.core.windows.net/testresources/alexnet_frozenmodel.pb) |
| 51 | +- [cifar10_gan_85](https://deeplearning4jblob.blob.core.windows.net/testresources/cifar10_gan_85_frozenmodel.pb) |
| 52 | +- [deeplab_mobilenetv2_coco_voc_trainval](http://download.tensorflow.org/models/deeplabv3_mnv2_pascal_trainval_2018_01_29.tar.gz) |
| 53 | +- [densenet_2018_04_27](https://storage.googleapis.com/download.tensorflow.org/models/tflite/model_zoo/upload_20180427/densenet_2018_04_27.tgz) |
| 54 | +- [inception_resnet_v2_2018_04_27](https://storage.googleapis.com/download.tensorflow.org/models/tflite/model_zoo/upload_20180427/inception_resnet_v2_2018_04_27.tgz) |
| 55 | +- [inception_v4_2018_04_27](https://storage.googleapis.com/download.tensorflow.org/models/tflite/model_zoo/upload_20180427/inception_v4_2018_04_27.tgz) |
| 56 | +- [labels](https://github.com/KonduitAI/dl4j-test-resources/tree/master/src/main/resources/tf_graphs/zoo_models/labels) |
| 57 | +- [mobilenet_v1_0.5_128](http://download.tensorflow.org/models/mobilenet_v1_2018_02_22/mobilenet_v1_0.5_128.tgz) |
| 58 | +- [mobilenet_v2_1.0_224](http://download.tensorflow.org/models/tflite_11_05_08/mobilenet_v2_1.0_224.tgz) |
| 59 | +- [nasnet_mobile_2018_04_27](https://storage.googleapis.com/download.tensorflow.org/models/tflite/model_zoo/upload_20180427/nasnet_mobile_2018_04_27.tgz) |
| 60 | +- [resnetv2_imagenet_frozen_graph](http://download.tensorflow.org/models/official/resnetv2_imagenet_frozen_graph.pb) |
| 61 | +- [squeezenet_2018_04_27](https://storage.googleapis.com/download.tensorflow.org/models/tflite/model_zoo/upload_20180427/squeezenet_2018_04_27.tgz) |
| 62 | +- [temperature_bidirectional_63](https://deeplearning4jblob.blob.core.windows.net/testresources/temperature_bidirectional_63_frozenmodel.pb) |
| 63 | +- [temperature_stacked_63](https://deeplearning4jblob.blob.core.windows.net/testresources/temperature_stacked_63_frozenmodel.pb) |
| 64 | +- [text_gen_81](https://deeplearning4jblob.blob.core.windows.net/testresources/text_gen_81_frozenmodel.pb) |
| 65 | + |
| 66 | +## Operations Coverage |
| 67 | + |
| 68 | +SameDiff's TensorFlow import is still being developed, and does not yet have support for every single operation and datatype in TensorFlow. |
| 69 | +Almost all of the common/standard operations are importable and tested, however - including almost everything in the tf, tf.math, tf.layers, tf.losses, tf.bitwise and tf.nn namespaces. The majority of existing pretrained models out there should be importable into SameDiff. |
| 70 | + |
| 71 | +If you run into an operation that can't be imported, feel free to [open an issue](https://github.com/eclipse/deeplearning4j/issues). |
0 commit comments