U-net looses connections, becomes linear rather than U-shaped (unetLayers)
3 views (last 30 days)
Show older comments
I'm trying to follow the example in Semantic Segmentation of Multispectral Images Using Deep Learning, with the goal of using the pretrained network for tranfer learning to my own semantic segmentation network to use on other terrain images. Unfortunately, my U-net becomes linear, and I cannot figure out why.
When training, the input image size is [256, 256, 6] and there are 18 classes. To keep things simple, I train my network with the data linked in the example and use the function unetLayers (rather than the helper function referenced in the example).
inputTileSize = [256,256,6];
lgraph = unetLayers(inputTileSize, 18, 'EncoderDepth', 4);
plot(lgraph)
I train the network using:
%the mat files and randomPatchExtractionDatastore function come from the
%linked matlab example page above
imds = imageDatastore("train_data.mat",FileExtensions=".mat",ReadFcn=@matRead6Channels);
pxds = pixelLabelDatastore("train_labels.png",classNames,pixelLabelIds);
dsTrain = randomPatchExtractionDatastore(imds,pxds,[256,256],PatchesPerImage=1000);
initialLearningRate = 0.05;
maxEpochs = 5; %low b/c proof of concept, not meant for actual use
minibatchSize = 8;
l2reg = 0.0001;
options = trainingOptions("sgdm",...
InitialLearnRate=initialLearningRate, ...
Momentum=0.9,...
L2Regularization=l2reg,...
MaxEpochs=maxEpochs,...
MiniBatchSize=minibatchSize,...
LearnRateSchedule="piecewise",...
Shuffle="every-epoch",...
GradientThresholdMethod="l2norm",...
GradientThreshold=0.05, ...
Plots="training-progress", ...
VerboseFrequency=20);
net = trainNetwork(dsTrain,lgraph,options);
save("my_multispectralUnet_2.mat", "net");
After training, I load and plot the network. It is linear, rather than U-shaped.
data = load("C:\\Work\\CMFD\\my_multispectralUnet_2.mat");
net = data.net;
plot(layerGraph(net.Layers))
No errors occur while running the above code. What happened to the connections between the encoder and decoder sections?
2 Comments
mohd akmal masud
on 14 Jun 2023
you lost your connection between encoder and decorder network.
you can edit in network deisgner apps
Accepted Answer
Richard
on 15 Jun 2023
This is not being caused by the training, or saving and loading: the network is likely correct at that point. The loss of connection data is just caused by the form of your second call to the plot() function:
plot(layerGraph(net.Layers))
This line is first extracting just the layers as a linear list when it calls net.Layers, then constructing a new LayerGraph which has none of the original connections from net. If you just call:
plot(net)
then you will see the correct network.
More Answers (0)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!