File Exchange

image thumbnail

Deep Learning Toolbox Importer for TensorFlow-Keras Models

Import pretrained Keras model for prediction and transfer learning

107 Downloads

Updated 20 Mar 2019

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, 'ClassNames', 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 (31)

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?

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

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.

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.

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.

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

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.

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

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor