效能陷阱:通用庫和輔助對象
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 < 1:
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).
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).
Here's the pull request: deepcell-imaging#358 Skip regionprops construction
By skipping the intermediate object, we got a decent performance improvement for the fill_holes operation:
Image size | Before | After | Speedup |
---|---|---|---|
260k pixels | 48ms | 40ms | 8ms (17%) |
140M pixels | 15.6s | 11.7s | 3.9s (25%) |
For the larger image, 4s is ~3% of the overall runtime– not the bulk of it, but not too shabby either.
以上是效能陷阱:通用庫和輔助對象的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

Python在科學計算中的應用包括數據分析、機器學習、數值模擬和可視化。 1.Numpy提供高效的多維數組和數學函數。 2.SciPy擴展Numpy功能,提供優化和線性代數工具。 3.Pandas用於數據處理和分析。 4.Matplotlib用於生成各種圖表和可視化結果。
