File Exchange

image thumbnail

Deep Learning Toolbox Importer for TensorFlow-Keras Models

Import pretrained Keras model for prediction and transfer learning

83 Downloads

Updated 16 Sep 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, ...
'IncludeSubfolders',true,'LabelSource','foldernames');

%Partition the dataset into training & test images

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

%Set some training options

options = trainingOptions('sgdm','MaxEpochs',20,...
'InitialLearnRate',0.001);

%Train network

convnet = trainNetwork(trainDigitData,layers,options);

%Read image for classification

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

%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, 'Classes', classNames);

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

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

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

Comments and Ratings (52)

Nursadul Mamun

It shows the following error "Unable to import network. Weight sharing is not yet supported."

smsinks

This function does not work. Not even for the example given here.

layers = importKerasLayers('digitsDAGnet.h5');
Brace indexing is not supported for variables of this type.

Error in
nnet.internal.cnn.keras.readModelAndTrainingConfigs>iVerifyKerasVersion (line
46)
MajorVersion = str2double(VersionCell{1}(1));

Error in nnet.internal.cnn.keras.readModelAndTrainingConfigs (line 8)
iVerifyKerasVersion(ConfigFile);

Error in nnet.internal.cnn.keras.importKerasLayers (line 11)
[ModelConfig, TrainingConfig] =
nnet.internal.cnn.keras.readModelAndTrainingConfigs(ConfigFile, Format);

Error in importKerasLayers (line 102)
Layers = nnet.internal.cnn.keras.importKerasLayers(modelfile, varargin{:});

Kyle Peterson

Is this function supported by compiled applications?

José Luis Sandoval

I have the next two bugs here:
Warning: File 'resnet50_pascal_cards.h5' was saved in Keras version '2.3.1'. Import of Keras versions newer than '2.2.4' is
not yet supported. The imported model may not exactly match the model saved in the Keras file.
Error using importKerasNetwork (line 94)
Unable to import network. Weight sharing is not yet supported.
Warning: File 'resnet50_pascal_cards_inference.h5' was saved in Keras version '2.3.1'. Import of Keras versions newer than
'2.2.4' is not yet supported. The imported model may not exactly match the model saved in the Keras file.
Error using importKerasNetwork (line 94)
Keras network has no output layer because the model file contains no loss information. To add an output layer to the imported
network, specify its type using the 'OutputLayerType' argument. To provide class names, use the 'ClassNames' argument.

Any advance with this?

Thanks

Mustafa Bingöl

While the regression part works fine for the ImportKerasNetwork function, the classification part does not work stably. Continuously giving error "Reference to non-existent field 'xxx-Layer'." The importKerasLayers section is also not used because tf's save and load weight is not working correctly.

tf.version=2.1.0
tf.keras.version=2.2.4-tf
Matlab.R2020a

James34602

Common Keras inference features are missing:
1. Dynamic input/output axis
2. Permute layer
3. TimeDistributed layer
4. Reshape layer

James34602

Could Mathworks add TimeDistributed support?

Nicola Franzoso

how can load the net on gpu ?
Thanks

Emil Kristiansen

Saikat Sarkar

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?

Larry Riddle

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?

Morgan

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?

Brian Broking

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.

Tengfei Feng

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.

Ben Wetherill

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.

Chijiang Duan

Hi,
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.
Chijiang

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
Ali

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?

Nathan Lepora

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

munaiyi munaiyi

SergeyLA

Hi! Along with upgarde to keras 2.2.4 please add support for Keras models with layers like this one:
model.add(TimeDistributed(Dense(len(mapping))))

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.

Bingzhao Shan

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.

Robert

Goodday,
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.

GAUTAM SHREEDHA BHAT

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).

GAUTAM SHREEDHA BHAT

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.
Thanks

Yodish

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?

Philipp Zelger

Will there be support for previous Matlab versions?

cui

David Kuske

cui

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.

Talmo Pereira

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 R2020b
Platform Compatibility
Windows macOS Linux