C# 基于Tensorflow.Net 的图像分类

using NumSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tensorflow;
using Tensorflow.Keras;
using static Tensorflow.Binding;
namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        string dir = System.Environment.CurrentDirectory +"\\"+ "saved_model";
        string pbFile = "frozen_graph.pb";
       
        List file_ndarrays = new List();

       

        private NDArray ReadTensorFromImageFile(string file_name,
                              int input_height =64,
                              int input_width = 64,
                              int input_mean = 0,
                              int input_std = 255)
        {
            var graph = tf.Graph().as_default();

            var file_reader = tf.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 string Run()
        {



            var graph = new Graph().as_default();
            //import GraphDef from pb file
            graph.Import(Path.Combine( dir , pbFile));
           
            var input_name = "x";
            var output_name = "Identity";

            var input_operation = graph.OperationByName(input_name);
            var output_operation = graph.OperationByName(output_name);

           // var sw = new Stopwatch();

            using (var sess = tf.Session(graph))
            {
                var nd = file_ndarrays[0];
               // 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 index = np.argmax(results);
                    string[] aa = { "1针 ","其他", "插片" };
                   
                    return aa[index]+","+index.ToString()+","+ results[index].ToString();
               
                }
            }
            //return "null";
          
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string files = Path.Combine(dir, "img", "2.jpg");

            var nd = ReadTensorFromImageFile(files);
           
            file_ndarrays.Add(nd);
            string a=  Run();

            //label1.Text = a;
            MessageBox.Show(a);
        }
    }
}

基于Tensorflow.Net 0.15.

https://github.com/SciSharp/TensorFlow.NET

读取照片,然后载入模型。注意input_std = 255一定要这个,不能设置0。不然预测的结果不对。

模型的话,一定要载入frozen的pb文件。是一整个文件,不是分开文件夹的。其他都不能,不然就报错。

下面的文件不能少,特别是tensorflow.dll,需要拷进来,我的没有自动进来。

C# 基于Tensorflow.Net 的图像分类_第1张图片

好了,现在就可以脱离python环境了。

源码链接: https://pan.baidu.com/s/1tIi6g2evQEI4ONXZon2z6g 提取码: bpge

你可能感兴趣的:(tensorflow,c#,图像识别,.net,python)