public static RecogReply PredictionFunc(Guid id, int timeBudgetInMS, RecogRequest req) { byte[] imgBuf = req.Data; byte[] imgType = System.Text.Encoding.UTF8.GetBytes("jpg"); Guid imgID = BufferCache.HashBufferAndType(imgBuf, imgType); string imgFileName = imgID.ToString() + ".jpg"; string filename = Path.Combine(saveImageDir, imgFileName); if (!File.Exists(filename)) { FileTools.WriteBytesToFileConcurrent(filename, imgBuf); } Stopwatch timer = Stopwatch.StartNew(); CaffeModel.SetDevice(gpu); string resultString = predictor.Predict(filename); timer.Stop(); File.Delete(filename); numImageRecognized++; Console.WriteLine("Image {0}:{1}:{2}: {3}", numImageRecognized, imgFileName, timer.Elapsed, resultString); return(VHubRecogResultHelper.FixedClassificationResult(resultString, resultString)); }
public static bool InitializeRecognizer(VHubBackendStartParam pa) { var bInitialized = true; var x = ImageCaptionInstance.Current; if (!Object.ReferenceEquals(x, null)) { /// <remarks> /// To implement your own image recognizer, please obtain a connection Guid by contacting [email protected] /// </remarks> x.RegisterAppInfo(new Guid("843EF294-C635-42DA-9AD8-E79E82F9A357"), "0.0.0.1"); Func <Guid, int, RecogRequest, RecogReply> del = PredictionFunc; /// <remarks> /// Register your prediction function here. /// </remarks> string exeDir = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); x.RegisterClassifierCS("#ImageCaption", Path.Combine(exeDir, "logo.jpg"), 100, del); string wordDetectorProto = Path.Combine(rootDir, @"Model\wordDetector.proto"); string wordDetectorModel = Path.Combine(rootDir, @"Model\wordDetector.caffemodel"); string featureDetectorProto = Path.Combine(rootDir, @"Model\FeaturesDetector.proto"); string featureDetectorModel = Path.Combine(rootDir, @"Model\FeaturesDetector.caffemodel"); string thresholdFile = Path.Combine(rootDir, @"Model\thresholds.txt"); string wordLabelMapFile = Path.Combine(rootDir, @"Model\labelmap.txt"); string languageModel = Path.Combine(rootDir, @"Model\sentencesGeneration.lblmmodel"); string referenceDssmModelFile = Path.Combine(rootDir, @"Model\reference.dssmmodel"); string candidateDssmModelFile = Path.Combine(rootDir, @"Model\candidate.dssmmodel"); string trigramFile = Path.Combine(rootDir, @"Model\coco.cap.l3g"); CaffeModel.SetDevice(gpu); //This needs to be set before instantiating the net var lmTestArguments = new LangModel.Arguments { NumWorkers = 8, MaxSentenceLength = 19, NumSentences = 500, BeamWidth = 200, AttributesCoverageBar = 5, KTopWords = 100 }; predictor = new Predictor(wordDetectorProto, wordDetectorModel, featureDetectorProto, featureDetectorModel, thresholdFile, wordLabelMapFile, languageModel, lmTestArguments, referenceDssmModelFile, candidateDssmModelFile, trigramFile); } else { bInitialized = false; } return(bInitialized); }
public void Init(string protoFile, string modelFile, string meanFile, string labelMapFile, string entityInfoFile, int gpu) { // Init caffe model CaffeModel.SetDevice(gpu); _caffeModel = new CaffeModel(protoFile, modelFile); _caffeModel.SetMeanFile(meanFile); Console.WriteLine("Succeed: Load Model File!\n"); _labelMap = File.ReadAllLines(labelMapFile) .Select(line => line.Split('\t')[0]) .ToArray(); if (!string.IsNullOrEmpty(entityInfoFile)) { _entityInfo = File.ReadLines(entityInfoFile) .Select(line => line.Split('\t')) .ToDictionary(cols => cols[0], cols => cols[1]); } }