/// <summary> /// Helper function to determine the model version that is being loaded. /// </summary> private static bool CheckModelVersion(ModelLoadContext ctx, VersionInfo versionInfo) { try { ctx.CheckVersionInfo(versionInfo); return(true); } catch (Exception) { //consume return(false); } }
public static FeatureNameCollection Create(ModelLoadContext ctx) { Contracts.AssertValue(ctx); ctx.CheckAtModel(); ctx.CheckVersionInfo(GetVersionInfo()); // *** Binary format *** // int: number of features (size) // int: number of indices (0 if dense) // int[]: indices (if sparse) // int[]: ids of names (matches either number of features or number of indices var size = ctx.Reader.ReadInt32(); Contracts.CheckDecode(size >= 0); var isize = ctx.Reader.ReadInt32(); Contracts.CheckDecode(isize >= -1); if (isize < 0) { // Dense case var names = new string[size]; for (int i = 0; i < size; i++) { var name = ctx.LoadStringOrNull(); names[i] = string.IsNullOrEmpty(name) ? null : name; } return(Create(size, names)); } var dict = new Dictionary <int, string>(); var indices = new int[isize]; var prev = -1; for (int ii = 0; ii < isize; ii++) { indices[ii] = ctx.Reader.ReadInt32(); Contracts.CheckDecode(prev < indices[ii]); prev = indices[ii]; } Contracts.CheckDecode(prev < size); for (int ii = 0; ii < isize; ii++) { var name = ctx.LoadStringOrNull(); if (!string.IsNullOrEmpty(name)) { dict.Add(indices[ii], name); } } return(Create(size, dict)); }