1
1
using System ;
2
+ using System . IO ;
2
3
using System . Linq ;
4
+ using System . Net ;
5
+ using ICSharpCode . SharpZipLib . GZip ;
6
+ using ICSharpCode . SharpZipLib . Tar ;
3
7
using Microsoft . ML . Data ;
4
8
5
9
namespace Microsoft . ML . Samples . Dynamic
@@ -13,7 +17,14 @@ public static void Example()
13
17
{
14
18
// Download the ResNet 101 model from the location below.
15
19
// https://storage.googleapis.com/download.tensorflow.org/models/tflite_11_05_08/resnet_v2_101.tgz
16
- var modelLocation = @"resnet_v2_101/resnet_v2_101_299_frozen.pb" ;
20
+
21
+ string modelLocation = "resnet_v2_101_299_frozen.pb" ;
22
+ if ( ! File . Exists ( modelLocation ) )
23
+ {
24
+ modelLocation = Download ( @"https://storage.googleapis.com/download.tensorflow.org/models/tflite_11_05_08/resnet_v2_101.tgz" , @"resnet_v2_101_299_frozen.tgz" ) ;
25
+ Unzip ( Path . Join ( Directory . GetCurrentDirectory ( ) , modelLocation ) , Directory . GetCurrentDirectory ( ) ) ;
26
+ modelLocation = "resnet_v2_101_299_frozen.pb" ;
27
+ }
17
28
18
29
var mlContext = new MLContext ( ) ;
19
30
var data = GetTensorData ( ) ;
@@ -22,7 +33,7 @@ public static void Example()
22
33
// Create a ML pipeline.
23
34
var pipeline = mlContext . Model . LoadTensorFlowModel ( modelLocation ) . ScoreTensorFlowModel (
24
35
new [ ] { nameof ( OutputScores . output ) } ,
25
- new [ ] { nameof ( TensorData . input ) } ) ;
36
+ new [ ] { nameof ( TensorData . input ) } , addBatchDimensionInput : true ) ;
26
37
27
38
// Run the pipeline and get the transformed values.
28
39
var estimator = pipeline . Fit ( idv ) ;
@@ -86,5 +97,31 @@ class OutputScores
86
97
{
87
98
public float [ ] output { get ; set ; }
88
99
}
100
+
101
+ private static string Download ( string baseGitPath , string dataFile )
102
+ {
103
+ using ( WebClient client = new WebClient ( ) )
104
+ {
105
+ client . DownloadFile ( new Uri ( $ "{ baseGitPath } ") , dataFile ) ;
106
+ }
107
+
108
+ return dataFile ;
109
+ }
110
+
111
+ /// <summary>
112
+ /// Taken from https://github.com/icsharpcode/SharpZipLib/wiki/GZip-and-Tar-Samples.
113
+ /// </summary>
114
+ private static void Unzip ( string path , string targetDir )
115
+ {
116
+ Stream inStream = File . OpenRead ( path ) ;
117
+ Stream gzipStream = new GZipInputStream ( inStream ) ;
118
+
119
+ TarArchive tarArchive = TarArchive . CreateInputTarArchive ( gzipStream ) ;
120
+ tarArchive . ExtractContents ( targetDir ) ;
121
+ tarArchive . Close ( ) ;
122
+
123
+ gzipStream . Close ( ) ;
124
+ inStream . Close ( ) ;
125
+ }
89
126
}
90
127
}
0 commit comments