日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

使用ML.NET实现猜动画片台词

發(fā)布時(shí)間:2024/4/14 asp.net 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用ML.NET实现猜动画片台词 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前面幾篇主要內(nèi)容出自微軟官方,經(jīng)我特意修改的案例的文章:

使用ML.NET實(shí)現(xiàn)情感分析[新手篇]

使用ML.NET預(yù)測(cè)紐約出租車費(fèi)

.NET Core玩轉(zhuǎn)機(jī)器學(xué)習(xí)

使用ML.NET實(shí)現(xiàn)情感分析[新手篇]后補(bǔ)

相信看過后大家對(duì)ML.NET有了一定的了解了,由于目前還是0.1的版本,也沒有更多官方示例放出來,大家普遍覺得提供的特性還不夠強(qiáng)大,所以處在觀望狀態(tài)也是能理解的。

本文結(jié)合Azure提供的語音識(shí)別服務(wù),向大家展示另一種ML.NET有趣的玩法——猜動(dòng)畫片臺(tái)詞。

這個(gè)場(chǎng)景特別容易想像,是一種你說我猜的游戲,我會(huì)事先用ML.NET對(duì)若干動(dòng)畫片的臺(tái)詞進(jìn)行分類學(xué)習(xí),然后使用麥克風(fēng),讓使用者隨便說一句動(dòng)畫片的臺(tái)詞(當(dāng)然得是數(shù)據(jù)集中已存在的,沒有的不要搞事情呀!),然后來預(yù)測(cè)出自哪一部。跟隨我動(dòng)手做做看。

準(zhǔn)備工作


這次需要使用Azure的認(rèn)知服務(wù)中一項(xiàng)API——Speaker Recognition,目前還處于免費(fèi)試用階段,打開https://azure.microsoft.com/zh-cn/try/cognitive-services/?api=speaker-recognition,能看到如下頁(yè)面:

點(diǎn)擊獲取API密鑰,用自己的Azure賬號(hào)登錄,然后就能看到自己的密鑰了,類似如下圖:

?

創(chuàng)建項(xiàng)目


這一次請(qǐng)注意,我們要?jiǎng)?chuàng)建一個(gè).NET Framework 4.6.1或以上版本的控制臺(tái)應(yīng)用程序,通過NuGet分別引用三個(gè)類庫(kù):Microsoft.ML,JiebaNet.Analyser,Microsoft.CognitiveServices.Speech。

然后把編譯平臺(tái)修改成x64,而不是Any CPU。(這一點(diǎn)非常重要)

?

代碼分解


在Main函數(shù)部分,我們只需要關(guān)心幾個(gè)主要步驟,先切詞,然后訓(xùn)練模型,最后在一個(gè)循環(huán)中等待使用者說話,用模型進(jìn)行預(yù)測(cè)。

static void Main(string[] args) {Segment(_dataPath, _dataTrainPath);var model = Train();Evaluate(model);ConsoleKeyInfo x;do{var speech = Recognize();speech.Wait();Predict(model, speech.Result);Console.WriteLine("\nRecognition done. Your Choice (0: Stop Any key to continue): ");x = Console.ReadKey(true);} while (x.Key != ConsoleKey.D0); }

初始化的變量主要就是訓(xùn)練數(shù)據(jù),Azure語音識(shí)別密鑰等。注意YourServiceRegion的值是“westus”,而不是網(wǎng)址。

const string SubscriptionKey = "你的密鑰"; const string YourServiceRegion = "westus"; const string _dataPath = @".\data\dubs.txt"; const string _dataTrainPath = @".\data\dubs_result.txt";

定義數(shù)據(jù)結(jié)構(gòu)和預(yù)測(cè)結(jié)構(gòu)和我之前的文章一樣,沒有什么特別之處。

public class DubbingData {[Column(ordinal: "0")]public string DubbingText;[Column(ordinal: "1", name: "Label")]public string Label; }public class DubbingPrediction {[ColumnName("PredictedLabel")]public string PredictedLabel; }

?切記部分注意對(duì)分隔符的過濾。

public static void Segment(string source, string result) {var segmenter = new JiebaSegmenter();using (var reader = new StreamReader(source)){using (var writer = new StreamWriter(result)){while (true){var line = reader.ReadLine();if (string.IsNullOrWhiteSpace(line))break;var parts = line.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);if (parts.Length != 2) continue;var segments = segmenter.Cut(parts[0]);writer.WriteLine("{0}\t{1}", string.Join(" ", segments), parts[1]);}}} }

訓(xùn)練部分依然使用熟悉的多分類訓(xùn)練器StochasticDualCoordinateAscentClassifier。TextFeaturizer用于對(duì)文本內(nèi)容向量化處理。

public static PredictionModel<DubbingData, DubbingPrediction> Train() {var pipeline = new LearningPipeline();pipeline.Add(new TextLoader<DubbingData>(_dataTrainPath, useHeader: false, separator: "tab"));pipeline.Add(new TextFeaturizer("Features", "DubbingText"));pipeline.Add(new Dictionarizer("Label"));pipeline.Add(new StochasticDualCoordinateAscentClassifier());pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" });var model = pipeline.Train<DubbingData, DubbingPrediction>();return model; }

驗(yàn)證部分這次重點(diǎn)是看損失程度分?jǐn)?shù)。

public static void Evaluate(PredictionModel<DubbingData, DubbingPrediction> model) {var testData = new TextLoader<DubbingData>(_dataTrainPath, useHeader: false, separator: "tab");var evaluator = new ClassificationEvaluator();var metrics = evaluator.Evaluate(model, testData);Console.WriteLine();Console.WriteLine("PredictionModel quality metrics evaluation");Console.WriteLine("------------------------------------------");//Console.WriteLine($"TopKAccuracy: {metrics.TopKAccuracy:P2}");Console.WriteLine($"LogLoss: {metrics.LogLoss:P2}"); }

預(yù)測(cè)部分沒有什么大變化,就是對(duì)中文交互進(jìn)行了友好展示。

public static void Predict(PredictionModel<DubbingData, DubbingPrediction> model, string sentence) {IEnumerable<DubbingData> sentences = new[]{new DubbingData{DubbingText = sentence}};var segmenter = new JiebaSegmenter();foreach (var item in sentences){item.DubbingText = string.Join(" ", segmenter.Cut(item.DubbingText));}IEnumerable<DubbingPrediction> predictions = model.Predict(sentences);Console.WriteLine();Console.WriteLine("Category Predictions");Console.WriteLine("---------------------");var sentencesAndPredictions = sentences.Zip(predictions, (sentiment, prediction) => (sentiment, prediction));foreach (var item in sentencesAndPredictions){Console.WriteLine($"臺(tái)詞: {item.sentiment.DubbingText.Replace(" ", string.Empty)} | 來自動(dòng)畫片: {item.prediction.PredictedLabel}");}Console.WriteLine(); }

Azure語音識(shí)別的調(diào)用如下。

static async Task<string> Recognize() {var factory = SpeechFactory.FromSubscription(SubscriptionKey, YourServiceRegion);var lang = "zh-cn";using (var recognizer = factory.CreateSpeechRecognizer(lang)){Console.WriteLine("Say something...");var result = await recognizer.RecognizeAsync().ConfigureAwait(false);if (result.RecognitionStatus != RecognitionStatus.Recognized){Console.WriteLine($"There was an error. Status:{result.RecognitionStatus.ToString()}, Reason:{result.RecognitionFailureReason}");return null;}else{Console.WriteLine($"We recognized: {result.RecognizedText}");return result.RecognizedText;}} }

運(yùn)行過程如下:

雖然這看上去有點(diǎn)幼稚,不過一樣讓你開心一笑了,不是么?請(qǐng)期待更多有趣的案例。

本文使用的數(shù)據(jù)集:下載

完整的代碼如下:

using System; using Microsoft.ML.Models; using Microsoft.ML.Runtime; using Microsoft.ML.Runtime.Api; using Microsoft.ML.Trainers; using Microsoft.ML.Transforms; using System.Collections.Generic; using System.Linq; using Microsoft.ML; using JiebaNet.Segmenter; using System.IO; using Microsoft.CognitiveServices.Speech; using System.Threading.Tasks;namespace DubbingRecognition {class Program{public class DubbingData{[Column(ordinal: "0")]public string DubbingText;[Column(ordinal: "1", name: "Label")]public string Label;}public class DubbingPrediction{[ColumnName("PredictedLabel")]public string PredictedLabel;}const string SubscriptionKey = "你的密鑰";const string YourServiceRegion = "westus";const string _dataPath = @".\data\dubs.txt";const string _dataTrainPath = @".\data\dubs_result.txt";static void Main(string[] args){Segment(_dataPath, _dataTrainPath);var model = Train();Evaluate(model);ConsoleKeyInfo x;do{var speech = Recognize();speech.Wait();Predict(model, speech.Result);Console.WriteLine("\nRecognition done. Your Choice (0: Stop Any key to continue): ");x = Console.ReadKey(true);} while (x.Key != ConsoleKey.D0);}public static void Segment(string source, string result){var segmenter = new JiebaSegmenter();using (var reader = new StreamReader(source)){using (var writer = new StreamWriter(result)){while (true){var line = reader.ReadLine();if (string.IsNullOrWhiteSpace(line))break;var parts = line.Split(new[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);if (parts.Length != 2) continue;var segments = segmenter.Cut(parts[0]);writer.WriteLine("{0}\t{1}", string.Join(" ", segments), parts[1]);}}}}public static PredictionModel<DubbingData, DubbingPrediction> Train(){var pipeline = new LearningPipeline();pipeline.Add(new TextLoader<DubbingData>(_dataTrainPath, useHeader: false, separator: "tab"));//pipeline.Add(new ColumnConcatenator("Features", "DubbingText")); pipeline.Add(new TextFeaturizer("Features", "DubbingText"));//pipeline.Add(new TextFeaturizer("Label", "Category"));pipeline.Add(new Dictionarizer("Label"));pipeline.Add(new StochasticDualCoordinateAscentClassifier());pipeline.Add(new PredictedLabelColumnOriginalValueConverter() { PredictedLabelColumn = "PredictedLabel" });var model = pipeline.Train<DubbingData, DubbingPrediction>();return model;}public static void Evaluate(PredictionModel<DubbingData, DubbingPrediction> model){var testData = new TextLoader<DubbingData>(_dataTrainPath, useHeader: false, separator: "tab");var evaluator = new ClassificationEvaluator();var metrics = evaluator.Evaluate(model, testData);Console.WriteLine();Console.WriteLine("PredictionModel quality metrics evaluation");Console.WriteLine("------------------------------------------");//Console.WriteLine($"TopKAccuracy: {metrics.TopKAccuracy:P2}");Console.WriteLine($"LogLoss: {metrics.LogLoss:P2}");}public static void Predict(PredictionModel<DubbingData, DubbingPrediction> model, string sentence){IEnumerable<DubbingData> sentences = new[]{new DubbingData{DubbingText = sentence}};var segmenter = new JiebaSegmenter();foreach (var item in sentences){item.DubbingText = string.Join(" ", segmenter.Cut(item.DubbingText));}IEnumerable<DubbingPrediction> predictions = model.Predict(sentences);Console.WriteLine();Console.WriteLine("Category Predictions");Console.WriteLine("---------------------");var sentencesAndPredictions = sentences.Zip(predictions, (sentiment, prediction) => (sentiment, prediction));foreach (var item in sentencesAndPredictions){Console.WriteLine($"臺(tái)詞: {item.sentiment.DubbingText.Replace(" ", string.Empty)} | 來自動(dòng)畫片: {item.prediction.PredictedLabel}");}Console.WriteLine();}static async Task<string> Recognize(){var factory = SpeechFactory.FromSubscription(SubscriptionKey, YourServiceRegion);var lang = "zh-cn";using (var recognizer = factory.CreateSpeechRecognizer(lang)){Console.WriteLine("Say something...");var result = await recognizer.RecognizeAsync().ConfigureAwait(false);if (result.RecognitionStatus != RecognitionStatus.Recognized){Console.WriteLine($"There was an error. Status:{result.RecognitionStatus.ToString()}, Reason:{result.RecognitionFailureReason}");return null;}else{Console.WriteLine($"We recognized: {result.RecognizedText}");return result.RecognizedText;}}}} }

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/BeanHsiang/p/9052751.html

總結(jié)

以上是生活随笔為你收集整理的使用ML.NET实现猜动画片台词的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。