Search Unity

Question Strange input shape after export from keras to ONNX and import in unity

Discussion in 'Barracuda' started by NchiaComuFai, Nov 8, 2022.

  1. NchiaComuFai

    NchiaComuFai

    Joined:
    Mar 26, 2014
    Posts:
    4
    Hello,

    this is my neural network model:

    The input is an example of 10000 features. Each feature is a number (0 or 1).
    The output is a number between 0 and 1.

    Code (CSharp):
    1. from tensorflow.keras.datasets import imdb
    2. (train_data, train_labels), (test_data, test_labels) = imdb.load_data(
    3. num_words=10000)
    4.  
    5. import numpy as np
    6. def vectorize_sequences(sequences, dimension=10000):
    7.   results = np.zeros((len(sequences), dimension))
    8.   for i, sequence in enumerate(sequences):
    9.     for j in sequence:
    10.       results[i, j] = 1.
    11.   return results
    12.  
    13. x_train = vectorize_sequences(train_data)
    14. x_test = vectorize_sequences(test_data)
    15.  
    16. y_train = np.asarray(train_labels).astype("float32")
    17. y_test = np.asarray(test_labels).astype("float32")
    18.  
    19. from tensorflow import keras
    20. from tensorflow.keras import layers
    21.  
    22. model = keras.Sequential([
    23. layers.Dense(16, activation="relu"),
    24. layers.Dense(16, activation="relu"),
    25. layers.Dense(1, activation="sigmoid")
    26. ])
    27.  
    28. model.compile(optimizer="rmsprop",
    29. loss="binary_crossentropy",
    30. metrics=["accuracy"])
    31.  
    32. x_val = x_train[:10000]
    33. partial_x_train = x_train[10000:]
    34. y_val = y_train[:10000]
    35. partial_y_train = y_train[10000:]
    36.  
    37. model.fit(partial_x_train,
    38. partial_y_train,
    39. epochs=20,
    40. batch_size=512,
    41. validation_data=(x_val, y_val))
    This is how I export the model:

    Code (CSharp):
    1. import tensorflow as tf
    2.  
    3. spec = (tf.TensorSpec(model.inputs[0].shape, tf.float32, name="my input"),)
    4.  
    5. nchw_inputs_list = [model.inputs[0].name]
    6.  
    7. import tf2onnx
    8.  
    9. model_proto, _ = tf2onnx.convert.from_keras(model, input_signature=spec, custom_ops=None, opset=9, inputs_as_nchw=nchw_inputs_list, output_path="example.onnx")
    When I import it in Unity:

    upload_2022-11-8_10-20-25.png

    What I am doing wrong? How can I export it to ONNX and import it in unity in the right way?

    Thank you
     
  2. yoonitee

    yoonitee

    Joined:
    Jun 27, 2013
    Posts:
    2,363
    As far as I can tell from my experiments, Barracuda has not fully implemented all functions so it will only work with some onnx if you're lucky.