Sorry I have been going around in circles, and can’t find a suitable example
I have previously used a UNET to segment singlechannel images with a binary classification output.
I am trying to adapt this to work with a 3 channel input, and a multiclass output.

Although in the builder there is a variable for numClasses, this doesn’t seem to get incorporated into the model anywhere. I presume that was just left in the builder from a copy and paste job of another model.

I figured given the output activation function would need to be changed from SIGMOID to SOFTMAX, and the loss function from XENT to MCXENT perhaps this is why the numClass variable is not used as it would not be as simple as just changing that value?

So I copied the UNet class into my own class and altered the last two layers from
.addLayer("conv10", new ConvolutionLayer.Builder(1,1).stride(1,1).nOut(1)
.convolutionMode(ConvolutionMode.Same).cudnnAlgoMode(cudnnAlgoMode)
.activation(Activation.IDENTITY).build(), "conv93")
.addLayer("output", new CnnLossLayer.Builder(LossFunctions.LossFunction.XENT)
.activation(Activation.SIGMOID).build(), "conv10")
to
.addLayer("conv10", new ConvolutionLayer.Builder(1,1).stride(1,1).nOut(numClasses)
.convolutionMode(ConvolutionMode.Same).cudnnAlgoMode(cudnnAlgoMode)
.activation(Activation.IDENTITY).build(), "conv93")
.addLayer("output", new CnnLossLayer.Builder(LossFunctions.LossFunction.MCXENT)
.activation(Activation.SOFTMAX).build(), "conv10")
I am not sure if this is correct as I am failing at the next step, but included it just in case I have messed something up there.
Here is the model
=======================================================================================================
VertexName (VertexType) nIn,nOut TotalParams ParamsShape Vertex Inputsinput (InputVertex) ,   
conv11 (ConvolutionLayer) 3,64 1,792 W:{64,3,3,3}, b:{1,64} [input]
conv12 (ConvolutionLayer) 64,64 36,928 W:{64,64,3,3}, b:{1,64} [conv11]
pool1 (SubsamplingLayer) , 0  [conv12]
conv21 (ConvolutionLayer) 64,128 73,856 W:{128,64,3,3}, b:{1,128} [pool1]
conv22 (ConvolutionLayer) 128,128 147,584 W:{128,128,3,3}, b:{1,128} [conv21]
pool2 (SubsamplingLayer) , 0  [conv22]
conv31 (ConvolutionLayer) 128,256 295,168 W:{256,128,3,3}, b:{1,256} [pool2]
conv32 (ConvolutionLayer) 256,256 590,080 W:{256,256,3,3}, b:{1,256} [conv31]
pool3 (SubsamplingLayer) , 0  [conv32]
conv41 (ConvolutionLayer) 256,512 1,180,160 W:{512,256,3,3}, b:{1,512} [pool3]
conv42 (ConvolutionLayer) 512,512 2,359,808 W:{512,512,3,3}, b:{1,512} [conv41]
drop4 (DropoutLayer) , 0  [conv42]
pool4 (SubsamplingLayer) , 0  [drop4]
conv51 (ConvolutionLayer) 512,1024 4,719,616 W:{1024,512,3,3}, b:{1,1024} [pool4]
conv52 (ConvolutionLayer) 1024,1024 9,438,208 W:{1024,1024,3,3}, b:{1,1024} [conv51]
drop5 (DropoutLayer) , 0  [conv52]
up61 (Upsampling2D) , 0  [drop5]
up62 (ConvolutionLayer) 1024,512 2,097,664 W:{512,1024,2,2}, b:{1,512} [up61]
merge6 (MergeVertex) ,   [drop4, up62]
conv61 (ConvolutionLayer) 1024,512 4,719,104 W:{512,1024,3,3}, b:{1,512} [merge6]
conv62 (ConvolutionLayer) 512,512 2,359,808 W:{512,512,3,3}, b:{1,512} [conv61]
up71 (Upsampling2D) , 0  [conv62]
up72 (ConvolutionLayer) 512,256 524,544 W:{256,512,2,2}, b:{1,256} [up71]
merge7 (MergeVertex) ,   [conv32, up72]
conv71 (ConvolutionLayer) 512,256 1,179,904 W:{256,512,3,3}, b:{1,256} [merge7]
conv72 (ConvolutionLayer) 256,256 590,080 W:{256,256,3,3}, b:{1,256} [conv71]
up81 (Upsampling2D) , 0  [conv72]
up82 (ConvolutionLayer) 256,128 131,200 W:{128,256,2,2}, b:{1,128} [up81]
merge8 (MergeVertex) ,   [conv22, up82]
conv81 (ConvolutionLayer) 256,128 295,040 W:{128,256,3,3}, b:{1,128} [merge8]
conv82 (ConvolutionLayer) 128,128 147,584 W:{128,128,3,3}, b:{1,128} [conv81]
up91 (Upsampling2D) , 0  [conv82]
up92 (ConvolutionLayer) 128,64 32,832 W:{64,128,2,2}, b:{1,64} [up91]
merge9 (MergeVertex) ,   [conv12, up92]
conv91 (ConvolutionLayer) 128,64 73,792 W:{64,128,3,3}, b:{1,64} [merge9]
conv92 (ConvolutionLayer) 64,64 36,928 W:{64,64,3,3}, b:{1,64} [conv91]
conv93 (ConvolutionLayer) 64,2 1,154 W:{2,64,3,3}, b:{1,2} [conv92]
conv10 (ConvolutionLayer) 2,5 15 W:{5,2,1,1}, b:{1,5} [conv93]
output (CnnLossLayer) , 0  [conv10]Total Parameters: 31,032,849 Trainable Parameters: 31,032,849 Frozen Parameters: 0
=======================================================================================================
I am struggling to understand how to load in the labels with a multiclass dataset. I have my input data which are RGB images, and I have an 8bit grayscale images for the labels. The pixel value in the label image refers to the class the pixel belongs to 0,1,2,3 or 4 (5 classes). I don’t know how to load this in, but from reading I am semisure these need to be onehot encoded, so in my PathLabelGenerator I open the the label image and create a new NDArray using the channels index to identify what output class it should belong to
private Writable oneHotEncode(INDArray in) {
System.out.println("Input Shape: " + in.shapeInfoToString());
float[][][][] oneHotEncoded = new float[1][labelNum][height][width];
INDArray outputShape = Nd4j.create(oneHotEncoded);
System.out.println("Output Shape " + outputShape.shapeInfoToString());
for(int w = 0; w < width; w++) {
for(int h = 0; h < height; h++) {
int value = (int) in.getFloat(new int[] {0, 0, h, w});
oneHotEncoded[0][value][h][w] = 1f;
}
}
return new NDArrayWritable(Nd4j.create(oneHotEncoded));
}
I am guessing this is not the correct way as I am getting the following error
Exception in thread "Thread7" java.lang.IllegalStateException: Input and label arrays do not have same shape: [4, 5, 512, 512] vs. [4, 1, 512, 512]
at org.nd4j.common.base.Preconditions.throwStateEx(Preconditions.java:638)
at org.nd4j.common.base.Preconditions.checkState(Preconditions.java:337)
at org.deeplearning4j.nn.layers.convolution.CnnLossLayer.backpropGradient(CnnLossLayer.java:67)
at org.deeplearning4j.nn.graph.vertex.impl.LayerVertex.doBackward(LayerVertex.java:148)
at org.deeplearning4j.nn.graph.ComputationGraph.calcBackpropGradients(ComputationGraph.java:2772)
at org.deeplearning4j.nn.graph.ComputationGraph.computeGradientAndScore(ComputationGraph.java:1381)
at org.deeplearning4j.nn.graph.ComputationGraph.computeGradientAndScore(ComputationGraph.java:1341)
at org.deeplearning4j.optimize.solvers.BaseOptimizer.gradientAndScore(BaseOptimizer.java:174)
at org.deeplearning4j.optimize.solvers.StochasticGradientDescent.optimize(StochasticGradientDescent.java:61)
at org.deeplearning4j.optimize.Solver.optimize(Solver.java:52)
at org.deeplearning4j.nn.graph.ComputationGraph.fitHelper(ComputationGraph.java:1165)
at org.deeplearning4j.nn.graph.ComputationGraph.fit(ComputationGraph.java:1115)
at org.deeplearning4j.nn.graph.ComputationGraph.fit(ComputationGraph.java:1082)
at org.deeplearning4j.nn.graph.ComputationGraph.fit(ComputationGraph.java:1018)
at org.cascade.ai.DeepLearningTrainerWorkspace.startTraining(DeepLearningTrainerWorkspace.java:247)
at org.cascade.ai.DeepLearningTrainerWorkspace.lambda$3(DeepLearningTrainerWorkspace.java:157)
at java.base/java.lang.Thread.run(Thread.java:833)
If someone can point out what I am doing wrong, or point me to an example that uses a convolution neural network with a CnnLossLayer output that uses multiclass then I can start trying to figure out how to put back in all the hair I have just pulled out.