<commit_before># Copyright 2022 The Scenic Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Implementation of data preprocessing ops.

All preprocessing ops should return a data processing functors. A data
is represented as a dictionary of tensors, where field "image" is reserved
for 3D images (height x width x channels). The functors output dictionary with
field "image" being modified. Potentially, other fields can also be modified
or added.
"""
from typing import Optional, Tuple
import numpy as np

from scenic.dataset_lib.big_transfer.preprocessing import autoaugment
from scenic.dataset_lib.big_transfer.preprocessing import utils
from scenic.dataset_lib.big_transfer.registry import Registry
import tensorflow.compat.v1 as tf
import tensorflow.compat.v2 as tf2

from tensorflow_addons import image as image_utils


@Registry.register("preprocess_ops.color_distort", "function")
@utils.InKeyOutKey()
@utils.BatchedImagePreprocessing()
def get_color_distortion():
  """Applies random brigthness/saturation/hue/contrast transformations."""

  def _color_distortion(image):
    image = tf.image.random_brightness(image, max_delta=128. / 255.)
    image = tf.image.random_saturation(image, lower=0.1, upper=2.0)
    image = tf.image.random_hue(image, max_delta=0.5)
    image = tf.image.random_contrast(image, lower=0.1, upper=2.0)
    return image

  return _color_distortion


@Registry.register("preprocess_ops.random_brightness", "function")
@utils.InKeyOutKey()
@utils.BatchedImagePreprocessing()
def get_random_brightness(max_delta=0.1):
  """Applies random brigthness transformations."""

  # A random value in [-max_delta, +max_delta] is added to the image values.
  # Small max_delta <1.0 assumes that the image values are within [0, 1].
  def _random_brightness(image):
    return tf.image.random_brightness(image, max_delta)

  return _random_brightness


@Registry.register("preprocess_ops.random_saturation", "function")
@utils.InKeyOutKey()
@utils.BatchedImagePreprocessing()
def get_random_saturation(lower=0.5, upper=2.0):
  """Applies random saturation transformations."""

  # Multiplies saturation channel in HSV (with converting from/to RGB) with a
  # random float value in [lower, upper].
  def _random_saturation(image):
    return tf.image.random_saturation(image, lower=lower, upper=upper)

  return _random_saturation


@Registry.register("preprocess_ops.random_hue", "function")
@utils.InKeyOutKey()
@utils.BatchedImagePreprocessing()
def get_random_hue(max_delta=0.1):
  """Applies random hue transformations."""

  # Adds to hue channel in HSV (with converting from/to RGB) a random offset
  # in [-max_delta, +max_delta].
  def _random_hue(image):
    return tf.image.random_hue(image, max_delta=max_delta)

  return _random_hue


@Registry.register("preprocess_ops.random_contrast", "function")
@utils.InKeyOutKey()
@utils.BatchedImagePreprocessing()
def get_random_contrast(lower=0.5, upper=2.0):
  """Applies random contrast transformations."""

  # Stretches/shrinks value stddev (per channel) by multiplying with a random
  # value in [lower, upper].
  def _random_contrast(image):
    return tf.image.random_contrast(image, lower=lower, upper=upper)

  return _random_contrast


@Registry.register("preprocess_ops.decode", "function")
@utils.InKeyOutKey()
def get_decode(channels=3):
  """Decode an encoded image string, see tf.io.decode_image."""

  def _decode(image):  # pylint: disable=missing-docstring
    # tf.io.decode_image does not set the shape correctly, so we use
    # tf.io.deocde_jpeg, which also works for png, see
    # https://github.com/tensorflow/tensorflow/issues/8551
    return tf.io.decode_jpeg(image, channels=channels)

  return _decode


@Registry.register("preprocess_ops.pad", "function")
@utils.InKeyOutKey()
@utils.BatchedImagePreprocessing()
def get_pad(pad_size):
  """Pads an image.

  Args:
    pad_size: either an integer u giving verticle and horizontal pad sizes u, or
      a list or tuple [u, v] of integers where u and v are vertical and
      horizontal pad sizes.

  Returns:
    A function for padding an image.

  """
  pad_size = utils.maybe_repeat(pad_size, 2)

  def _pad(image):
    return tf.pad(
        image, [[pad_size[0], pad_size[0]], [pad_size[1], pad_size[1]], [0, 0]])<commit_msg>Train Pixel LM with wikipedia-pixel dataset.<commit_after># Copyright 2022 The Scenic Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Implementation of data preprocessing ops.

All preprocessing ops should return a data processing functors. A data
is represented as a dictionary of tensors, where field "image" is reserved
for 3D images (height x width x channels). The functors output dictionary with
field "image" being modified. Potentially, other fields can also be modified
or added.
"""
from typing import Optional, Tuple
import numpy as np

from scenic.dataset_lib.big_transfer.preprocessing import autoaugment
from scenic.dataset_lib.big_transfer.preprocessing import utils
from scenic.dataset_lib.big_transfer.registry import Registry
import tensorflow.compat.v1 as tf
import tensorflow.compat.v2 as tf2

from tensorflow_addons import image as image_utils


@Registry.register("preprocess_ops.color_distort", "function")
@utils.InKeyOutKey()
@utils.BatchedImagePreprocessing()
def get_color_distortion():
  """Applies random brigthness/saturation/hue/contrast transformations."""

  def _color_distortion(image):
    image = tf.image.random_brightness(image, max_delta=128. / 255.)
    image = tf.image.random_saturation(image, lower=0.1, upper=2.0)
    image = tf.image.random_hue(image, max_delta=0.5)
    image = tf.image.random_contrast(image, lower=0.1, upper=2.0)
    return image

  return _color_distortion


@Registry.register("preprocess_ops.random_brightness", "function")
@utils.InKeyOutKey()
@utils.BatchedImagePreprocessing()
def get_random_brightness(max_delta=0.1):
  """Applies random brigthness transformations."""

  # A random value in [-max_delta, +max_delta] is added to the image values.
  # Small max_delta <1.0 assumes that the image values are within [0, 1].
  def _random_brightness(image):
    return tf.image.random_brightness(image, max_delta)

  return _random_brightness


@Registry.register("preprocess_ops.random_saturation", "function")
@utils.InKeyOutKey()
@utils.BatchedImagePreprocessing()
def get_random_saturation(lower=0.5, upper=2.0):
  """Applies random saturation transformations."""

  # Multiplies saturation channel in HSV (with converting from/to RGB) with a
  # random float value in [lower, upper].
  def _random_saturation(image):
    return tf.image.random_saturation(image, lower=lower, upper=upper)

  return _random_saturation


@Registry.register("preprocess_ops.random_hue", "function")
@utils.InKeyOutKey()
@utils.BatchedImagePreprocessing()
def get_random_hue(max_delta=0.1):
  """Applies random hue transformations."""

  # Adds to hue channel in HSV (with converting from/to RGB) a random offset
  # in [-max_delta, +max_delta].
  def _random_hue(image):
    return tf.image.random_hue(image, max_delta=max_delta)

  return _random_hue


@Registry.register("preprocess_ops.random_contrast", "function")
@utils.InKeyOutKey()
@utils.BatchedImagePreprocessing()
def get_random_contrast(lower=0.5, upper=2.0):
  """Applies random contrast transformations."""

  # Stretches/shrinks value stddev (per channel) by multiplying with a random
  # value in [lower, upper].
  def _random_contrast(image):
    return tf.image.random_contrast(image, lower=lower, upper=upper)

  return _random_contrast


@Registry.register("preprocess_ops.decode", "function")
@utils.InKeyOutKey()
def get_decode(channels=3):
  """Decode an encoded image string, see tf.io.decode_image."""

  def _decode(image):  # pylint: disable=missing-docstring
    # tf.io.decode_image does not set the shape correctly, so we use
    # tf.io.deocde_jpeg, which also works for png, see
    # https://github.com/tensorflow/tensorflow/issues/8551
    return tf.io.decode_jpeg(image, channels=channels)

  return _decode


@Registry.register("preprocess_ops.decode_grayscale", "function")
@utils.InKeyOutKey()
def get_decode_grayscale(channels=1):
  """Decode an encoded image string, see tf.io.decode_image."""

  def _decode_gray(image):  # pylint: disable=missing-docstring
    # tf.io.decode_image does not set the shape correctly, so we use
    # tf.io.deocde_jpeg, which also works for png, see
    # https://github.com/tensorflow/tensorflow/issues/8551
    return tf.io.decode_jpeg(image, channels=channels)

  return _decode_gray


@Registry.register("preprocess_ops.pad", "function")
@utils.InKeyOutKey()
@utils.BatchedImagePreprocessing()
def get_pad(pad_size):
  """Pads an image.

  Args:
    pad_size: either an integer u giving verticle and horizontal pad sizes u, or
      a list or tuple [u, v] of integers where u and v are vertical and
      horizontal pad sizes.

  Returns:
    A function for padding an image.

  """
  pad_size = utils.maybe_repeat(pad_size, 2)

  def _pad(image):
    return tf.pad(
        image, [[pad_size[0], pad_size[0]], [pad_size[1], pad_size[1]], [0, 0]])
{'fraction_non_alphanumeric': 0.10177690029615005, 'fraction_numerical': 0.01154985192497532, 'mean_word_length': 5.001777251184834, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1558069', 'n_tokens_mistral': 3214, 'n_tokens_neox': 3048, 'n_words': 939}