muti input cnn in matalb how to do that and how to feed the data in the model?
1 view (last 30 days)
Show older comments
I want to train a muti input cnn in matalb how to do that and how to feed the data in the model?
like in python we do this:
history=final_model.fit(x=[X_insp_tr, X_exp_tr], y=y_tr,
epochs=100, batch_size=32,
validation_data=([X_insp_val, X_exp_val], y_val))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1781285/image.png)
0 Comments
Answers (2)
Jaimin
on 30 Sep 2024
Hello @Arka Roy
You can utilise “connectLayers” function to build a cnn based deep learning model that accepts multiple inputs.
Kindly refer to the below code for sample model created using the “connectLayers” function.
% Define input layers for each input branch
inputLayer1 = imageInputLayer([28 28 1], 'Name', 'input1'); % Example size
inputLayer2 = imageInputLayer([28 28 1], 'Name', 'input2'); % Example size
% Define branches for each input with explicit layer names
branch1 = [
inputLayer1
convolution2dLayer(3, 8, 'Padding', 'same', 'Name', 'conv1_1')
reluLayer('Name', 'relu1_1')
maxPooling2dLayer(2, 'Stride', 2, 'Name', 'maxpool1_1')
];
branch2 = [
inputLayer2
convolution2dLayer(3, 8, 'Padding', 'same', 'Name', 'conv1_2')
reluLayer('Name', 'relu1_2')
maxPooling2dLayer(2, 'Stride', 2, 'Name', 'maxpool1_2')
];
% Concatenate branches
concatLayer = concatenationLayer(3, 2, 'Name', 'concat');
% Define the rest of the network
finalLayers = [
fullyConnectedLayer(10, 'Name', 'fc')
softmaxLayer('Name', 'softmax')
classificationLayer('Name', 'classoutput')
];
% Assemble the network
layers = layerGraph(branch1);
layers = addLayers(layers, branch2);
layers = addLayers(layers, concatLayer);
layers = connectLayers(layers, 'maxpool1_1', 'concat/in1');
layers = connectLayers(layers, 'maxpool1_2', 'concat/in2');
layers = addLayers(layers, finalLayers);
layers = connectLayers(layers, 'concat', 'fc');
For more information on “connectLayers” function, kindly refer the following MathWorks Documentation:
I hope this will be helpful.
0 Comments
Arka Roy
on 1 Oct 2024
Edited: Arka Roy
on 1 Oct 2024
1 Comment
Jaimin
on 1 Oct 2024
You can utilize "imageDatastore" and "arrayDatastore" to read images and labels. Once done, you can merge them using the "combine" function.
Kindly refer to the snippet below.
% Combine data into a datastore
dsTrain = combine(imageDatastore(X_insp_tr), imageDatastore(X_exp_tr), arrayDatastore(y_tr));
dsVal = combine(imageDatastore(X_insp_val), imageDatastore(X_exp_val), arrayDatastore(y_val));
For more information on "imageDatastore", "arrayDatastore" and "combine" function, kindly refer the following MathWorks Documentation:
I hope this will be helpful.
See Also
Categories
Find more on Deep Learning for Image Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!