using NumSharp;using System.Collections.Generic;using System.Diagnostics;using System.Drawing;using System.IO;using Tensorflow;using Tensorflow.Keras.Utils;using static Tensorflow.Binding;using Console = Colorful.Console;namespace TensorFlowNET.Examples{/// <summary>/// Inception v3 is a widely-used image recognition model/// that has been shown to attain greater than 78.1% accuracy on the ImageNet dataset./// The model is the culmination of many ideas developed by multiple researchers over the years./// </summary>public class ImageRecognitionInception : SciSharpExample, IExample{string dir = "ImageRecognitionInception";string pbFile = "tensorflow_inception_graph.pb";string labelFile = "imagenet_comp_graph_label_strings.txt";List<NDArray> file_ndarrays = new List<NDArray>();public ExampleConfig InitConfig()=> Config = new ExampleConfig{Name = "Image Recognition Inception",Enabled = true,IsImportingGraph = false};public bool Run(){tf.compat.v1.disable_eager_execution();PrepareData();var graph = new Graph();//import GraphDef from pb filegraph.Import(Path.Join(dir, pbFile));var input_name = "input";var output_name = "output";var input_operation = graph.OperationByName(input_name);var output_operation = graph.OperationByName(output_name);var labels = File.ReadAllLines(Path.Join(dir, labelFile));var result_labels = new List<string>();var sw = new Stopwatch();using (var sess = tf.Session(graph)){foreach (var nd in file_ndarrays){sw.Restart();var results = sess.run(output_operation.outputs[0], (input_operation.outputs[0], nd));results = np.squeeze(results);int idx = np.argmax(results);Console.WriteLine($"{labels[idx]}{results[idx]} in {sw.ElapsedMilliseconds}ms", Color.Tan);result_labels.Add(labels[idx]);}}return result_labels.Contains("military uniform");}private NDArray ReadTensorFromImageFile(string file_name,int input_height = 224,int input_width = 224,int input_mean = 117,int input_std = 1){var graph = tf.Graph().as_default();var file_reader = tf.io.read_file(file_name, "file_reader");var decodeJpeg = tf.image.decode_jpeg(file_reader, channels: 3, name: "DecodeJpeg");var cast = tf.cast(decodeJpeg, tf.float32);var dims_expander = tf.expand_dims(cast, 0);var resize = tf.constant(new int[] { input_height, input_width });var bilinear = tf.image.resize_bilinear(dims_expander, resize);var sub = tf.subtract(bilinear, new float[] { input_mean });var normalized = tf.divide(sub, new float[] { input_std });using (var sess = tf.Session(graph))return sess.run(normalized);}public override void PrepareData(){Directory.CreateDirectory(dir);// get model filestring url = "https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip";Web.Download(url, dir, "inception5h.zip");Compress.UnZip(Path.Join(dir, "inception5h.zip"), dir);// download sample pictureDirectory.CreateDirectory(Path.Join(dir, "img"));url = $"https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/label_image/data/grace_hopper.jpg";Web.Download(url, Path.Join(dir, "img"), "grace_hopper.jpg");url = $"https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/data/shasta-daisy.jpg";Web.Download(url, Path.Join(dir, "img"), "shasta-daisy.jpg");// load image filevar files = Directory.GetFiles(Path.Join(dir, "img"));for (int i = 0; i < files.Length; i++){var nd = ReadTensorFromImageFile(files[i]);file_ndarrays.Add(nd);}}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。