change 'classWeights' in unetLayers

R
reena · May 28, 2021 · 2K views
Question
Hello,   im using Unet to perform semantic segmentation (I want to segment liver metastasis). I want to weight 'more' the pixels of the metastasis respect to the background but I can't manage to do it...   Code for creating unet architecture:   lgraph = unetLayers(imageSize, numClasses, 'EncoderDepth', 4);   How can I change the 'classWeights' in lgraph.Layers(end).pixelClassificationLayer in order to do this??   If, for example, I do this:   lgraph.Layers(end).pixelClassificationLayer.classWeights = [2, 0.5];   I receive an error telling me that i have first to put the 'labels' to the 'classes' options in lgraph.Layers(end), but if i do this:   lgraph.Layers(end) = {'metastasis', 'background'};   I receive an error telling that that layer is in 'read only' mode....
Expert Answer
Profile picture of John Michell
John Michell PhD Expert
Answered Aug 16, 2026
Hi,
 
The unetLayers object in MATLAB is in read only mode and we cannot change the properties of layers as such. You may remove the last layer which is the Segmentation-Layer and can add a new Segmentation-Layer with the properties that are mentioned.
 
Following Code will help.
 
 
% Last Layer Name is 'Segmentation-Layer' 
% for checking layer name use :- lgraph.Layers 
lgraph = removeLayers(lgraph,'Segmentation-Layer'); 
layerlast = pixelClassificationLayer('Classes',{'metastatis','background'},'ClassWeights',[2,0.5],'Name','New_segmentation_Layer'); 
layer_to_add = [layerlast]; 
lgraph = addLayers(lgraph,layer_to_add); 

 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!