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

46 次查看(过去 30 天)
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))

回答(2 个)

Jaimin
Jaimin 2024-9-30,10:55
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 2024-10-1,7:25
编辑:Arka Roy 2024-10-1,7:33
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 个评论
Jaimin
Jaimin 2024-10-1,10:55
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.

请先登录,再进行评论。

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by