Home > front end >  Rescaling image to get values between 0 and 255
Rescaling image to get values between 0 and 255

Time:12-20

A piece of code taken from here is trying to plot the intermediate outputs of a convolutional neural network. The outputs are taken and rescaled in this way:

if channel_image.sum()!=0:
    channel_image -= channel_image.mean()
    channel_image /= channel_image.std()
    channel_image *= 64
    channel_image  = 128
            
channel_image = np.clip(channel_image, 0, 255).astype("uint8")

Everything in this transformation is pretty clear, except the multiplication by 64: what is that for? Is that an empirical value?

CodePudding user response:

After subtracting the mean and dividing by the standard deviation, you have calculated the Z-Score for the channel image values.

It looks like they have chosen /- 2 for the values to show as the number of standard deviations from the mean as:

2*64   128 = 256
-2*64   128 = 0

So multiplying by 64 scales the values to between -128 and 128, then shifting by 128 moves the values to between 0 and 256.

Any values outside the /-2 standard deviations are then clipped by the line:

np.clip(channel_image, 0, 255).astype("uint8")

Really the 64 value should be calculated as something like:

def multiplier(num_std_deviations):
    return 256/(num_std_deviations*2)

CodePudding user response:

After division by the standard deviation, the values are in a small range (virtually all of them in [-3,3] for a normal distribution). They are rescaled to better fill the [-128,127] range.

  • Related