class_docstr: Definition of a SubPolicy.
  
  A SubPolicy consists of two augmentation operations,
  each of those parametrized as operation, probability, magnitude.
  The two operations are applied sequentially on the image upon call.
class_docstr_tok: ["Definition","of","a","SubPolicy",".","A","SubPolicy","consists","of","two","augmentation","operations",",","each","of","those","parametrized","as","operation",",","probability",",","magnitude",".","The","two","operations","are","applied","sequentially","on","the","image","upon","call","."]
docstr: Initialize a SubPolicy.
  
  Args:
      operation1 (str): Key specifying the first augmentation operation.
      There are fourteen key values altogether (see supported_ops below
      listing supported operations). probability1 (float): Probability
      within [0., 1.] of applying the first augmentation operation.
      magnitude_idx1 (int): Integer specifiying the strength of the first
      operation as an index further used to derive the magnitude from a
      range of possible values.
      operation2 (str): Key specifying the second augmentation operation.
      probability2 (float): Probability within [0., 1.] of applying the
      second augmentation operation.
      magnitude_idx2 (int): Integer specifiying the strength of the
      second operation as an index further used to derive the magnitude
      from a range of possible values.
      fillcolor (tuple): RGB color components of the color to be used for
      filling.
  Returns:
docstr_tok: ["Initialize","a","SubPolicy",".","Args",":","operation1","(","str",")",":","Key","specifying","the","first","augmentation","operation",".","There","are","fourteen","key","values","altogether","(","see","supported_ops","below","listing","supported","operations",")",".","probability1","(","float",")",":","Probability","within","[","0.",",","1",".","]","of","applying","the","first","augmentation","operation",".","magnitude_idx1","(","int",")",":","Integer","specifiying","the","strength","of","the","first","operation","as","an","index","further","used","to","derive","the","magnitude","from","a","range","of","possible","values",".","operation2","(","str",")",":","Key","specifying","the","second","augmentation","operation",".","probability2","(","float",")",":","Probability","within","[","0.",",","1",".","]","of","applying","the","second","augmentation","operation",".","magnitude_idx2","(","int",")",":","Integer","specifiying","the","strength","of","the","second","operation","as","an","index","further","used","to","derive","the","magnitude","from","a","range","of","possible","values",".","fillcolor","(","tuple",")",":","RGB","color","components","of","the","color","to","be","used","for","filling",".","Returns",":"]
code: def __init__(
      self,
      operation1,
      probability1,
      magnitude_idx1,
      operation2,
      probability2,
      magnitude_idx2,
      fillcolor,
  ):
      """Initialize a SubPolicy.
  
      Args:
          operation1 (str): Key specifying the first augmentation operation.
          There are fourteen key values altogether (see supported_ops below
          listing supported operations). probability1 (float): Probability
          within [0., 1.] of applying the first augmentation operation.
          magnitude_idx1 (int): Integer specifiying the strength of the first
          operation as an index further used to derive the magnitude from a
          range of possible values.
          operation2 (str): Key specifying the second augmentation operation.
          probability2 (float): Probability within [0., 1.] of applying the
          second augmentation operation.
          magnitude_idx2 (int): Integer specifiying the strength of the
          second operation as an index further used to derive the magnitude
          from a range of possible values.
          fillcolor (tuple): RGB color components of the color to be used for
          filling.
      Returns:
      """
      # List of supported operations for operation1 and operation2.
      supported_ops = [
          "shearX",
          "shearY",
          "translateX",
          "translateY",
          "rotate",
          "color",
          "posterize",
          "solarize",
          "contrast",
          "sharpness",
          "brightness",
          "autocontrast",
          "equalize",
          "invert",
      ]
      assert (operation1 in supported_ops) and (
          operation2 in supported_ops
      ), "SubPolicy:one of oper1 or oper2 refers to an unsupported operation."
  
      assert (
          0.0 <= probability1 <= 1.0 and 0.0 <= probability2 <= 1.0
      ), "SubPolicy: prob1 and prob2 should be within [0., 1.]."
  
      assert (
          isinstance(magnitude_idx1, int) and 0 <= magnitude_idx1 <= 10
      ), "SubPolicy: idx1 should be specified as an integer within [0, 10]."
  
      assert (
          isinstance(magnitude_idx2, int) and 0 <= magnitude_idx2 <= 10
      ), "SubPolicy: idx2 should be specified as an integer within [0, 10]."
  
      # Define a dictionary where each key refers to a specific type of
      # augmentation and the corresponding value is a range of ten possible
      # magnitude values for that augmentation.
      num_levels = _MAX_LEVEL + 1
      ranges = {
          "shearX": np.linspace(0, 0.3, num_levels),
          "shearY": np.linspace(0, 0.3, num_levels),
          "translateX": np.linspace(0, 150 / 331, num_levels),
          "translateY": np.linspace(0, 150 / 331, num_levels),
          "rotate": np.linspace(0, 30, num_levels),
          "color": np.linspace(0.0, 0.9, num_levels),
          "posterize": np.round(
              np.linspace(8, 4, num_levels), 0
          ).astype(np.int32),
          "solarize": np.linspace(256, 0, num_levels),  # range [0, 256]
          "contrast": np.linspace(0.0, 0.9, num_levels),
          "sharpness": np.linspace(0.0, 0.9, num_levels),
          "brightness": np.linspace(0.0, 0.9, num_levels),
          "autocontrast": [0]
          * num_levels,  # This augmentation doesn't use magnitude parameter.
          "equalize": [0]
          * num_levels,  # This augmentation doesn't use magnitude parameter.
          "invert": [0]
          * num_levels,  # This augmentation doesn't use magnitude parameter.
      }
  
      def rotate_with_fill(img, magnitude):
          """Define rotation transformation with fill.
  
          The input image is first rotated, then it is blended together with
          a gray mask of the same size. Note that fillcolor as defined
          elsewhere in this module doesn't apply here.
  
          Args:
              magnitude (float): rotation angle in degrees.
          Returns:
              rotated_filled (PIL Image): rotated image with gray filling for
              disoccluded areas unveiled by the rotation.
          """
          rotated = img.convert("RGBA").rotate(magnitude)
          rotated_filled = Image.composite(
              rotated,
              Image.new("RGBA", rotated.size, (128,) * 4),
              rotated,
          )
          return rotated_filled.convert(img.mode)
  
      # Define a dictionary of augmentation functions where each key refers
      # to a specific type of augmentation and the corresponding value defines
      # the augmentation itself using a lambda function.
      # pylint: disable=unnecessary-lambda
      func_dict = {
          "shearX": lambda img, magnitude: img.transform(
              img.size,
              Image.AFFINE,
              (1, magnitude * random.choice([-1, 1]), 0, 0, 1, 0),
              Image.BICUBIC,
              fillcolor=fillcolor,
          ),
          "shearY": lambda img, magnitude: img.transform(
              img.size,
              Image.AFFINE,
              (1, 0, 0, magnitude * random.choice([-1, 1]), 1, 0),
              Image.BICUBIC,
              fillcolor=fillcolor,
          ),
          "translateX": lambda img, magnitude: img.transform(
              img.size,
              Image.AFFINE,
              (
                  1,
                  0,
                  magnitude * img.size[0] * random.choice([-1, 1]),
                  0,
                  1,
                  0,
              ),
              fillcolor=fillcolor,
          ),
          "translateY": lambda img, magnitude: img.transform(
              img.size,
              Image.AFFINE,
              (
                  1,
                  0,
                  0,
                  0,
                  1,
                  magnitude * img.size[1] * random.choice([-1, 1]),
              ),
              fillcolor=fillcolor,
          ),
          "rotate": lambda img, magnitude: rotate_with_fill(
              img, magnitude
          ),
          "color": lambda img, magnitude: ImageEnhance.Color(
              img
          ).enhance(1 + magnitude * random.choice([-1, 1])),
          "posterize": lambda img, magnitude: ImageOps.posterize(
              img, magnitude
          ),
          "solarize": lambda img, magnitude: ImageOps.solarize(
              img, magnitude
          ),
          "contrast": lambda img, magnitude: ImageEnhance.Contrast(
              img
          ).enhance(1 + magnitude * random.choice([-1, 1])),
          "sharpness": lambda img, magnitude: ImageEnhance.Sharpness(
              img
          ).enhance(1 + magnitude * random.choice([-1, 1])),
          "brightness": lambda img, magnitude: ImageEnhance.Brightness(
              img
          ).enhance(1 + magnitude * random.choice([-1, 1])),
          "autocontrast": lambda img, magnitude: ImageOps.autocontrast(
              img
          ),
          "equalize": lambda img, magnitude: ImageOps.equalize(img),
          "invert": lambda img, magnitude: ImageOps.invert(img),
      }
  
      # Store probability, function and magnitude of the first augmentation
      # for the sub-policy.
      self.probability1 = probability1
      self.operation1 = func_dict[operation1]
      self.magnitude1 = ranges[operation1][magnitude_idx1]
  
      # Store probability, function and magnitude of the second augmentation
      # for the sub-policy.
      self.probability2 = probability2
      self.operation2 = func_dict[operation2]
      self.magnitude2 = ranges[operation2][magnitude_idx2]
  
file_path: megatron-lm/megatron/data/autoaugment.py
imports: import random
  import numpy
  from PIL import Image
  from PIL import ImageEnhance
  from PIL import ImageOps
label_desc: Use image node_id 1 to create a new SubPolicy object  with example: obj = SubPolicy(operation1, probability1, magnitude_idx1, operation2, probability2, magnitude_idx2, fillcolor)