File Exchange

image thumbnail

Deep Learning Toolbox Importer for TensorFlow-Keras Models

Import pretrained Keras model for prediction and transfer learning


Updated 18 Mar 2020

The importer for the TensorFlow-Keras models would enable you to import a pretrained Keras model and weights. You can then use this model for prediction or transfer learning. Alternatively, you can import layer architecture as a Layer array or a LayerGraph object. You can then train this model.
Opening the kerasimporter.mlpkginstall file from your operating system or from within MATLAB will initiate the installation process for the release you have.
This mlpkginstall file is functional for R2017b and beyond.
Usage Example:
1. Import Keras Layers

% Import the Layers

layers = importKerasLayers('digitsDAGnet.h5');

%Load a dataset for training a classifier to recognize the digits
digitDatasetPath = fullfile(toolboxdir('nnet'), 'nndemos', ...
'nndatasets', 'DigitDataset');

digitData = imageDatastore(digitDatasetPath, ...

%Partition the dataset into training & test images

rng(1) % For reproducibility
trainingFileSplitRatio = 0.6;
[trainDigitData,testDigitData] = splitEachLabel(digitData,...

%Set some training options

options = trainingOptions('sgdm','MaxEpochs',20,...

%Train network

convnet = trainNetwork(trainDigitData,layers,options);

%Read image for classification

I = imread(fullfile(digitDatasetPath,'5','image4009.png'));

%Classify the image using the network
label = classify(convnet, I);
title(['Classification result ' char(label)])

2. Import a pretrained Keras Network
netfile = 'digitsDAGnet.h5';
classNames = {'0','1','2','3','4','5','6','7','8','9'};
network = importKerasNetwork(netfile, 'ClassNames', classNames);

%Read the image to classify
digitDatasetPath = fullfile(toolboxdir('nnet'), 'nndemos','nndatasets',...
I = imread(fullfile(digitDatasetPath,'5','image4009.png'));

%Classify the image using the network
label = classify(network, I);

%Show the image and classification result
title(['Classification result ' char(label)])

Comments and Ratings (44)

Error using importKerasNetwork (line 94). Unable to import network. Weight sharing is not yet supported. Any update on this ?

I'm also getting this error: "Importing Keras networks with more than 1 input or output layer is not yet supported". Are there any updates?

And what about MIMO (multiple input and multiple output) network?

Still looking for an offline installer or importer for this, I have a non-internet access Windows 10 system with 2018b & the Deep Learning Tb installed and this package will not install without 'Phoning home'. Any ideas?


I have the same issue as Tengfei Feng. I suppose it is the Timedistributed(Dense) layer that causes the issue. Anyidea when this will be fixed?

Is there anyway to get an Offline Installer for Linux? Or instructions on how to download this offline for Linux? I have a situation where this is needed without any networking.

I just get an error feedback 'Unable to import network because some network layers are not yet supported'; I just design an easy model including lstm and TimeDistributed (Dense) layer. So I would be very satisfied if these functions could be supported in future.

Seconding Jeff Bailey's question regarding installing on off line matlab instance...

Umut Demir

Hi, I keep getting "Reference to non-existent field 'class_name'." error. I could not find any source to fix this.

Yang Tao

Hi, I wonder if there's a way to import a model containing custom layers? Thank.

Jeff Bailey

How does one get a copy of the Deep Learning Toolbox Importer for TensorFlow-Keras Models for an offline, non-Internet system that has Matlab installed? The download button on this page only points to a matlab package install script that must be opened within Matlab.

I've got this warning when I import a tf.keras model with batch normalization layer:

Warning: Unable to import layer. Keras layer 'BatchNormalization' with the specified settings is not yet
supported. The problem was: Layer 'bn_1': Unable to import layer. 'axis' values other than
-1 or 3 are not yet supported.

I am pretty sure that 'axis' parameter in BatchNormalization layer of the keras model has been set to -1.
Does anyone have any idea how should I solve this problem?
Or maybe it is just a bug for the importing function?

Thank you guys.
And great job for developing this great tool.

Ali Durmaz

Hi guys,

are there any plans to include padding value support in Conv2DTranspose?

I got the following warning:
Warning: Unable to import layer. Keras layer 'Conv2DTranspose' with the specified settings is not yet supported. The problem was: Layer
'conv2d_transpose_3': Unable to import layer. Padding value not yet supported.

Thanks in advance for your help.
Best regards

I got this error:

Error using importKerasLayers (line 96)
Unable to import layers from file 'mask_rcnn.h5'
because it contains no 'model_config' attribute.

Any idea how to solve?

This would be great for us if it could support >1 output layers... any plans?


Hi! Along with upgarde to keras 2.2.4 please add support for Keras models with layers like this one:

Or if Matlab approach for TimeDistributed layers is different one, please add comments how to make model with similar configuration.

Ting Su

Hi Bingzhao and Argo, MIMO (multiple input and multiple output) network is not supported yet. We will consider to support importing MIMO network in the future.

Ting Su

Hi Issac, the issue of keras 2.2.4 was caused by a breaking change introduce in Keras 2.2.3. We will provide a solution soon.

Isaac Lenton

Hi, I'm unable to import networks saved with keras==2.2.4. Downgrading to keras==2.1.0 seems to work fine.

Hi! I also get the following error 'Importing Keras networks with more than 1 input or output layer is not yet supported.' Also, sincerely hope that non-image input layers are supported. Thanks!

jianY xu

I want to create a special layer to add noise to the data. But my matlab version is 2017b, I don't have the example " gaussianNoiseLayer.m". I really want to know the coding structure of adding noise layer.
thank you very much!!

Kivanc Kose

I am using the Linux installation of Matlab 2018a. Even if my toolbox configuration and version satisfies the requirements, the can not install the package.
The add-on explorer portal states "This add-on is not supported on your platform" and does not let me install the package.

Don Mathis

Chia-Yi Tai, the match between Keras and MATLAB should be accurate to at least 1e-4. Maybe your preprocessing is not exactly the same in the two cases. Please contact Support or MATLAB Answers for a more specific response.

Chia-Yi Tai

I have import keras training model and success classify images , but I got the different result between Python(tensorflow) and MATLAB classify answer , the model are exactly same and I also using resize and flip to match different , is it any others possible reason or it still have little different when neural network running,
thank you

argo yang

Hello,I also get the error "Import Keras networks with more than 1 input or output layer is not yet supported " when I imported yolo model by keras. But there is a document in Mathworks about yolonet.mat, How do you get yolonet.mat ? import form other model or totally trained by matlab!
Thank you!

Wen Liu

Hi! I also get the following error 'Importing Keras networks with more than 1 input or output layer is not yet supported.' Hope you could solve it next time by supporting Multi-input or Multi-output. Many thanks.


I get the following error 'Importing Keras networks with more than 1 input or output layer is not yet supported.' Any idea on when this will be supported? Many thanks.

Hi Don Mathis, to pass the input as vector inputs, I changed the command to this
model = importKerasNetwork('model16.json','WeightFile','model16.h5','OutputLayerType','classification','ImageInputSize',[1 37], 'classnames',classnames);
But when I did tis, it says the 'ImageInputSize' is not a recognized parameter. Is there any other way to pass the input as vector.Kindly help

Don Mathis

Gautam, the MATLAB network will always have an inputImageLayer as the first layer. If your Keras network had vector inputs instead of image inputs, you would pass them to the MATLAB network as "row images", (height=1).

Does this only work for image inputs? The input I am using is not an image and when I check the classifier output in keras and matlab, They both are different. Can someone please clarify this?

Yufan He

Hope you can make it support PReLu.


does it work with windows 10?? get error message

dudy karl

Is it possible to import layers defined in keras.layers like keras.layers.ConvLSTM2D?
what about wrappers like TimeDistributed? is there a way to import it or is there a similar layer in Matlab?

Will there be support for previous Matlab versions?


David Kuske


Nice to see it today! I'll try tomorry

David Kuske

Is there any way around so far for using a NN with LSTM regression in Matlab? I tried the Matlab nn toolbox, not yet supported.
Then I implemented it in Keras, now I cant Import my trained network for use :/.

David Kuske

When will LSTM support be implemented? thank you

Ting Su

Hi Talmo, Thanks for your feedback. We will contact you for more details of your use case on Reshape layers.

Excellent toolbox! Any chance we could get support for Reshape layers? Makes it kind of hard to go from Dense layers to Conv2d/ConvTranspose2d otherwise.

Hi, I’m getting an error when trying to install this: “The support package is not compatible with your version of MATLAB or operating system.”

I’m on macOS Sierra 10.12.6 using Matlab R2017b

MATLAB Release Compatibility
Created with R2017b
Compatible with R2017b to R2020a
Platform Compatibility
Windows macOS Linux