91 Downloads
Updated 15 Nov 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)])
Create scripts with code, output, and formatted text in a single executable document.
Seyed Mostafa Mousavi Kahaki (view profile)
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?
Diego Ribeiro de Oliveira Galdino (view profile)
And what about MIMO (multiple input and multiple output) network?
Larry Riddle (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
Seconding Jeff Bailey's question regarding installing on off line matlab instance...
Umut Demir (view profile)
Hi, I keep getting "Reference to non-existent field 'class_name'." error. I could not find any source to fix this.
Yang Tao (view profile)
Hi, I wonder if there's a way to import a model containing custom layers? Thank.
Jeff Bailey (view profile)
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 (view profile)
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 (view profile)
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
Ali Özgür Argunsah (view profile)
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 (view profile)
This would be great for us if it could support >1 output layers... any plans?
munaiyi munaiyi (view profile)
SergeyLA (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
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 (view profile)
Hope you can make it support PReLu.
Thanks
Yodish (view profile)
does it work with windows 10?? get error message
dudy karl (view profile)
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 (view profile)
Will there be support for previous Matlab versions?
cui (view profile)
David Kuske (view profile)
cui (view profile)
Nice to see it today! I'll try tomorry
David Kuske (view profile)
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 (view profile)
When will LSTM support be implemented? thank you
Ting Su (view profile)
Hi Talmo, Thanks for your feedback. We will contact you for more details of your use case on Reshape layers.
Talmo Pereira (view profile)
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.
Eftychios Pnevmatikakis (view profile)
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