Import the model trained in tensorflow into Matlab for prediction

The blogger is currently working on a graduation project using tensorflow2.0. Since the trained model needs to be used by another student who uses Matlab, I checked some information and recorded it here. In fact, Matlab's official website has already had a complete and detailed tutorial, but the essence of human beings is a repeater, so I will explain it again here. Let me talk about my environment first: python3.7 tensorflow2.0-gpu matlab 2019a 

The first step is to obtain a trained model

The second step is to download Matlab's Deep Learning Toolbox Importer for TensorFlow-Keras Models support package

The third step is to import the model

The fourth step is to use the model

Precautions

appendix

Description

#define 保护 {wang shang xue ke}   

The first step is to obtain a trained model

First, you have to have a trained model and save it in .h5 format. For example, the following picture:

The model file can be obtained by executing the following code in tensorflow:

model.save('MyMode_03.h5')

At this point, the model is ready.

 

The second step is to download Matlab's Deep Learning Toolbox Importer for TensorFlow-Keras Models support package

At this time, assuming that your computer has not installed this support package, type in the matlab command line:

importKerasNetwork

Matlab will report an error and give the download link at the end of the error message:

We click on this link and after a long wait (if it can be protected, it will be faster), we will enter the download interface (note that if you have not logged in to matlab before, you need to log in with your account and password here, if you don't have an account, please register one , Don’t worry about the cracked version, you can download it):

 

Then click install and wait (note that this step is completed after I protect it, and there have been network errors before)

 

If someone can’t protect, I’ll give a link to the support package’s network disk at the end of the article. You can try to install it offline by yourself according to the readme tutorial, but I encountered an error when I installed it offline, suggesting a problem such as the matlab version. ,good luck!

 

After installation, open matlab and repeat the previous instructions, prompting:

The installation is successful!

 

The third step is to import the model

Execute in matlab (replace "'MyMode_03.h5") with your own model)

net = importKerasNetwork('MyMode_03.h5');  %导入自己的模型
net.Layers                                 %显示模型的结构

This is the network structure of my model.

 

The fourth step is to use the model

x = ones(7,1000);
y=net.predict(x);
plot(y)

You can use the predict() function to make predictions. In fact, there are more ways to use them, I won't expand them. If you are interested, you can check them on the matlab reference page, and I will give them later.

 

Precautions

  • It must be protected. If it won't be protected this year, it would be too difficult. As for how to protect, you can Baidu by yourself.
  • The matlab support package is not supported by all layers of tensorflow. There is a support list as follows:

More support information can be viewed in the following link.

 

appendix

pdf version of matlab corresponding reference page:

Reference page

Deep Learning Toolbox Importer for TensorFlow-Keras Models support package offline version:

Support package

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_39545674/article/details/105111571