From dfc3409f67a7f3ffa4a8653250309461543a305f Mon Sep 17 00:00:00 2001 From: islam_amin Date: Mon, 6 Jul 2020 13:26:53 -0400 Subject: [PATCH] Update RandomHorizontalFlipWithBBox and BoundingBouxAugment C++ Ops to use floats --- .../ccsrc/dataset/api/python_bindings.cc | 2 +- .../dataset/kernels/image/CMakeLists.txt | 2 +- .../kernels/image/bounding_box_augment_op.cc | 27 +- ...=> random_horizontal_flip_with_bbox_op.cc} | 23 +- ... => random_horizontal_flip_with_bbox_op.h} | 0 .../bounding_box_augment_crop_c_result.npz | Bin 1654 -> 1654 bytes ...bounding_box_augment_rotation_c_result.npz | Bin 1654 -> 1654 bytes ...unding_box_augment_valid_edge_c_result.npz | Bin 1654 -> 1654 bytes ...nding_box_augment_valid_ratio_c_result.npz | Bin 1654 -> 1654 bytes ..._horizontal_flip_with_bbox_01_c_result.npz | Bin 1654 -> 1654 bytes .../dataset/test_bounding_box_augment.py | 274 ++++++++++++++++++ .../test_random_horizontal_flip_with_bbox.py | 221 ++++++++++++++ 12 files changed, 521 insertions(+), 28 deletions(-) rename mindspore/ccsrc/dataset/kernels/image/{random_horizontal_flip_bbox_op.cc => random_horizontal_flip_with_bbox_op.cc} (74%) rename mindspore/ccsrc/dataset/kernels/image/{random_horizontal_flip_bbox_op.h => random_horizontal_flip_with_bbox_op.h} (100%) create mode 100644 tests/ut/python/dataset/test_bounding_box_augment.py create mode 100644 tests/ut/python/dataset/test_random_horizontal_flip_with_bbox.py diff --git a/mindspore/ccsrc/dataset/api/python_bindings.cc b/mindspore/ccsrc/dataset/api/python_bindings.cc index ed3f993fb8..0ae64db671 100644 --- a/mindspore/ccsrc/dataset/api/python_bindings.cc +++ b/mindspore/ccsrc/dataset/api/python_bindings.cc @@ -60,7 +60,7 @@ #include "dataset/kernels/image/random_crop_decode_resize_op.h" #include "dataset/kernels/image/random_crop_op.h" #include "dataset/kernels/image/random_crop_with_bbox_op.h" -#include "dataset/kernels/image/random_horizontal_flip_bbox_op.h" +#include "dataset/kernels/image/random_horizontal_flip_with_bbox_op.h" #include "dataset/kernels/image/random_horizontal_flip_op.h" #include "dataset/kernels/image/random_resize_op.h" #include "dataset/kernels/image/random_resize_with_bbox_op.h" diff --git a/mindspore/ccsrc/dataset/kernels/image/CMakeLists.txt b/mindspore/ccsrc/dataset/kernels/image/CMakeLists.txt index fef698912c..c0c575de9a 100644 --- a/mindspore/ccsrc/dataset/kernels/image/CMakeLists.txt +++ b/mindspore/ccsrc/dataset/kernels/image/CMakeLists.txt @@ -15,7 +15,7 @@ add_library(kernels-image OBJECT random_crop_op.cc random_crop_with_bbox_op.cc random_horizontal_flip_op.cc - random_horizontal_flip_bbox_op.cc + random_horizontal_flip_with_bbox_op.cc bounding_box_augment_op.cc random_resize_op.cc random_rotation_op.cc diff --git a/mindspore/ccsrc/dataset/kernels/image/bounding_box_augment_op.cc b/mindspore/ccsrc/dataset/kernels/image/bounding_box_augment_op.cc index 04e00d878d..a1c29c5307 100644 --- a/mindspore/ccsrc/dataset/kernels/image/bounding_box_augment_op.cc +++ b/mindspore/ccsrc/dataset/kernels/image/bounding_box_augment_op.cc @@ -43,28 +43,29 @@ Status BoundingBoxAugmentOp::Compute(const TensorRow &input, TensorRow *output) std::shared_ptr crop_out; std::shared_ptr res_out; std::shared_ptr input_restore = CVTensor::AsCVTensor(input[0]); - for (uint32_t i = 0; i < num_to_aug; i++) { - uint32_t min_x = 0; - uint32_t min_y = 0; - uint32_t b_w = 0; - uint32_t b_h = 0; + float min_x = 0; + float min_y = 0; + float b_w = 0; + float b_h = 0; // get the required items - input[1]->GetItemAt(&min_x, {selected_boxes[i], 0}); - input[1]->GetItemAt(&min_y, {selected_boxes[i], 1}); - input[1]->GetItemAt(&b_w, {selected_boxes[i], 2}); - input[1]->GetItemAt(&b_h, {selected_boxes[i], 3}); - Crop(input_restore, &crop_out, min_x, min_y, b_w, b_h); + RETURN_IF_NOT_OK(input[1]->GetItemAt(&min_x, {selected_boxes[i], 0})); + RETURN_IF_NOT_OK(input[1]->GetItemAt(&min_y, {selected_boxes[i], 1})); + RETURN_IF_NOT_OK(input[1]->GetItemAt(&b_w, {selected_boxes[i], 2})); + RETURN_IF_NOT_OK(input[1]->GetItemAt(&b_h, {selected_boxes[i], 3})); + RETURN_IF_NOT_OK(Crop(input_restore, &crop_out, static_cast(min_x), static_cast(min_y), + static_cast(b_w), static_cast(b_h))); // transform the cropped bbox region - transform_->Compute(crop_out, &res_out); + RETURN_IF_NOT_OK(transform_->Compute(crop_out, &res_out)); // place the transformed region back in the restored input std::shared_ptr res_img = CVTensor::AsCVTensor(res_out); // check if transformed crop is out of bounds of the box if (res_img->mat().cols > b_w || res_img->mat().rows > b_h || res_img->mat().cols < b_w || res_img->mat().rows < b_h) { // if so, resize to fit in the box - std::shared_ptr resize_op = std::make_shared(b_h, b_w); - resize_op->Compute(std::static_pointer_cast(res_img), &res_out); + std::shared_ptr resize_op = + std::make_shared(static_cast(b_h), static_cast(b_w)); + RETURN_IF_NOT_OK(resize_op->Compute(std::static_pointer_cast(res_img), &res_out)); res_img = CVTensor::AsCVTensor(res_out); } res_img->mat().copyTo(input_restore->mat()(cv::Rect(min_x, min_y, res_img->mat().cols, res_img->mat().rows))); diff --git a/mindspore/ccsrc/dataset/kernels/image/random_horizontal_flip_bbox_op.cc b/mindspore/ccsrc/dataset/kernels/image/random_horizontal_flip_with_bbox_op.cc similarity index 74% rename from mindspore/ccsrc/dataset/kernels/image/random_horizontal_flip_bbox_op.cc rename to mindspore/ccsrc/dataset/kernels/image/random_horizontal_flip_with_bbox_op.cc index 5a5c632e81..7c0fe82fc7 100644 --- a/mindspore/ccsrc/dataset/kernels/image/random_horizontal_flip_bbox_op.cc +++ b/mindspore/ccsrc/dataset/kernels/image/random_horizontal_flip_with_bbox_op.cc @@ -14,7 +14,7 @@ * limitations under the License. */ #include -#include "dataset/kernels/image/random_horizontal_flip_bbox_op.h" +#include "dataset/kernels/image/random_horizontal_flip_with_bbox_op.h" #include "dataset/kernels/image/image_utils.h" #include "dataset/util/status.h" #include "dataset/core/cv_tensor.h" @@ -31,21 +31,19 @@ Status RandomHorizontalFlipWithBBoxOp::Compute(const TensorRow &input, TensorRow // To test bounding boxes algorithm, create random bboxes from image dims size_t num_of_boxes = input[1]->shape()[0]; // set to give number of bboxes float img_center = (input[0]->shape()[1] / 2.); // get the center of the image - for (int i = 0; i < num_of_boxes; i++) { - uint32_t b_w = 0; // bounding box width - uint32_t min_x = 0; + float b_w = 0; // bounding box width + float min_x = 0; // get the required items - input[1]->GetItemAt(&min_x, {i, 0}); - input[1]->GetItemAt(&b_w, {i, 2}); + RETURN_IF_NOT_OK(input[1]->GetItemAt(&min_x, {i, 0})); + RETURN_IF_NOT_OK(input[1]->GetItemAt(&b_w, {i, 2})); // do the flip - float diff = img_center - min_x; // get distance from min_x to center - uint32_t refl_min_x = diff + img_center; // get reflection of min_x - uint32_t new_min_x = refl_min_x - b_w; // subtract from the reflected min_x to get the new one - input[1]->SetItemAt({i, 0}, new_min_x); + float diff = img_center - min_x; // get distance from min_x to center + float refl_min_x = diff + img_center; // get reflection of min_x + float new_min_x = refl_min_x - b_w; // subtract from the reflected min_x to get the new one + RETURN_IF_NOT_OK(input[1]->SetItemAt({i, 0}, new_min_x)); } - (*output).push_back(nullptr); - (*output).push_back(nullptr); + (*output).resize(2); // move input to output pointer of bounding boxes (*output)[1] = std::move(input[1]); // perform HorizontalFlip on the image @@ -55,6 +53,5 @@ Status RandomHorizontalFlipWithBBoxOp::Compute(const TensorRow &input, TensorRow *output = input; return Status::OK(); } - } // namespace dataset } // namespace mindspore diff --git a/mindspore/ccsrc/dataset/kernels/image/random_horizontal_flip_bbox_op.h b/mindspore/ccsrc/dataset/kernels/image/random_horizontal_flip_with_bbox_op.h similarity index 100% rename from mindspore/ccsrc/dataset/kernels/image/random_horizontal_flip_bbox_op.h rename to mindspore/ccsrc/dataset/kernels/image/random_horizontal_flip_with_bbox_op.h diff --git a/tests/ut/data/dataset/golden/bounding_box_augment_crop_c_result.npz b/tests/ut/data/dataset/golden/bounding_box_augment_crop_c_result.npz index e4e92210d7a4b8d01498673fad591c90deed965c..ce9abea5165033e04266f5c72655ebc9c9a8bcc6 100644 GIT binary patch delta 498 zcmeyy^NmL+z?+#xgaHB+8JHinYHbu+z$kFiWcrMA?^Y#IWV^#R`n)(+< zCX28D-H9&f0TeV@CjBqt<}Q8T3$YXSo)Jr!TmuwL;CT5htwg|>#h5|k(EE~ilQ#eb zAKEhfn*C;}vG=$9PjdO^pH6-P6l_1_5O+0VTS>5M*TfGyWV9wru&M}@Fb3CG&oi0G zcJ=2(UpZE*$v!|qSGAno?M-2-IybeR{+E_Yo!kHv{BhxP*Nm=DzgJv6F{O3#wyw!r zfPxj?GipN~SJ`|C_LiM$euZc93!tE&L|~)#Ud374ie66ptGBgsvJ9In3n*GA+p<{# E0Hy!WH2?qr delta 498 zcmeyy^NmL+z?+#xgaHB+85U<yCf>vW!PKB+cCW+?2@4T1+Yeb69K7s4)7Nxh>UD zteJfJ`s6sEU{(GmroV~v!rgAS>#OaVx_9yM(*4Mt= zVOjy3o2E=o0t$Y6nQ;DkRPw4%JD-KN73_UFc^Xh~E$7b4cYBtI>3Yg^yqtRP+~iZt zRsz|F^0o8!v07vv%HFqE>;~gx5f&9pK@Xtdfv_)|e8N=bx$2reU3hBdx5+g?!JU4~ zt8cMZ7>eg4A@0^3;p2{r1g&vR&~%Ccgj*hP|43r1FZOAvr7Z;!spSQY-frvAl|$s#N&n1UWa zL6c?D|1xgw()Ya(J7Mn`v4qJrK*0o#m*3J#1dLgX88i;PFL^h415ogxEyJ(bZ z0~B;s%h}!D6sD?kQ|sw}X{pr74M4#k7e05*==$_~#nls2S|@Mon!E)lSm8aRHso=Y u&6i+r*{SANcqYF93JOXDHfrxxoVBg!<+Q(gTPr8au*tH3{5#o}%@P2$|8$1{ delta 267 zcmeyy^NmL+z?+#xgaHB+8AO-F@@^FR#4M0~C|^5oAFD;?q3nHo#cnW87GY7r6!ZWJ z9tiug$tO%@o~y3u(}kyIew$na6x`{zy!sYvg`s$!HuLgQO{>WpfP$_qrySqv&B*7x z9ND%}^v(ww@Mhk$|DxUDHy2HA01955Kkbp}wUnh<9@k86ZFjplc?(c*uK)F*p6=Jb u*>B(cC)*YOWAY22VA!jfM=GxfNgW(fd-OK}ST diff --git a/tests/ut/data/dataset/golden/bounding_box_augment_valid_edge_c_result.npz b/tests/ut/data/dataset/golden/bounding_box_augment_valid_edge_c_result.npz index dafea520fe945736224799c6687e9aa4125126fb..a72643457b8184ebb37b60dbf951c9f57082794b 100644 GIT binary patch delta 267 zcmeyy^NmL+z?+#xgaHB+8N_=Ivu+gn#4O5{}mbv_sr5Ji?FC*3VL7@ ztidL@0h{0xprDrDE9=s0H#2W%74ZG};dy1U1gna`{{9xhb6bPeZmfH>bjcj%$&-D6 zg1`RHy!)xgUB4x@)lK`BP2l7Ppx{-X7gxg16h=tiW!jNly1jMs7NB6@@BYZf2f>jO cj`R!EwPu!1egPE3@R1CgEDOlLlWo~70ZTe?Jpcdz delta 267 zcmeyy^NmL+z?+#xgaHB+86@xN?b;~xiCJJxPShRk(qA_vWbbf<<{oLCEW)CKDd>Ss zum+pp25f>)fP!ZpJ-Xs3{rG%;|ExOiIk8TYC0JDiavDpD7He_Exwy|0P`cB5d$JEu zkn7$Zc5O?>(gzya;>9-tzfEoc3SL~hf5LnZ*BX;wtJGgPy0}f=0u)@eZtmt~3F dQ>RREnYC^EAvr7Z;!spSQY-frvAl|$s#N&n1UWa zL6c?D|1xgw()Ya(J7Mn`v4qJrK*0o#m*3J#1dLgX88i;PFL^h415ogxEyJ(bZ z0~B;s%h}!D6sD?kQ|sw}X{pr74M4#k7e05*==$_~#nls2S|@Mon!E)lSm8aRHso=Y u&6i+r*{SANcqYF93JOXDHfrxxoVBg!<+Q(gTPr8au*tH3{5#o}%@P3o!*vk= delta 267 zcmeyy^NmL+z?+#xgaHB+8S=fB%-<;ViCG}~P`-BFK30p&L)rWGirrwGEW)CKDd+(d zJP`I}lTVn+JXc-QrwdQb{5H7;D7e#adG#&U3PbTcZRX{rnpTrH00muJPC35Sn~~3X zIkIh|=$%uOp8y55=d*3izQ--W@#Ut$;{5?nCQGma1;gtWp0``}==xoc7n4tm8cp^A z3O3~(d+WZW;LW^g|3$mQZ!VhL02I79f7&C{Ybi^!Jg%AC+U|C9@)n@rT>tAqJ>9Q= uv){h?Pqr)m$K)44!LU~|k5pa}lzhr{ZS%z+YWpY4u*tH3{5#o}%@P1thH||C diff --git a/tests/ut/data/dataset/golden/random_horizontal_flip_with_bbox_01_c_result.npz b/tests/ut/data/dataset/golden/random_horizontal_flip_with_bbox_01_c_result.npz index d360bb98ec7b551d207f73903b519123b5244939..416223ff4de858af0916906d6beb4a88141f232b 100644 GIT binary patch delta 267 zcmeyy^NmL+z?+#xgaHB+8CIt$oZcw(iCN%C>Avr7Z;!spSQY-frvAl|$s#N&n1UWa z!QB~K1h1v^H%VW;=B2ROsCse@P%wex<+rpF0b>?p28~1SOWsZ102H*IC%oK%~imNB4v`*gEHF*nAu)=#rZOG#) un=irMvQy2k@JxOI6ikc}xc_L~z3c^CnfH!fR((2IhE0|QRYT8hT?hJ%*#tPttM{(3T9X$_^eho zl`l=7P&fGrP*8nO+Ve6))upFv5>{^V+TA=^f>lLeId>M*-0~+vb9V2xiwkFpnd}1; zY|1D0p%Hv`41bQkG_UTr;_~-R