The script worked fine, I thought. Now I realized if the movie size was modest (hundreds of frames), it is ok. But if it is thousands, now I see significant slowing down of the process, like from 1.5 sec/frame to 2~3 sec/frame (Laptop PC Let’s Note).
How can I solve this? Here is the ChatGPT’s suggestion.
スレッドプールの「適切な数」は、システムのCPUやタスクの性質に基づいています。特に、CPUバウンドのタスクでは、C++コードの最適化とスレッドプールの使い方が鍵です。
スレッド数の選定
スレッドプールの「適切な数」は、通常、使用可能なCPUコアの数に基づいています。cv2.matchTemplateがGILを解放する場合、スレッド数を多くすることも考慮されます。
タスクのためのスレッド数
CPUバウンドのタスクには、最大スレッド数をシステムの論理CPU数に合わせるのが良い。タスクの待機状態を避けるため、スレッド数をシステムに最適化している。
CPU割り当ての違いを探る
Let’s Noteは6秒、DeepStationは12秒。DeepStationのCPU配分がうまくいっていない可能性を模索中。
最適なスレッド数を見極める
cv2.matchTemplateを使用している場合、タスクの数をシステムのCPUコア数に合わせる必要がある。過剰なスレッドはパフォーマンスの悪化を招く可能性がある。
タスクの負荷を見極める
cv2.matchTemplateを使っていると、タスクの負荷が大きくなりがち。タスク数をシステムのコア数に合わせることが重要だね。
タスクの最適配分を探る
cv2.matchTemplateを使ってると、タスクをシステムのコア数に合わせるのが大事。タスク数を増やすと、空きコアがなければスレッドが溢れるかもね。
企業の物流戦略を探る
企業が物流コストを内外製品に均等に分配し、ニッチな分野の製品を特定の業者と提携して供給するアプローチを選択している。
o3-mini-high
I tested the suggested change. I also commented out the image saving part. Now it seems no delay in the processing at least at 5200 frame (2.2 sec / frame, Desktop PC).
import os
os.cpu_count()
フレーム 0 処理中 2025-02-05 14:34:30.5
フレーム 1 処理中 2025-02-05 14:34:32.9
フレーム 2 処理中 2025-02-05 14:34:35.8
フレーム 3 処理中 2025-02-05 14:34:38.2
フレーム 4 処理中 2025-02-05 14:34:40.3
フレーム 5 処理中 2025-02-05 14:34:42.5
フレーム 6 処理中 2025-02-05 14:34:44.7
フレーム 7 処理中 2025-02-05 14:34:46.9
フレーム 8 処理中 2025-02-05 14:34:49.0
フレーム 9 処理中 2025-02-05 14:34:51.1
フレーム 10 処理中 2025-02-05 14:34:53.3
フレーム 5190 処理中 2025-02-05 17:48:40.8 フレーム 5191 処理中 2025-02-05 17:48:43.2 フレーム 5192 処理中 2025-02-05 17:48:45.8 フレーム 5193 処理中 2025-02-05 17:48:48.1 フレーム 5194 処理中 2025-02-05 17:48:50.3 フレーム 5195 処理中 2025-02-05 17:48:52.5 フレーム 5196 処理中 2025-02-05 17:48:54.8 フレーム 5197 処理中 2025-02-05 17:48:57.0 フレーム 5198 処理中 2025-02-05 17:48:59.2 フレーム 5199 処理中 2025-02-05 17:49:01.3 フレーム 5200 処理中 2025-02-05 17:49:03.6
Script name: template matching (larval fish tracking) parallel ThreadPool.jpynb
The major change is that the ThreadPoolExecutor was moved in the function def process_video from the funciton def template_matching_with_rotation_parallel in the script so that we do not need call executor everytime. Not sure if this was the cause or saving the image was the cause.
def process_video(avi_path, template_path, mask_path, output_txt):
# システムの論理コア数に合わせたスレッドプールを動画全体で使い回す
with ThreadPoolExecutor(max_workers=os.cpu_count()) as executor: