/// <summary> /// loads the domain information /// </summary> private void LoadDomainInfo() { var localSetting = GetStorageSetting(); CurrentDomain = null; try { if (localSetting.Values.ContainsKey(GatewayHttpInterface.DomainKey)) { var buf = localSetting.Values[GatewayHttpInterface.DomainKey] as byte[]; CurrentDomain = GatewayHttpInterface.DecodeFromBytes <RecogInstance>(buf); VMHub.CurrentDomain = CurrentDomain; } else { CurrentDomain = null; VMHub.CurrentDomain = null; } } catch (Exception e) { Debug.WriteLine("LoadDomainInfo fails with exception {0}", e); // Swallow exception if the store can not be used. } }
/// <summary> /// Load information of gateway for Local Storage /// </summary> public void LoadGatewayInfo() { var localSetting = GetStorageSetting(); try { if (localSetting.Values.ContainsKey(GatewayHttpInterface.GatewayCollectionKey)) { var buf = localSetting.Values[GatewayHttpInterface.GatewayCollectionKey] as byte[]; var gatewayCollection = GatewayHttpInterface.DecodeFromBytes <OneServerInfo[]>(buf); foreach (var info in gatewayCollection) { GatewayCollection.GetOrAdd(info.HostName, info.HostInfo); } } } catch (Exception e) { Debug.WriteLine("LoadGatewayInfo fails with exception {0}", e); // Swallow exception if the store can not be used. } if (localSetting.Values.ContainsKey(GatewayHttpInterface.GatewayKey)) { CurrentGateway = localSetting.Values[GatewayHttpInterface.GatewayKey].ToString(); } else { CurrentGateway = App.DefaultGateway; } GatewayCollection.GetOrAdd(this.CurrentGateway, "Default"); VMHub.CurrentGateway = this.CurrentGateway; }
/// <summary> /// saves the domain information /// </summary> internal void SaveDomainInfo() { if (!Object.ReferenceEquals(CurrentDomain, null)) { var localSetting = GetStorageSetting(); localSetting.Values[GatewayHttpInterface.DomainKey] = GatewayHttpInterface.EncodeToBytes <RecogInstance>(CurrentDomain); VMHub.CurrentDomain = CurrentDomain; } }
/// <summary> /// saves the provider information /// </summary> internal void SaveProviderInfo() { if (!Object.ReferenceEquals(CurrentProvider, null)) { var localSetting = GetStorageSetting(); localSetting.Values[GatewayHttpInterface.ProviderKey] = GatewayHttpInterface.EncodeToBytes <RecogEngine>(CurrentProvider); VMHub.CurrentProvider = CurrentProvider; } }
/// <summary> /// Save information of gateway to local storage /// </summary> /// <param name="gateway"></param> /// <param name="gatewayCollection"></param> public void SaveGatewayInfo(String gateway, List <OneServerInfo> gatewayCollection) { var localSetting = GetStorageSetting(); localSetting.Values[GatewayHttpInterface.GatewayKey] = gateway; var arr = gatewayCollection.ToArray(); localSetting.Values[GatewayHttpInterface.GatewayCollectionKey] = GatewayHttpInterface.EncodeToBytes <OneServerInfo[]>(arr); CurrentGateway = gateway; VMHub.CurrentGateway = gateway; GatewayCollection.Clear(); foreach (var info in gatewayCollection) { GatewayCollection.GetOrAdd(info.HostName, info.HostInfo); } GatewayCollection.GetOrAdd(this.CurrentGateway, "Default"); }
public PrajnaHub(string Gateway, Guid CustomerID, string CustomerKey) { Hub = new GatewayHttpInterface(Gateway, CustomerID, CustomerKey); bInitialized = true; }
public async Task Eval(int concurrency, CancellationToken cancelToken, bool isResume) { if (!Directory.Exists(evalSetting.evaluationLogDir)) { Directory.CreateDirectory(evalSetting.evaluationLogDir); } timeStart = DateTime.UtcNow; string logDetail = null; if (isResume) { logDetail = GetLastUnfinishedLog(serviceGuid); } if (string.IsNullOrEmpty(logDetail)) { logDetail = Path.Combine(evalSetting.evaluationLogDir, string.Format("{0}_{1}.log", serviceGuid.ToString(), timeStart.ToString("yyyyMMdd_HHmmss"))); } else { Console.WriteLine("{0}: resume from {1}", serviceGuid, logDetail); } if (!File.Exists(evalSetting.measurementSetCountFile)) { Console.WriteLine("Count total number of images in the measurement set"); int count = File.ReadLines(evalSetting.measurementSetFile).Count(); File.WriteAllText(evalSetting.measurementSetCountFile, count.ToString()); } TotalNumOfImages = Convert.ToInt32(File.ReadLines(evalSetting.measurementSetCountFile).First()); Console.WriteLine("{0}: total images to evaluate: {1}", serviceGuid, TotalNumOfImages); int colImageKey = evalSetting.columns["imagekey"]; int colImageData = evalSetting.columns["imagedata"]; int colRecogResult = colImageData; TriedNumOfImages = 0; double minutes_to_wait = 1; int global_retry = 0; for (global_retry = 0; global_retry < evalSetting.maxGlobalRetry; global_retry++) { HashSet <string> succeededImageKeys; if (File.Exists(logDetail)) { var cols_expected = File.ReadLines(evalSetting.measurementSetFile) .Select(line => line.Split('\t').Length) .First() + 1; // +1 for time stamp in log file succeededImageKeys = new HashSet <string>(File.ReadLines(logDetail) .Select(line => line.Split('\t')) .Where(cols => cols.Length == cols_expected) // filter corrupted lines due to break and resume .Where(cols => !cols[colRecogResult].StartsWith(SystemError)) .Select(cols => cols[colImageKey]) .Distinct(), StringComparer.Ordinal); SucceededNumOfImages = succeededImageKeys.Count(); Console.WriteLine("{0}: succeeded images: {1}", serviceGuid, SucceededNumOfImages); } else { succeededImageKeys = new HashSet <string>(); } if (succeededImageKeys.Count() >= TotalNumOfImages) { break; } if (global_retry > 0) { //double minutes_to_wait = Math.Min(evalSetting.maxWaitMins, 1.0 * Math.Pow(2, global_retry - 1)); double minutes = Math.Min(evalSetting.maxWaitMins, minutes_to_wait); Console.WriteLine("{0}: retry {1}, wait for {2:F2} minutes...", serviceGuid, global_retry, minutes); await Task.Delay(TimeSpan.FromMinutes(minutes), cancelToken); minutes_to_wait *= 2; } GatewayHttpInterface vmHub = new GatewayHttpInterface(gateWay, Guid.Empty, "SecretKeyShouldbeLongerThan10"); // run evaluation in parallel var lines = File.ReadLines(evalSetting.measurementSetFile) .AsParallel().AsOrdered().WithDegreeOfParallelism(concurrency) .Select(line => line.Split('\t')) .Where(cols => !succeededImageKeys.Contains(cols[colImageKey])) //.Where(cols => !string.IsNullOrEmpty(cols[1])) .Select(async cols => { if (cancelToken.IsCancellationRequested) { cols[colRecogResult] = string.Empty; return(cols); } byte[] imageData = Convert.FromBase64String(cols[colImageData]); string result = string.Empty; for (int local_retry = 0; local_retry < 3; local_retry++) { try { result = await vmHub.ProcessAsyncString(Guid.Empty, Guid.Empty, Guid.Parse(serviceGuid), Guid.Empty, Guid.Empty, imageData); result = result.Trim(); if (result.IndexOf("return 0B.") >= 0) { result = SystemError + ": " + result; } } catch (Exception) { // timeout or other system error Console.WriteLine("\n{0}: Fails to get a reply: {1}", SystemError, serviceGuid); result = SystemError + ": Fails to get a reply"; // SystemError + e.Message; } if (!result.StartsWith(SystemError)) { break; } } if (!result.StartsWith(SystemError)) { Interlocked.Increment(ref SucceededNumOfImages); minutes_to_wait = 1; } Interlocked.Increment(ref TriedNumOfImages); Console.Write("Lines processed: {0}\r", TriedNumOfImages); //Console.WriteLine("{0}: {1}", ProcessedNumOfImages, result); cols[colRecogResult] = result; return(cols); }) .Select(cols => string.Join("\t", cols.Result) + "\t" + DateTime.UtcNow.ToString("yyyyMMdd_HHmmss.fff")); using (var sw = File.AppendText(logDetail)) { int count = 0; foreach (var line in lines) { if (cancelToken.IsCancellationRequested) { Console.WriteLine("Cancel requested. Lines logged: {0}", count); sw.Flush(); break; } sw.WriteLine(line); if (++count % 100 == 0) { sw.Flush(); } } } if (cancelToken.IsCancellationRequested) { break; } } DateTime timeEnd = DateTime.UtcNow; // write to log try { logWriteLock.AcquireWriterLock(1000 * 600); // wait up to 10 minutes using (var sw = File.AppendText(evalSetting.measurementResultFile)) { //sw.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}", serviceGuid, timeStart, timeEnd, accuracies.Count(), SucceededNumOfImages, top1_acc, top5_acc); TimeSpan span = timeEnd - timeStart; string log_msg = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}", serviceGuid, Path.GetFileName(logDetail), timeStart, timeEnd, global_retry, TriedNumOfImages, SucceededNumOfImages, TriedNumOfImages / span.TotalSeconds); sw.WriteLine(log_msg); Console.WriteLine(); Console.WriteLine(log_msg); } logWriteLock.ReleaseLock(); } catch (ApplicationException) { Console.WriteLine("Time out in getting write permission for eval result file: {0}", evalSetting.measurementResultFile); } }