<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

/*!
 *  Copyright (c) 2015 by Contributors
 * \file image_aug_default.cc
 * \brief Default augmenter.
 */
#include <mxnet/base.h>
#include <dmlc/optional.h>
#include <utility>
#include <string>
#include <algorithm>
#include <vector>
#include "./image_augmenter.h"
#include "../common/utils.h"

#if MXNET_USE_OPENCV
#include "./opencv_compatibility.h"
// Registers
namespace dmlc {
DMLC_REGISTRY_ENABLE(::mxnet::io::ImageAugmenterReg);
}  // namespace dmlc
#endif

namespace mxnet {
namespace io {

/*! \brief image augmentation parameters*/
struct DefaultImageAugmentParam : public dmlc::Parameter<DefaultImageAugmentParam> {
  /*! \brief resize shorter edge to size before applying other augmentations */
  int resize;
  /*! \brief whether we do random cropping */
  bool rand_crop;
  /*! \brief whether we do random resized cropping */
  bool random_resized_crop;
  /*! \brief [-max_rotate_angle, max_rotate_angle] */
  int max_rotate_angle;
  /*! \brief max aspect ratio */
  float max_aspect_ratio;
  /*! \brief min aspect ratio */
  dmlc::optional<float> min_aspect_ratio;
  /*! \brief random shear the image [-max_shear_ratio, max_shear_ratio] */
  float max_shear_ratio;
  /*! \brief max crop size */
  int max_crop_size;
  /*! \brief min crop size */
  int min_crop_size;
  /*! \brief max scale ratio */
  float max_random_scale;
  /*! \brief min scale ratio */
  float min_random_scale;
  /*! \brief max area */
  float max_random_area;
  /*! \brief min area */
  float min_random_area;
  /*! \brief min image size */
  float min_img_size;
  /*! \brief max image size */
  float max_img_size;
  /*! \brief max random brightness */
  float brightness;
  /*! \brief max random contrast */
  float contrast;
  /*! \brief max random saturation */
  float saturation;
  /*! \brief pca noise level */
  float pca_noise;
  /*! \brief max random in H channel */
  int random_h;
  /*! \brief max random in S channel */
  int random_s;
  /*! \brief max random in L channel */
  int random_l;
  /*! \brief rotate angle */
  int rotate;
  /*! \brief filled color while padding */
  int fill_value;
  /*! \brief interpolation method 0-NN 1-bilinear 2-cubic 3-area 4-lanczos4 9-auto 10-rand  */
  int inter_method;
  /*! \brief padding size */
  int pad;
  /*! \brief shape of the image data*/
  mxnet::TShape data_shape;

  // declare parameters
  DMLC_DECLARE_PARAMETER(DefaultImageAugmentParam) {
    DMLC_DECLARE_FIELD(resize).set_default(-1)
        .describe("Down scale the shorter edge to a new size  "
                  "before applying other augmentations.");
    DMLC_DECLARE_FIELD(rand_crop).set_default(false)
        .describe("If or not randomly crop the image");
    DMLC_DECLARE_FIELD(random_resized_crop).set_default(false)
        .describe("If or not perform random resized cropping "
                  "on the image, as a standard preprocessing "
                  "for resnet training on ImageNet data.");
    DMLC_DECLARE_FIELD(max_rotate_angle).set_default(0.0f)
        .describe("Rotate by a random degree in ``[-v, v]``");
    DMLC_DECLARE_FIELD(max_aspect_ratio).set_default(0.0f)
        .describe("Change the aspect (namely width/height) to a random value. "
                  "If min_aspect_ratio is None then the aspect ratio ins sampled from "
                  "[1 - max_aspect_ratio, 1 + max_aspect_ratio], "
                  "else it is in ``[min_aspect_ratio, max_aspect_ratio]``");
    DMLC_DECLARE_FIELD(min_aspect_ratio).set_default(dmlc::optional<float>())
        .describe("Change the aspect (namely width/height) to a random value "
                  "in ``[min_aspect_ratio, max_aspect_ratio]``");
    DMLC_DECLARE_FIELD(max_shear_ratio).set_default(0.0f)
        .describe("Apply a shear transformation (namely ``(x,y)->(x+my,y)``) "
                  "with ``m`` randomly chose from "
                  "``[-max_shear_ratio, max_shear_ratio]``");
    DMLC_DECLARE_FIELD(max_crop_size).set_default(-1)
        .describe("Crop both width and height into a random size in "
                  "``[min_crop_size, max_crop_size].``"
                  "Ignored if ``random_resized_crop`` is True.");
    DMLC_DECLARE_FIELD(min_crop_size).set_default(-1)
        .describe("Crop both width and height into a random size in "
                  "``[min_crop_size, max_crop_size].``"
                  "Ignored if ``random_resized_crop`` is True.");
    DMLC_DECLARE_FIELD(max_random_scale).set_default(1.0f)
        .describe("Resize into ``[width*s, height*s]`` with ``s`` randomly"
                  " chosen from ``[min_random_scale, max_random_scale]``. "
                  "Ignored if ``random_resized_crop`` is True.");
    DMLC_DECLARE_FIELD(min_random_scale).set_default(1.0f)
        .describe("Resize into ``[width*s, height*s]`` with ``s`` randomly"
                  " chosen from ``[min_random_scale, max_random_scale]``"
                  "Ignored if ``random_resized_crop`` is True.");
    DMLC_DECLARE_FIELD(max_random_area).set_default(1.0f)
        .describe("Change the area (namely width * height) to a random value "
                  "in ``[min_random_area, max_random_area]``. "
                  "Ignored if ``random_resized_crop`` is False.");
    DMLC_DECLARE_FIELD(min_random_area).set_default(1.0f)
        .describe("Change the area (namely width * height) to a random value "
                  "in ``[min_random_area, max_random_area]``. "
                  "Ignored if ``random_resized_crop`` is False.");
    DMLC_DECLARE_FIELD(max_img_size).set_default(1e10f)
        .describe("Set the maximal width and height after all resize and"
                  " rotate argumentation  are applied");
    DMLC_DECLARE_FIELD(min_img_size).set_default(0.0f)
        .describe("Set the minimal width and height after all resize and"
                  " rotate argumentation  are applied");
    DMLC_DECLARE_FIELD(brightness).set_default(0.0f)
        .describe("Add a random value in ``[-brightness, brightness]`` to "
                  "the brightness of image.");
    DMLC_DECLARE_FIELD(contrast).set_default(0.0f)
        .describe("Add a random value in ``[-contrast, contrast]`` to "
                  "the contrast of image.");
    DMLC_DECLARE_FIELD(saturation).set_default(0.0f)
        .describe("Add a random value in ``[-saturation, saturation]`` to "
                  "the saturation of image.");
        DMLC_DECLARE_FIELD(pca_noise).set_default(0.0f)
                .describe("Add PCA based noise to the image.");
    DMLC_DECLARE_FIELD(random_h).set_default(0)
        .describe("Add a random value in ``[-random_h, random_h]`` to "
                  "the H channel in HSL color space.");
    DMLC_DECLARE_FIELD(random_s).set_default(0)
        .describe("Add a random value in ``[-random_s, random_s]`` to "
                  "the S channel in HSL color space.");
    DMLC_DECLARE_FIELD(random_l).set_default(0)
        .describe("Add a random value in ``[-random_l, random_l]`` to "
                  "the L channel in HSL color space.");
    DMLC_DECLARE_FIELD(rotate).set_default(-1.0f)
        .describe("Rotate by an angle. If set, it overwrites the ``max_rotate_angle`` option.");
    DMLC_DECLARE_FIELD(fill_value).set_default(255)
        .describe("Set the padding pixels value to ``fill_value``.");
    DMLC_DECLARE_FIELD(data_shape)
        .set_expect_ndim(3).enforce_nonzero()
        .describe("The shape of a output image.");
    DMLC_DECLARE_FIELD(inter_method).set_default(1)
        .describe("The interpolation method: 0-NN 1-bilinear 2-cubic 3-area "
                  "4-lanczos4 9-auto 10-rand.");
    DMLC_DECLARE_FIELD(pad).set_default(0)
        .describe("Change size from ``[width, height]`` into "
                  "``[pad + width + pad, pad + height + pad]`` by padding pixes");
  }
};

DMLC_REGISTER_PARAMETER(DefaultImageAugmentParam);<commit_msg>indent changes (#15321)<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

/*!
 *  Copyright (c) 2015 by Contributors
 * \file image_aug_default.cc
 * \brief Default augmenter.
 */
#include <mxnet/base.h>
#include <dmlc/optional.h>
#include <utility>
#include <string>
#include <algorithm>
#include <vector>
#include "./image_augmenter.h"
#include "../common/utils.h"

#if MXNET_USE_OPENCV
#include "./opencv_compatibility.h"
// Registers
namespace dmlc {
DMLC_REGISTRY_ENABLE(::mxnet::io::ImageAugmenterReg);
}  // namespace dmlc
#endif

namespace mxnet {
namespace io {

/*! \brief image augmentation parameters*/
struct DefaultImageAugmentParam : public dmlc::Parameter<DefaultImageAugmentParam> {
  /*! \brief resize shorter edge to size before applying other augmentations */
  int resize;
  /*! \brief whether we do random cropping */
  bool rand_crop;
  /*! \brief whether we do random resized cropping */
  bool random_resized_crop;
  /*! \brief [-max_rotate_angle, max_rotate_angle] */
  int max_rotate_angle;
  /*! \brief max aspect ratio */
  float max_aspect_ratio;
  /*! \brief min aspect ratio */
  dmlc::optional<float> min_aspect_ratio;
  /*! \brief random shear the image [-max_shear_ratio, max_shear_ratio] */
  float max_shear_ratio;
  /*! \brief max crop size */
  int max_crop_size;
  /*! \brief min crop size */
  int min_crop_size;
  /*! \brief max scale ratio */
  float max_random_scale;
  /*! \brief min scale ratio */
  float min_random_scale;
  /*! \brief max area */
  float max_random_area;
  /*! \brief min area */
  float min_random_area;
  /*! \brief min image size */
  float min_img_size;
  /*! \brief max image size */
  float max_img_size;
  /*! \brief max random brightness */
  float brightness;
  /*! \brief max random contrast */
  float contrast;
  /*! \brief max random saturation */
  float saturation;
  /*! \brief pca noise level */
  float pca_noise;
  /*! \brief max random in H channel */
  int random_h;
  /*! \brief max random in S channel */
  int random_s;
  /*! \brief max random in L channel */
  int random_l;
  /*! \brief rotate angle */
  int rotate;
  /*! \brief filled color while padding */
  int fill_value;
  /*! \brief interpolation method 0-NN 1-bilinear 2-cubic 3-area 4-lanczos4 9-auto 10-rand  */
  int inter_method;
  /*! \brief padding size */
  int pad;
  /*! \brief shape of the image data*/
  mxnet::TShape data_shape;

  // declare parameters
  DMLC_DECLARE_PARAMETER(DefaultImageAugmentParam) {
    DMLC_DECLARE_FIELD(resize).set_default(-1)
        .describe("Down scale the shorter edge to a new size  "
                  "before applying other augmentations.");
    DMLC_DECLARE_FIELD(rand_crop).set_default(false)
        .describe("If or not randomly crop the image");
    DMLC_DECLARE_FIELD(random_resized_crop).set_default(false)
        .describe("If or not perform random resized cropping "
                  "on the image, as a standard preprocessing "
                  "for resnet training on ImageNet data.");
    DMLC_DECLARE_FIELD(max_rotate_angle).set_default(0.0f)
        .describe("Rotate by a random degree in ``[-v, v]``");
    DMLC_DECLARE_FIELD(max_aspect_ratio).set_default(0.0f)
        .describe("Change the aspect (namely width/height) to a random value. "
                  "If min_aspect_ratio is None then the aspect ratio ins sampled from "
                  "[1 - max_aspect_ratio, 1 + max_aspect_ratio], "
                  "else it is in ``[min_aspect_ratio, max_aspect_ratio]``");
    DMLC_DECLARE_FIELD(min_aspect_ratio).set_default(dmlc::optional<float>())
        .describe("Change the aspect (namely width/height) to a random value "
                  "in ``[min_aspect_ratio, max_aspect_ratio]``");
    DMLC_DECLARE_FIELD(max_shear_ratio).set_default(0.0f)
        .describe("Apply a shear transformation (namely ``(x,y)->(x+my,y)``) "
                  "with ``m`` randomly chose from "
                  "``[-max_shear_ratio, max_shear_ratio]``");
    DMLC_DECLARE_FIELD(max_crop_size).set_default(-1)
        .describe("Crop both width and height into a random size in "
                  "``[min_crop_size, max_crop_size].``"
                  "Ignored if ``random_resized_crop`` is True.");
    DMLC_DECLARE_FIELD(min_crop_size).set_default(-1)
        .describe("Crop both width and height into a random size in "
                  "``[min_crop_size, max_crop_size].``"
                  "Ignored if ``random_resized_crop`` is True.");
    DMLC_DECLARE_FIELD(max_random_scale).set_default(1.0f)
        .describe("Resize into ``[width*s, height*s]`` with ``s`` randomly"
                  " chosen from ``[min_random_scale, max_random_scale]``. "
                  "Ignored if ``random_resized_crop`` is True.");
    DMLC_DECLARE_FIELD(min_random_scale).set_default(1.0f)
        .describe("Resize into ``[width*s, height*s]`` with ``s`` randomly"
                  " chosen from ``[min_random_scale, max_random_scale]``"
                  "Ignored if ``random_resized_crop`` is True.");
    DMLC_DECLARE_FIELD(max_random_area).set_default(1.0f)
        .describe("Change the area (namely width * height) to a random value "
                  "in ``[min_random_area, max_random_area]``. "
                  "Ignored if ``random_resized_crop`` is False.");
    DMLC_DECLARE_FIELD(min_random_area).set_default(1.0f)
        .describe("Change the area (namely width * height) to a random value "
                  "in ``[min_random_area, max_random_area]``. "
                  "Ignored if ``random_resized_crop`` is False.");
    DMLC_DECLARE_FIELD(max_img_size).set_default(1e10f)
        .describe("Set the maximal width and height after all resize and"
                  " rotate argumentation  are applied");
    DMLC_DECLARE_FIELD(min_img_size).set_default(0.0f)
        .describe("Set the minimal width and height after all resize and"
                  " rotate argumentation  are applied");
    DMLC_DECLARE_FIELD(brightness).set_default(0.0f)
        .describe("Add a random value in ``[-brightness, brightness]`` to "
                  "the brightness of image.");
    DMLC_DECLARE_FIELD(contrast).set_default(0.0f)
        .describe("Add a random value in ``[-contrast, contrast]`` to "
                  "the contrast of image.");
    DMLC_DECLARE_FIELD(saturation).set_default(0.0f)
        .describe("Add a random value in ``[-saturation, saturation]`` to "
                  "the saturation of image.");
    DMLC_DECLARE_FIELD(pca_noise).set_default(0.0f)
        .describe("Add PCA based noise to the image.");
    DMLC_DECLARE_FIELD(random_h).set_default(0)
        .describe("Add a random value in ``[-random_h, random_h]`` to "
                  "the H channel in HSL color space.");
    DMLC_DECLARE_FIELD(random_s).set_default(0)
        .describe("Add a random value in ``[-random_s, random_s]`` to "
                  "the S channel in HSL color space.");
    DMLC_DECLARE_FIELD(random_l).set_default(0)
        .describe("Add a random value in ``[-random_l, random_l]`` to "
                  "the L channel in HSL color space.");
    DMLC_DECLARE_FIELD(rotate).set_default(-1.0f)
        .describe("Rotate by an angle. If set, it overwrites the ``max_rotate_angle`` option.");
    DMLC_DECLARE_FIELD(fill_value).set_default(255)
        .describe("Set the padding pixels value to ``fill_value``.");
    DMLC_DECLARE_FIELD(data_shape)
        .set_expect_ndim(3).enforce_nonzero()
        .describe("The shape of a output image.");
    DMLC_DECLARE_FIELD(inter_method).set_default(1)
        .describe("The interpolation method: 0-NN 1-bilinear 2-cubic 3-area "
                  "4-lanczos4 9-auto 10-rand.");
    DMLC_DECLARE_FIELD(pad).set_default(0)
        .describe("Change size from ``[width, height]`` into "
                  "``[pad + width + pad, pad + height + pad]`` by padding pixes");
  }
};

DMLC_REGISTER_PARAMETER(DefaultImageAugmentParam);
{'fraction_non_alphanumeric': 0.11697221261385911, 'fraction_numerical': 0.008243975556324225, 'mean_word_length': 2.985067769354468, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 23, 'https://': 0, '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': '4303998', 'n_tokens_mistral': 5613, 'n_tokens_neox': 5316, 'n_words': 1501}