Error importing Keras Network (2024)

18 views (last 30 days)

Show older comments

Fellarrusto on 3 Feb 2023

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/1906305-error-importing-keras-network

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/1906305-error-importing-keras-network

Answered: Vinayak Choyyan on 6 Feb 2023

Open in MATLAB Online

I have succesfully trained a model with TensorFlow and Keras. I have saved the trained model as a h5 file, I'm willing to make test on the model in MATLAB, by the way I'm having trouble importing the model.

First of all I tried to the function

net = importKerasLayers('optflow.h5')

It load the network without any problem, I've also checked the layout in the Deep Neural Designer app.

net =

LayerGraph with properties:

Layers: [23×1 nnet.cnn.layer.Layer]

Connections: [24×2 table]

InputNames: {'imageinput'}

OutputNames: {'RegressionLayer_regressionoutput'}

Now wen i try to import the network and the training weights with the following line of code

net = importKerasNetwork("optflow.h5")

I get the following error

Error using assembleNetwork (line 47)

Invalid network.

Error in nnet.internal.cnn.keras.importKerasNetwork (line 39)

Network = assembleNetwork(LayersOrGraph);

Error in importKerasNetwork (line 76)

Network = nnet.internal.cnn.keras.importKerasNetwork(modelfile, varargin{:});

Caused by:

Layer 'concat': Input size mismatch. Size of input to this layer is different from the expected input size.

Inputs to this layer:

from layer 'fc_1' (size 1(S) × 1(S) × 200000(C) × 1(B))

from layer 'fc_2' (size 1(S) × 1(S) × 50000(C) × 1(B))

from layer 'fc_3' (size 1(S) × 1(S) × 12500(C) × 1(B))

This is the model definition in python

## Model definition

input_layer = Input(shape=(500, 1000, 3), name='imageinput')

x = AveragePooling2D(pool_size=(20, 20), padding='same', name='avgpool2d_3')(input_layer)

x = Conv2D(filters=10, kernel_size=(3, 3), padding='same', name='conv_3')(x)

x = ReLU(name='relu_3')(x)

x = Conv2D(filters=10, kernel_size=(3, 3), padding='same', name='conv_6')(x)

x = ReLU(name='relu_6')(x)

x = Flatten(name='fc_3')(x)

y = AveragePooling2D(pool_size=(10, 10), padding='same', name='avgpool2d_2')(input_layer)

y = Conv2D(filters=10, kernel_size=(3, 3), padding='same', name='conv_2')(y)

y = ReLU(name='relu_2')(y)

y = Conv2D(filters=10, kernel_size=(3, 3), padding='same', name='conv_5')(y)

y = ReLU(name='relu_5')(y)

y = Flatten(name='fc_2')(y)

z = AveragePooling2D(pool_size=(5, 5), padding='same', name='avgpool2d_1')(input_layer)

z = Conv2D(filters=10, kernel_size=(5, 5), padding='same', name='conv_1')(z)

z = ReLU(name='relu_1')(z)

z = Conv2D(filters=10, kernel_size=(5, 5), padding='same', name='conv_4')(z)

z = ReLU(name='relu_4')(z)

z = Flatten(name='fc_1')(z)

concat = Concatenate(axis=1, name='concat')([z, y, x])

fc_4 = Dense(units=5, name='fc_4')(concat)

output_layer = Dense(units=3, activation='linear', name='regressionoutput')(fc_4)

model = Model(inputs=input_layer, outputs=output_layer)

Is there a solution?

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (1)

Vinayak Choyyan on 6 Feb 2023

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1906305-error-importing-keras-network#answer_1164610

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1906305-error-importing-keras-network#answer_1164610

Open in MATLAB Online

Hello Fellarrusto,

As per my understanding, you have trained a model in Python using Keras and you would like to import the trained model to MATLAB. You are trying to use the ‘importKerasNetwork()’ function while you faced this issue.

As per the documentation of ‘importKerasNetwork()’ function (Import pretrained Keras network and weights - MATLAB importKerasNetwork - MathWorks India), the ‘Concatenate’ function of TensorFlow-Keras is translated into ‘depthConcatenationLayer()’ in MATLAB. A depth concatenation layer takes inputs that have the same height and width and concatenates them along the third dimension (the channel dimension). Since you had flattened the outputs ‘x’, ‘y’, ‘z’ to get ‘fc_1’, fc_2’, ‘fc_3’ which will be of two dimensional.

Try running the following code to verify that ‘depthConcatenationLayer()’ does give a similar error as you had received and replacing it with ‘concatenationLayer()’ does resolve the concat error.

im=imageInputLayer([500 1000 3],Normalization="none");

layer1 = [im

averagePooling2dLayer([20 20],Padding="same")

convolution2dLayer([3 3],10,Padding="same")

convolution2dLayer([3 3],10,Padding="same")

flattenLayer

];

layer2 = [im

averagePooling2dLayer([10 10],Padding="same")

convolution2dLayer([3 3],10,Padding="same")

convolution2dLayer([3 3],10,Padding="same")

flattenLayer

];

layer3 = [im

averagePooling2dLayer([5 5],Padding="same")

convolution2dLayer([5 5],10,Padding="same")

convolution2dLayer([5 5],10,Padding="same")

flattenLayer

];

l=layerGraph;

l=addLayers(l,layer1);

l=addLayers(l,layer2);

l=addLayers(l,layer3);

%l=addLayers(l,concatenationLayer(1,3));

l=addLayers(l,depthConcatenationLayer(3));

l=connectLayers(l,'flatten','concat/in1');

l=connectLayers(l,'flatten_1','concat/in2');

l=connectLayers(l,'flatten_2','concat/in3');

plot(l);

analyzeNetwork(l);

dlnet=dlnetwork(l);

analyzeNetwork(dlnet);

As a workaround for the issue you are facing, I would suggest you could build the model in MATLAB, similar to the above code, and then load the weights into the model. You can get the trained weights of your model from Python and save it to a .mat file and then use this .mat file to load the weights into MATLAB.

Here are some resources I found online on how one can get the weights from a Keras model and how to save data to a .mat file.

For more details, please check out the following documentations too.

  • Flatten layer - MATLAB - MathWorks India
  • Concatenation layer - MATLAB - MathWorks India
  • Depth concatenation layer - MATLAB - MathWorks India

I hope this workaround helps resolves the issue you are facing.

0 Comments

Show -2 older commentsHide -2 older comments

Sign in to comment.

Sign in to answer this question.

See Also

Categories

AI, Data Science, and StatisticsDeep Learning ToolboxDeep Learning FundamentalsImport Deep Neural NetworksPretrained Networks from External Platforms

Find more on Pretrained Networks from External Platforms in Help Center and File Exchange

Tags

  • deep learning
  • tensorflow

Products

  • Deep Learning Toolbox

Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Error importing Keras Network (3)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

Contact your local office

Error importing Keras Network (2024)
Top Articles
Latest Posts
Article information

Author: Sen. Ignacio Ratke

Last Updated:

Views: 6216

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Sen. Ignacio Ratke

Birthday: 1999-05-27

Address: Apt. 171 8116 Bailey Via, Roberthaven, GA 58289

Phone: +2585395768220

Job: Lead Liaison

Hobby: Lockpicking, LARPing, Lego building, Lapidary, Macrame, Book restoration, Bodybuilding

Introduction: My name is Sen. Ignacio Ratke, I am a adventurous, zealous, outstanding, agreeable, precious, excited, gifted person who loves writing and wants to share my knowledge and understanding with you.