-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[GSoC22] Data Augmentation Module in OpenCV (imgaug) #3335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.x
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add the short-form license and the documentation for the functionalites.
|
||
image = compose(image) | ||
|
||
plt.imshow(image) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you call cv2.imshow() here to show the result?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will change the python sample in next commit.
modules/aug/src/transforms.cpp
Outdated
// CV_EXPORTS_W void randomCropV1(InputOutputArray _src, const Size& sz, const Vec4i& padding, bool pad_if_need, int fill, int padding_mode){ | ||
// Mat src = _src.getMat(); | ||
// | ||
// if(padding != Vec4i()){ | ||
// copyMakeBorder(src, src, padding[0], padding[1], padding[2], padding[3], padding_mode, fill); | ||
// } | ||
// | ||
// // NOTE: make sure src.rows == src.size().height and src.cols = src.size().width | ||
// // pad the height if needed | ||
// if(pad_if_need && src.rows < sz.height){ | ||
// Vec4i _padding = {sz.height - src.rows, sz.height - src.rows, 0, 0}; | ||
// copyMakeBorder(src, src, _padding[0], _padding[1], _padding[2], _padding[3], padding_mode, fill); | ||
// } | ||
// // pad the width if needed | ||
// if(pad_if_need && src.cols < sz.width){ | ||
// Vec4i _padding = {0, 0, sz.width - src.cols, sz.width - src.cols}; | ||
// copyMakeBorder(src, src, _padding[0], _padding[1], _padding[2], _padding[3], padding_mode, fill); | ||
// } | ||
// | ||
// int x, y; | ||
// getRandomCropParams(src.rows, src.cols, sz.height, sz.width, &x, &y); | ||
// Mat cropped(src, Rect(x, y, sz.width, sz.height)); | ||
// (*(Mat*)_src.getObj()) = cropped; | ||
// } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove code which are not finished, experimental, for debug and the like.
Hi @kaingwade, thanks for the advice. I have added the license and add documentation for imgaug module. Junk codes in header files have also been cleaned. |
… range of AdjustHue and ColorJitter
Hello @ZhaoChuyang! I have added support for |
Hi @asenyaev, I have commited the latest changes. |
extern uint64 state; | ||
|
||
//! Random number generator for data augmentation module | ||
extern cv::RNG rng; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Global variables with direct access is very bad idea:
- C++ does not define initialization order.
rng
andstate
may be initialized in any order as soon as global variables in user's code. It becomes even worse, if library is linked statically. - It's not clear how to use this in multi-threaded environment.
I propose 2 options:
- Use standard OpenCV theRNG. It uses thread local seed.
- Provide own theRNG-like function. It returns reference to RNG object that could be re-initialized.
*/ | ||
CV_WRAP explicit Convert(int code); | ||
|
||
/** @brief Apply data augmentation method on source image and its annotation. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify the method.
}; | ||
|
||
//! Convert the color space of the given image | ||
class CV_EXPORTS_W Convert: public Transform{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I propose to rename to ColorConvert
. Convert
is too generic.
template<> struct pyopencvVecConverter<Ptr<cv::imgaug::Transform> > | ||
{ | ||
static bool to(PyObject* obj, std::vector<cv::Ptr<cv::imgaug::Transform> >& value, const ArgInfo& info) | ||
{ | ||
return pyopencv_to_generic_vec(obj, value, info); | ||
} | ||
|
||
}; | ||
|
||
template<> struct pyopencvVecConverter<Ptr<cv::imgaug::det::Transform> > | ||
{ | ||
static bool to(PyObject* obj, std::vector<cv::Ptr<cv::imgaug::det::Transform> >& value, const ArgInfo& info) | ||
{ | ||
return pyopencv_to_generic_vec(obj, value, info); | ||
} | ||
|
||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need custom bindings for it?
* Brightness factor should be >= 0. When brightness factor is larger than 1, the output image will be brighter than original. | ||
* When brightness factor is less than 1, the output image will be darker than original. | ||
*/ | ||
void adjustBrightness(Mat& img, double brightness_factor); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It makes sense to use InputArray
and OutputArray
for all functions in this header. Rationale:
- Generic OpenCV interface.
- UMat support, if
InputArray
is promoted fromTransform
classes directly without getMat() call.
template<> struct PyOpenCV_Converter<unsigned long long> | ||
{ | ||
static bool to(PyObject* obj, unsigned long long& value, const ArgInfo& info){ | ||
if(!obj || obj == Py_None) | ||
return true; | ||
if(PyLong_Check(obj)){ | ||
value = PyLong_AsUnsignedLongLong(obj); | ||
}else{ | ||
return false; | ||
} | ||
return value != (unsigned int)-1 || !PyErr_Occurred(); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have not found long log
or unsigned long long
usage in interface. Most probably the manual binding is not required.
std::vector<Mat> gray_arrays = {gray, gray, gray}; | ||
merge(gray_arrays, gray); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it chould be cvtColor with COLOR_GRAY2BGR.
std::vector<Mat> new_channels; | ||
for(int i=0; i < num_channels; i++){ | ||
Mat& channel = channels[i]; | ||
Scalar avg = mean(channel); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function cv::mean calculates the mean value M of array elements, independently for each channel, and return it. No need to allocate array and iterate over channels the next arithmetic steps could be dome for all channels together without loop.
General notes on test code:
|
@ZhaoChuyang Friendly reminder. |
Hi, sorry for the delay, I have been caught up in a DDL. I'll fix them ASAP. |
What's new about this module? |
PR for GSoC'22 project on Efficient Data Augmentation Module in OpenCV for DL Training
I implemented data augmentation methods based on basic image processing and have tested the performance of these already implemented methods in python enviroment. The following table demonstrates the running time (in seconds) comparison between OpenCV-Aug module and torchvision transforms on a subset of ImageNet:
single method:
compose multiple methods:
Besides augmentation methods for pure images, augmentation methods for detection task and segmentation task is also added, which requires processing the target labels of corresponding tasks.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.