pjreddie / darknet

Convolutional Neural Networks
http://pjreddie.com/darknet/
Other
25.86k stars 21.33k forks source link

imtest with opencv4.0.1 #1421

Open tomsang opened 5 years ago

tomsang commented 5 years ago

Following the patch https://github.com/pjreddie/darknet/commit/24cff08086b573c3341e91d072430c9f624a2208#diff-25d902c24283ab8cfbac54dfa101ad31, I successfully built darknet with opencv4.0.1. But I found 2 bad images(Original and c1) when I run ./darknet imtest data/eagle.jpg. See the attached pictures. badimtest

What is wrong with the test ? Is it due to opencv4.0.1 ?

nkalupahana commented 5 years ago

Does it do this with other images as well? (for example, ones that you take?)

tomsang commented 5 years ago

@nkalupahana The issue happens on all images under data\ @tiagoshibata Did you meet the same issue with your patch ?

tiagoshibata commented 5 years ago

No, I didn't. I will test it more extensively and try imtest and report my results once I get home.

tiagoshibata commented 5 years ago

Ok, I can reproduce it. Bug seems unrelated to my fix - if you open image.c and save the images using save_image, you get the correct output. Only the windowed display is corrupted.

A workaround is to redraw all windows at every update in image.c. Move these lines: image.c#L1261-L1266 inside the while(1){ loop at line 1268.

Update: Garbled output happens both under X and under Wayland, unless the window is continuously updated.

Update 2: Seems like OpenCV is corrupting the first call to imshow. On my machine, adding a dummy call to imshow by duplicating line image.c#L1261 also works around the issue.

tomsang commented 5 years ago

I tried Update 2. It works well!
Thanks!

tomsang commented 5 years ago

It's better to find why OpenCV4.0.1 has this issue.

luoan commented 5 years ago

Ok, I can reproduce it. Bug seems unrelated to my fix - if you open image.c and save the images using save_image, you get the correct output. Only the windowed display is corrupted.

A workaround is to redraw all windows at every update in image.c. Move these lines: image.c#L1261-L1266 inside the while(1){ loop at line 1268.

Update: Garbled output happens both under X and under Wayland, unless the window is continuously updated.

Update 2: Seems like OpenCV is corrupting the first call to imshow. On my machine, adding a dummy call to imshow by duplicating line image.c#L1261 also works around the issue.

(1)if commented out free(data) in line 30 on src/image_opencv.cpp ./darknet imtest data/eagle.jpg works fine but will cause Memory Leak, we need to return pointer data as well and free it after cv::imshow. or let data be global static(easy to be forget to free after cv::imshow). maybe reallocate memory is better 2019-04-09 11-28-06 的屏幕截图

(2)if reallocate memory for picture, in src/image_opencv.cpp add m = m.clonde();
after Mat m(im.h, im.w, CV_MAKETYPE(CV_8U, im.c), data); it works fine
2019-04-09 13-09-03 的屏幕截图

2019-04-09 13-50-01 的屏幕截图

i think opencv just use your original pointer, and did not do any copy. so if use free(data), system will reclaim memory of the picutre.

https://docs.opencv.org/4.0.1/d3/d63/classcv_1_1Mat.html Mat() [11/29]
data | Pointer to the user data. Matrix constructors that take data and step parameters do not allocate matrix data. Instead, they just initialize the matrix header that points to the specified data, which means that no data is copied. This operation is very efficient and can be used to process external data using OpenCV functions. The external data is not automatically deallocated, so you should take care of it.

i think we should find another place to free pointer(after cv::imshow in show_image_cv function ), or just reallocate memory

luoan commented 5 years ago

It's better to find why OpenCV4.0.1 has this issue.

you can try edit src/image_opencv.cpp add a line m = m.clone(); after Mat m(im.h, im.w, CV_MAKETYPE(CV_8U, im.c), data); this might be helpful. maybe this is not a problem of opencv

luoan commented 5 years ago

Ok, I can reproduce it. Bug seems unrelated to my fix - if you open image.c and save the images using save_image, you get the correct output. Only the windowed display is corrupted.

A workaround is to redraw all windows at every update in image.c. Move these lines: image.c#L1261-L1266 inside the while(1){ loop at line 1268.

Update: Garbled output happens both under X and under Wayland, unless the window is continuously updated.

Update 2: Seems like OpenCV is corrupting the first call to imshow. On my machine, adding a dummy call to imshow by duplicating line image.c#L1261 also works around the issue.

i think if you want to save image after convert from struct image to class cv::Mat, we need to do it in show_image_cv function in src/image_opencv.cpp.
not in src/image.c, save_image function seems will not call image_to_mat

tiagoshibata commented 5 years ago

@luoan good catch :smile: , I pushed a fix at https://github.com/tiagoshibata/darknet/commit/795e5ac1ebee56dac1f792938a9698604e9e4638

luoan commented 5 years ago

@luoan good catch , I pushed a fix at tiagoshibata@795e5ac

great solution, your solution is very efficient !