如何使用C#將Tensorflow訓(xùn)練的.pb文件用在生產(chǎn)環(huán)境詳解
前言
TensorFlow是Google開(kāi)源的一款人工智能學(xué)習(xí)系統(tǒng)。為什么叫這個(gè)名字呢?Tensor的意思是張量,代表N維數(shù)組;Flow的意思是流,代表基于數(shù)據(jù)流圖的計(jì)算。把N維數(shù)字從流圖的一端流動(dòng)到另一端的過(guò)程,就是人工智能神經(jīng)網(wǎng)絡(luò)進(jìn)行分析和處理的過(guò)程。
訓(xùn)練了很久的Tf模型,終于要到生產(chǎn)環(huán)境中去考研一番了。今天花費(fèi)了一些時(shí)間去研究tf的模型如何在生產(chǎn)環(huán)境中去使用。大概整理了這些方法。
繼續(xù)使用分步驟保存了的ckpt文件
這個(gè)貌似脫離不了tensorflow框架,而且生成的ckpt文件比較大,發(fā)布到生產(chǎn)環(huán)境的時(shí)候,還得把python的算法文件一起搞上去,如何和其他程序交互,可能還得自己去寫(xiě)服務(wù)。估計(jì)很少有人這么做,貌似性能也很一般。
使用tensorflow Serving
tf Serving貌似是大家都比較推崇的方法。需要編譯tfServing,然后把模型導(dǎo)出來(lái)。直接執(zhí)行tf Serving的進(jìn)程,就可以對(duì)外提供服務(wù)了。具體調(diào)用的時(shí)候,還得自己寫(xiě)客戶端,使用人gRPC去調(diào)用Serving,然后再對(duì)外提供服務(wù),聽(tīng)上去比較麻煩。而且我今天沒(méi)太多的時(shí)間去研究gRPC,網(wǎng)絡(luò)上關(guān)于客戶端很多都是用python寫(xiě)的,我感覺(jué)自己的python水平比較菜,沒(méi)信心能寫(xiě)好。所以這個(gè)方式就先沒(méi)研究。
生產(chǎn).pb文件,然后寫(xiě)程序去調(diào)用.pb文件
生成了.pb文件以后,就可以被程序去直接調(diào)用,傳入?yún)?shù),然后就可以傳出來(lái)參數(shù),而且生成的.pb文件非常的小。而我又有比較豐富的.net開(kāi)發(fā)經(jīng)驗(yàn)。在想,是否可以用C#來(lái)解析.pb文件,然后做一個(gè).net core的對(duì)外服務(wù)的API,這樣貌似更加高效,關(guān)鍵是自己熟悉這款的開(kāi)發(fā),不用花費(fèi)太多的時(shí)間去摸索。、
具體的思路
使用.net下面的TensorFlow框架tensorflowSharp(貌似還是沒(méi)脫離了框架).去調(diào)用pb文件,然后做成.net core web API 對(duì)外提供服務(wù)。
具體的實(shí)現(xiàn)
直接上代碼,非常簡(jiǎn)單,本身設(shè)計(jì)到tensorflowsharp的地方非常的少
var graph = new TFGraph(); //重點(diǎn)是下面的這句,把訓(xùn)練好的pb文件給讀出來(lái)字節(jié),然后導(dǎo)入 var model = File.ReadAllBytes(model_file); graph.Import(model); Console.WriteLine("請(qǐng)輸入一個(gè)圖片的地址"); 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這個(gè)類庫(kù)是tensorflowSharp官方的例子中一個(gè)把圖片轉(zhuǎn)成tensor的類庫(kù),我直接copy過(guò)來(lái)了,根據(jù)我的網(wǎng)絡(luò),修改了幾個(gè)參數(shù)。
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 image using (var graph = ConstructGraphToNormalizeImage(out input, out output, destinationDataType)) { // 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]; } } } // 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 image using (var graph = ConstructGraphToNormalizeImage(out input, out output, destinationDataType)) { // 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. 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; } }
搞定
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)我們的支持。
上一篇:C#中的應(yīng)用程序接口介紹及實(shí)現(xiàn),密封類與密封方法
欄 目:C#教程
本文標(biāo)題:如何使用C#將Tensorflow訓(xùn)練的.pb文件用在生產(chǎn)環(huán)境詳解
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5013.html
您可能感興趣的文章
- 01-10Extjs4如何處理后臺(tái)json數(shù)據(jù)中日期和時(shí)間
- 01-10C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放
- 01-10C#3.0使用EventLog類寫(xiě)Windows事件日志的方法
- 01-10C#實(shí)現(xiàn)將窗體固定在顯示器的左上角且不能移動(dòng)的方法
- 01-10C#實(shí)現(xiàn)將程序鎖定到Win7任務(wù)欄的方法
- 01-10C#使用windows服務(wù)開(kāi)啟應(yīng)用程序的方法
- 01-10asp.net中XML如何做增刪改查操作
- 01-10c# ArrayList的使用方法小總結(jié)
- 01-10C#使用ADO.Net部件來(lái)訪問(wèn)Access數(shù)據(jù)庫(kù)的方法
- 01-10C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫(xiě)Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10C#中split用法實(shí)例總結(jié)
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改