Stereo
Stereo

Reputation: 1193

Centering of array of images in python

I have an array of images that I want to feed to TensorFlow. I want to center the images around the mean and standardize the standard deviation. I followed this answer but I cannot seem to get the mean to zero. I am learning numpy so maybe I am missing something simple.

My current code is:

import numpy as np

# Load pickled data
import pickle

# TODO: Fill this in based on where you saved the training and testing data

training_file = 'train.p'

with open(training_file, mode='rb') as f:
    train = pickle.load(f)

X_train, y_train = train['features'], train['labels']

# Let us inspect whether the data is centered.
for ch in range(3):
    print("for channel %s mean or clahe data: %s" %(
            ch, X_train[:,ch].mean()))

X_norm = np.copy(X_train)
for ch in range(3):
    X_norm[:, ch] = (X_norm[:, ch] - X_norm[:,ch].mean())/ X_norm[:, ch].std()

# Let us inspect our new mean.
for ch in range(3):
    print("for channel %s new mean for CLAHE data: %s new std: %s" % (
            ch, X_norm[:,ch].mean(), X_norm[:,ch].std()))

The picked data set can be obtained from here

With output:

for channel 0 mean or clahe data: 88.9090870931
for channel 1 mean or clahe data: 88.2472258708
for channel 2 mean or clahe data: 87.5765175619
for channel 0 new mean for CLAHE data: 8.77830238806 new std: 45.7207148838
for channel 1 new mean for CLAHE data: 8.79695563094 new std: 45.7780456089
for channel 2 new mean for CLAHE data: 8.71418658131 new std: 45.5661789057

My desired outcome would have for each channel a mean around zero and standard deviation of 1.

Upvotes: 2

Views: 913

Answers (1)

user6655984
user6655984

Reputation:

The main issue is that the array is of type uint8 (integers 0..255). This can't really be centered or normalized without changing the type of the array. Like this:

X_norm = np.array(X_train, dtype=np.float, copy=True)

Now the entries are floating point numbers, so centering and scaling works fine. However, you may run out of memory (the array is big), so while trying things out, I'd use only a slice of the data:

X_norm = np.array(X_train[:100], dtype=np.float, copy=True)

There's another issue with your code: [:, ch] selector does not do what you think it does. It slices along the second axis (axis=1), not the last one. What you meant is [..., ch] where the ellipsis stands for "as many colons as are needed". See NumPy indexing.


Useful for debugging: print(X_norm.dtype), print(X_norm[:, 0].shape)

Upvotes: 2

Related Questions