怎么使用C#將Tensorflow訓(xùn)練的.pb文件用在生產(chǎn)環(huán)境-創(chuàng)新互聯(lián)

這篇文章主要介紹怎么使用C#將Tensorflow訓(xùn)練的.pb文件用在生產(chǎn)環(huán)境,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)從2013年開(kāi)始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元盱眙做網(wǎng)站,已為上家服務(wù),為盱眙各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575

TensorFlow是Google開(kāi)源的一款人工智能學(xué)習(xí)系統(tǒng)。Tensor的意思是張量,代表N維數(shù)組;Flow的意思是流,代表基于數(shù)據(jù)流圖的計(jì)算。把N維數(shù)字從流圖的一端流動(dòng)到另一端的過(guò)程,就是人工智能神經(jīng)網(wǎng)絡(luò)進(jìn)行分析和處理的過(guò)程。

怎么使用C#將Tensorflow訓(xùn)練的.pb文件用在生產(chǎn)環(huán)境

使用分步驟保存了的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;
}
}

以上是“怎么使用C#將Tensorflow訓(xùn)練的.pb文件用在生產(chǎn)環(huán)境”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道!

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

當(dāng)前題目:怎么使用C#將Tensorflow訓(xùn)練的.pb文件用在生產(chǎn)環(huán)境-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://muchs.cn/article22/cdsejc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作微信公眾號(hào)、自適應(yīng)網(wǎng)站、關(guān)鍵詞優(yōu)化、軟件開(kāi)發(fā)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)