#pragma once /* SuaKIT runtime API 2.2.6.3 Copyright (c) 2018 SuaLab, All right reserved. */ /** * @file SuaKITTrainer.h */ #include "SuaKIT.h" namespace SuaKIT { namespace API { /** * @brief This is a configuration of the ClassificationTrainer. * @details It is used as classification train setting parameter for ClassificationTrainer. * @author ⓒSualab SuaKIT Team */ class SUAKIT_API ClassificationTrainConfig { public: /** * @brief This is a constructor of ClassificationTrainConfig. * @param numberOfClasses The number of classes to train. * @param validationRatio Ratio of validation set. If user does not provide validation set, validation set is split from train set by this ratio. This ratio is calculated as ((validationRatio/100)*100)% of train set. This argument is meaningless when the user selects and uses a validation set as a separate ClassificationTrainData object. * @param epochs In training neural network, one epoch means one sweep of the full training set. This is converted to an iteration by the number of train sets. * @param modelCapacity The base (pre-defined) model capacity for executing ClassificationTrainer. {0, 1, 2, 3}. - 0 means small model capacity. - 1 means normal model capacity. - 2 means large model capacity. - 3 means extra large model capacity. * @param dataRatio This is a weight parameter for each class. If you want to give a larger weight to a specific class, you can increase this array's value corresponding index to the location of the specific class. This value will be applied at batch-sampling-time. This value affects the ratio between each class sample in training batch. * @param imageHeight Height of image for train. If the learning image size is variable, set it to the largest height value in the training image. * @param imageWidth Width of images for train. If the learning image size is variable, set it to the largest width value in the training image. * @param imageChannel The number of channels of images for train. * @param augmentationConfig Augmentation parameters for train. * @param inputDataType This parameter is used to configure input type of neural network. Set it to one of {InputDataType::SINGLE, InputDataType::COMPARISON, InputDataType::MULTIMG}. In case of multi-image classification, we provide only 1 modelCapacity: {NORMAL}. User needs to select modelCapacity parameter NORMAL capacity. * @param multiImgCount The number of images for classification trainer, depends on inputDataType parameter. - In case of InputDataType::SINGLE, multiImgCount parameter needs to be initialized with 1. - In case of InputDataType::COMPARISON, multiImgCount parameter needs to be initialized with 2. - In case of InputDataType::MULTIMG, multiImgCount parameter needs to be initialized with multiImgCount >= 2. * @param imageDepth Depth of images for train. * @param numModelSelection Save the top N (numModelSelection) models based on the best loss. If the number of generated models is less than this value, only the currently created models are saved. * @param minimumEpoch The minimum number of epochs to save the model. * @param patience The number of epochs to wait before early stop if no progress on the validation set. The patience is often set somewhere between 10 and 100 (10 or 20 is more common), but it really depends on your dataset. - In case of ClassificationTrainer, the learning rate decay is performed once. */ __stdcall ClassificationTrainConfig( SuaKIT_Int32 numberOfClasses, SuaKIT_Int32 validationRatio, SuaKIT_Int32 epochs, SuaKIT_Int32 modelCapacity, const Int32Array& dataRatio, SuaKIT_Int32 imageHeight, SuaKIT_Int32 imageWidth, SuaKIT_Int32 imageChannel, const AugmentationConfig& augmentationConfig = AugmentationConfig(), InputDataType inputDataType = InputDataType::SINGLE, SuaKIT_Int32 multiImgCount = 1, DepthType imageDepth = DepthType::_8U, SuaKIT_Int32 numModelSelection = 1, float minimumEpoch = 0.0f, SuaKIT_Int64 patience = 9223372036854775807ll ); __stdcall ~ClassificationTrainConfig(); void* _GetInternal() const { return internal; } /** * @brief Methods to set the class name * @param classIdx Class Index * @param className String to set as the name of class corresponding to Class Index. On Windows, it should be mbcs encoding, and On Linux, this should be utf8 encoding. * @return Returns Status. */ Status __stdcall SetClassName(SuaKIT_Int32 classIdx, const char * className); /** * @return Returns Status. */ Status __stdcall GetStatus() const; private: ClassificationTrainConfig(const ClassificationTrainConfig& src); //=delete; ClassificationTrainConfig& operator=(const ClassificationTrainConfig& rhs); //=delete; void *internal; Status m_status; }; /** * @brief This is a configuration of the PostTrainer. * @details It is used as post train setting parameter for PostTrainer. * @author ⓒSualab SuaKIT Team */ class SUAKIT_API PostTrainConfig { public: /** * @brief This is a constructor of PostTrainConfig. * @param numberOfClasses The number of classes to train. You should include only one new class. This value should be the same with (number of old class (trained classification model) + number of new class) * @param validationRatio Ratio of validation set. If user does not provide validation set, validation set is split from train set by this ratio. This ratio is calculated as ((validationRatio/100)*100)% of train set. This argument is meaningless when the user selects and uses a validation set as a separate PostTrainData object. * @param epochs In training neural network, one epoch means one sweep of the full training set. This is converted to an iteration by the number of train sets. Internally, several model of Post trainer would be trained referencing this value. * @param dataRatio This is a weight parameter for each class. If you want to give a larger weight to a specific class, you can increase this array's value corresponding index to the location of the specific class. This value will be applied at batch-sampling-time. This value affects the ratio between each class sample in training batch. * @param imageHeight Height of image for train. If the learning image size is variable, set it to the largest height value in the training image. * @param imageWidth Width of images for train. If the learning image size is variable, set it to the largest width value in the training image. * @param imageChannel The number of channels of images for train. * @param augmentationConfig Augmentation parameters for train. * @param imageDepth Depth of images for train. * @param minimumEpoch The minimum number of epochs to save the model. * @param patience The number of epochs to wait before early stop if no progress on the validation set. The patience is often set somewhere between 10 and 100 (10 or 20 is more common), but it really depends on your dataset. - In case of PostTrainer, the learning rate decay is performed once. */ __stdcall PostTrainConfig( SuaKIT_Int32 numberOfClasses, SuaKIT_Int32 validationRatio, SuaKIT_Int32 epochs, const Int32Array& dataRatio, SuaKIT_Int32 imageHeight, SuaKIT_Int32 imageWidth, SuaKIT_Int32 imageChannel, const AugmentationConfig& augmentationConfig = AugmentationConfig(), DepthType imageDepth = DepthType::_8U, float minimumEpoch = 0.0f, SuaKIT_Int64 patience = 9223372036854775807ll ); __stdcall ~PostTrainConfig(); void* _GetInternal() const { return internal; } /** * @brief Methods to set the class name * @param classIdx Class Index * @param className String to set as the name of class corresponding to Class Index. On Windows, it should be mbcs encoding, and On Linux, this should be utf8 encoding. * @return Returns Status. */ Status __stdcall SetClassName(SuaKIT_Int32 classIdx, const char * className); /** * @return Returns Status. */ Status __stdcall GetStatus() const; private: PostTrainConfig(const PostTrainConfig& src); //=delete; PostTrainConfig& operator=(const PostTrainConfig& rhs); //=delete; void *internal; Status m_status; }; /** * @brief This is a configuration of the SegmentationTrainer. * @details It is used as segmentation train setting parameter for SegmentationTrainer. * @author ⓒSualab SuaKIT Team */ class SUAKIT_API SegmentationTrainConfig { public: /** * @brief This is a constructor of the SegmentationTrainConfig. * @param numberOfClasses The number of classes to train. In segmentation training case, this parameter should be calculated except unlabeled class. * @param validationRatio Ratio of validation set. If user does not provide validation set, validation set is split from train set by this ratio. This ratio is calculated as ((validationRatio/100)*100)% of train set. * @param epochs In training neural network, one epoch means one sweep of the full training set. This is converted to an iteration by the number of train sets. * @param modelCapacity The base (pre-defined) model capacity for executing SegmentationTrainer. {0, 1, 2, 3}. - 0 means small model capacity. - 1 means normal model capacity. - 2 means large model capacity. - 3 means extra large model capacity. * @param dataRatio This is a weight parameter for each class. If you want to give a larger weight to specific class, you can increase this array's value corresponding index to the location of the specific class. This value will be applied at batch-sampling-time. This value affects the ratio between each class sample in training batch. * @param unlabeledRatio If you also want to adjust ratio of unlabeled patch, you can append unlabeledRatio to original dataRatio by this parameter. * @param strideRatio Training patch data is sampled from original image by sliding windows manner. strideRatio value means stride of shifting. If you assign 50 into this parameter, then 50% size corresponding to patch size (patchSize * 0.5) would be shifting scale. Internally, this value must be in the range [10, 100]. * @param patchSize The size of training patch data. This value should be multiple of 4, and must be in the range [128, max]. You can set arbitrary value satisfying those condition, but WE HIGHLY RECOMMEND 128 or 256 for this parameter. * @param imageChannel The number of channels of images for train. * @param augmentationConfig Augmentation parameter for train. * @param inputDataType This parameter is used to configure input type of neural network. Set it to one of {InputDataType::SINGLE, InputDataType::COMPARISON, InputDataType::MULTIMG}. In case of multi-image segmentation, we provide only 2 modelCapacity: {NORMAL, LARGE}. User needs to select modelCapacity parameter NORMAL or LARGE capacity. * @param multiImgCount The number of images for segmentation trainer, depends on inputDataType parameter. - In case of InputDataType::SINGLE, multiImgCount parameter needs to be initialized with 1. - In case of InputDataType::COMPARISON, multiImgCount parameter needs to be initialized with 2. - In case of InputDataType::MULTIMG, multiImgCount parameter needs to be initialized with multiImgCount >= 2. * @param imageDepth Depth of images for train. * @param numModelSelection Save the top N (numModelSelection) models based on the best loss. If the number of generated models is less than this value, only the currently created models are saved. * @param minimumEpoch The minimum number of epochs to save the model. * @param patience The number of epochs to wait before early stop if no progress on the validation set. The patience is often set somewhere between 10 and 100 (10 or 20 is more common), but it really depends on your dataset. - In case of SegmentationTrainer, the learning rate decay is performed twice. * @param segmentationModelType This parameter is used to configure model type of segmentation. Set it to one of {ModelType::SEGMENTATION, ModelType::SEGMENTATION_SENSITIVE, ModelType::SEGMENTATION_CONTEXTUAL}. */ __stdcall SegmentationTrainConfig( SuaKIT_Int32 numberOfClasses, SuaKIT_Int32 validationRatio, SuaKIT_Int32 epochs, SuaKIT_Int32 modelCapacity, const Int32Array& dataRatio, SuaKIT_Int32 unlabeledRatio, SuaKIT_Int32 strideRatio, SuaKIT_Int32 patchSize, SuaKIT_Int32 imageChannel, const AugmentationConfig& augmentationConfig = AugmentationConfig(), InputDataType inputDataType = InputDataType::SINGLE, SuaKIT_Int32 multiImgCount = 1, DepthType imageDepth = DepthType::_8U, SuaKIT_Int32 numModelSelection = 1, float minimumEpoch = 0.0f, SuaKIT_Int64 patience = 9223372036854775807ll, ModelType segmentationModelType = ModelType::SEGMENTATION ); __stdcall ~SegmentationTrainConfig(); void* _GetInternal() const { return internal; } /** * @brief Methods to set the class name * @param classIdx Class Index * @param className String to set as the name of class corresponding to Class Index. On Windows, it should be mbcs encoding, and On Linux, this should be utf8 encoding. * @return Returns Status. */ Status __stdcall SetClassName(SuaKIT_Int32 classIdx, const char * className); /** * @return Returns Status. */ Status __stdcall GetStatus() const; private: SegmentationTrainConfig(const SegmentationTrainConfig& src); //=delete; SegmentationTrainConfig& operator=(const SegmentationTrainConfig& rhs); //=delete; void *internal; Status m_status; }; /** * @brief This is a configuration of the OneClassSegmentationTrainer. * @details It is used as OneClassSegmentation train setting parameter for OneClassSegmentationTrainer. * @author ⓒSualab SuaKIT Team */ class SUAKIT_API OneClassSegmentationTrainConfig { public: /** * @brief This is a constructor of the OneClassSegmentationTrainConfig. * @param validationRatio Ratio of validation set. If user does not provide validation set, validation set is split from train set by this ratio. This ratio is calculated as ((validationRatio/100)*100)% of train set. * @param epochs In training neural network, one epoch means one sweep of the full training set. This is converted to an iteration by the number of train sets. * @param modelCapacity Internal model capacity for executing OneClassSegmentationTrainer. {1, 2}. - 1 means normal model capacity. - 2 means large model capacity. In case of one class segmentation, we provide only 2 modelCapacity: {NORMAL, LARGE}. User needs to select modelCapacity parameter NORMAL or LARGE capacity. * @param strideRatio Training patch data is sampled from original image by sliding windows manner. strideRatio value means stride of shifting. If you assign 50 into this parameter, then 50% size corresponding to patch size (patchSize * 0.5) would be shifting scale. Internally, this value must be in the range [10, 100]. * @param patchSize The size of training patch data. This value should be multiple of 4, and must be in the range [64, max]. You can set arbitrary value satisfying those condition, but WE HIGHLY RECOMMEND 64 or 128 or 256 for this parameter. * @param imageChannel The number of channels of images for train. * @param augmentationConfig Augmentation parameter for train. Currently, augmentation for OneClassSegmentation is not supported. Just put default constructor. * @param inputDataType This parameter is used to configure input type of neural network. Set it to one of {InputDataType::SINGLE, InputDataType::COMPARISON, InputDataType::MULTIMG}. Currently, OneClassSegmentation only support SINGLE type. * @param multiImgCount The number of images for segmentation trainer, depends on inputDataType parameter. - In case of InputDataType::SINGLE, multiImgCount parameter needs to be initialized with 1. - In case of InputDataType::COMPARISON, multiImgCount parameter needs to be initialized with 2. - In case of InputDataType::MULTIMG, multiImgCount parameter needs to be initialized with multiImgCount >= 2. Currently, OneClassSegmentation only support SINGLE type. (multiImgCount should be 1) * @param imageDepth Depth of images for train. * @param numModelSelection Save the top N (numModelSelection) models based on the best loss. If the number of generated models is less than this value, only the currently created models are saved. * @param minimumEpoch The minimum number of epochs to save the model. * @param patience The number of epochs to wait before early stop if no progress on the validation set. The patience is often set somewhere between 10 and 100 (10 or 20 is more common), but it really depends on your dataset. - In case of OneClassSegmentationTrainer, the learning rate decay is not performed. */ __stdcall OneClassSegmentationTrainConfig( SuaKIT_Int32 validationRatio, SuaKIT_Int32 epochs, SuaKIT_Int32 modelCapacity, SuaKIT_Int32 strideRatio, SuaKIT_Int32 patchSize, SuaKIT_Int32 imageChannel, const AugmentationConfig& augmentationConfig = AugmentationConfig(), InputDataType inputDataType = InputDataType::SINGLE, SuaKIT_Int32 multiImgCount = 1, DepthType imageDepth = DepthType::_8U, SuaKIT_Int32 numModelSelection = 1, float minimumEpoch = 0.0f, SuaKIT_Int64 patience = 9223372036854775807ll ); __stdcall ~OneClassSegmentationTrainConfig(); void* _GetInternal() const { return internal; } /** * @return Returns Status. */ Status __stdcall GetStatus() const; private: OneClassSegmentationTrainConfig(const OneClassSegmentationTrainConfig& src); //=delete; OneClassSegmentationTrainConfig& operator=(const OneClassSegmentationTrainConfig& rhs); //=delete; void *internal; Status m_status; }; /** * @brief This is a configuration of the DetectionTrainer. * @details It is used as detection train setting parameter for DetectionTrainer. * @author ⓒSualab SuaKIT Team */ class SUAKIT_API DetectionTrainConfig { public: /** * @brief This is a constructor of the DetectionTrainConfig. * @param numberOfClasses The number of classes to train. In detection training case, this parameter should be calculated except unlabeled class. * @param validationRatio Ratio of validation set. If user does not provide validation set, validation set is split from train set by this ratio. This ratio is calculated as ((validationRatio/100)*100)% of train set. * @param epochs In training neural network, one epoch means one sweep of the full training set. This is converted to an iteration according to the number of train sets. * @param modelCapacity The base (pre-defined) model capacity for executing DetectionTrainer. {0, 1, 2, 3}. - 0 means small model capacity. - 1 means normal model capacity. - 2 means large model capacity. - 3 means extra large model capacity. * @param dataRatio This is a weight parameter for each class. If you want to give a larger weight to specific class, you can increase this array's value corresponding index to the location of the specific class. * @param imageHeight Height of image for train. If the learning image size is variable, set it to the largest height value in the training image. * @param imageWidth Width of images for train. If the learning image size is variable, set it to the largest width value in the training image. * @param imageChannel The number of channels of images for train. * @param anchors Size of anchor boxes. Actual shape : (2, N) s.t. N = number of anchor box if you want to apply 3 anchor boxes, then Actual shape will be = (2, 3) and N = 3 and configuration of array would be { width1, width2, width3, height1, height2, height3 } Value range : [0, 1] => (normalized to image size) * @param augmentationConfig Augmentation parameters for train. * @param inputDataType This parameter is used to configure input type of neural network. Set it to one of {InputDataType::SINGLE, InputDataType::COMPARISON, InputDataType::MULTIMG}. InputDataType::COMPARISON and InputDataType::MULTIMG are currently not supported. These features will be updated later. * @param multiImgCount The number of images for detection trainer, depends on inputDataType parameter. - In case of InputDataType::SINGLE, multiImgCount parameter needs to be initialized with 1. - In case of InputDataType::COMPARISON, multiImgCount parameter needs to be initialized with 2. - In case of InputDataType::MULTIMG, multiImgCount parameter needs to be initialized with multiImgCount >= 2. * @param imageDepth Depth of images for train. * @param detectionModelType This parameter is used to configure model type of detection. Set it to one of {ModelType::DETECTION_DOLPHIN, ModelType::DETECTION_DRAGON}. * @param numModelSelection Save the top N (numModelSelection) models based on the best loss. If the number of generated models is less than this value, only the currently created models are saved. * @param minimumEpoch The minimum number of epochs to save the model. * @param patience The number of epochs to wait before early stop if no progress on the validation set. The patience is often set somewhere between 10 and 100 (10 or 20 is more common), but it really depends on your dataset. - In case of DetectionTrainer, the learning rate decay is not performed. */ __stdcall DetectionTrainConfig( SuaKIT_Int32 numberOfClasses, SuaKIT_Int32 validationRatio, SuaKIT_Int32 epochs, SuaKIT_Int32 modelCapacity, const Int32Array& dataRatio, SuaKIT_Int32 imageHeight, SuaKIT_Int32 imageWidth, SuaKIT_Int32 imageChannel, const FloatArray& anchors, const AugmentationConfig& augmentationConfig = AugmentationConfig(), InputDataType inputDataType = InputDataType::SINGLE, SuaKIT_Int32 multiImgCount = 1, DepthType imageDepth = DepthType::_8U, ModelType detectionModelType = ModelType::DETECTION_DOLPHIN, SuaKIT_Int32 numModelSelection = 1, float minimumEpoch = 0.0f, SuaKIT_Int64 patience = 9223372036854775807ll ); __stdcall ~DetectionTrainConfig(); void* _GetInternal() const { return internal; } /** * @brief Methods to set the class name * @param classIdx Class Index * @param className String to set as the name of class corresponding to Class Index. On Windows, it should be mbcs encoding, and On Linux, this should be utf8 encoding. * @return Returns Status. */ Status SetClassName(SuaKIT_Int32 classIdx, const char * className); /** * @return Returns Status. */ Status __stdcall GetStatus() const; private: DetectionTrainConfig(const DetectionTrainConfig& src); //=delete; DetectionTrainConfig& operator=(const DetectionTrainConfig& rhs); //=delete; void *internal; Status m_status; }; /** * @brief This class is used to manage data for classification train. * @details This class contains image and label information for training ClassificationTrainer. * @author ⓒSualab SuaKIT Team */ class SUAKIT_API ClassificationTrainData { public: /** * @brief A constructor of ClassificationTrainData. * @param inputDataType This parameter is used to configure input type of neural network. It initializes {InputDataType::SINGLE, InputDataType::COMPARISON, InputDataType::MULTIMG}. */ __stdcall ClassificationTrainData(InputDataType inputDataType = SINGLE); __stdcall ~ClassificationTrainData(); /** * @brief Push single data for train ClassificationTrainer. It should be used for InputDataType::SINGLE only. * @param imgPath A path of image to be trained. * @param classNum The class identifier for the image. If the total number of classes is (N), this parameter should be a value less than N (classNum