tensorflow / keras mean image subtraction

  • Last Update :
  • Techknowledgy :

To subtract your mean image from a batch of your image data, you can simply use the minus operator (which is just a syntax sugar for tf.subtract):

In [28]: x = tf.zeros((2, 3, 3))

In [29]: x
Out[29]:
<tf.Tensor: id=38, shape=(2, 3, 3), dtype=float32, numpy=array([[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]], [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]], dtype=float32)>

   In [30]: mean = tf.eye(3)

   In [31]: mean
   Out[31]:
   <tf.Tensor: id=42, shape=(3, 3), dtype=float32, numpy=array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], dtype=float32)>

      In [32]: x - mean
      Out[32]:
      <tf.Tensor: id=44, shape=(2, 3, 3), dtype=float32, numpy=array([[[-1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]], [[-1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]]], dtype=float32)>

To get your PNG image as a TensorFlow tensor, just wrap a numpy array with tf.constant:

import cv2

mean_img = cv2.imread('/path/to/the/image')
mean_img_tensor = tf.constant(mean_img)

Pay attention that OpenCV will read the image into BGR color space by default. You may want to convert it into RGB then:

mean_img = cv2.cvtColor(mean_img, cv2.COLOR_BGR2RGB))

Since you use TF Dataset API, I believe map_and_batch must be a better solution for performance:

def datasetLoader(dataSetPath, batchSize, mean_image_path):
   dataset = tf.data.TFRecordDataset(dataSetPath)
mean_img = cv2.cvtColor(cv2.imread(mean_image_path), cv2.COLOR_BGR2RGB)
mean = tf.constant(mean_img)

dataset = dataset.map(_ds_parser, num_parallel_calls = 8)

# This dataset will go on forever
dataset = dataset.repeat()

def preprocess(X, Y):
   # Bring the date back in shape
X = tf.reshape(X, [-1, 66, 198, 3])
Y = tf.reshape(Y, [-1, 1])
X = X - mean
return X, Y

# Set the batchsize
dataset = dataset.apply(tf.contrib.data.map_and_batch(map_func = preprocess, batch_size = batchSize, num_parallel_calls = 8))

return dataset.make_one_shot_iterator().get_next()
import cv2
import numpy as np

img = img.astype(np.float32)
img -= img.mean()
img /= img.std()

Suggestion : 2

Last updated 2022-06-28 UTC.

View aliases

Compat aliases for migration

See Migration guide for more details.

tf.compat.v1.keras.layers.Subtract

tf.keras.layers.Subtract( **
   kwargs
)

Examples:

    import keras

    input1 = keras.layers.Input(shape = (16, ))
    x1 = keras.layers.Dense(8, activation = 'relu')(input1)
    input2 = keras.layers.Input(shape = (32, ))
    x2 = keras.layers.Dense(8, activation = 'relu')(input2)
    # Equivalent to subtracted = keras.layers.subtract([x1, x2])
    subtracted = keras.layers.Subtract()([x1, x2])

    out = keras.layers.Dense(4)(subtracted)
    model = keras.models.Model(inputs = [input1, input2], outputs = out)

Suggestion : 3

The question is: How can I use my local png anycodings_tensorflow mean image to perform the zero-center and anycodings_tensorflow normalize task? .,During training data generation a mean image anycodings_tensorflow (not mean pixel values per channel) is anycodings_tensorflow calculated. ,In order to improve the learning process, I anycodings_tensorflow would like to apply a simplified method to anycodings_tensorflow zero-center and normalize my network input anycodings_tensorflow data, which mainly consists of RGB images:,The variables X and Y are just tensors which anycodings_tensorflow get populated during a tensorflow session anycodings_tensorflow later on.

In order to improve the learning process, I anycodings_tensorflow would like to apply a simplified method to anycodings_tensorflow zero-center and normalize my network input anycodings_tensorflow data, which mainly consists of RGB images:

image = (image - meanImage + 1.0) / 2.0

At some point of my loading pipeline, I've anycodings_tensorflow the input (X) and output (Y) tensors:

def datasetLoader(dataSetPath, batchSize):
   dataset = tf.data.TFRecordDataset(dataSetPath)

dataset = dataset.map(_ds_parser, num_parallel_calls = 8)

# This dataset will go on forever
dataset = dataset.repeat()

# Set the batchsize
dataset = dataset.batch(batchSize)

# Create an iterator
iterator = dataset.make_one_shot_iterator()

# Create your tf representation of the iterator
X, Y = iterator.get_next()

# Bring the date back in shape
X = tf.reshape(I, [-1, 66, 198, 3])
Y = tf.reshape(Y, [-1, 1])

return X, Y

To subtract your mean image from a batch anycodings_deep-learning of your image data, you can simply use anycodings_deep-learning the minus operator (which is just a anycodings_deep-learning syntax sugar for tf.subtract):

In [28]: x = tf.zeros((2, 3, 3))

In [29]: x
Out[29]:
<tf.Tensor: id=38, shape=(2, 3, 3), dtype=float32, numpy=array([[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]], [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]], dtype=float32)>

   In [30]: mean = tf.eye(3)

   In [31]: mean
   Out[31]:
   <tf.Tensor: id=42, shape=(3, 3), dtype=float32, numpy=array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], dtype=float32)>

      In [32]: x - mean
      Out[32]:
      <tf.Tensor: id=44, shape=(2, 3, 3), dtype=float32, numpy=array([[[-1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]], [[-1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]]], dtype=float32)>

To get your PNG image as a TensorFlow anycodings_deep-learning tensor, just wrap a numpy array with anycodings_deep-learning tf.constant:

import cv2

mean_img = cv2.imread('/path/to/the/image')
mean_img_tensor = tf.constant(mean_img)

Pay attention that OpenCV will read the anycodings_deep-learning image into BGR color space by default. anycodings_deep-learning You may want to convert it into RGB anycodings_deep-learning then:

mean_img = cv2.cvtColor(mean_img, cv2.COLOR_BGR2RGB))

Since you use TF Dataset API, I believe anycodings_deep-learning map_and_batch must be a better solution anycodings_deep-learning for performance:

def datasetLoader(dataSetPath, batchSize, mean_image_path):
   dataset = tf.data.TFRecordDataset(dataSetPath)
mean_img = cv2.cvtColor(cv2.imread(mean_image_path), cv2.COLOR_BGR2RGB)
mean = tf.constant(mean_img)

dataset = dataset.map(_ds_parser, num_parallel_calls = 8)

# This dataset will go on forever
dataset = dataset.repeat()

def preprocess(X, Y):
   # Bring the date back in shape
X = tf.reshape(X, [-1, 66, 198, 3])
Y = tf.reshape(Y, [-1, 1])
X = X - mean
return X, Y

# Set the batchsize
dataset = dataset.apply(tf.contrib.data.map_and_batch(map_func = preprocess, batch_size = batchSize, num_parallel_calls = 8))

return dataset.make_one_shot_iterator().get_next()
import cv2
import numpy as np

img = img.astype(np.float32)
img -= img.mean()
img /= img.std()

Suggestion : 4

4 days ago Here are the examples of the python api tensorflow.subtract taken from open source projects. By voting up you can indicate which examples are most useful and appropriate. By voting up … , We will learn how to do suntraction in TensorFlow using tf.subtract () function. It can subtract list, tuple, scaler, TensorFlow variable/constant/placeholder/SparceMatrix with each other and with scaler and with list/tuple. , 4 days ago May 12, 2020  · We compute the mean from the training image and then subtract that from each image that we’re passing through the network. Then we apply this exact same mean to the … , 3 days ago img = tf.subtract(img, mean_img) is correct as long as the last dimension of img (tf.shape(img)[-1]) is 3. You can check Broadcasting operation in tensorflow Glossary and numpy Broadcasting for further information. how to create the mean_image with the same shape as img? You can try:


IMG_MEAN = np.array((104.00698793, 116.66876762, 122.67891434), dtype = np.float32)

img = tf.ones([100, 100, 3], dtype = tf.float32) #(100, 100, 3) mean = tf.constant([1, 2, 3], dtype = tf.float32) #(3) mean = tf.reshape(mean, [1, 1, 3]) img_m = img - mean with tf.Session() as sess: a = sess.run(img_m) # shape of a(100, 100, 3) # a[: ,: , 0] = 0 # a[: ,: , 1] = -1 # a[: ,: , 1] = -2
IMG_MEAN = np.array((104.00698793, 116.66876762, 122.67891434), dtype = np.float32)
img = tf.subtract(img, mean_img)
img = tf.ones([100, 100, 3], dtype = tf.float32) #(100, 100, 3) mean = tf.constant([1, 2, 3], dtype = tf.float32) #(3) mean = tf.reshape(mean, [1, 1, 3]) img_m = img - mean with tf.Session() as sess: a = sess.run(img_m) # shape of a(100, 100, 3) # a[: ,: , 0] = 0 # a[: ,: , 1] = -1 # a[: ,: , 1] = -2
tf.reshape(tf.tile(m, [tf.reduce_prod(tf.shape(img)[: -1])]), tf.shape(img)).eval()

Suggestion : 5

Generally, we want to always preprocess some standard types of preprocessing on data before training a neural network like, take your original data and to zero mean them or normalize that or normalized by the standard deviation. ,In this tutorial, we propose a method to enhance image recognition performance through image normalization.,Before you start any, you will need a set of images you want to normalize. You can use an archive of creative-commons licensed flower photos from Google.,The mean and standard deviation required to standardize pixel values can be calculated from the pixel values in each image only (sample-wise) or across the entire training dataset (feature-wise).

Before you start any, you will need a set of images you want to normalize. You can use an archive of creative-commons licensed flower photos from Google.

data_root = tf.keras.utils.get_file(
   'flower_photos', 'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
   untar = True)

This example assumes that you have the cv2 Python library installed. The example below loads the image and converts it into a NumPy array.

all_images = list(data_dir.glob('*/*'))
all_images = [str(path) for path in all_images]
random.shuffle(all_images)

data_size = len(all_images)

channels = 3
IMG_SIZE = 150

dataset = np.ndarray(shape = (len(all_images), IMG_SIZE, IMG_SIZE, channels),
   dtype = np.float32)

i = 0
for _file in all_images:
   image = cv2.imread(_file, 1)
image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))

dataset[i] = image
i += 1

Inputs with large integer values can disrupt or slow down the learning process. As such it is good practice to normalize the pixel values so that each pixel value has a value between 0 and 1.This can be achieved by dividing all pixel values by the largest pixel value(255). This is performed across all channels.

dataset = dataset / 255.
dataset.min(), dataset.max(), dataset.shape

We do zero-center by just of substracting a per-channel mean, instead of having an entire mean image. This is just because it turns out that it was similar enough across the whole image, it didn’t make such a big difference to subtract the mean image vs a per-channel value. This is easier to just pass around and deal with.

dataset[..., 0] -= mean[0]
dataset[..., 1] -= mean[1]
dataset[..., 2] -= mean[2]

The distribution of image pixel values often follows a Normal or Gaussian distribution. There may be a benefit in transforming the distribution of image pixel values to be a standard Gaussian. It’s centering the image pixel values on zero and normalizing the values by the standard deviation. The result is a standard Gaussian of pixel values with a mean of 0.0 and a standard deviation of 1.0.

dataset[..., 0] /= std[0]
dataset[..., 1] /= std[1]
dataset[..., 2] /= std[2]

Suggestion : 6

During training data generation a mean image (not mean pixel values per channel) is calculated. ,In order to improve the learning process, I would like to apply a simplified method to zero-center and normalize my network input data, which mainly consists of RGB images:,The question is: How can I use my local png mean image to perform the zero-center and normalize task? .,As DL Framework, I'm using Keras - to load the training data tfrecords files like described here are used.

In order to improve the learning process, I would like to apply a simplified method to zero-center and normalize my network input data, which mainly consists of RGB images:

image = (image - meanImage + 1.0) / 2.0

At some point of my loading pipeline, I've the input (X) and output (Y) tensors:

def datasetLoader(dataSetPath, batchSize):
   dataset = tf.data.TFRecordDataset(dataSetPath)

dataset = dataset.map(_ds_parser, num_parallel_calls = 8)

# This dataset will go on forever
dataset = dataset.repeat()

# Set the batchsize
dataset = dataset.batch(batchSize)

# Create an iterator
iterator = dataset.make_one_shot_iterator()

# Create your tf representation of the iterator
X, Y = iterator.get_next()

# Bring the date back in shape
X = tf.reshape(I, [-1, 66, 198, 3])
Y = tf.reshape(Y, [-1, 1])

return X, Y

To subtract your mean image from a batch of your image data, you can simply use the minus operator (which is just a syntax sugar for tf.subtract):

In [28]: x = tf.zeros((2, 3, 3))

In [29]: x
Out[29]:
<tf.Tensor: id=38, shape=(2, 3, 3), dtype=float32, numpy=array([[[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]], [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]], dtype=float32)>

   In [30]: mean = tf.eye(3)

   In [31]: mean
   Out[31]:
   <tf.Tensor: id=42, shape=(3, 3), dtype=float32, numpy=array([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], dtype=float32)>

      In [32]: x - mean
      Out[32]:
      <tf.Tensor: id=44, shape=(2, 3, 3), dtype=float32, numpy=array([[[-1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]], [[-1., 0., 0.], [ 0., -1., 0.], [ 0., 0., -1.]]], dtype=float32)>

To get your PNG image as a TensorFlow tensor, just wrap a numpy array with tf.constant:

import cv2

mean_img = cv2.imread('/path/to/the/image')
mean_img_tensor = tf.constant(mean_img)

Pay attention that OpenCV will read the image into BGR color space by default. You may want to convert it into RGB then:

mean_img = cv2.cvtColor(mean_img, cv2.COLOR_BGR2RGB))

Since you use TF Dataset API, I believe map_and_batch must be a better solution for performance:

def datasetLoader(dataSetPath, batchSize, mean_image_path):
   dataset = tf.data.TFRecordDataset(dataSetPath)
mean_img = cv2.cvtColor(cv2.imread(mean_image_path), cv2.COLOR_BGR2RGB)
mean = tf.constant(mean_img)

dataset = dataset.map(_ds_parser, num_parallel_calls = 8)

# This dataset will go on forever
dataset = dataset.repeat()

def preprocess(X, Y):
   # Bring the date back in shape
X = tf.reshape(X, [-1, 66, 198, 3])
Y = tf.reshape(Y, [-1, 1])
X = X - mean
return X, Y

# Set the batchsize
dataset = dataset.apply(tf.contrib.data.map_and_batch(map_func = preprocess, batch_size = batchSize, num_parallel_calls = 8))

return dataset.make_one_shot_iterator().get_next()