Add UseGpu property to ModelConfiguration and update network runtime configuration
This commit is contained in:
parent
2e9da07638
commit
f4f8a58646
3 changed files with 50 additions and 30 deletions
|
|
@ -139,6 +139,12 @@
|
||||||
must match the class ordering used by the trained recognition network.
|
must match the class ordering used by the trained recognition network.
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="P:AIFotoONLUS.Core.ModelConfiguration.UseGpu">
|
||||||
|
<summary>
|
||||||
|
When enabled, request OpenCV DNN CUDA backend/target for inference.
|
||||||
|
The installed OpenCV runtime must have CUDA support or model loading/forwarding may fail.
|
||||||
|
</summary>
|
||||||
|
</member>
|
||||||
<member name="P:AIFotoONLUS.Core.ModelConfiguration.EnableCropSaving">
|
<member name="P:AIFotoONLUS.Core.ModelConfiguration.EnableCropSaving">
|
||||||
<summary>
|
<summary>
|
||||||
When enabled, recognition crops will be saved to disk under
|
When enabled, recognition crops will be saved to disk under
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,12 @@ namespace AIFotoONLUS.Core
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string[] NumberClasses { get; set; } = new[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
|
public string[] NumberClasses { get; set; } = new[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// When enabled, request OpenCV DNN CUDA backend/target for inference.
|
||||||
|
/// The installed OpenCV runtime must have CUDA support or model loading/forwarding may fail.
|
||||||
|
/// </summary>
|
||||||
|
public bool UseGpu { get; set; } = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// When enabled, recognition crops will be saved to disk under
|
/// When enabled, recognition crops will be saved to disk under
|
||||||
/// "logs/crops" for diagnostic inspection. Disabled by default.
|
/// "logs/crops" for diagnostic inspection. Disabled by default.
|
||||||
|
|
|
||||||
|
|
@ -95,10 +95,8 @@ namespace AIFotoONLUS.Core
|
||||||
_detectionNet = CvDnn.ReadNetFromDarknet(_cfg.DetectionCfg, _cfg.DetectionWeights);
|
_detectionNet = CvDnn.ReadNetFromDarknet(_cfg.DetectionCfg, _cfg.DetectionWeights);
|
||||||
_recognitionNet = CvDnn.ReadNetFromDarknet(_cfg.RecognitionCfg, _cfg.RecognitionWeights);
|
_recognitionNet = CvDnn.ReadNetFromDarknet(_cfg.RecognitionCfg, _cfg.RecognitionWeights);
|
||||||
|
|
||||||
_detectionNet.SetPreferableBackend(Backend.OPENCV);
|
ConfigureNetRuntime(_detectionNet, _cfg.UseGpu);
|
||||||
_detectionNet.SetPreferableTarget(Target.CPU);
|
ConfigureNetRuntime(_recognitionNet, _cfg.UseGpu);
|
||||||
_recognitionNet.SetPreferableBackend(Backend.OPENCV);
|
|
||||||
_recognitionNet.SetPreferableTarget(Target.CPU);
|
|
||||||
// Let OpenCV use multiple threads internally (use number of logical processors)
|
// Let OpenCV use multiple threads internally (use number of logical processors)
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|
@ -127,6 +125,19 @@ namespace AIFotoONLUS.Core
|
||||||
|
|
||||||
private string[] GetOutputLayerNames(Net net) => net.GetUnconnectedOutLayersNames();
|
private string[] GetOutputLayerNames(Net net) => net.GetUnconnectedOutLayersNames();
|
||||||
|
|
||||||
|
private static void ConfigureNetRuntime(Net net, bool useGpu)
|
||||||
|
{
|
||||||
|
if (useGpu)
|
||||||
|
{
|
||||||
|
net.SetPreferableBackend(Backend.CUDA);
|
||||||
|
net.SetPreferableTarget(Target.CUDA);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
net.SetPreferableBackend(Backend.OPENCV);
|
||||||
|
net.SetPreferableTarget(Target.CPU);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Detect text regions in the supplied image using the detection network.
|
/// Detect text regions in the supplied image using the detection network.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -162,15 +173,15 @@ namespace AIFotoONLUS.Core
|
||||||
var fallback = new List<Mat>();
|
var fallback = new List<Mat>();
|
||||||
for (int on = 0; on < outNames.Length; on++)
|
for (int on = 0; on < outNames.Length; on++)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var single = detectionNet.Forward(outNames[on]);
|
var single = detectionNet.Forward(outNames[on]);
|
||||||
fallback.Add(single);
|
fallback.Add(single);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger?.LogError(ex, "Fallback Forward failed for {name}", outNames[on]);
|
_logger?.LogError(ex, "Fallback Forward failed for {name}", outNames[on]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (fallback.Count > 0)
|
if (fallback.Count > 0)
|
||||||
{
|
{
|
||||||
|
|
@ -221,15 +232,15 @@ namespace AIFotoONLUS.Core
|
||||||
}
|
}
|
||||||
|
|
||||||
if (maxScore > _cfg.ConfidenceThreshold)
|
if (maxScore > _cfg.ConfidenceThreshold)
|
||||||
{
|
{
|
||||||
int x = (int)Math.Max(0, Math.Round(cx - w / 2));
|
int x = (int)Math.Max(0, Math.Round(cx - w / 2));
|
||||||
int y = (int)Math.Max(0, Math.Round(cy - h / 2));
|
int y = (int)Math.Max(0, Math.Round(cy - h / 2));
|
||||||
var rect = new Rect(x, y, (int)Math.Round(w), (int)Math.Round(h));
|
var rect = new Rect(x, y, (int)Math.Round(w), (int)Math.Round(h));
|
||||||
boxes.Add(rect);
|
boxes.Add(rect);
|
||||||
confidences.Add(maxScore);
|
confidences.Add(maxScore);
|
||||||
classIds.Add(bestClass);
|
classIds.Add(bestClass);
|
||||||
centerXList.Add(cx);
|
centerXList.Add(cx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -486,10 +497,8 @@ namespace AIFotoONLUS.Core
|
||||||
{
|
{
|
||||||
var det = CvDnn.ReadNetFromDarknet(_cfg.DetectionCfg, _cfg.DetectionWeights);
|
var det = CvDnn.ReadNetFromDarknet(_cfg.DetectionCfg, _cfg.DetectionWeights);
|
||||||
var rec = CvDnn.ReadNetFromDarknet(_cfg.RecognitionCfg, _cfg.RecognitionWeights);
|
var rec = CvDnn.ReadNetFromDarknet(_cfg.RecognitionCfg, _cfg.RecognitionWeights);
|
||||||
det.SetPreferableBackend(Backend.OPENCV);
|
ConfigureNetRuntime(det, _cfg.UseGpu);
|
||||||
det.SetPreferableTarget(Target.CPU);
|
ConfigureNetRuntime(rec, _cfg.UseGpu);
|
||||||
rec.SetPreferableBackend(Backend.OPENCV);
|
|
||||||
rec.SetPreferableTarget(Target.CPU);
|
|
||||||
netsBag.Add((det, rec));
|
netsBag.Add((det, rec));
|
||||||
return (det, rec);
|
return (det, rec);
|
||||||
});
|
});
|
||||||
|
|
@ -525,8 +534,7 @@ namespace AIFotoONLUS.Core
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using var tempRec = CvDnn.ReadNetFromDarknet(_cfg.RecognitionCfg, _cfg.RecognitionWeights);
|
using var tempRec = CvDnn.ReadNetFromDarknet(_cfg.RecognitionCfg, _cfg.RecognitionWeights);
|
||||||
tempRec.SetPreferableBackend(Backend.OPENCV);
|
ConfigureNetRuntime(tempRec, _cfg.UseGpu);
|
||||||
tempRec.SetPreferableTarget(Target.CPU);
|
|
||||||
var alt = RecognizeDigits(crop, tempRec, ctx);
|
var alt = RecognizeDigits(crop, tempRec, ctx);
|
||||||
if (!string.IsNullOrEmpty(alt)) txt = alt;
|
if (!string.IsNullOrEmpty(alt)) txt = alt;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue