Skip to content

Fixes #22747. Support [crop] configuration for DarkNet#24384

Merged
asmorkalov merged 2 commits intoopencv:4.xfrom
Dhanwanth1803:feat-crop
Dec 22, 2023
Merged

Fixes #22747. Support [crop] configuration for DarkNet#24384
asmorkalov merged 2 commits intoopencv:4.xfrom
Dhanwanth1803:feat-crop

Conversation

@Dhanwanth1803
Copy link
Copy Markdown
Contributor

@Dhanwanth1803 Dhanwanth1803 commented Oct 10, 2023

Request for comments. This is my first PR.

Merge with extra: opencv/opencv_extra#1112

resolves #22747

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake
opencv_extra=feat-crop1

@opencv-alalek
Copy link
Copy Markdown
Contributor

It makes sense to add test to avoid regressions in the future

@opencv-alalek opencv-alalek added the pr: needs test New functionality requires minimal tests set label Oct 11, 2023
@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

It makes sense to add test to avoid regressions in the future

Yes sure @opencv-alalek. I will add the tests very soon. Can you please assign this issue to me.

int crop_width = getParam<int>(layer_params, "crop_width", 224);
int flip = getParam<int>(layer_params, "flip", 1);
int exposure = getParam<int>(layer_params, "exposure", 1);
int saturation = getParam<int>(layer_params, "saturation", 1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use boolean parameters for true/false flags:

bool flip = getParam<bool>(layer_params, "flip", true);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. thankyou @dkurt. I will make the changes asap.

@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

Dhanwanth1803 commented Oct 14, 2023

WhatsApp Image 2023-10-14 at 09 22 35
test_backends.txt

@dkurt @opencv-alalek Hi guys, I am trying to add the test layers. Its actually a lot of test layers and I am finding it hard to understand the parameters and add it accordingly. I added a couple until now idk if they are correct. Can you guys help me with any resources or tell me how can I go about this problem. It would be a great help. thanks :) (request for comments)

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Oct 18, 2023

@Dhanwanth1803, you can create a single layer network similar to models from https://github.com/opencv/opencv_extra/tree/4.x/testdata/dnn/darknet. Then, use a script generate_darknet_models.py to create test data files. As Crop layer has no weights, it's enough to write only a .cfg file. Take a look at average pooling as a reference: https://github.com/opencv/opencv_extra/blob/4.x/testdata/dnn/darknet/avgpool_softmax.cfg

After that, add all the generated files to a new branch to opencv_extra with the same name: [feat-crop](https://github.com/Dhanwanth1803/opencv/tree/feat-crop) and open a PR.

@asmorkalov asmorkalov added this to the 4.9.0 milestone Oct 20, 2023
@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

@Dhanwanth1803, you can create a single layer network similar to models from https://github.com/opencv/opencv_extra/tree/4.x/testdata/dnn/darknet. Then, use a script generate_darknet_models.py to create test data files. As Crop layer has no weights, it's enough to write only a .cfg file. Take a look at average pooling as a reference: https://github.com/opencv/opencv_extra/blob/4.x/testdata/dnn/darknet/avgpool_softmax.cfg

After that, add all the generated files to a new branch to opencv_extra with the same name: [feat-crop](https://github.com/Dhanwanth1803/opencv/tree/feat-crop) and open a PR.

Thank you @dkurt. I will be working on it.

@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

@dkurt Like you said, i tried to generate test data files using generate_darknet_models.py . But it gave me the following error.
AttributeError: module 'darknet' has no attribute 'load_net'
So I checked for load_net function in the darknet repo as well. Turns out there is no such function there. There is a load_network function but i tried using it already. Its not helping.

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Oct 23, 2023

@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

@dkurt I have installed darknet and set the path using export and also i have given read and write permissions to the config files in testdata/dnn/darknet. when the run the generate_darknet_models.py I am getting the following error message.

Couldn't open file: shortcut.cfg

This is happening for every function call trying to read a .cfg file.

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Oct 24, 2023

@Dhanwanth1803, Run the script from opencv_extra/testdata/dnn/darknet folder

@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

@Dhanwanth1803, you can create a single layer network similar to models from https://github.com/opencv/opencv_extra/tree/4.x/testdata/dnn/darknet. Then, use a script generate_darknet_models.py to create test data files. As Crop layer has no weights, it's enough to write only a .cfg file. Take a look at average pooling as a reference: https://github.com/opencv/opencv_extra/blob/4.x/testdata/dnn/darknet/avgpool_softmax.cfg

After that, add all the generated files to a new branch to opencv_extra with the same name: [feat-crop](https://github.com/Dhanwanth1803/opencv/tree/feat-crop) and open a PR.

@dkurt I have put up PR in opencv_extra. Please do check it out and let me know if any changes are required. Thank you.

@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

@Dhanwanth1803, you can create a single layer network similar to models from https://github.com/opencv/opencv_extra/tree/4.x/testdata/dnn/darknet. Then, use a script generate_darknet_models.py to create test data files. As Crop layer has no weights, it's enough to write only a .cfg file. Take a look at average pooling as a reference: https://github.com/opencv/opencv_extra/blob/4.x/testdata/dnn/darknet/avgpool_softmax.cfg
After that, add all the generated files to a new branch to opencv_extra with the same name: [feat-crop](https://github.com/Dhanwanth1803/opencv/tree/feat-crop) and open a PR.

@dkurt I have put up PR in opencv_extra. Please do check it out and let me know if any changes are required. Thank you.

I have put up a PR again @dkurt . Please check it out.

fused_layer_names.push_back(last_layer);
}

void setCrop(int crop_height, int crop_width, int flip, int exposure, int saturation, int angle)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused arguments. How layer can work without them?

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Oct 27, 2023

@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

@asmorkalov
Copy link
Copy Markdown
Contributor

@Dhanwanth1803 Friendly reminder.

@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

@Dhanwanth1803 Friendly reminder.

have exams currently in my school right now. I continue on 8th November. Is that fine?

@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

@Dhanwanth1803, add a test to https://github.com/opencv/opencv/blob/4.x/modules/dnn/test/test_darknet_importer.cpp

@dkurt Should I add a test to

@Dhanwanth1803, add a test to https://github.com/opencv/opencv/blob/4.x/modules/dnn/test/test_darknet_importer.cpp

@dkurt I have made changes for addition of crop layer in https://github.com/opencv/opencv/blob/4.x/modules/dnn/src/darknet/darknet_io.cpp and test in https://github.com/opencv/opencv/blob/4.x/modules/dnn/test/test_darknet_importer.cpp. Should I be making changes to any new file in addition to these two?

@asmorkalov
Copy link
Copy Markdown
Contributor

@Dhanwanth1803 the PR still includes changes for single file. Looks like you forgot to add test changes to the commit.

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Nov 12, 2023

Please remove an old branch https://github.com/Dhanwanth1803/opencv_extra/tree/feat-crop

@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

Please remove an old branch https://github.com/Dhanwanth1803/opencv_extra/tree/feat-crop

Yes Sir deleted just now/

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Nov 13, 2023

@Dhanwanth1803, please make test pass locally. Build OpenCV with ts module and run opencv_test_dnn --gtest_filter=Test_Darknet_layers.crop/0

For now problem is not solved:

[ RUN      ] Test_Darknet_layers.crop/0, where GetParam() = OCV/CPU
unknown file: error: C++ exception with description "OpenCV(4.8.0-dev) c:\build\precommit_windows64\4.x\opencv\modules\dnn\src\net_impl.hpp:108: error: (-2:Unspecified error) Can't create layer "crop_0" of type "CropLayer" in function 'cv::dnn::dnn4_v20230620::Net::Impl::getLayerInstance'
" thrown in the test body.
[  FAILED  ] Test_Darknet_layers.crop/0, where GetParam() = OCV/CPU (1 ms)

crop_param.set<int>("crop_height", crop_height);
crop_param.set<int>("crop_width", crop_width);
crop_param.name = "CropLayer-name";
crop_param.type = "CropLayer";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no "CropLayer" in OpenCV - just "Crop"

Copy link
Copy Markdown
Member

@dkurt dkurt Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover, there is no crop_height and crop_width attributes for Crop. You need to utilize Slice with it's begin, end or size (https://github.com/opencv/opencv/blob/4.x/modules/dnn/src/layers/slice_layer.cpp):

std::vector<int> begin = {0, 0, 0, 0};
std::vector<int> sizes = {-1, -1, crop_height, crop_width};
crop_param.set("begin", DictValue::arrayInt(&begin[0], begin.size()));
crop_param.set("size", DictValue::arrayInt(&sizes[0], sizes.size()));
crop_param.type = "Slice";

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if crop_height and crop_width are not attributes for crop. Why did we generate test data using those parameters in opencv_extra

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are native Darknet names and this is ok for test data. Importer should remap this parameters to expected ones by OpenCV

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I will replace it with the code snippet you gave.

@asmorkalov
Copy link
Copy Markdown
Contributor

@Dhanwanth1803 Friendly reminder

@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

@Dhanwanth1803 Friendly reminder

Yes sir still working on it @asmorkalov

@asmorkalov
Copy link
Copy Markdown
Contributor

@dkurt

[ RUN      ] Test_Darknet_layers.crop/0, where GetParam() = OCV/CPU
unknown file: Failure
C++ exception with description "OpenCV(4.8.0-dev) /home/ci/opencv/modules/ts/src/ts.cpp:1071: error: (-2:Unspecified error) OpenCV tests: Can't find required data file: dnn/darknet/crop_in.npy in function 'findData'
" thrown in the test body.
[  FAILED  ] Test_Darknet_layers.crop/0, where GetParam() = OCV/CPU (0 ms)

@asmorkalov asmorkalov removed the pr: needs test New functionality requires minimal tests set label Dec 20, 2023
@dkurt
Copy link
Copy Markdown
Member

dkurt commented Dec 20, 2023

@asmorkalov, GitHub Actions CI ignores Merge with extra which points to a different branch name. Buildbot CI passes.

@asmorkalov
Copy link
Copy Markdown
Contributor

@dkurt Extra CI options are handled by buildbot only. Please ignore Github actions for now.

@asmorkalov asmorkalov merged commit 027aee8 into opencv:4.x Dec 22, 2023
@dkurt
Copy link
Copy Markdown
Member

dkurt commented Dec 22, 2023

@dkurt dkurt mentioned this pull request Dec 22, 2023
6 tasks
@Dhanwanth1803
Copy link
Copy Markdown
Contributor Author

@dkurt Thank you for making the changes. I was not able to work for personal reasons I will continue to contribute soon.

@asmorkalov asmorkalov mentioned this pull request Jan 19, 2024
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
Fixes opencv#22747. Support [crop] configuration for DarkNet opencv#24384

Request for comments. This is my first PR. 

**Merge with extra**: opencv/opencv_extra#1112

resolves opencv#22747

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

darknet crop layer missing implementation

4 participants