Normalizing CNN network output to get a distance output between 0 and 1
I'm using PyTorch based CNNs to do feature extraction on images of humans in order to use it to re-identify that same person given a different picture. After the whole process I am left with a 1D vector, about 2048x1 which I then compare using L2 distance as a metric. I am currently trying to normalize that L2 distance output so I can represent the model's prediction as a confidence from 0-1.
I noticed that PyTorch recommends using the where images are loaded in as loaded in to a range of [0, 1] and then normalized using mean = [0.485, 0.456, 0.406] and std = [0.229, 0.224, 0.225]. When it does this, it seems to alter the range of the L2 distance output which changes the range of values for the L2 distance which makes it no longer 0-1. I am wondering is there a way to normalize the output vector back to the 0-1 range or even the L2 distance itself so I can represent it from 0-1
EDIT:
The following is the code that I am using to calculate the L2 distance
distmat = torch.pow(qf, 2).sum(dim=1, keepdim=True).expand(m, n) + \
              torch.pow(gf, 2).sum(dim=1, keepdim=True).expand(n, m).t()
distmat.addmm_(qf, gf.t(), beta=1, alpha=-2, )
distmat = distmat.cpu().numpy()

As was suggested by a commenter, I am attempting to change the L2 distance calculation to instead use cosine distance. This is my current code:
distmat = torch.mm(qf, gf.t())

However when I ran this, I was getting outputs that looked like this:
tensor([[ 0.3244,  0.2478,  0.1808, -0.0249,  0.2137,  0.2113]])
Wondering if this is the right way to do the cosine distance calculation?
EDIT 2:
Here's how cosine similarity's final implementation looked like for me:
qf_norm = qf / qf.norm(dim=1)[:, None]
gf_norm = gf / gf.norm(dim=1)[:, None]
distmat = torch.mm(qf_norm, gf_norm.transpose(0, 1)).cpu().numpy()


Using normalization vs not using normalization
Are you using a pretrained network?
If yes, and the pretrained model was trained with normalized input (mean/std transformation) then you should use those operations. If you use without those operations, the embeddings will not be useful or less useful. Simply, put you should input normalized images, because model was trained on normalized input.
Normalized distances
L2 distance is unbounded. Even if you compute the range over a sample of typical input, it is not guaranteed that a new input image would fall in that range. For this reason, people use cosine similarity which is akin to a normalized dot product and thus will always be between 0 and 1 -1 and 1.
