diff --git a/mindspore/ccsrc/minddata/dataset/include/transforms.h b/mindspore/ccsrc/minddata/dataset/include/transforms.h index a8bade5396f2098a65fb2540c8c93bd3b00f0ee4..1788f9ce51344c51c2c92f56cf8f47a35bd6be73 100644 --- a/mindspore/ccsrc/minddata/dataset/include/transforms.h +++ b/mindspore/ccsrc/minddata/dataset/include/transforms.h @@ -46,59 +46,21 @@ class TensorOperation : public std::enable_shared_from_this { // Transform operations for performing computer vision. namespace vision { -class NormalizeOperation; +// Transform Op classes (in alphabetical order) +class CenterCropOperation; +class CropOperation; +class CutOutOperation; class DecodeOperation; -class ResizeOperation; +class NormalizeOperation; +class PadOperation; +class RandomColorAdjustOperation; class RandomCropOperation; -class CenterCropOperation; -class UniformAugOperation; class RandomHorizontalFlipOperation; -class RandomVerticalFlipOperation; class RandomRotationOperation; -class PadOperation; -class CutOutOperation; -class RandomColorAdjustOperation; -class CropOperation; +class RandomVerticalFlipOperation; +class ResizeOperation; class SwapRedBlueOperation; - -/// \brief Function to create a Normalize TensorOperation. -/// \notes Normalize the input image with respect to mean and standard deviation. -/// \param[in] mean - a vector of mean values for each channel, w.r.t channel order. -/// \param[in] std - a vector of standard deviations for each channel, w.r.t. channel order. -/// \return Shared pointer to the current TensorOperation. -std::shared_ptr Normalize(std::vector mean, std::vector std); - -/// \brief Function to create a Decode TensorOperation. -/// \notes Decode the input image in RGB mode. -/// \param[in] rgb - a boolean of whether to decode in RGB mode or not. -/// \return Shared pointer to the current TensorOperation. -std::shared_ptr Decode(bool rgb = true); - -/// \brief Function to create a Resize TensorOperation. -/// \notes Resize the input image to the given size.. -/// \param[in] size - a vector representing the output size of the resized image. -/// If size is a single value, the image will be resized to this value with -/// the same image aspect ratio. If size has 2 values, it should be (height, width). -/// \param[in] interpolation An enum for the mode of interpolation -/// \return Shared pointer to the current TensorOperation. -std::shared_ptr Resize(std::vector size, - InterpolationMode interpolation = InterpolationMode::kLinear); - -/// \brief Function to create a RandomCrop TensorOperation. -/// \notes Crop the input image at a random location. -/// \param[in] size - a vector representing the output size of the cropped image. -/// If size is a single value, a square crop of size (size, size) is returned. -/// If size has 2 values, it should be (height, width). -/// \param[in] padding - a vector with the value of pixels to pad the image. If 4 values are provided, -/// it pads the left, top, right and bottom respectively. -/// \param[in] pad_if_needed - a boolean whether to pad the image if either side is smaller than -/// the given output size. -/// \param[in] fill_value - a vector representing the pixel intensity of the borders, it is used to -/// fill R, G, B channels respectively. -/// \return Shared pointer to the current TensorOperation. -std::shared_ptr RandomCrop(std::vector size, std::vector padding = {0, 0, 0, 0}, - bool pad_if_needed = false, - std::vector fill_value = {0, 0, 0}); +class UniformAugOperation; /// \brief Function to create a CenterCrop TensorOperation. /// \notes Crops the input image at the center to the given size. @@ -108,37 +70,32 @@ std::shared_ptr RandomCrop(std::vector size, std:: /// \return Shared pointer to the current TensorOperation. std::shared_ptr CenterCrop(std::vector size); -/// \brief Function to create a UniformAugment TensorOperation. -/// \notes Tensor operation to perform randomly selected augmentation. -/// \param[in] transforms - a vector of TensorOperation transforms. -/// \param[in] num_ops - integer representing the number of OPs to be selected and applied. -/// \return Shared pointer to the current TensorOperation. -std::shared_ptr UniformAugment(std::vector> transforms, - int32_t num_ops = 2); +/// \brief Function to create a Crop TensorOp +/// \notes Crop an image based on location and crop size +/// \param[in] coordinates Starting location of crop. Must be a vector of two values, in the form of {x_coor, y_coor} +/// \param[in] size Size of the cropped area. Must be a vector of two values, in the form of {height, width} +/// \return Shared pointer to the current TensorOp +std::shared_ptr Crop(std::vector coordinates, std::vector size); -/// \brief Function to create a RandomHorizontalFlip TensorOperation. -/// \notes Tensor operation to perform random horizontal flip. -/// \param[in] prob - float representing the probability of flip. -/// \return Shared pointer to the current TensorOperation. -std::shared_ptr RandomHorizontalFlip(float prob = 0.5); +/// \brief Function to create a CutOut TensorOp +/// \notes Randomly cut (mask) out a given number of square patches from the input image +/// \param[in] length Integer representing the side length of each square patch +/// \param[in] num_patches Integer representing the number of patches to be cut out of an image +/// \return Shared pointer to the current TensorOp +std::shared_ptr CutOut(int32_t length, int32_t num_patches = 1); -/// \brief Function to create a RandomVerticalFlip TensorOperation. -/// \notes Tensor operation to perform random vertical flip. -/// \param[in] prob - float representing the probability of flip. +/// \brief Function to create a Decode TensorOperation. +/// \notes Decode the input image in RGB mode. +/// \param[in] rgb - a boolean of whether to decode in RGB mode or not. /// \return Shared pointer to the current TensorOperation. -std::shared_ptr RandomVerticalFlip(float prob = 0.5); +std::shared_ptr Decode(bool rgb = true); -/// \brief Function to create a RandomRotation TensorOp -/// \notes Rotates the image according to parameters -/// \param[in] degrees A float vector size 2, representing the starting and ending degree -/// \param[in] resample An enum for the mode of interpolation -/// \param[in] expand A boolean representing whether the image is expanded after rotation -/// \param[in] center A float vector size 2, representing the x and y center of rotation. -/// \param[in] fill_value A uint8_t vector size 3, representing the rgb value of the fill color -/// \return Shared pointer to the current TensorOp -std::shared_ptr RandomRotation( - std::vector degrees, InterpolationMode resample = InterpolationMode::kNearestNeighbour, bool expand = false, - std::vector center = {-1, -1}, std::vector fill_value = {0, 0, 0}); +/// \brief Function to create a Normalize TensorOperation. +/// \notes Normalize the input image with respect to mean and standard deviation. +/// \param[in] mean - a vector of mean values for each channel, w.r.t channel order. +/// \param[in] std - a vector of standard deviations for each channel, w.r.t. channel order. +/// \return Shared pointer to the current TensorOperation. +std::shared_ptr Normalize(std::vector mean, std::vector std); /// \brief Function to create a Pad TensorOp /// \notes Pads the image according to padding parameters @@ -162,13 +119,6 @@ std::shared_ptr RandomRotation( std::shared_ptr Pad(std::vector padding, std::vector fill_value = {0}, BorderType padding_mode = BorderType::kConstant); -/// \brief Function to create a CutOut TensorOp -/// \notes Randomly cut (mask) out a given number of square patches from the input image -/// \param[in] length Integer representing the side length of each square patch -/// \param[in] num_patches Integer representing the number of patches to be cut out of an image -/// \return Shared pointer to the current TensorOp -std::shared_ptr CutOut(int32_t length, int32_t num_patches = 1); - /// \brief Randomly adjust the brightness, contrast, saturation, and hue of the input image /// \param[in] brightness Brightness adjustment factor. Must be a vector of one or two values /// if it's a vector of two values it needs to be in the form of [min, max]. Default value is {1, 1} @@ -185,222 +135,273 @@ std::shared_ptr RandomColorAdjust(std::vector std::vector saturation = {1.0, 1.0}, std::vector hue = {0.0, 0.0}); -/// \brief Function to create a Crop TensorOp -/// \notes Crop an image based on location and crop size -/// \param[in] coordinates Starting location of crop. Must be a vector of two values, in the form of {x_coor, y_coor} -/// \param[in] size Size of the cropped area. Must be a vector of two values, in the form of {height, width} +/// \brief Function to create a RandomCrop TensorOperation. +/// \notes Crop the input image at a random location. +/// \param[in] size - a vector representing the output size of the cropped image. +/// If size is a single value, a square crop of size (size, size) is returned. +/// If size has 2 values, it should be (height, width). +/// \param[in] padding - a vector with the value of pixels to pad the image. If 4 values are provided, +/// it pads the left, top, right and bottom respectively. +/// \param[in] pad_if_needed - a boolean whether to pad the image if either side is smaller than +/// the given output size. +/// \param[in] fill_value - a vector representing the pixel intensity of the borders, it is used to +/// fill R, G, B channels respectively. +/// \return Shared pointer to the current TensorOperation. +std::shared_ptr RandomCrop(std::vector size, std::vector padding = {0, 0, 0, 0}, + bool pad_if_needed = false, + std::vector fill_value = {0, 0, 0}); + +/// \brief Function to create a RandomHorizontalFlip TensorOperation. +/// \notes Tensor operation to perform random horizontal flip. +/// \param[in] prob - float representing the probability of flip. +/// \return Shared pointer to the current TensorOperation. +std::shared_ptr RandomHorizontalFlip(float prob = 0.5); + +/// \brief Function to create a RandomRotation TensorOp +/// \notes Rotates the image according to parameters +/// \param[in] degrees A float vector size 2, representing the starting and ending degree +/// \param[in] resample An enum for the mode of interpolation +/// \param[in] expand A boolean representing whether the image is expanded after rotation +/// \param[in] center A float vector size 2, representing the x and y center of rotation. +/// \param[in] fill_value A uint8_t vector size 3, representing the rgb value of the fill color /// \return Shared pointer to the current TensorOp -std::shared_ptr Crop(std::vector coordinates, std::vector size); +std::shared_ptr RandomRotation( + std::vector degrees, InterpolationMode resample = InterpolationMode::kNearestNeighbour, bool expand = false, + std::vector center = {-1, -1}, std::vector fill_value = {0, 0, 0}); + +/// \brief Function to create a RandomVerticalFlip TensorOperation. +/// \notes Tensor operation to perform random vertical flip. +/// \param[in] prob - float representing the probability of flip. +/// \return Shared pointer to the current TensorOperation. +std::shared_ptr RandomVerticalFlip(float prob = 0.5); + +/// \brief Function to create a Resize TensorOperation. +/// \notes Resize the input image to the given size.. +/// \param[in] size - a vector representing the output size of the resized image. +/// If size is a single value, the image will be resized to this value with +/// the same image aspect ratio. If size has 2 values, it should be (height, width). +/// \param[in] interpolation An enum for the mode of interpolation +/// \return Shared pointer to the current TensorOperation. +std::shared_ptr Resize(std::vector size, + InterpolationMode interpolation = InterpolationMode::kLinear); /// \brief Function to create a SwapRedBlue TensorOp /// \notes Swaps the red and blue channels in image /// \return Shared pointer to the current TensorOp std::shared_ptr SwapRedBlue(); +/// \brief Function to create a UniformAugment TensorOperation. +/// \notes Tensor operation to perform randomly selected augmentation. +/// \param[in] transforms - a vector of TensorOperation transforms. +/// \param[in] num_ops - integer representing the number of OPs to be selected and applied. +/// \return Shared pointer to the current TensorOperation. +std::shared_ptr UniformAugment(std::vector> transforms, + int32_t num_ops = 2); + /* ####################################### Derived TensorOperation classes ################################# */ -class NormalizeOperation : public TensorOperation { +class CenterCropOperation : public TensorOperation { public: - NormalizeOperation(std::vector mean, std::vector std); + explicit CenterCropOperation(std::vector size); - ~NormalizeOperation() = default; + ~CenterCropOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - std::vector mean_; - std::vector std_; + std::vector size_; }; -class DecodeOperation : public TensorOperation { +class CropOperation : public TensorOperation { public: - explicit DecodeOperation(bool rgb = true); + CropOperation(std::vector coordinates, std::vector size); - ~DecodeOperation() = default; + ~CropOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - bool rgb_; + std::vector coordinates_; + std::vector size_; }; -class ResizeOperation : public TensorOperation { +class CutOutOperation : public TensorOperation { public: - explicit ResizeOperation(std::vector size, - InterpolationMode interpolation_mode = InterpolationMode::kLinear); + explicit CutOutOperation(int32_t length, int32_t num_patches = 1); - ~ResizeOperation() = default; + ~CutOutOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - std::vector size_; - InterpolationMode interpolation_; + int32_t length_; + int32_t num_patches_; }; -class RandomCropOperation : public TensorOperation { +class DecodeOperation : public TensorOperation { public: - RandomCropOperation(std::vector size, std::vector padding = {0, 0, 0, 0}, - bool pad_if_needed = false, std::vector fill_value = {0, 0, 0}); + explicit DecodeOperation(bool rgb = true); - ~RandomCropOperation() = default; + ~DecodeOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - std::vector size_; - std::vector padding_; - bool pad_if_needed_; - std::vector fill_value_; + bool rgb_; }; -class CenterCropOperation : public TensorOperation { +class NormalizeOperation : public TensorOperation { public: - explicit CenterCropOperation(std::vector size); + NormalizeOperation(std::vector mean, std::vector std); - ~CenterCropOperation() = default; + ~NormalizeOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - std::vector size_; + std::vector mean_; + std::vector std_; }; -class UniformAugOperation : public TensorOperation { +class PadOperation : public TensorOperation { public: - explicit UniformAugOperation(std::vector> transforms, int32_t num_ops = 2); + PadOperation(std::vector padding, std::vector fill_value = {0}, + BorderType padding_mode = BorderType::kConstant); - ~UniformAugOperation() = default; + ~PadOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - std::vector> transforms_; - int32_t num_ops_; + std::vector padding_; + std::vector fill_value_; + BorderType padding_mode_; }; -class RandomHorizontalFlipOperation : public TensorOperation { +class RandomColorAdjustOperation : public TensorOperation { public: - explicit RandomHorizontalFlipOperation(float probability = 0.5); + RandomColorAdjustOperation(std::vector brightness = {1.0, 1.0}, std::vector contrast = {1.0, 1.0}, + std::vector saturation = {1.0, 1.0}, std::vector hue = {0.0, 0.0}); - ~RandomHorizontalFlipOperation() = default; + ~RandomColorAdjustOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - float probability_; + std::vector brightness_; + std::vector contrast_; + std::vector saturation_; + std::vector hue_; }; -class RandomVerticalFlipOperation : public TensorOperation { +class RandomCropOperation : public TensorOperation { public: - explicit RandomVerticalFlipOperation(float probability = 0.5); + RandomCropOperation(std::vector size, std::vector padding = {0, 0, 0, 0}, + bool pad_if_needed = false, std::vector fill_value = {0, 0, 0}); - ~RandomVerticalFlipOperation() = default; + ~RandomCropOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - float probability_; + std::vector size_; + std::vector padding_; + bool pad_if_needed_; + std::vector fill_value_; }; -class RandomRotationOperation : public TensorOperation { +class RandomHorizontalFlipOperation : public TensorOperation { public: - RandomRotationOperation(std::vector degrees, InterpolationMode interpolation_mode, bool expand, - std::vector center, std::vector fill_value); + explicit RandomHorizontalFlipOperation(float probability = 0.5); - ~RandomRotationOperation() = default; + ~RandomHorizontalFlipOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - std::vector degrees_; - InterpolationMode interpolation_mode_; - std::vector center_; - bool expand_; - std::vector fill_value_; + float probability_; }; -class PadOperation : public TensorOperation { +class RandomRotationOperation : public TensorOperation { public: - PadOperation(std::vector padding, std::vector fill_value = {0}, - BorderType padding_mode = BorderType::kConstant); + RandomRotationOperation(std::vector degrees, InterpolationMode interpolation_mode, bool expand, + std::vector center, std::vector fill_value); - ~PadOperation() = default; + ~RandomRotationOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - std::vector padding_; + std::vector degrees_; + InterpolationMode interpolation_mode_; + std::vector center_; + bool expand_; std::vector fill_value_; - BorderType padding_mode_; }; -class CutOutOperation : public TensorOperation { +class RandomVerticalFlipOperation : public TensorOperation { public: - explicit CutOutOperation(int32_t length, int32_t num_patches = 1); + explicit RandomVerticalFlipOperation(float probability = 0.5); - ~CutOutOperation() = default; + ~RandomVerticalFlipOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - int32_t length_; - int32_t num_patches_; + float probability_; }; -class RandomColorAdjustOperation : public TensorOperation { +class ResizeOperation : public TensorOperation { public: - RandomColorAdjustOperation(std::vector brightness = {1.0, 1.0}, std::vector contrast = {1.0, 1.0}, - std::vector saturation = {1.0, 1.0}, std::vector hue = {0.0, 0.0}); + explicit ResizeOperation(std::vector size, + InterpolationMode interpolation_mode = InterpolationMode::kLinear); - ~RandomColorAdjustOperation() = default; + ~ResizeOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - std::vector brightness_; - std::vector contrast_; - std::vector saturation_; - std::vector hue_; + std::vector size_; + InterpolationMode interpolation_; }; -class CropOperation : public TensorOperation { +class UniformAugOperation : public TensorOperation { public: - CropOperation(std::vector coordinates, std::vector size); + explicit UniformAugOperation(std::vector> transforms, int32_t num_ops = 2); - ~CropOperation() = default; + ~UniformAugOperation() = default; std::shared_ptr Build() override; bool ValidateParams() override; private: - std::vector coordinates_; - std::vector size_; + std::vector> transforms_; + int32_t num_ops_; }; class SwapRedBlueOperation : public TensorOperation {