2

I'm trying to export a PPO2-trained neural network to MATLAB. It was saved as a zip file using

model.save(os.path.join(save_dir, 'best_overall_model'))

I can load my model with

model = PPO2.load(os.path.join(load_dir), env=env, tensorboard_log=save_dir)

Because I could not find a direct way to do the exporting to MATLAB, I thought of using Open Neural Network Exchange (ONNX) as an intermediate format. I could not find info on how to do this conversion from Stable Baselines, so I resaved my model with TensorFlow using simple_save. Note: I'm using TensorFlow 1.14.

tf.saved_model.simple_save(model.sess, os.path.join(save_dir, 'tensorflow_model'), inputs={"obs": model.act_model.obs_ph}, outputs={"action": model.action_ph})

Finally, I use the following command to obtain the ONNX file:

python -m tf2onnx.convert --saved-model tensorflow_model --output model.onnx

I used netron to visualise the resulting ONNX file. Clearly, something went wrong:
ONNX

Alternative suggestions to get my neural network into MATLAB are also appreciated.

gilianzz
  • 199
  • 2
  • 14
  • Instead of saving your model, saving your weight of layers, [Explain](https://www.tensorflow.org/tutorials/keras/save_and_load), Do you try this apprach? then load your weight and input each weight to same layer in matlab. – I'mahdi Jun 22 '22 at 11:37
  • The `save_weights` function is not available. `model` is a PPO2 object. – gilianzz Jun 22 '22 at 13:27

1 Answers1

0

I think it is worth trying what is described here.

To summarize/outline:

  1. Save the model in HDF5 format in Python (as it seems you are using TF 1.x).
  2. Load the model into Matlab using importKerasNetwork

For more info look here: https://blogs.mathworks.com/deep-learning/2022/03/18/importing-models-from-tensorflow-pytorch-and-onnx/#:~:text=Importing%20TensorFlow%20Models%20using%20the%C2%A0HDF5%20(Keras)%20Format

Phoenix
  • 952
  • 2
  • 12