How to normalize a 4D numpy array? How to normalize a 4D numpy array? numpy numpy

How to normalize a 4D numpy array?


Assuming you're working with image data of shape (W, H, 3), you should probably normalize over each channel (axis=2) separately, as mentioned in the other answer.

You can do this with:

# keepdims makes the result shape (1, 1, 3) instead of (3,). This doesn't matter here, but# would matter if you wanted to normalize over a different axis.v_min = v.min(axis=(0, 1), keepdims=True)v_max = v.max(axis=(0, 1), keepdims=True)(v - v_min)/(v_max - v_min)


  1. Along which axis do we take the min and max?

To answer this we probably need more information about your data, but in general, when discussing 3 channel images for example, we would normalize using the per-channel min and max. this means that we would perform the normalization 3 times - once per channel.Here's an example:

    img = numpy.random.randint(0, 100, size=(10, 10, 3))  # Generating some random numbers    img = img.astype(numpy.float32)  # converting array of ints to floats    img_a = img[:, :, 0]    img_b = img[:, :, 1]    img_c = img[:, :, 2]  # Extracting single channels from 3 channel image    # The above code could also be replaced with cv2.split(img) << which will return 3 numpy arrays (using opencv)    # normalizing per channel data:    img_a = (img_a - numpy.min(img_a)) / (numpy.max(img_a) - numpy.min(img_a))    img_b = (img_b - numpy.min(img_b)) / (numpy.max(img_b) - numpy.min(img_b))    img_c = (img_c - numpy.min(img_c)) / (numpy.max(img_c) - numpy.min(img_c))    # putting the 3 channels back together:    img_norm = numpy.empty((10, 10, 3), dtype=numpy.float32)    img_norm[:, :, 0] = img_a    img_norm[:, :, 1] = img_b    img_norm[:, :, 2] = img_c

Edit: It just occurred to me that once you have the one channel data (32x32 image for instance) you can simply use:

from sklearn.preprocessing import normalizeimg_a_norm = normalize(img_a)
  1. How do we work with the 3D array?

Well, this is a bit of a big question. If you need functions like array-wise min and max I would use the Numpy versions. Indexing, for instance, is achieved through axis-wide separators - as you can see from my example above. Also, please refer to Numpy's documentation of ndarray @ https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.htmlto learn more. they really have an amazing set of tools for n-dimensional arrays.


There are different approaches here. You can either decide to normalize over the whole batch of images or normalize per single image. To do that you can either use the mean of a single image or use the mean of the whole batch of images or use a fixed mean from another dataset - e.g. you can use the ImageNet mean value.

If you want to do the same as Tensorflow's tf.image.per_image_standardization you should normalize per single image with the mean of this image. So you loop through all images and do the normalization for all axes in a single image like this:

import mathimport numpy as npfrom PIL import Image# open imagesimage_1 = Image.open("your_image_1.jpg")image_2 = Image.open("your_image_2.jpg")images = [image_1, image_2]images = np.array(images)standardized_images = []# standardize imagesfor image in images:    mean = image.mean()    stddev = image.std()    adjusted_stddev = max(stddev, 1.0/math.sqrt(image.size))    standardized_image = (image - mean) / adjusted_stddev    standardized_images.append(standardized_image)standardized_images = np.array(standardized_images)