@@ -7,57 +7,40 @@ namespace OnnxObjectDetection
7
7
{
8
8
public class OnnxModelConfigurator
9
9
{
10
- private readonly MLContext _mlContext ;
11
- private readonly ITransformer _mlModel ;
10
+ private readonly MLContext mlContext ;
11
+ private readonly ITransformer mlModel ;
12
12
13
- public OnnxModelConfigurator ( string onnxModelFilePath )
13
+ public OnnxModelConfigurator ( IOnnxModel onnxModel )
14
14
{
15
- _mlContext = new MLContext ( ) ;
15
+ mlContext = new MLContext ( ) ;
16
16
// Model creation and pipeline definition for images needs to run just once,
17
17
// so calling it from the constructor:
18
- _mlModel = SetupMlNetModel ( onnxModelFilePath ) ;
18
+ mlModel = SetupMlNetModel ( onnxModel ) ;
19
19
}
20
20
21
- public struct ImageSettings
21
+ private ITransformer SetupMlNetModel ( IOnnxModel onnxModel )
22
22
{
23
- public const int imageHeight = 416 ;
24
- public const int imageWidth = 416 ;
25
- }
26
-
27
- public struct TinyYoloModelSettings
28
- {
29
- // To check Tiny Yolo2 Model input and output parameter names,
30
- // you can use tools like Netron: https://github.com/lutzroeder/netron
31
-
32
- // Input tensor name
33
- public const string ModelInput = "image" ;
34
-
35
- // Output tensor name
36
- public const string ModelOutput = "grid" ;
37
- }
38
-
39
- public ITransformer SetupMlNetModel ( string onnxModelFilePath )
40
- {
41
- var dataView = _mlContext . Data . LoadFromEnumerable ( new List < ImageInputData > ( ) ) ;
23
+ var dataView = mlContext . Data . LoadFromEnumerable ( new List < ImageInputData > ( ) ) ;
42
24
43
- var pipeline = _mlContext . Transforms . ResizeImages ( resizing : ImageResizingEstimator . ResizingKind . Fill , outputColumnName : "image" , imageWidth : ImageSettings . imageWidth , imageHeight : ImageSettings . imageHeight , inputColumnName : nameof ( ImageInputData . Image ) )
44
- . Append ( _mlContext . Transforms . ExtractPixels ( outputColumnName : "image" ) )
45
- . Append ( _mlContext . Transforms . ApplyOnnxModel ( modelFile : onnxModelFilePath , outputColumnNames : new [ ] { TinyYoloModelSettings . ModelOutput } , inputColumnNames : new [ ] { TinyYoloModelSettings . ModelInput } ) ) ;
25
+ var pipeline = mlContext . Transforms . ResizeImages ( resizing : ImageResizingEstimator . ResizingKind . Fill , outputColumnName : onnxModel . ModelInput , imageWidth : ImageSettings . imageWidth , imageHeight : ImageSettings . imageHeight , inputColumnName : nameof ( ImageInputData . Image ) )
26
+ . Append ( mlContext . Transforms . ExtractPixels ( outputColumnName : onnxModel . ModelInput ) )
27
+ . Append ( mlContext . Transforms . ApplyOnnxModel ( modelFile : onnxModel . ModelPath , outputColumnName : onnxModel . ModelOutput , inputColumnName : onnxModel . ModelInput ) ) ;
46
28
47
29
var mlNetModel = pipeline . Fit ( dataView ) ;
48
30
49
31
return mlNetModel ;
50
32
}
51
33
52
- public PredictionEngine < ImageInputData , ImageObjectPrediction > GetMlNetPredictionEngine ( )
34
+ public PredictionEngine < ImageInputData , T > GetMlNetPredictionEngine < T > ( )
35
+ where T : class , IOnnxObjectPrediction , new ( )
53
36
{
54
- return _mlContext . Model . CreatePredictionEngine < ImageInputData , ImageObjectPrediction > ( _mlModel ) ;
37
+ return mlContext . Model . CreatePredictionEngine < ImageInputData , T > ( mlModel ) ;
55
38
}
56
39
57
40
public void SaveMLNetModel ( string mlnetModelFilePath )
58
41
{
59
42
// Save/persist the model to a .ZIP file to be loaded by the PredictionEnginePool
60
- _mlContext . Model . Save ( _mlModel , null , mlnetModelFilePath ) ;
43
+ mlContext . Model . Save ( mlModel , null , mlnetModelFilePath ) ;
61
44
}
62
45
}
63
46
}
0 commit comments