麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C# > 正文

如何使用C#將Tensorflow訓練的.pb文件用在生產環境詳解

2019-10-29 19:41:24
字體:
來源:轉載
供稿:網友

前言

TensorFlow是Google開源的一款人工智能學習系統。為什么叫這個名字呢?Tensor的意思是張量,代表N維數組;Flow的意思是流,代表基于數據流圖的計算。把N維數字從流圖的一端流動到另一端的過程,就是人工智能神經網絡進行分析和處理的過程。

訓練了很久的Tf模型,終于要到生產環境中去考研一番了。今天花費了一些時間去研究tf的模型如何在生產環境中去使用。大概整理了這些方法。

繼續使用分步驟保存了的ckpt文件

這個貌似脫離不了tensorflow框架,而且生成的ckpt文件比較大,發布到生產環境的時候,還得把python的算法文件一起搞上去,如何和其他程序交互,可能還得自己去寫服務。估計很少有人這么做,貌似性能也很一般。

使用tensorflow Serving

tf Serving貌似是大家都比較推崇的方法。需要編譯tfServing,然后把模型導出來。直接執行tf Serving的進程,就可以對外提供服務了。具體調用的時候,還得自己寫客戶端,使用人gRPC去調用Serving,然后再對外提供服務,聽上去比較麻煩。而且我今天沒太多的時間去研究gRPC,網絡上關于客戶端很多都是用python寫的,我感覺自己的python水平比較菜,沒信心能寫好。所以這個方式就先沒研究。

生產.pb文件,然后寫程序去調用.pb文件

生成了.pb文件以后,就可以被程序去直接調用,傳入參數,然后就可以傳出來參數,而且生成的.pb文件非常的小。而我又有比較豐富的.net開發經驗。在想,是否可以用C#來解析.pb文件,然后做一個.net core的對外服務的API,這樣貌似更加高效,關鍵是自己熟悉這款的開發,不用花費太多的時間去摸索。、

具體的思路

使用.net下面的TensorFlow框架tensorflowSharp(貌似還是沒脫離了框架).去調用pb文件,然后做成.net core web API 對外提供服務。

具體的實現

直接上代碼,非常簡單,本身設計到tensorflowsharp的地方非常的少

var graph = new TFGraph();//重點是下面的這句,把訓練好的pb文件給讀出來字節,然后導入var model = File.ReadAllBytes(model_file);graph.Import(model);Console.WriteLine("請輸入一個圖片的地址");var src = Console.ReadLine();var tensor = ImageUtil.CreateTensorFromImageFile(src);using (var sess = new TFSession(graph)){var runner = sess.GetRunner();runner.AddInput(graph["Cast_1"][0], tensor);var r = runner.Run(graph.softmax(graph["softmax_linear/softmax_linear"][0]));var v = (float[,])r.GetValue();Console.WriteLine(v[0,0]);Console.WriteLine(v[0, 1]);}

ImageUtil這個類庫是tensorflowSharp官方的例子中一個把圖片轉成tensor的類庫,我直接copy過來了,根據我的網絡,修改了幾個參數。

public static class ImageUtil{public static TFTensor CreateTensorFromImageFile(byte[] contents, TFDataType destinationDataType = TFDataType.Float){var tensor = TFTensor.CreateString(contents);TFOutput input, output;// Construct a graph to normalize the imageusing (var graph = ConstructGraphToNormalizeImage(out input, out output, destinationDataType)){// Execute that graph to normalize this one imageusing (var session = new TFSession(graph)){var normalized = session.Run(inputs: new[] { input },inputValues: new[] { tensor },outputs: new[] { output });return normalized[0];}}}// Convert the image in filename to a Tensor suitable as input to the Inception model.public static TFTensor CreateTensorFromImageFile(string file, TFDataType destinationDataType = TFDataType.Float){var contents = File.ReadAllBytes(file);// DecodeJpeg uses a scalar String-valued tensor as input.var tensor = TFTensor.CreateString(contents);TFOutput input, output;// Construct a graph to normalize the imageusing (var graph = ConstructGraphToNormalizeImage(out input, out output, destinationDataType)){// Execute that graph to normalize this one imageusing (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.private static TFGraph ConstructGraphToNormalizeImage(out TFOutput input, out TFOutput output, TFDataType destinationDataType = TFDataType.Float){// 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 = 128;const int H = 128;const float Mean = 0;const float Scale = 1f;var graph = new TFGraph();input = graph.Placeholder(TFDataType.String);output = graph.Cast(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")), destinationDataType);return graph;}}

搞定

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产在线观看 | 国产一级在线免费观看 | 久久精品国产精品亚洲 | 亚洲精华液久久含羞草 | 国产91九色 | 成人羞羞在线观看网站 | 日本看片一区二区三区高清 | 免费视频www在线观看 | 国产精品久久久久久久久久 | 成人三级免费电影 | 国产精品亚洲一区二区三区久久 | 国产成人精品免费视频大全最热 | 黄色片网站免费在线观看 | 毛片免费网 | 日韩视频在线观看免费 | asiass极品裸体女pics | 成人超碰| 天天操综| 亚洲啪啪 | 久久青草热 | 91精品福利视频 | 国产精品自拍av | 欧美一级毛片欧美一级成人毛片 | 成人在线视频在线观看 | 日韩黄色一级视频 | 毛片视频大全 | wwwav国产| 999精品国产| 欧美老外a级毛片 | 国产毛片视频 | 91精品免费在线 | 国产亚洲精品久久久久婷婷瑜伽 | 羞羞视频免费观看入口 | 欧美日韩夜夜 | 欧洲狠狠鲁| 中文字幕爱爱视频 | 久久成人精品视频 | 国产视频在线观看一区二区三区 | 色999久久久精品人人澡69 | 法国性xxx精品hd | 国产午夜亚洲精品 |