How to solve: Input datastore returned more than one observation per row for network input 2.
조회 수: 7 (최근 30일)
이전 댓글 표시
I am following the example from here with an extension in mind. My CNN layers are taken from VGG-16 as follows.
layers = vgg16('Weights', 'none');
lgraph = layerGraph(layers);
layers = removeLayers(lgraph, ["fc7","fc8","prob","output","drop6","drop7","relu6","relu7"]);
flatten = flattenLayer('Name', 'flatten_fc6');
layers = addLayers(layers, flatten);
layers = connectLayers(layers, 'fc6', 'flatten_fc6');
Next, I add my feature input layer (observations in training: 7007, NumFeatures =754) as the following.
L=754;
fInput = featureInputLayer(L, 'Name', 'f_input');
concatLayer = concatenationLayer(1, 2, 'Name', 'concat_fc6_f');
newLayers = [
fullyConnectedLayer(1024, 'Name', 'fc7_new')
reluLayer('Name', 'relu7_new')
fullyConnectedLayer(numel(unique(classNames)), 'Name', 'fc8_new')
softmaxLayer('Name', 'softmax')
classificationLayer('Name', 'output')
];
layers = addLayers(layers, fInput);
layers = addLayers(layers, concatLayer);
layers = addLayers(layers, newLayers);
layers = connectLayers(layers, 'f_input', 'concat_fc6_f/in2');
layers = connectLayers(layers, 'flatten_fc6', 'concat_fc6_f/in1');
layers = connectLayers(layers, 'concat_fc6_f', 'fc7_new');
After this, I create my combined datastore with 3 cells: images, feature vectors, and labels as follows.
tbl = table(imdsTrain.Files, XTrain', imdsTrain.Labels, ...
'VariableNames', {'imagePath','topo','label'});
cds = fileDatastore(tbl.imagePath, 'ReadFcn', @(x) imread(x)); % NumObservations X 1 cell of images
fDS = arrayDatastore(tbl.topo'); % tbl.topo' -> NumFeatures x NumObservations
labelDS = arrayDatastore(tbl.label);
combinedDS = combine(cds, topoDS, labelDS);
Now, comes the training with options, which brings up the error.
Error using trainNetwork
Input datastore returned more than one observation per row for network input 2.
options = trainingOptions('adam', ...
'MiniBatchSize', miniBatchSize, ...
'MaxEpochs', 1000, ...
'Shuffle', 'every-epoch', ...
'Verbose', true, ...
'Plots', 'training-progress');
trainedNet = trainNetwork(combinedDS, layers, options);
When I do preview(combinedDS), I see the following:
1×3 cell array
Column 1
{28×28×3 uint8}
Column 2
{[-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 … ]} % one 754-length feature vector
Column 3
{[0]} % class/label
What am I doing wrong and how can I fix the error?
댓글 수: 0
답변 (1개)
Cris LaPierre
2025년 5월 31일
편집: Cris LaPierre
2025년 5월 31일
The issue is that trainNetwork expects the feature input to be a numfeatures x 1 cell array. You are passing in a 1 x numfeatures array instead. This is leading to the error "Input datastore returned more than one observation per row for network input 2." Each colun is considred an observation.
Assuming your images are already the expected size (224 x 224 x 3), try this to organize the feature input to be what is required
fDS = arrayDatastore(num2cell(tbl.topo',1)',"OutputType","same");
I also used an imageDataStore instead of a fileDataStore, though I don't think that makes a difference.
Here's your code with my updates. It worked on a simple example I created using the dataset in the example you linked to (after some modifications).
layers = vgg16('Weights', 'none');
lgraph = layerGraph(layers);
layers = removeLayers(lgraph, ["fc7","fc8","prob","output","drop6","drop7","relu6","relu7"]);
flatten = flattenLayer('Name', 'flatten_fc6');
layers = addLayers(layers, flatten);
layers = connectLayers(layers, 'fc6', 'flatten_fc6');
L=754;
fInput = featureInputLayer(L, 'Name', 'f_input');
concatLayer = concatenationLayer(1, 2, 'Name', 'concat_fc6_f');
newLayers = [
fullyConnectedLayer(1024, 'Name', 'fc7_new')
reluLayer('Name', 'relu7_new')
fullyConnectedLayer(numel(unique(classNames)), 'Name', 'fc8_new')
softmaxLayer('Name', 'softmax')
classificationLayer('Name', 'output')
];
layers = addLayers(layers, fInput);
layers = addLayers(layers, concatLayer);
layers = addLayers(layers, newLayers);
layers = connectLayers(layers, 'f_input', 'concat_fc6_f/in2');
layers = connectLayers(layers, 'flatten_fc6', 'concat_fc6_f/in1');
layers = connectLayers(layers, 'concat_fc6_f', 'fc7_new');
tbl = table(imdsTrain.Files, XTrain', imdsTrain.Labels, ...
'VariableNames', {'imagePath','topo','label'});
cds = imageDatastore(tbl.imagePath, 'ReadFcn', @(x) imread(x)); % NumObservations X 1 cell of images
fDS = arrayDatastore(num2cell(tbl.topo',1)',"OutputType","same"); % tbl.topo' -> NumFeatures x NumObservations
labelDS = arrayDatastore(tbl.label);
combinedDS = combine(cds, fDS, labelDS);
options = trainingOptions('adam', ...
'MiniBatchSize', miniBatchSize, ...
'MaxEpochs', 1000, ...
'Shuffle', 'every-epoch', ...
'Verbose', true, ...
'Plots', 'training-progress');
trainedNet = trainNetwork(combinedDS, layers, options);
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Data Workflows에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!