Convenience and performance are typically inversely correlated. If the code is easy to use, it's less optimized. If it's optimized, it's less convenient. Efficient code needs to get closer to the nitty gritty details of what is actually running, how.
I came across an example in our ongoing work to run & optimize DeepCell cellular segmentation for cancer research. The DeepCell AI model predicts which pixels are most likely to be in a cell. From there, we "flood fill" from the most likely pixels, until reaching the cell border (below some threshold).
Part of this process involves smoothing over small gaps inside predicted cells, which can happen for various reasons but isn't biologically possible. (Think donut holes, not a cell's porous membrane.)
The hole-filling algorithm goes like this:
- Identify objects (contiguous pixels with a given cell label with the same numeric id).
- Compute the "Euler number" of these cells, a measure of the shape's surface.
- If the Euler Number is less than 1 (aka the surface has gaps), smooth out the holes.
Here is an example of Euler numbers from the Wikipedia article; a circle (just the line part) has an Euler characteristic of zero whereas a disk (the "filled-in" circle) has value 1.
We're not here to talk about defining or computing Euler numbers though. We'll talk about how the library's easy path to computing Euler numbers is quite inefficient.
First things first. We noticed the problem by looking at this profile using Speedscope:
It shows ~32ms (~15%) spent in regionprops. This view is left-heavy, if we go to timeline view and zoom in, we get this:
(Note that we do this twice, hence ~16ms here and ~16ms elsewhere, not shown.)
This is immediately suspect: the "interesting" part of finding the objects with find_objects is that first sliver, 0.5ms. It returns a list of tuples, not a generator, so when it's done it's done. So what's up with all the other stuff? We're constructing RegionProperties objects. Let's zoom in on one of them.
The tiny slivers (which we won't zoom into) are custom __setattr__ calls: the RegionProperties objects support aliasing, for instance if you set the attribute ConvexArea it redirects to a standard attribute area_convex. Even though we're not making use of that we still go through the attribute converter.
Furthermore: we aren't even using most of the properties calculated in the region properties. We only care about the Euler number:
props = regionprops(np.squeeze(label_img.astype('int')), cache=False) for prop in props: if prop.euler_number <p>in turn, that only uses the most basic aspect of the region properties: the image regions detected by find_objects (slices of the original image).</p> <p>So, we changed the code to fill_holes code to simply bypass the regionprops general-purpose function. Instead, we call find_objects and pass the resulting image sub-regions to the euler_number function (not the method on a RegionProperties object).</p> <p>Here's the pull request: deepcell-imaging#358 Skip regionprops construction</p> <p>By skipping the intermediate object, we got a decent performance improvement for the fill_holes operation:</p> <div class="table-wrapper-paragraph"><table> <thead> <tr> <th>Image size</th> <th>Before</th> <th>After</th> <th>Speedup</th> </tr> </thead> <tbody> <tr> <td>260k pixels</td> <td>48ms</td> <td>40ms</td> <td>8ms (17%)</td> </tr> <tr> <td>140M pixels</td> <td>15.6s</td> <td>11.7s</td> <td>3.9s (25%)</td> </tr> </tbody> </table></div> <p>For the larger image, 4s is ~3% of the overall runtime– not the bulk of it, but not too shabby either.</p>
以上是效能陷阱:通用庫和輔助對象的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Tomergelistsinpython,YouCanusethe操作員,estextMethod,ListComprehension,Oritertools

在Python3中,可以通過多種方法連接兩個列表:1)使用 運算符,適用於小列表,但對大列表效率低;2)使用extend方法,適用於大列表,內存效率高,但會修改原列表;3)使用*運算符,適用於合併多個列表,不修改原列表;4)使用itertools.chain,適用於大數據集,內存效率高。

使用join()方法是Python中從列表連接字符串最有效的方法。 1)使用join()方法高效且易讀。 2)循環使用 運算符對大列表效率低。 3)列表推導式與join()結合適用於需要轉換的場景。 4)reduce()方法適用於其他類型歸約,但對字符串連接效率低。完整句子結束。

pythonexecutionistheprocessoftransformingpypythoncodeintoExecutablestructions.1)InternterPreterReadSthecode,ConvertingTingitIntObyTecode,whepythonvirtualmachine(pvm)theglobalinterpreterpreterpreterpreterlock(gil)the thepythonvirtualmachine(pvm)

Python的關鍵特性包括:1.語法簡潔易懂,適合初學者;2.動態類型系統,提高開發速度;3.豐富的標準庫,支持多種任務;4.強大的社區和生態系統,提供廣泛支持;5.解釋性,適合腳本和快速原型開發;6.多範式支持,適用於各種編程風格。

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 Linux新版
SublimeText3 Linux最新版

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。