@@ -29,14 +29,12 @@ public class ImageRecognitionInception : IExample
2929 string dir = "ImageRecognitionInception" ;
3030 string pbFile = "tensorflow_inception_graph.pb" ;
3131 string labelFile = "imagenet_comp_graph_label_strings.txt" ;
32+ List < NDArray > file_ndarrays = new List < NDArray > ( ) ;
3233
3334 public bool Run ( )
3435 {
3536 PrepareData ( ) ;
36-
37- var labels = File . ReadAllLines ( Path . Join ( dir , labelFile ) ) ;
38- var files = Directory . GetFiles ( Path . Join ( dir , "img" ) ) ;
39-
37+
4038 var graph = new Graph ( ) . as_default ( ) ;
4139 //import GraphDef from pb file
4240 graph . Import ( Path . Join ( dir , pbFile ) ) ;
@@ -47,23 +45,21 @@ public bool Run()
4745 var input_operation = graph . OperationByName ( input_name ) ;
4846 var output_operation = graph . OperationByName ( output_name ) ;
4947
48+ var labels = File . ReadAllLines ( Path . Join ( dir , labelFile ) ) ;
5049 var result_labels = new List < string > ( ) ;
5150 var sw = new Stopwatch ( ) ;
5251
5352 with ( tf . Session ( graph ) , sess =>
5453 {
55- foreach ( var file in files )
54+ foreach ( var nd in file_ndarrays )
5655 {
5756 sw . Restart ( ) ;
5857
59- // load image file
60- var tensor = ReadTensorFromImageFile ( file ) ;
61- var results = sess . run ( output_operation . outputs [ 0 ] , new FeedItem ( input_operation . outputs [ 0 ] , tensor ) ) ;
58+ var results = sess . run ( output_operation . outputs [ 0 ] , new FeedItem ( input_operation . outputs [ 0 ] , nd ) ) ;
6259 results = np . squeeze ( results ) ;
6360 int idx = np . argmax ( results ) ;
6461
65- Console . WriteLine ( $ "{ file . Split ( Path . DirectorySeparatorChar ) . Last ( ) } : { labels [ idx ] } { results [ idx ] } in { sw . ElapsedMilliseconds } ms", Color . Tan ) ;
66-
62+ Console . WriteLine ( $ "{ labels [ idx ] } { results [ idx ] } in { sw . ElapsedMilliseconds } ms", Color . Tan ) ;
6763 result_labels . Add ( labels [ idx ] ) ;
6864 }
6965 } ) ;
@@ -77,7 +73,7 @@ private NDArray ReadTensorFromImageFile(string file_name,
7773 int input_mean = 117 ,
7874 int input_std = 1 )
7975 {
80- return with ( tf . Graph ( ) , graph =>
76+ return with ( tf . Graph ( ) . as_default ( ) , graph =>
8177 {
8278 var file_reader = tf . read_file ( file_name , "file_reader" ) ;
8379 var decodeJpeg = tf . image . decode_jpeg ( file_reader , channels : 3 , name : "DecodeJpeg" ) ;
@@ -110,6 +106,14 @@ public void PrepareData()
110106
111107 url = $ "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/data/shasta-daisy.jpg";
112108 Utility . Web . Download ( url , Path . Join ( dir , "img" ) , "shasta-daisy.jpg" ) ;
109+
110+ // load image file
111+ var files = Directory . GetFiles ( Path . Join ( dir , "img" ) ) ;
112+ for ( int i = 0 ; i < files . Length ; i ++ )
113+ {
114+ var nd = ReadTensorFromImageFile ( files [ i ] ) ;
115+ file_ndarrays . Add ( nd ) ;
116+ }
113117 }
114118 }
115119}
0 commit comments