バルクローバルOCR中のハイピークメモリー
一度に多くのPDFセグメントに対してOCRを実行することはメモリー消費を倍増させます。 各タスクは、OcrInput を通してフルページのビットマップをレンダリングし、新しい IronTesseract エンジンがセグメントごとに毎回言語モデルファイルを再ロードします。プロセッサの並行処理が最大の場合、ピークメモリは複数GBの範囲に達し、メモリに制限がある環境ではスパイクが発生して失敗することがあります。
OCRは生来メモリを多く使用します。 すべてのOcrInputがフルページビットマップをレンダリングし、すべてのIronTesseractエンジンが言語モデルファイルをメモリにロードします。 セグメントごとに新しいエンジンを作成すると、モデルが繰り返しリロードされ、各CPUコアごとに一つのOCRタスクを実行すると(Environment.ProcessorCount)、多くのビットマップ中心のジョブを並行して実行できます。アクティブなタスクの数に制限がない場合、ピークメモリは同時実行数に直接比例してスケールします。
修正方法は、飛行中のジョブの数を制限し: 同時実行性の制限を設定し、プールからエンジンを再利用し、セマフォで作業をゲートします。
解決策
1. OCRの同時実行性を制限します
同時に行うOCRタスクの数を小さな天井にクランプします。 少ない同時タスクは、一度にメモリーにあるフルページビットマップの数が減り、それがピークを直接下げます。 機械の能力に合わせて天井を調整します。
// Clamp concurrency to avoid memory saturation and CPU over-subscription.
int concurrency = Math.Clamp(Environment.ProcessorCount / 2, 1, 4);Dim concurrency As Integer = Math.Clamp(Environment.ProcessorCount \ 2, 1, 4)Dim concurrency As Integer = Math.Clamp(Environment.ProcessorCount \ 2, 1, 4)2. エンジンをプールします
スタートアップ時に同時スロットごとに必ず1つのIronTesseractエンジンを作成し、各セグメントで再利用してください。新しいエンジンを構築して言語モデルを毎回リロードするのではなく。
// Pre-create one engine per concurrent slot and reuse them across segments.
var enginePool = new ConcurrentBag<IronTesseract>(
Enumerable.Range(0, concurrency).Select(_ => new IronTesseract())
);// Pre-create one engine per concurrent slot and reuse them across segments.
var enginePool = new ConcurrentBag<IronTesseract>(
Enumerable.Range(0, concurrency).Select(_ => new IronTesseract())
);' Pre-create one engine per concurrent slot and reuse them across segments.
Dim enginePool = New ConcurrentBag(Of IronTesseract)(
Enumerable.Range(0, concurrency).Select(Function(_) New IronTesseract())
)' Pre-create one engine per concurrent slot and reuse them across segments.
Dim enginePool = New ConcurrentBag(Of IronTesseract)(
Enumerable.Range(0, concurrency).Select(Function(_) New IronTesseract())
)一度プールを構築することで、セグメントごとに支払うのではなく、全体の実行にわたって言語モデルのロードコストを分割します。
3. セマフォで作業をゲートします
SemaphoreSlim を同時実行制限に初期化し、それを using でラップします。 各タスクは開始する前にWaitAsync()を呼び出し、finallyでRelease()を呼び出します。そのため、許可された数のセグメントだけが常にフライト中です。
using var semaphore = new SemaphoreSlim(concurrency);
await semaphore.WaitAsync();
try
{
// Rent a pre-loaded engine from the pool.
if (!enginePool.TryTake(out var ocr))
ocr = new IronTesseract(); // Defensive fallback; should never be reached.
try
{
using var input = new OcrInput();
input.LoadPdf(segmentStream); // page-range segment produced upstream
var ocrResult = await ocr.ReadAsync(input);
ocrResult.SaveAsSearchablePdf(outputPath);
}
finally
{
enginePool.Add(ocr); // Return engine to pool for the next waiting segment.
}
}
finally
{
semaphore.Release();
}using var semaphore = new SemaphoreSlim(concurrency);
await semaphore.WaitAsync();
try
{
// Rent a pre-loaded engine from the pool.
if (!enginePool.TryTake(out var ocr))
ocr = new IronTesseract(); // Defensive fallback; should never be reached.
try
{
using var input = new OcrInput();
input.LoadPdf(segmentStream); // page-range segment produced upstream
var ocrResult = await ocr.ReadAsync(input);
ocrResult.SaveAsSearchablePdf(outputPath);
}
finally
{
enginePool.Add(ocr); // Return engine to pool for the next waiting segment.
}
}
finally
{
semaphore.Release();
}Using semaphore As New SemaphoreSlim(concurrency)
Await semaphore.WaitAsync()
Try
' Rent a pre-loaded engine from the pool.
Dim ocr As IronTesseract
If Not enginePool.TryTake(ocr) Then
ocr = New IronTesseract() ' Defensive fallback; should never be reached.
End If
Try
Using input As New OcrInput()
input.LoadPdf(segmentStream) ' page-range segment produced upstream
Dim ocrResult = Await ocr.ReadAsync(input)
ocrResult.SaveAsSearchablePdf(outputPath)
End Using
Finally
enginePool.Add(ocr) ' Return engine to pool for the next waiting segment.
End Try
Finally
semaphore.Release()
End Try
End UsingUsing semaphore As New SemaphoreSlim(concurrency)
Await semaphore.WaitAsync()
Try
' Rent a pre-loaded engine from the pool.
Dim ocr As IronTesseract
If Not enginePool.TryTake(ocr) Then
ocr = New IronTesseract() ' Defensive fallback; should never be reached.
End If
Try
Using input As New OcrInput()
input.LoadPdf(segmentStream) ' page-range segment produced upstream
Dim ocrResult = Await ocr.ReadAsync(input)
ocrResult.SaveAsSearchablePdf(outputPath)
End Using
Finally
enginePool.Add(ocr) ' Return engine to pool for the next waiting segment.
End Try
Finally
semaphore.Release()
End Try
End UsingWaitAsync() 呼び出しはスロットが空くまでブロックされ、内部 finally でエンジンを返すことで、事前にロードされたエンジンを次の待機セグメントに直接渡します。
4. 各セグメントごとにOcrInputを破棄します
各OcrInputをusingで囲み、セグメントが読み込まれた瞬間にそのレンダリングされたページビットマップが解放されるようにしてください。次のタスクがそのスロットを取得する前に解放されます。
using は OcrInput のビットマップメモリがセグメント間で蓄積されるのを防ぎます。 それがなければ、解放されたスロットは依然としてそのページのビットマップを保持します。5. 各Read内の並列性を制限する
上記の手順は、同時に実行されるセグメント数を制限しますが、各IronTesseractも単一のRead呼び出し内でページを並列に読み込み、論理プロセッサあたり1ページをデフォルトとしています。 2つの制限が掛け合わされます: 4つの同時セグメントに対してデフォルトの16である場合、4つではなく64のネイティブエンジンになります。 各プールされたエンジンにMaxDegreeOfParallelismを設定して、内部レイヤーも制限されるようにしてください。
// Bound the inner layer as well as the outer one.
// Without this, each of the `concurrency` segments still fans out
// across every logical processor.
foreach (var engine in enginePool)
engine.MaxDegreeOfParallelism = 2;// Bound the inner layer as well as the outer one.
// Without this, each of the `concurrency` segments still fans out
// across every logical processor.
foreach (var engine in enginePool)
engine.MaxDegreeOfParallelism = 2;' Bound the inner layer as well as the outer one.
' Without this, each of the `concurrency` segments still fans out
' across every logical processor.
For Each engine In enginePool
engine.MaxDegreeOfParallelism = 2
Next' Bound the inner layer as well as the outer one.
' Without this, each of the `concurrency` segments still fans out
' across every logical processor.
For Each engine In enginePool
engine.MaxDegreeOfParallelism = 2
Next並列OCRメモリ使用量を制御する方法については、2つのプロパティがどのように相互作用し、各同時ページのコストがどのようになるかをご覧ください。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。