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.