DAGネットワークにおける複数入力について

複数入力のDAGネットワークの設計を考えています。
下記図はイメージです。
DAGnet.jpg
上記のような複数のimageinputにそれぞれ異なる画像を入力したいと考えています。
trainNetwo​rkの関数を用いる際、どのように書いて、指定すればよろしいでしょうか?
よろしくお願いいたします。

3 Comments

Kenta
Kenta on 29 Mar 2020
Yuki Yoshino
Yuki Yoshino on 4 Apr 2020
Kenta 様
参考プログラムのご紹介ありがとうございます.
早速,実装を行った結果,以前作成いたしましたネットワークより学習収束条件が広く,高い分類精度を確認することができました.
カスタマイズできるネットワークでしたので,今後様々なネットワーク構造を試してみようと思います!
Kenta
Kenta on 4 Apr 2020
ありがとうございます。まだまだ作りこみが足りていないところもあると思いますので、またなにかあればご連絡ください。

Sign in to comment.

 Accepted Answer

Shunichi Kusano
Shunichi Kusano on 6 Feb 2020

0 votes

こんにちは。
複数入力一出力の場合(Multi Input Signle Output = MISO)、combine関数でデータストアをひとまとめにしてからtrainNetworkに入力します。このとき、ラベルも一緒にcombineしてあげる必要があるのですが、ラベルだけを管理するdatastoreは現状無いため、ここだけカスタムで作成する必要があります。正式なサポートプロダクトではありませんが、カテゴリカルデータを扱えるカスタムのdatastoreを添付しますので、お使いください。
以上を踏まえてコードの大まかな流れは以下のようになります。
% 画像のデータストアは準備しておく。imds1, imds2とする
% ラベルのデータストアを作成(ラベルはカテゴリカル型のベクトルとして作成しておく。categoricalVectorとする。
labelds = CategoricalDatastore(categoricalVector); % 添付のプログラムを使う
% combineで画像1、画像2、ラベルのデータストアをひとまとめにする
combds = combine(imds1, imds2, labelds);
% 学習(ネットワークとオプションはすでにできているとして)
net = trainNetwork(combds, lgraph, options);
ネットワークにおける入力層の順番は、作成したレイヤーグラフのInputNamesというフィールドでわかります。この順番と、combineした順が対応しますので、combineで入力した一番目のimds1はInputNames(1)に、2番目のimds2はInputNames(2)の入力層に投入されます。今回はどちらでもいいかもしれませんが…。
augmentationなどが必要な場合は、transform関数でtransformedDatastoreを作って、それをcombineするのがいいです。
作成していてまたわからないことがありましたら質問してください。
下記は参考まで
trainNetworkでのcombinedDatastoreの関連記述:

7 Comments

Yuki Yoshino
Yuki Yoshino on 7 Feb 2020
Shunichi Kusano 様
ご回答ありがとうございます。
ご提案いただいた方法により、上記DAGネットワークを用いることができました。
Yuki Yoshino
Yuki Yoshino on 7 Feb 2020
Edited: Yuki Yoshino on 8 Feb 2020
学習データの入力層への入力方法なのですが
上図のように、imageinput1,2の入力にサブフォルダーとして、A,Bの関連データ(同数を各imageinputに)を入力し、出力でそのA,Bの予測は可能でしょうか?
可能であれば
labelds = CategoricalDatastore(categoricalVector);
のcategoricalVectorに
categoricalVector = categorical({A};{A};{B};{B});
(imageinput1にAが2つ、Bが2つのデータの場合、imageinput2も同数)のような形で入力すればよろしいでしょうか?
また、classifyでの分類につきましても、combds = combine(imds1, imds2, labelds);を用いた検証データ(A,B)を作成したものを入力として用いればよろしいでしょうか?
複数の質問で申し訳ありませんが、よろしくお願いいたします。
Kenta
Kenta on 9 Feb 2020
こんにちは。
A,Bの関連データを入力・出力というところの意味がくみ取れませんでした。
ただ、実際に上のコードを動かしてみるとイメージがわくのではないかと思います。
classifyについては、少なくとも、複数の画像入力では、ご認識の通りです。
下に複数入力の例を示します。
capture.JPG
close all;clear;clc
% image dataset1 which includes two categories. 1) the onion image with
% a rectange 2) onion image with a circle
imds1 = imageDatastore('myImages1', ...
'IncludeSubfolders',true,'LabelSource','foldernames');
% the image dataset 2 was made with a corn image
imds2 = imageDatastore('myImages2', ...
'IncludeSubfolders',true,'LabelSource','foldernames');
% randomly divide the image dataset into train and validation data
[imdsTrain1,imdsValidation1] = splitEachLabel(imds1,.7,'randomize');
[imdsTrain2,imdsValidation2] = splitEachLabel(imds2,.7,'randomize');
% to prepare a variable of labels of the training and validation dataset
categoricalVectorTrain=imdsTrain1.Labels;
categoricalVectorValidation=imdsValidation1.Labels;
% convert the variable into categorical datastore
labelDS_train = CategoricalDatastore(categoricalVectorTrain);
labelDS_validation = CategoricalDatastore(categoricalVectorValidation);
% combine the imagedata set 1, 2 and the label data
% herein, the datasets were simply combined since the number of images were
% identical throught the dataset and categories
combdsTrain = combine(imdsTrain1, imdsTrain2, labelDS_train);
combdsValidation = combine(imdsValidation1, imdsValidation2, labelDS_validation);
% load the layer graph variable corresponding to the network to be trained
load lgraph_1
% training options. note that the combination of the minibatch data cannot
% be shuffled. A custom datastore is requried for that purpose.
options = trainingOptions('adam', ...
'InitialLearnRate',0.001, ...
'MiniBatchSize',12, ...
'MaxEpochs',10, ...
'Shuffle','never', ...
'Verbose',false, ...
'Plots','training-progress');
% the network can be trained as well as simple CNNs
net = trainNetwork(combdsTrain, lgraph_1, options);
% use classify function for the prediction
prediction=classify(net,combdsValidation);
% calculate the classification accuracy with the validation dataset
accuracy=mean(prediction==imdsValidation1.Labels)
あまり良い例ではないかもしれませんが、画像に長方形/円を追加で挿入していて、それがどちらかを推論しています。ここでは、1つの画像だけを見せるのではなく、たまねぎ・コーンという2種類の画像を見せて判断させています。円や長方形がフレームアウトして、見えない場合があるので、そのような場合は、2つの画像を与えたら効果的と思いました。詳しくはZIPファイルを見てください。こちら、コードのほう追っていただけましたら、ご質問内容が可能かどうか予想できそうです。
Yuki Yoshino
Yuki Yoshino on 10 Feb 2020
Kenta 様
ご回答ありがとうございます。
サンプルプログラムを拝見させていただきました。DAGネットワークのイメージがつかめ、イメージと合致しており、自身のプログラムにフィードバックができそうです。
Yuki Yoshino
Yuki Yoshino on 10 Feb 2020
Shunichi Kusano様 , Kenta 様
おかげさまで、自身がイメージしておりましたDAGネットワークを作成し、動作させることができました。
お二方のご協力ありがとうございました。
Kenta
Kenta on 10 Feb 2020
ご返信いただきありがとうございます。お役に立ててよかったです。
Rd
Rd on 17 Nov 2020
Dear Kenta,
I too need to do the same thing asmentioned by Yuki Yoshino. I have sequence of six convolutional, relu, batchnormalization and maxpooling layer. i have trained the network with three separate images. Finally i need to fuse these three output. At which layer i have to perform fusion and why? how to do fusion?
Thanks.

Sign in to comment.

More Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products

Release

R2019b

Asked:

on 5 Feb 2020

Commented:

Rd
on 17 Nov 2020

Community Treasure Hunt

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

Start Hunting!