# Barracuda
###### tags: `Dragon Cloud AI`
## Use Keras pretrained model
To use Keras pretrained models, one have to transform them to **Tensorflow** or **ONNX**
### h5 to Barracuda format (.nn file)
#### 1. Transform .pb file created by savedmodel method
See Step 1 in https://hackmd.io/_Iwl-DnpRAGo0gQPk341Bw
#### 2. Transform previous .pb file to barracuda readable .pb file
Run the python file **savedmodel_to_graph.py** at t1:DATA2/Johnnie
savedmodel_to_graph.py:
```
import tensorflow as tf
from tensorflow.python.saved_model import tag_constants
from tensorflow.python.framework import graph_util
from tensorflow.python.tools import freeze_graph
def describe_graph(graph_def, show_nodes=False):
print('Input Feature Nodes: {}'.format(
[node.name for node in graph_def.node if node.op=='Placeholder']))
print('')
print('Unused Nodes: {}'.format(
[node.name for node in graph_def.node if 'unused' in node.name]))
print('')
print('Output Nodes: {}'.format(
[node.name for node in graph_def.node if (
'predictions' in node.name or 'softmax' in node.name)]))
print('')
print('Quantization Nodes: {}'.format(
[node.name for node in graph_def.node if 'quant' in node.name]))
print('')
print('Constant Count: {}'.format(
len([node for node in graph_def.node if node.op=='Const'])))
print('')
print('Variable Count: {}'.format(
len([node for node in graph_def.node if 'Variable' in node.op])))
print('')
print('Identity Count: {}'.format(
len([node for node in graph_def.node if node.op=='Identity'])))
print('', 'Total nodes: {}'.format(len(graph_def.node)), '')
if show_nodes==True:
for node in graph_def.node:
print('Op:{} - Name: {}'.format(node.op, node.name))
with tf.Session(graph=tf.Graph()) as sess:
tf.saved_model.loader.load(sess, [tag_constants.SERVING], "merge_softmax_deployment/model/export/Servo/1/")
graph = tf.get_default_graph()
freeze_var_names = list(v.op.name for v in tf.global_variables())
output_names = []
output_names += [v.op.name for v in tf.global_variables()]
graph_def = graph.as_graph_def()
if True:
for node in graph_def.node:
node.device = ""
frozen_graph = tf.graph_util.convert_variables_to_constants(sess, graph_def, output_names, freeze_var_names)
tf.train.write_graph(frozen_graph, "./", "my_model.pb", as_text=False)
```
#### 3. Transform .pb file to .nn file
Run the python file **tensorflow_to_barracuda.py** at t1:DATA2/Johnnie
reference: https://github.com/mantasp/barracuda-release/blob/release/1.0.1/Tools/tensorflow_to_barracuda.py
### h5 to ONNX
The following code is **h5_to_onnx.py** in t1:DATA2/Johnnie
To execute it, use command:
**python3 h5_to_onnx.py {h5 source file path}**
```
from tensorflow.keras.models import load_model
import sys
import os
if (len(sys.argv) <= 1):
print("too few arguments! Need filename of .h5 model")
exit()
model = load_model(sys.argv[1])
# Importing ONNX conversion tools and converting
# Instructions: https://medium.com/analytics-vidhya/how-to-convert-your-keras-model-to-onnx-8d8b092c4e4f
# Tools (install first): https://github.com/onnx/keras-onnx
os.environ['TF_KERAS'] = '1'
import onnx
import keras2onnx
onnx_model = keras2onnx.convert_keras(model, model.name)
onnx.save_model(onnx_model, sys.argv[1][:-3] + ".onnx") # saved in current working directory as .onnx
# Testing if converted properly by importing back
# NOTE: need to install onnxruntime package
print("Reimporting to check whether conversion succesful (need onnxruntime package installed)...")
try:
import onnxruntime
except ImportError as e:
print("no onnxruntime module found! This is used just to check whether exported correctly, and is not required")
exit()
new_model = onnxruntime.InferenceSession(sys.argv[1][:-3] + ".onnx")
print('Successfully exported!')
```
### Use ONNX file in Unity
Unity support ONNX file now, so it could be loaded directly into Unity.
However, there are limited supported operators, please check https://docs.unity3d.com/Packages/com.unity.barracuda@1.0/manual/SupportedOperators.html for details