Python tensorflow.histogram_fixed_width() Examples
The following are 18
code examples of tensorflow.histogram_fixed_width().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow
, or try the search function
.
Example #1
Source File: histogram_ops_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def test_two_updates_on_constant_input(self): # Bins will be: # (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) value_range = [0.0, 5.0] values_1 = [-1.0, 0.0, 1.5, 2.0, 5.0, 15] values_2 = [1.5, 4.5, 4.5, 4.5, 0.0, 0.0] expected_bin_counts_1 = [2, 1, 1, 0, 2] expected_bin_counts_2 = [2, 1, 0, 0, 3] with self.test_session(): values = tf.placeholder(tf.float32, shape=[6]) hist = tf.histogram_fixed_width(values, value_range, nbins=5) # The values in hist should depend on the current feed and nothing else. self.assertAllClose(expected_bin_counts_1, hist.eval(feed_dict={values: values_1})) self.assertAllClose(expected_bin_counts_2, hist.eval(feed_dict={values: values_2})) self.assertAllClose(expected_bin_counts_1, hist.eval(feed_dict={values: values_1})) self.assertAllClose(expected_bin_counts_1, hist.eval(feed_dict={values: values_1}))
Example #2
Source File: histogram_ops_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def test_two_updates_on_scalar_input(self): # Bins will be: # (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) value_range = [0.0, 5.0] values_1 = 1.5 values_2 = 2.5 expected_bin_counts_1 = [0, 1, 0, 0, 0] expected_bin_counts_2 = [0, 0, 1, 0, 0] with self.test_session(): values = tf.placeholder(tf.float32, shape=[]) hist = tf.histogram_fixed_width(values, value_range, nbins=5) # The values in hist should depend on the current feed and nothing else. self.assertAllClose(expected_bin_counts_2, hist.eval(feed_dict={values: values_2})) self.assertAllClose(expected_bin_counts_1, hist.eval(feed_dict={values: values_1})) self.assertAllClose(expected_bin_counts_1, hist.eval(feed_dict={values: values_1})) self.assertAllClose(expected_bin_counts_2, hist.eval(feed_dict={values: values_2}))
Example #3
Source File: train_regression.py From nuke-ML-server with Apache License 2.0 | 5 votes |
def histogram(tensor, value_range=[0.0, 1.0], nbins=100): """Return histogram of tensor""" h, w, c = tensor.shape hist = tf.histogram_fixed_width(tensor, value_range, nbins=nbins) hist = tf.divide(hist, h * w * c) return hist
Example #4
Source File: model.py From CGvsPhoto with MIT License | 5 votes |
def histogram(x, nbins): """Returns the Tensor containing the nbins values of the normalized histogram of x""" h = tf.histogram_fixed_width(x, value_range = [-1.0,1.0], nbins = nbins, dtype = tf.float32) return(h)
Example #5
Source File: augment.py From models with Apache License 2.0 | 5 votes |
def equalize(image: tf.Tensor) -> tf.Tensor: """Implements Equalize function from PIL using TF ops.""" def scale_channel(im, c): """Scale the data in the channel to implement equalize.""" im = tf.cast(im[:, :, c], tf.int32) # Compute the histogram of the image channel. histo = tf.histogram_fixed_width(im, [0, 255], nbins=256) # For the purposes of computing the step, filter out the nonzeros. nonzero = tf.where(tf.not_equal(histo, 0)) nonzero_histo = tf.reshape(tf.gather(histo, nonzero), [-1]) step = (tf.reduce_sum(nonzero_histo) - nonzero_histo[-1]) // 255 def build_lut(histo, step): # Compute the cumulative sum, shifting by step // 2 # and then normalization by step. lut = (tf.cumsum(histo) + (step // 2)) // step # Shift lut, prepending with 0. lut = tf.concat([[0], lut[:-1]], 0) # Clip the counts to be in range. This is done # in the C code for image.point. return tf.clip_by_value(lut, 0, 255) # If step is zero, return the original image. Otherwise, build # lut from the full histogram and step and then index from it. result = tf.cond(tf.equal(step, 0), lambda: im, lambda: tf.gather(build_lut(histo, step), im)) return tf.cast(result, tf.uint8) # Assumes RGB for now. Scales each channel independently # and then stacks the result. s1 = scale_channel(image, 0) s2 = scale_channel(image, 1) s3 = scale_channel(image, 2) image = tf.stack([s1, s2, s3], 2) return image
Example #6
Source File: augment.py From models with Apache License 2.0 | 5 votes |
def contrast(image: tf.Tensor, factor: float) -> tf.Tensor: """Equivalent of PIL Contrast.""" degenerate = tf.image.rgb_to_grayscale(image) # Cast before calling tf.histogram. degenerate = tf.cast(degenerate, tf.int32) # Compute the grayscale histogram, then compute the mean pixel value, # and create a constant image size of that value. Use that as the # blending degenerate target of the original image. hist = tf.histogram_fixed_width(degenerate, [0, 255], nbins=256) mean = tf.reduce_sum(tf.cast(hist, tf.float32)) / 256.0 degenerate = tf.ones_like(degenerate, dtype=tf.float32) * mean degenerate = tf.clip_by_value(degenerate, 0.0, 255.0) degenerate = tf.image.grayscale_to_rgb(tf.cast(degenerate, tf.uint8)) return blend(degenerate, image, factor)
Example #7
Source File: autoaugment_utils.py From Live-feed-object-device-identification-using-Tensorflow-and-OpenCV with Apache License 2.0 | 5 votes |
def equalize(image): """Implements Equalize function from PIL using TF ops.""" def scale_channel(im, c): """Scale the data in the channel to implement equalize.""" im = tf.cast(im[:, :, c], tf.int32) # Compute the histogram of the image channel. histo = tf.histogram_fixed_width(im, [0, 255], nbins=256) # For the purposes of computing the step, filter out the nonzeros. nonzero = tf.where(tf.not_equal(histo, 0)) nonzero_histo = tf.reshape(tf.gather(histo, nonzero), [-1]) step = (tf.reduce_sum(nonzero_histo) - nonzero_histo[-1]) // 255 def build_lut(histo, step): # Compute the cumulative sum, shifting by step // 2 # and then normalization by step. lut = (tf.cumsum(histo) + (step // 2)) // step # Shift lut, prepending with 0. lut = tf.concat([[0], lut[:-1]], 0) # Clip the counts to be in range. This is done # in the C code for image.point. return tf.clip_by_value(lut, 0, 255) # If step is zero, return the original image. Otherwise, build # lut from the full histogram and step and then index from it. result = tf.cond(tf.equal(step, 0), lambda: im, lambda: tf.gather(build_lut(histo, step), im)) return tf.cast(result, tf.uint8) # Assumes RGB for now. Scales each channel independently # and then stacks the result. s1 = scale_channel(image, 0) s2 = scale_channel(image, 1) s3 = scale_channel(image, 2) image = tf.stack([s1, s2, s3], 2) return image
Example #8
Source File: autoaugment_utils.py From Live-feed-object-device-identification-using-Tensorflow-and-OpenCV with Apache License 2.0 | 5 votes |
def contrast(image, factor): """Equivalent of PIL Contrast.""" degenerate = tf.image.rgb_to_grayscale(image) # Cast before calling tf.histogram. degenerate = tf.cast(degenerate, tf.int32) # Compute the grayscale histogram, then compute the mean pixel value, # and create a constant image size of that value. Use that as the # blending degenerate target of the original image. hist = tf.histogram_fixed_width(degenerate, [0, 255], nbins=256) mean = tf.reduce_sum(tf.cast(hist, tf.float32)) / 256.0 degenerate = tf.ones_like(degenerate, dtype=tf.float32) * mean degenerate = tf.clip_by_value(degenerate, 0.0, 255.0) degenerate = tf.image.grayscale_to_rgb(tf.cast(degenerate, tf.uint8)) return blend(degenerate, image, factor)
Example #9
Source File: autoaugment_v1.py From mobilenetv2-yolov3 with MIT License | 5 votes |
def equalize(image): """Implements Equalize function from PIL using TF ops.""" def scale_channel(im, c): """Scale the data in the channel to implement equalize.""" im = tf.cast(im[:, :, c], tf.int32) # Compute the histogram of the image channel. histo = tf.histogram_fixed_width(im, [0, 255], nbins=256) # For the purposes of computing the step, filter out the nonzeros. nonzero = tf.where(tf.not_equal(histo, 0)) nonzero_histo = tf.reshape(tf.gather(histo, nonzero), [-1]) step = (tf.reduce_sum(nonzero_histo) - nonzero_histo[-1]) // 255 def build_lut(histo, step): # Compute the cumulative sum, shifting by step // 2 # and then normalization by step. lut = (tf.cumsum(histo) + (step // 2)) // step # Shift lut, prepending with 0. lut = tf.concat([[0], lut[:-1]], 0) # Clip the counts to be in range. This is done # in the C code for image.point. return tf.clip_by_value(lut, 0, 255) # If step is zero, return the original image. Otherwise, build # lut from the full histogram and step and then index from it. result = tf.cond(tf.equal(step, 0), lambda: im, lambda: tf.gather(build_lut(histo, step), im)) return tf.cast(result, tf.uint8) # Assumes RGB for now. Scales each channel independently # and then stacks the result. s1 = scale_channel(image, 0) s2 = scale_channel(image, 1) s3 = scale_channel(image, 2) image = tf.stack([s1, s2, s3], 2) return image
Example #10
Source File: autoaugment_v1.py From mobilenetv2-yolov3 with MIT License | 5 votes |
def contrast(image, factor): """Equivalent of PIL Contrast.""" degenerate = tf.image.rgb_to_grayscale(image) # Cast before calling tf.histogram. degenerate = tf.cast(degenerate, tf.int32) # Compute the grayscale histogram, then compute the mean pixel value, # and create a constant image size of that value. Use that as the # blending degenerate target of the original image. hist = tf.histogram_fixed_width(degenerate, [0, 255], nbins=256) mean = tf.reduce_sum(tf.cast(hist, tf.float32)) / 256.0 degenerate = tf.ones_like(degenerate, dtype=tf.float32) * mean degenerate = tf.clip_by_value(degenerate, 0.0, 255.0) degenerate = tf.image.grayscale_to_rgb(tf.cast(degenerate, tf.uint8)) return blend(degenerate, image, factor)
Example #11
Source File: histogram_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def test_one_update_on_constant_2d_input(self): # Bins will be: # (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) value_range = [0.0, 5.0] values = [[-1.0, 0.0, 1.5], [2.0, 5.0, 15]] expected_bin_counts = [2, 1, 1, 0, 2] with self.test_session(): hist = tf.histogram_fixed_width(values, value_range, nbins=5) # Hist should start "fresh" with every eval. self.assertAllClose(expected_bin_counts, hist.eval()) self.assertAllClose(expected_bin_counts, hist.eval())
Example #12
Source File: histogram_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def test_one_update_on_constant_input(self): # Bins will be: # (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) value_range = [0.0, 5.0] values = [-1.0, 0.0, 1.5, 2.0, 5.0, 15] expected_bin_counts = [2, 1, 1, 0, 2] with self.test_session(): hist = tf.histogram_fixed_width(values, value_range, nbins=5) # Hist should start "fresh" with every eval. self.assertAllClose(expected_bin_counts, hist.eval()) self.assertAllClose(expected_bin_counts, hist.eval())
Example #13
Source File: histogram_ops_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def test_empty_input_gives_all_zero_counts(self): # Bins will be: # (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) value_range = [0.0, 5.0] values = [] expected_bin_counts = [0, 0, 0, 0, 0] with self.test_session(): hist = tf.histogram_fixed_width(values, value_range, nbins=5) # Hist should start "fresh" with every eval. self.assertAllClose(expected_bin_counts, hist.eval()) self.assertAllClose(expected_bin_counts, hist.eval())
Example #14
Source File: autoaugment.py From tpu_models with Apache License 2.0 | 5 votes |
def equalize(image): """Implements Equalize function from PIL using TF ops.""" def scale_channel(im, c): """Scale the data in the channel to implement equalize.""" im = tf.cast(im[:, :, c], tf.int32) # Compute the histogram of the image channel. histo = tf.histogram_fixed_width(im, [0, 255], nbins=256) # For the purposes of computing the step, filter out the nonzeros. nonzero = tf.where(tf.not_equal(histo, 0)) nonzero_histo = tf.reshape(tf.gather(histo, nonzero), [-1]) step = (tf.reduce_sum(nonzero_histo) - nonzero_histo[-1]) // 255 def build_lut(histo, step): # Compute the cumulative sum, shifting by step // 2 # and then normalization by step. lut = (tf.cumsum(histo) + (step // 2)) // step # Shift lut, prepending with 0. lut = tf.concat([[0], lut[:-1]], 0) # Clip the counts to be in range. This is done # in the C code for image.point. return tf.clip_by_value(lut, 0, 255) # If step is zero, return the original image. Otherwise, build # lut from the full histogram and step and then index from it. result = tf.cond(tf.equal(step, 0), lambda: im, lambda: tf.gather(build_lut(histo, step), im)) return tf.cast(result, tf.uint8) # Assumes RGB for now. Scales each channel independently # and then stacks the result. s1 = scale_channel(image, 0) s2 = scale_channel(image, 1) s3 = scale_channel(image, 2) image = tf.stack([s1, s2, s3], 2) return image
Example #15
Source File: autoaugment.py From tpu_models with Apache License 2.0 | 5 votes |
def contrast(image, factor): """Equivalent of PIL Contrast.""" degenerate = tf.image.rgb_to_grayscale(image) # Cast before calling tf.histogram. degenerate = tf.cast(degenerate, tf.int32) # Compute the grayscale histogram, then compute the mean pixel value, # and create a constant image size of that value. Use that as the # blending degenerate target of the original image. hist = tf.histogram_fixed_width(degenerate, [0, 255], nbins=256) mean = tf.reduce_sum(tf.cast(hist, tf.float32)) / 256.0 degenerate = tf.ones_like(degenerate, dtype=tf.float32) * mean degenerate = tf.clip_by_value(degenerate, 0.0, 255.0) degenerate = tf.image.grayscale_to_rgb(tf.cast(degenerate, tf.uint8)) return blend(degenerate, image, factor)
Example #16
Source File: autoaugment_utils.py From tpu_models with Apache License 2.0 | 5 votes |
def equalize(image): """Implements Equalize function from PIL using TF ops.""" def scale_channel(im, c): """Scale the data in the channel to implement equalize.""" im = tf.cast(im[:, :, c], tf.int32) # Compute the histogram of the image channel. histo = tf.histogram_fixed_width(im, [0, 255], nbins=256) # For the purposes of computing the step, filter out the nonzeros. nonzero = tf.where(tf.not_equal(histo, 0)) nonzero_histo = tf.reshape(tf.gather(histo, nonzero), [-1]) step = (tf.reduce_sum(nonzero_histo) - nonzero_histo[-1]) // 255 def build_lut(histo, step): # Compute the cumulative sum, shifting by step // 2 # and then normalization by step. lut = (tf.cumsum(histo) + (step // 2)) // step # Shift lut, prepending with 0. lut = tf.concat([[0], lut[:-1]], 0) # Clip the counts to be in range. This is done # in the C code for image.point. return tf.clip_by_value(lut, 0, 255) # If step is zero, return the original image. Otherwise, build # lut from the full histogram and step and then index from it. result = tf.cond(tf.equal(step, 0), lambda: im, lambda: tf.gather(build_lut(histo, step), im)) return tf.cast(result, tf.uint8) # Assumes RGB for now. Scales each channel independently # and then stacks the result. s1 = scale_channel(image, 0) s2 = scale_channel(image, 1) s3 = scale_channel(image, 2) image = tf.stack([s1, s2, s3], 2) return image
Example #17
Source File: autoaugment_utils.py From tpu_models with Apache License 2.0 | 5 votes |
def contrast(image, factor): """Equivalent of PIL Contrast.""" degenerate = tf.image.rgb_to_grayscale(image) # Cast before calling tf.histogram. degenerate = tf.cast(degenerate, tf.int32) # Compute the grayscale histogram, then compute the mean pixel value, # and create a constant image size of that value. Use that as the # blending degenerate target of the original image. hist = tf.histogram_fixed_width(degenerate, [0, 255], nbins=256) mean = tf.reduce_sum(tf.cast(hist, tf.float32)) / 256.0 degenerate = tf.ones_like(degenerate, dtype=tf.float32) * mean degenerate = tf.clip_by_value(degenerate, 0.0, 255.0) degenerate = tf.image.grayscale_to_rgb(tf.cast(degenerate, tf.uint8)) return blend(degenerate, image, factor)
Example #18
Source File: color_ops.py From addons with Apache License 2.0 | 4 votes |
def equalize_image(image: TensorLike, data_format: str = "channels_last") -> tf.Tensor: """Implements Equalize function from PIL using TF ops.""" @tf.function def scale_channel(image, channel): """Scale the data in the channel to implement equalize.""" image_dtype = image.dtype if data_format == "channels_last": image = tf.cast(image[:, :, channel], tf.int32) elif data_format == "channels_first": image = tf.cast(image[channel], tf.int32) else: raise ValueError( "data_format can either be channels_last or channels_first" ) # Compute the histogram of the image channel. histo = tf.histogram_fixed_width(image, [0, 255], nbins=256) # For the purposes of computing the step, filter out the nonzeros. nonzero = tf.where(tf.not_equal(histo, 0)) nonzero_histo = tf.reshape(tf.gather(histo, nonzero), [-1]) step = (tf.reduce_sum(nonzero_histo) - nonzero_histo[-1]) // 255 def build_lut(histo, step): # Compute the cumulative sum, shifting by step // 2 # and then normalization by step. lut = (tf.cumsum(histo) + (step // 2)) // step # Shift lut, prepending with 0. lut = tf.concat([[0], lut[:-1]], 0) # Clip the counts to be in range. This is done # in the C code for image.point. return tf.clip_by_value(lut, 0, 255) # If step is zero, return the original image. Otherwise, build # lut from the full histogram and step and then index from it. if step == 0: result = image else: result = tf.gather(build_lut(histo, step), image) return tf.cast(result, image_dtype) idx = 2 if data_format == "channels_last" else 0 image = tf.stack([scale_channel(image, c) for c in range(image.shape[idx])], idx) return image