muti input cnn in matalb how to do that and how to feed the data in the model?

2 views (last 30 days)
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))

Answers (2)

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

Arka Roy
Arka Roy on 1 Oct 2024
Edited: Arka Roy on 1 Oct 2024
Thank you for helping me.
Just another query...if you please help on that...
Now, lets say i have traiing data={X_train1, X_train2, y_train} and validation data={X_val1, X_val2, y_val}. Then how to feed these data in trainnetwork to feed to model
In python i did this. How to do it in matlab?
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))
  1 Comment
Jaimin
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.

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!