Convert tensor into -1 to 1 range

I want to do image restoration, restore_image = (orig_image - B)/C.
orig_image is in the range (-1,1) but B is in the range of (0,1) and C is random output of LeakyRelU (-infinity, + infinity).

I want to convert B and C in the range (-1,1) for image restoration.

tensor = (tensor/torch.max(tensor) -0.5 ) * 2, should get the job done

Is this formula remain same if the negative value is higher than positive means minimum value is -30 and maximum value is 10?

I gave that solution assuming you were doing it for regular images. Anyways, for C, the following might be a better option.

tensor = (tensor/torch.max(torch.abs(tensor))

This itself should squish the values between - 1 and +1. For B (whose values are between 0 to 1), you can use the previous formula

I restored the image using, restore_image = (orig_image - B)/C.

Here C is output of training. The value comes in the range of (-2,10). After that i apply the transformation to get the value between (-1,1) as mentioned. After transformation,C values are in the range tensor([[[[-1.0349e-02, 4.3450e-02, 3.3275e-02, …, 3.0451e-02, etc.

So when i divide (orig_image - B) by C, the output values are tensor([[[[ 4.2118e+02, -1.7778e+01, -1.5082e+02, …, -2.7659e+02, -4.8812e+01, -1.6656e+01] etc.

Again I transformed it in the range of (-1,1) and I got the final value tensor([[[[ 2.9290e-04, -1.2363e-05, -1.0488e-04, …, -1.9235e-04, -3.3945e-05, -1.1583e-05] etc.

This output restored image is constant with some gray level.
Divide by fraction is creating issue. Can you throw some light to solve the issue?

You are displaying the image with the pixel values lying lie within (-1,1). Try converting the output back between (0-255) before displaying it.
For values between -1 and 1

img = (tensor/2 + 0.5) * 255
img = img.detach().numpy()
img = img.astype(np.uint8)

Then display the image