最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2018年11月14日 10:31:08   作者:bbird2018  
這篇文章主要給大家介紹了關(guān)于如何使用C#將Tensorflow訓(xùn)練的.pb文件用在生產(chǎn)環(huán)境的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

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

訓(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的算法文件一起搞上去,如何和其他程序交互,可能還得自己去寫服務(wù)。估計(jì)很少有人這么做,貌似性能也很一般。

使用tensorflow Serving

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

生產(chǎn).pb文件,然后寫程序去調(diào)用.pb文件

生成了.pb文件以后,就可以被程序去直接調(diào)用,傳入?yún)?shù),然后就可以傳出來參數(shù),而且生成的.pb文件非常的小。而我又有比較豐富的.net開發(fā)經(jīng)驗(yàn)。在想,是否可以用C#來解析.pb文件,然后做一個(gè).net core的對(duì)外服務(wù)的API,這樣貌似更加高效,關(guān)鍵是自己熟悉這款的開發(fā),不用花費(fèi)太多的時(shí)間去摸索。、

具體的思路

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

具體的實(shí)現(xiàn)

直接上代碼,非常簡單,本身設(shè)計(jì)到tensorflowsharp的地方非常的少

var graph = new TFGraph();
//重點(diǎn)是下面的這句,把訓(xùn)練好的pb文件給讀出來字節(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è)類庫是tensorflowSharp官方的例子中一個(gè)把圖片轉(zhuǎn)成tensor的類庫,我直接copy過來了,根據(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é)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • 聊聊C# 中HashTable與Dictionary的區(qū)別說明

    聊聊C# 中HashTable與Dictionary的區(qū)別說明

    這篇文章主要介紹了聊聊C# 中HashTable與Dictionary的區(qū)別說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • 淺談c# 浮點(diǎn)數(shù)計(jì)算

    淺談c# 浮點(diǎn)數(shù)計(jì)算

    本文通過具體的示例給大家演示了下C#中浮點(diǎn)數(shù)運(yùn)算所遇到的問題及解決方法,有需要的小伙伴可以參考下
    2017-09-09
  • C# wx獲取token的基本方法

    C# wx獲取token的基本方法

    這篇文章主要為大家詳細(xì)介紹了C# wx獲取token的基本方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • C#中TransactionScope的使用小結(jié)

    C#中TransactionScope的使用小結(jié)

    本篇文章主要是對(duì)C#中TransactionScope的使用方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助
    2014-01-01
  • c#中值類型和引用類型的基礎(chǔ)教程

    c#中值類型和引用類型的基礎(chǔ)教程

    這篇文章主要給大家介紹了關(guān)于c#中值類型和引用類型的基礎(chǔ)教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • C#委托用法詳解

    C#委托用法詳解

    本文詳細(xì)講解了C#中委托的用法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • C#字符串中去除多余的空格保留一個(gè)的實(shí)例

    C#字符串中去除多余的空格保留一個(gè)的實(shí)例

    下面小編就為大家分享一篇C#字符串中去除多余的空格保留一個(gè)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • 快速學(xué)習(xí)C# 設(shè)計(jì)模式之職責(zé)鏈模式

    快速學(xué)習(xí)C# 設(shè)計(jì)模式之職責(zé)鏈模式

    這篇文章主要介紹了C# 設(shè)計(jì)模式之職責(zé)鏈模式的的相關(guān)資料,文中代碼非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • WPF輕松實(shí)現(xiàn)進(jìn)度條的示例代碼

    WPF輕松實(shí)現(xiàn)進(jìn)度條的示例代碼

    WPF中的ProgressBar控件用于表示任務(wù)進(jìn)度,適用于文件下載、數(shù)據(jù)處理等場景,本文將通過XAML和C#代碼展示如何創(chuàng)建一個(gè)基本的WPF進(jìn)度條,并演示如何通過事件處理程序更新進(jìn)度條的值來模擬耗時(shí)操作,感興趣的小伙伴跟著小編一起來看看吧
    2024-12-12
  • C#實(shí)現(xiàn)歸并排序

    C#實(shí)現(xiàn)歸并排序

    這篇文章介紹了C#實(shí)現(xiàn)歸并排序的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04

最新評(píng)論

广灵县| 囊谦县| 达州市| 金门县| 金门县| 仲巴县| 建水县| 绥棱县| 潍坊市| 乐东| 长垣县| 肥乡县| 灌云县| 前郭尔| 南丰县| 南宁市| 宁远县| 沁阳市| 富宁县| 邵阳县| 龙南县| 漠河县| 林西县| 文化| 苏尼特左旗| 五河县| 余姚市| 庐江县| 辽阳市| 遵义市| 广汉市| 象山县| 萨迦县| 苍溪县| 海淀区| 比如县| 砀山县| 台东市| 涪陵区| 行唐县| 宝山区|