Description
Description
I am running a windef
-based code for a banch of 1636 images. The code suddenly stops at a random image and doesn't show any error message.
Details
This has been a persistent issue. These are my findings (briefly).
-
StackExchange suggests it can be a memory leak. Most often memory leaks are due to regular expressions which I use in my own code and which are used in
windef
functions. I "turned off" PIV processing part of my code and ran it with all my regular expressions - didn't have any problems. I concluded it wasn't my regular expressions that lead to the memory leak. -
I decided the next most likely trouble-maker is
multiprocessor
. I decided to try and runwindef
withoutmultiprocessor
. I copiedfunc()
function from thepiv()
function ofwindef
module and assembled a customwindef
algorithm that runs in a good oldfor
loop withoutmultiprocessor
. And I got the same problem - the code crashed at the same PIV image pair. -
Then I noticed that I supplied PIV images with masked bubbles to my custom
windef
algorithm. I went ahead and the supplied the original PIV images (where the bubbles were not masked). And I had success! I concluded that, most likely, the correlation-finding algorithm ofwindef.multipass_img_deform()
didn't like "empty" spaces in PIV images and, somehow, those empty spaces result in memory leak. By "empty" spaces, I of course mean the masked regions of the PIV images. -
So, I supplied the non-masked (original) PIV image to my custom
windef
algorithm and my code could process the problematic PIV image pair (it was PIV image pair number 246). But then it stopped again at PIV image pair number 1063 without any error. I subtract background from my PIV images before supplying them to my algorithm. The background of my PIV images was varying over time (I used LED backlight that created time-varying lighting). I collected background images and picked the brightest to subtract from my PIV images. When I had a look at the PIV images with subtracted backgrounds, I noticed that pair 1063 had very "rarefied" PIV image after background subtraction. I.e., there were a lot of "empty" spaces. So, instead of subtracting the brightest background, I subtracted the average background. And I had success! PIV image pair number 1063 got processed. -
But then, PIV image pair number 554 from a subsequent batch of 1636 images crashed the code. And again I noticed to many "empty" spaced after subtraction of the average background for this particular PIV image pair. So, I took the average background, multiplied it by a coefficient so as to bring its average intensity to the average intensity of the original PIV image from which I am subtracting it. And then I subtracted it. And I had success! The code has been running for more than 9 hours without a crash so far.
-
Based on that, I concluded that when
windef.multipass_img_deform()
can't find enough PIV particles within an interrogation window, a memory leaks occurs. That's where I stopped.
How to reproduce
Here's the link to my Google drive where I am sharing my code and images. Please note, this is not, yet, a published code and images. I plan to publish them soon. So, please, use it solely for the purposes of finding the bug in windef
and then delete it from your computers.
Following the link, you'll find a folder. Its structure is the following. Folder "backgrounds" contains 100 backgrounds for frames A and B. Folder "images" contains all the PIV pairs that crashed my code. Folder "PIVres" has the folder "temp". The folder "temp" has the following folders. "Backgrounds" folder: 1) its subfolder "PIV" contains the brightest backgrounds for frames A and B, 2) its subfolder "shadow" contains the dimmest backgrounds for frames A and B, 3) its subfolder "average" contains the average backgrounds for frames A and B. "windefBubbleMasks" folder contains bubble masks. "windefImages" contains PIV images with subtracted backgrounds. "windefResults" folder contains folder "rawvLtxtwindef" folder with the OpenPIV txt files right after the windef
step and the same OpenPIV txt files but with bubble masks.
The code itself is in the root and is called "memoryLeak.py".
Copy the folder onto your computer. Navigate into the folder. Run the code from within the folder. You'll see no issues with the code.
Now, go to lines 597 and 598 of the code where I subtract the backgrounds. There, replace the argument newBgrAave
with the argument bgrAave
on the line 597 and the argument newBgrBave
with the argument bgrBave
on the line 598. And you will see that PIV image pair number 554 crashes the code without an error. The arguments newBgrAave
and newBgrBave
are the average backgrounds multiplied by a coefficient to make their average intensity equal to the average intensity of the row frames A and B. The arguments bgrAave
and bgrBave
are just the average backgrounds.
Then do the same, but use the argument bgrAPIV
for newBgrAave
on line 597 and the argument bgrBPIV
for newBgrBave
on line 598. Here the arguments bgrAPIV
and bgrBPIV
are the brightest backgrounds for frames A and B. You'll see how PIV image pair number 1063 crushes the code.
Then go ahead and comment out lines 625-630 and uncomment lines 615-620. On lines 625-630 I supply the original PIV images (of course with subtracted backgrounds) to my custom windef
algorithm. Whereas on lines 615-620 I supply the images where bubbles are masked. You will see how PIV image pairs 246, 335, 336 crash the code.
You can play with all that and see with your own eyes how wrong background subtraction can easily create a lot of "empty" spaces (those images are in the folder "windefImages").
I do think those "empty" space lead to memory leaks not in the OpenPIV package, but in the numpy
fast Fourier transform functions that cannot find correlation peaks where there are not particles in the interrogation windows.