LINUX实战:TensorFlowSharp入门使用C#编写TensorFlow人工智能应
《LINUX实战:TensorFlowSharp入门使用C#编写TensorFlow人工智能应用》要点: TensorFlowSharp入门使用C#编写TensorFlow人工智能利用学习. TensorFlow简单先容
使用TensorFlowSharp?GitHub:https://github.com/migueldeicaza/TensorFlowSharp 官方源码库,该项目支持跨平台,使用Mono. 可以使用NuGet 安装TensorFlowSharp,如下: Install-Package TensorFlowSharp 编写简单应用使用VS2017新建一个.NET Framework 节制台应用 tensorflowdemo,接着添加TensorFlowSharp 引用. TensorFlowSharp 包比拟大,需要耐心等待. 然后在项目属性中生成->平台目的 改为 x64. 打开Program.cs 写入如下代码: 运行程序成果如下: ? TensorFlow C# image recognition图像辨认示例体验 https://github.com/migueldeicaza/TensorFlowSharp/tree/master/Examples/ExampleInceptionInference 下面学习一个实际的人工智能应用,是非常简单的一个示例,图像辨认. 新建一个 imagerecognition .NET Framework 节制台应用项目,接着添加TensorFlowSharp 引用. 然后在项目属性中生成->平台目的 改为 x64. 接着编写如下代码: class Program { static string dir,modelFile,labelsFile; public static void Main(string[] args) { dir = "tmp"; List<string> files = Directory.GetFiles("img").ToList(); ModelFiles(dir); var graph = new TFGraph(); // 从文件加载序列化的GraphDef var model = File.ReadAllBytes(modelFile); //导入GraphDef graph.Import(model,""); using (var session = new TFSession(graph)) { var labels = File.ReadAllLines(labelsFile); Console.WriteLine("TensorFlow图像识别 LineZero"); foreach (var file in files) { // Run inference on the image files // For multiple images,session.Run() can be called in a loop (and // concurrently). Alternatively,images can be batched since the model // accepts batches of image data as input. var tensor = CreateTensorFromImageFile(file); var runner = session.GetRunner(); runner.AddInput(graph["input"][0],tensor).Fetch(graph["output"][0]); var output = runner.Run(); // output[0].Value() is a vector containing probabilities of // labels for each image in the "batch". The batch size was 1. // Find the most probably label index. var result = output[0]; var rshape = result.Shape; if (result.NumDims != 2 || rshape[0] != 1) { var shape = ""; foreach (var d in rshape) { shape += $"{d} "; } shape = shape.Trim(); Console.WriteLine($"Error: expected to produce a [1 N] shaped tensor where N is the number of labels,instead it produced one with shape [{shape}]"); Environment.Exit(1); } // You can get the data in two ways,as a multi-dimensional array,or arrays of arrays,// code can be nicer to read with one or the other,pick it based on how you want to process // it bool jagged = true; var bestIdx = 0; float p = 0,best = 0; if (jagged) { var probabilities = ((float[][])result.GetValue(jagged: true))[0]; for (int i = 0; i < probabilities.Length; i++) { if (probabilities[i] > best) { bestIdx = i; best = probabilities[i]; } } } else { var val = (float[,])result.GetValue(jagged: false); // Result is [1,N],flatten array for (int i = 0; i < val.GetLength(1); i++) { if (val[0,i] > best) { bestIdx = i; best = val[0,i]; } } } Console.WriteLine($"{Path.GetFileName(file)} 最佳匹配: [{bestIdx}] {best * 100.0}% 标识为:{labels[bestIdx]}"); } } Console.ReadKey(); } // Convert the image in filename to a Tensor suitable as input to the Inception model. static TFTensor CreateTensorFromImageFile(string file) { var contents = File.ReadAllBytes(file); // DecodeJpeg uses a scalar String-valued tensor as input. var tensor = TFTensor.CreateString(contents); TFGraph graph; TFOutput input,output; // Construct a graph to normalize the image ConstructGraphToNormalizeImage(out graph,out input,out output); // Execute that graph to normalize this one image using (var session = new TFSession(graph)) { var normalized = session.Run( inputs: new[] { input },inputValues: new[] { tensor },outputs: new[] { output }); return normalized[0]; } } // The inception model takes as input the image described by a Tensor in a very // specific normalized format (a particular image size,shape of the input tensor,// normalized pixel values etc.). // // This function constructs a graph of TensorFlow operations which takes as // input a JPEG-encoded string and returns a tensor suitable as input to the // inception model. static void ConstructGraphToNormalizeImage(out TFGraph graph,out TFOutput input,out TFOutput output) { // Some constants specific to the pre-trained model at: // https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip // // - The model was trained after with images scaled to 224x224 pixels. // - The colors,represented as R,G,B in 1-byte each were converted to // float using (value - Mean)/Scale. const int W = 224; const int H = 224; const float Mean = 117; const float Scale = 1; graph = new TFGraph(); input = graph.Placeholder(TFDataType.String); output = graph.Div( x: graph.Sub( x: graph.ResizeBilinear( images: graph.ExpandDims( input: graph.Cast( graph.DecodeJpeg(contents: input,channels: 3),DstT: TFDataType.Float),dim: graph.Const(0,"make_batch")),size: graph.Const(new int[] { W,H },"size")),y: graph.Const(Mean,"mean")),y: graph.Const(Scale,"scale")); } /// <summary> /// 下载初始Graph和标签 /// </summary> /// <param name="dir"></param> static void ModelFiles(string dir) { string url = "https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip"; modelFile = Path.Combine(dir,"tensorflow_inception_graph.pb"); labelsFile = Path.Combine(dir,"imagenet_comp_graph_label_strings.txt"); var zipfile = Path.Combine(dir,"inception5h.zip"); if (File.Exists(modelFile) && File.Exists(labelsFile)) return; Directory.CreateDirectory(dir); var wc = new WebClient(); wc.DownloadFile(url,zipfile); ZipFile.ExtractToDirectory(zipfile,dir); File.Delete(zipfile); } } 这里必要注意的是由于必要下载初始Graph和标签,而且是google的站点,所以得使用一些特殊手段. 最终我随意下载了几张图放到binDebugimg ? ?然后运行法式,首先确保binDebugtmp文件夹下有tensorflow_inception_graph.pb及imagenet_comp_graph_label_strings.txt. ? 人工智能的魅力非常年夜,本文只是一个入门,复制上面的代码,你没法训练模型等等操作.所以道路还是很远,需一步一步来. 更多可以查看 https://github.com/migueldeicaza/TensorFlowSharp 及?https://github.com/tensorflow/models 本文永远更新链接地址: 学习更多LINUX教程,请查看站内专栏,如果有LINUX疑问,可以加QQ交流《LINUX实战:TensorFlowSharp入门使用C#编写TensorFlow人工智能应用》。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |