11using System ;
2+ using System . IO ;
23using System . Linq ;
4+ using System . Net ;
5+ using ICSharpCode . SharpZipLib . GZip ;
6+ using ICSharpCode . SharpZipLib . Tar ;
37using Microsoft . ML . Data ;
48
59namespace Microsoft . ML . Samples . Dynamic
@@ -13,7 +17,14 @@ public static void Example()
1317 {
1418 // Download the ResNet 101 model from the location below.
1519 // 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+ }
1728
1829 var mlContext = new MLContext ( ) ;
1930 var data = GetTensorData ( ) ;
@@ -22,7 +33,7 @@ public static void Example()
2233 // Create a ML pipeline.
2334 var pipeline = mlContext . Model . LoadTensorFlowModel ( modelLocation ) . ScoreTensorFlowModel (
2435 new [ ] { nameof ( OutputScores . output ) } ,
25- new [ ] { nameof ( TensorData . input ) } ) ;
36+ new [ ] { nameof ( TensorData . input ) } , addBatchDimensionInput : true ) ;
2637
2738 // Run the pipeline and get the transformed values.
2839 var estimator = pipeline . Fit ( idv ) ;
@@ -86,5 +97,31 @@ class OutputScores
8697 {
8798 public float [ ] output { get ; set ; }
8899 }
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+ }
89126 }
90127}
0 commit comments