<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Erxin, Shang(Edwin, 尚尔鑫)&#039;s Home Page &#187; Performance</title>
	<atom:link href="http://www.shangerxin.com/?feed=rss2&#038;tag=performance" rel="self" type="application/rss+xml" />
	<link>http://www.shangerxin.com</link>
	<description>Knowledge should be free.</description>
	<lastBuildDate>Tue, 24 Aug 2021 02:27:23 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.8.13</generator>
	<item>
		<title>Memory increasing issue when convert Pillow Image (8.3.1) either to Numpy array or OpenCV Image</title>
		<link>http://www.shangerxin.com/?p=424</link>
		<comments>http://www.shangerxin.com/?p=424#comments</comments>
		<pubDate>Wed, 11 Aug 2021 06:29:05 +0000</pubDate>
		<dc:creator><![CDATA[Shang Erxin]]></dc:creator>
				<category><![CDATA[Machine Learning]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[OpenCV]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://www.shangerxin.com/?p=424</guid>
		<description><![CDATA[Background introduction Recently our project encounter a memory increase issue after upgrade from Pillow 8.2.0 to Pillow 8.3.0. Memory consumption after upgrading the Pillow (8.3.1): The memory consumption before upgrading the Pillow (8.2.0): It seems the library bring lots of memory increments. BTW. The issue can not be produce before we build the project. Solution After [&#8230;]]]></description>
				<content:encoded><![CDATA[<h1>Background introduction</h1>
<p>Recently our project encounter a memory increase issue after upgrade from <a href="https://python-pillow.org/">Pillow</a> 8.2.0 to Pillow 8.3.0.</p>
<p>Memory consumption after upgrading the Pillow (8.3.1):</p>
<p><a href="http://www.shangerxin.com/wordpress/wp-content/uploads/2021/08/issue.png"><img class="aligncenter  wp-image-425" alt="issue" src="http://www.shangerxin.com/wordpress/wp-content/uploads/2021/08/issue.png" width="1123" height="583" /></a></p>
<p>The memory consumption before upgrading the Pillow (8.2.0):</p>
<p><a href="http://www.shangerxin.com/wordpress/wp-content/uploads/2021/08/normal.png"><img class="aligncenter size-full wp-image-426" alt="normal" src="http://www.shangerxin.com/wordpress/wp-content/uploads/2021/08/normal.png" width="985" height="392" /></a></p>
<p>It seems the library bring lots of memory increments. BTW. The issue can not be produce before we build the project.</p>
<h1>Solution</h1>
<p>After I spent a few days to find the root cause and the solution. The issue code is located to a pillow image conversion</p>
<p>At the beginning, I suspect this would be a regression in Pillow, so I search their github issues</p>
<p><a href="https://github.com/python-pillow/Pillow/issues/2019">https://github.com/python-pillow/Pillow/issues/2019</a></p>
<p><a href="https://github.com/python-pillow/Pillow/issues/5180">https://github.com/python-pillow/Pillow/issues/5180</a></p>
<p>Both of these two threads indicate the issues are fixed. But I still tried to pull the fix code into our project. Without luck.</p>
<p>To keep the long story short, I grab two heap memory dump and compare with objects which increased most. This great help me reduce the search scope.</p>
<p><a href="http://www.shangerxin.com/wordpress/wp-content/uploads/2021/08/stack-trace.png"><img class="aligncenter size-full wp-image-439" alt="stack-trace" src="http://www.shangerxin.com/wordpress/wp-content/uploads/2021/08/stack-trace.png" width="680" height="948" /></a></p>
<p>The relative issue code:</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">np.<span style="color: #dc143c;">array</span><span style="color: black;">&#40;</span>pillow_image, <span style="color: black;">[</span>dtype=...<span style="color: black;">]</span><span style="color: black;">&#41;</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">np.<span style="color: black;">asarray</span><span style="color: black;">&#40;</span>pillow_image<span style="color: black;">&#41;</span></div></li></ol></pre></div></div>
<p>&nbsp;</p>
<p>All the above conversion will lead to the numpy array keep exist till a certain point trigger python release the accumulated objects.</p>
<p>Then I check the release note of the Pillow, there is only one place directly relative to numpy array. First I try to withdraw the changes directly in 8.3.1, no help. Second try to directly down grade to 8.3.0. Still not works.</p>
<p>Now, what? Checked Pillow API, Numpy API, OpenCV API again and also check their source. A idea pop up and write a workaround code</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">numpy_array = np.<span style="color: black;">frombuffer</span><span style="color: black;">&#40;</span>pillow_image.<span style="color: black;">tobytes</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, dtype=np.<span style="color: black;">uint8</span><span style="color: black;">&#41;</span></div></li></ol></pre></div></div>
<p>To convert the image array to OpenCV</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.shangerxin.com/wordpress/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="python" style="font-family:monospace;"><ol><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #808080; font-style: italic;"># 3 or 4 base on your image mode is RGB or RGBA</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">numpy_array.<span style="color: black;">shape</span> = <span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #008000;">reversed</span><span style="color: black;">&#40;</span>image.<span style="color: black;">size</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span> </div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">&nbsp;</div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #808080; font-style: italic;"># If want to convert the image to BGR then call the OpenCV method</span></div></li><li style="font-weight: bold; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;"><span style="color: #808080; font-style: italic;"># COLOR_RGB2BGR will required the image mode is 3</span></div></li><li style="font-weight: normal; vertical-align:top;"><div style="font: normal normal 1em/1.2em monospace; margin:0; padding:0; background:none; vertical-align:top;">numpy_array= cv2.<span style="color: black;">cvtColor</span><span style="color: black;">&#40;</span>numpy_array, cv2.<span style="color: black;">COLOR_RGB2BGR</span><span style="color: black;">&#41;</span></div></li></ol></pre></div></div>
<p>It works! Now the memory consumption return to normal level.</p>
<h1><a href="http://www.shangerxin.com/wordpress/wp-content/uploads/2021/08/fixed.png"><img class="aligncenter size-full wp-image-427" alt="fixed" src="http://www.shangerxin.com/wordpress/wp-content/uploads/2021/08/fixed.png" width="1034" height="536" /></a></h1>
<p>There are several places directly use the image conversion, we should consider unify the similar logic into a utility class and add a wrapper for the third party library the isolation the influences.</p>
<h1>Summary</h1>
<p>Third party library upgrade is a risk to a product. We have to upgrade the library due to a security reason in this time. The Pillow changes are checked too, have found several suspicious code will cause the issue. Need time to test, maybe update the result later.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shangerxin.com/?feed=rss2&#038;p=424</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
