| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent a5d2aa9 commit ddbbe06
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,7 +21,7 @@ public class BaseSession | |||
| 21 | 21 | ||
| 22 | 22 | public BaseSession(string target = "", Graph g = null, SessionOptions opts = null) | |
| 23 | 23 | { | |
| 24 | - _graph = graph is null ? ops.get_default_graph() : g; | ||
| 24 | + _graph = g is null ? ops.get_default_graph() : g; | ||
| 25 | 25 | ||
| 26 | 26 | _target = UTF8Encoding.UTF8.GetBytes(target); | |
| 27 | 27 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,14 +29,12 @@ public class ImageRecognitionInception : IExample | |||
| 29 | 29 | string dir = "ImageRecognitionInception"; | |
| 30 | 30 | string pbFile = "tensorflow_inception_graph.pb"; | |
| 31 | 31 | string labelFile = "imagenet_comp_graph_label_strings.txt"; | |
| 32 | + List<NDArray> file_ndarrays = new List<NDArray>(); | ||
| 32 | 33 | ||
| 33 | 34 | public bool Run() | |
| 34 | 35 | { | |
| 35 | 36 | PrepareData(); | |
| 36 | - | ||
| 37 | - var labels = File.ReadAllLines(Path.Join(dir, labelFile)); | ||
| 38 | - var files = Directory.GetFiles(Path.Join(dir, "img")); | ||
| 39 | - | ||
| 37 | + | ||
| 40 | 38 | var graph = new Graph().as_default(); | |
| 41 | 39 | //import GraphDef from pb file | |
| 42 | 40 | graph.Import(Path.Join(dir, pbFile)); | |
@@ -47,23 +45,21 @@ public bool Run() | |||
| 47 | 45 | var input_operation = graph.OperationByName(input_name); | |
| 48 | 46 | var output_operation = graph.OperationByName(output_name); | |
| 49 | 47 | ||
| 48 | + var labels = File.ReadAllLines(Path.Join(dir, labelFile)); | ||
| 50 | 49 | var result_labels = new List<string>(); | |
| 51 | 50 | var sw = new Stopwatch(); | |
| 52 | 51 | ||
| 53 | 52 | with(tf.Session(graph), sess => | |
| 54 | 53 | { | |
| 55 | - foreach (var file in files) | ||
| 54 | + foreach (var nd in file_ndarrays) | ||
| 56 | 55 | { | |
| 57 | 56 | sw.Restart(); | |
| 58 | 57 | ||
| 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)); | ||
| 62 | 59 | results = np.squeeze(results); | |
| 63 | 60 | int idx = np.argmax(results); | |
| 64 | 61 | ||
| 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); | ||
| 67 | 63 | result_labels.Add(labels[idx]); | |
| 68 | 64 | } | |
| 69 | 65 | }); | |
@@ -77,7 +73,7 @@ private NDArray ReadTensorFromImageFile(string file_name, | |||
| 77 | 73 | int input_mean = 117, | |
| 78 | 74 | int input_std = 1) | |
| 79 | 75 | { | |
| 80 | - return with(tf.Graph(), graph => | ||
| 76 | + return with(tf.Graph().as_default(), graph => | ||
| 81 | 77 | { | |
| 82 | 78 | var file_reader = tf.read_file(file_name, "file_reader"); | |
| 83 | 79 | var decodeJpeg = tf.image.decode_jpeg(file_reader, channels: 3, name: "DecodeJpeg"); | |
@@ -110,6 +106,14 @@ public void PrepareData() | |||
| 110 | 106 | ||
| 111 | 107 | url = $"https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/data/shasta-daisy.jpg"; | |
| 112 | 108 | 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 | + } | ||
| 113 | 117 | } | |
| 114 | 118 | } | |
| 115 | 119 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments