def apply_policy(policy,
                 data,
                 image_size,
                 style_augmentor=None,
                 verbose=False):
    """
    Apply the `policy` to the numpy `img`.

    Args:
        policy: A list of tuples with the form (name, probability, level) where
        `name` is the name of the augmentation operation to apply, `probability`
        is the probability of applying the operation and `level` is what strength
        the operation to apply.
        data: Numpy image list that will have `policy` applied to it.
        image_size: (height, width) of image.
        count: number of operations to apply
        style_augmentor: function that augments data with randomized style
        verbose: Whether to print applied augmentations.

    Returns:
        The result of applying `policy` to `data`.
    """
    # PBA cifar10 policy modified
    # count = np.random.choice([0, 1, 2, 3], p=[0.10, 0.20, 0.30, 0.40]) # old
    count = np.random.choice([0, 1, 2, 3], p=[0.20, 0.20, 0.50, 0.10])  # new
    #count = np.random.choice([0, 1, 2], p=[0.30, 0.50, 0.20])  # new2
    # count = np.random.choice([0, 1, 2, 3], p=[0.2, 0.3, 0.5, 0.0])  # original

    if count != 0:
        data['img_data'] = pil_wrap(data['img_data'])
        policy = copy.copy(policy)
        random.shuffle(policy)
        for xform in policy:
            assert len(xform) == 3
            name, probability, level = xform
            assert 0. <= probability <= 1.
            assert 0 <= level <= PARAMETER_MAX
            xform_fn = NAME_TO_TRANSFORM[name].pil_transformer(
                probability, level, image_size, style_augmentor)
            data, res = xform_fn(data)
            if verbose and res:
                tf.logging.info("Op: {}, Magnitude: {}, Prob: {}".format(
                    name, level, probability))
            count -= res
            assert count >= 0
            if count == 0:
                break
        data['img_data'] = pil_unwrap(
            data['img_data'], image_size)  # apply pil_unwrap on imgs only
        return data
    else:
        return data
Example #2
0
def apply_policy(policy, img, aug_policy, dset, image_size, verbose=False):
    """Apply the `policy` to the numpy `img`.

  Args:
    policy: A list of tuples with the form (name, probability, level) where
      `name` is the name of the augmentation operation to apply, `probability`
      is the probability of applying the operation and `level` is what strength
      the operation to apply.
    img: Numpy image that will have `policy` applied to it.
    aug_policy: Augmentation policy to use.
    dset: Dataset, one of the keys of MEANS or STDS.
    image_size: Width and height of image.
    verbose: Whether to print applied augmentations.

  Returns:
    The result of applying `policy` to `img`.
  """
    if aug_policy == 'cifar100':
        count = np.random.choice([0, 1, 2, 3], p=[0.2, 0.3, 0.5, 0.0])
    else:
        raise ValueError('Unknown aug policy.')
    if count != 0:
        pil_img = pil_wrap(img, dset)
        policy = copy.copy(policy)
        random.shuffle(policy)
        for xform in policy:
            assert len(xform) == 3
            name, probability, level = xform
            assert 0. <= probability <= 1.
            assert 0 <= level <= PARAMETER_MAX
            xform_fn = NAME_TO_TRANSFORM[name].pil_transformer(
                probability, level, image_size)
            pil_img, res = xform_fn(pil_img)
            if verbose and res:
                print("Op: {}, Magnitude: {}, Prob: {}".format(
                    name, level, probability))
            count -= res
            assert count >= 0
            if count == 0:
                break
        return pil_unwrap(pil_img, dset, image_size)
    else:
        return img