Manual Input of Classes for unet Deep Learning Network

3 次查看(过去 30 天)
I am attempting to use a U-Net Network to expedite image segmentation and am encountering some difficulty with regards to using validation data. Here is my code:
% Define and train U-Net Convolutional Neural Network
unetNetwork = unet([sz1, sz2], 7); % Six options plus background
options = trainingOptions('adam','InitialLearnRate',1e-3,'MaxEpochs',5, ...
'MiniBatchSize',16,'Shuffle','every-epoch', ...
'Plots','training-progress','Verbose',true, ...
'ValidationData', {inputValidArray, targetValidArray}, ...
'ValidationFrequency', 30);
% Net will output a sz1 x sz2 x 7 array
[net, info] = trainnet(training_data, unetNetwork, "crossentropy", options);
Above, sz1 and sz2 are the size of interest for my input images, and inputValidArray and targetValidArray are double and categorical arrays of size 224 x 208 x 1 x 1344 respectively. training_data is a CombinedDatastore with two ArrayDatastores, the first being a 224 x 208 double cell array and the second being a 224 x 208 categorical cell array. I am receiving the following error:
Error using trainnet (line 54)
Categories of the validation data must match the categories of the training data.
Error in wireSeg_DL_v2 (line 86)
[net, info] = trainnet(training_data, unetNetwork, "crossentropy", options);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
My target data comes from a manual segmentation I have performed, where each pixel value corresponds to a distinct part of the image that I wanted to segment ('0' is the background, '1' is one object, '2' is another object, etc.). From what I have been able to gather, it seems that the issue is coming from the fact that the ordering of my categories do not line up between the two different data sets. Within the trainnet function, examining the metadata shows that:
% Training Data
metadata.ClassNames{1,2} =
7×1 cell array
{'0'}
{'6'}
{'3'}
{'4'}
{'1'}
{'2'}
{'5'}
% Validation Data
metadata.ClassNames{1,2} =
7×1 cell array
{'0'}
{'6'}
{'3'}
{'1'}
{'2'}
{'4'}
{'5'}
Is there a way for me to set it up so that the Training Data and Validation Data have the same order? I would prefer it so that each of these lists are sequential, but even just making them the same would be enough for me. As an aside, how are these lists generated? Thank you!
  2 个评论
Matt J
Matt J 2025-4-25
Please attach enough data in a .mat file (sz1,sz2, and a small subset of the training and validation data) with which we can run the code ourselves.
Warren Boschen
Warren Boschen 2025-4-25
Certainly. I've attached sz1, sz2, and small samples of inputValidArray, targetValidArray, and training_data. Please let me know if you need anything else.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2025-4-26
编辑:Matt J 2025-4-26
This worked for me:
load('debug.mat')
%Reorganize training data
T=training_data.UnderlyingDatastores{2}.readall;
T=reordercats(cat(4,T{:}));
ds=arrayDatastore( T , IterationDimension=4);
training_data=combine(training_data.UnderlyingDatastores{1}, ds);
%Reorganize validation data
targetValidArray=reordercats(targetValidArray);
validation_data=...
combine(arrayDatastore(inputValidArray,IterationDimension=4),...
arrayDatastore(targetValidArray,IterationDimension=4));
% Define and train U-Net Convolutional Neural Network
unetNetwork = unetLayers([sz1, sz2], 7); % Six options plus background
options = trainingOptions('adam','InitialLearnRate',1e-3,'MaxEpochs',5, ...
'MiniBatchSize',1,'Shuffle','every-epoch', ...
'Plots','none','Verbose',true, ...
'ValidationData', validation_data, ...
'ValidationFrequency', 30);
% Net will output a sz1 x sz2 x 7 array
[net, info] = trainNetwork(training_data, unetNetwork ,options);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by