Friday, 1 July 2022

How to export trained stable-baselines/TensorFlow neural network to MATLAB?

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.



from How to export trained stable-baselines/TensorFlow neural network to MATLAB?

No comments:

Post a Comment