Hi Luis!
First off, you didn’t answer my specific questions.  You’ve said a
number of inconsistent things, which makes it hard to guess what’s
going on.



 Luis_Costa:

My input is 256x256 images and this is a segmentation problem. I implementing an active learning pipeline, therefore I am trying to get the probabilities and then get the top two classes confidences for each sample.
…
I am afraid that applying softmax on top of sigmoid might be complicating things a bit. This is also a two class problem (Positive/Negative).


Let me make some comments based on what I think you might be
trying to do.
You are trying to segment images.  That means that you assign to
each pixel in the image a class label.  You say this is a two-class
problem.  Therefore you are labelling each pixel as either a “Positive”
pixel or a “Negative” pixel.  (This could be, for example, “foreground”
vs. “background” pixels.)
You say that your images are 256x256 pixels, and that your batch
size is 2.  Therefore I would expect that the input to your model is
a tensor that holds a batch of two images, and therefore has shape
[2, 256, 256].
You have been unclear about the shape of the output of your model,
but you have said more often than not that it is [2, 1, 256, 256].
This mostly makes sense, but it is unclear why you have that dimension
of size 1.  Let me ignore this size-1 dimension, and think of your output
as having shape [2, 256, 256].  So for each sample in your batch,
your output has the shape of an input image, namely, [256, 256].
Normally for a multiclass segmentation problem with nClass classes,
you would have nClass outputs for each pixel – either raw-score
logits or probabilities for that pixel to be in each of the nClass
classes.  But for your two-class, binary segmentation problem, it is
much more common to have only a single output value per pixel, and
this value is either the raw-score logit or the probability of that pixel
being in the “Positive” class.  (You only need one value because the
probability of being in the “Negative” class is simply 1 minus the
probability of being in the “Positive” class.)
You say that the “activation function of my model (Unet) is sigmoid.”
The sigmoid() function converts raw-score logits (that run from
-inf to inf) into probabilities (that run from 0.0 to 1.0).  So the
output of your model should be (for each sample image in your batch)
an “image” of 256x256 “pixels,” each of which is the (predicted)
probability of the corresponding pixel in the input image being in the
“Positive” class.
Based on this, all talk of using softmax() to get probabilities is
confused.  softmax() is used to convert a set of nClass logits in
a multiclass problem into a set of nClass probabilities that sum
to 1.0.  My guess is that you have been trying to apply softmax()
across horizontal rows of (output) pixels (as if you had a 256-class
problem) and are getting garbage.  Assuming that a final sigmoid()
is built into your model, your (output) pixels are already binary
classification probabilities – that is the probability of the input pixel
being in the “Positive” class, with no explicit “Negative” class probability
being given.
Also, based on this, the notion of getting “the top two classes
confidences” doesn’t make sense.  You only have two classes, so
your top two classes will always be “Positive” and “Negative.”
Coming back to


Then, when trying to select the top two classes confidences for each sample,
toptwo = torch.topk(probabilities, 2, dim=1)[0]
I get
RuntimeError: selected index k out of range


If the shape of probabilities really is [2, 1, 256, 256], then this
error message makes sense.  To avoid confusion, note that pytorch
tensors are zero-based, that is, the indices start at 0.  So in the
topk call, dim = 0 would mean the first (in one-based counting)
dimension, that is, the nBatch dimension with size 2.  dim = 1
means the second dimension, that is, the size-1 dimension.  (And
dim = 2 and dim = 3 would be the width and height dimensions,
both of size 256.)
So your topk call is asking for the 2 largest values along a size-1
dimension, that is, along a dimension that only has 1 value.
I suspect that you are somehow mixing together code and concepts
for a multiclass segmentation problem with those for a binary
segmentation problem.
Good luck.
K. Frank