public static TFTensor TensorFromBitmap(Bitmap image, ImageDims dims) { int INPUT_SIZE = dims.InputSize; int IMAGE_MEAN = dims.ImageMean; float IMAGE_STD = dims.ImageStd; var bitmap = new Bitmap(image, INPUT_SIZE, INPUT_SIZE); Color[] colors = new Color[bitmap.Size.Width * bitmap.Size.Height]; int z = 0; for (int y = bitmap.Size.Height - 1; y >= 0; y--) { for (int x = 0; x < bitmap.Size.Width; x++) { colors[z] = bitmap.GetPixel(x, y); z++; } } float[] floatValues = new float[(INPUT_SIZE * INPUT_SIZE) * 3]; for (int i = 0; i < colors.Length; i++) { var color = colors[i]; floatValues[i * 3] = (color.R - IMAGE_MEAN) / IMAGE_STD; floatValues[i * 3 + 1] = (color.G - IMAGE_MEAN) / IMAGE_STD; floatValues[i * 3 + 2] = (color.B - IMAGE_MEAN) / IMAGE_STD; } TFShape shape = new TFShape(1, INPUT_SIZE, INPUT_SIZE, 3); return(TFTensor.FromBuffer(shape, floatValues, 0, floatValues.Length)); }
///<summary> ///<param>Model graph</param> ///<param>Model labels</param> ///<param>Base module used for training</param> ///<param>Model SDK version</param> ///</summary> public CoachModel(TFGraph graph, string[] labels, string module, float coachVersion) { if (COACH_VERSION != coachVersion) { throw new Exception($"Coach model v{coachVersion} incompatible with SDK version {COACH_VERSION}"); } this.Graph = graph; this.Labels = labels; this.Session = new TFSession(this.Graph); int size = int.Parse(module.Substring(module.Length - 3, 3)); this.ImageDims = new ImageDims(size, 0, 255); }
private static Tensor ToTensor(this Texture2D tex, ImageDims dims) { var pic = tex.GetPixels32(); int INPUT_SIZE = dims.InputSize; int IMAGE_MEAN = dims.ImageMean; float IMAGE_STD = dims.ImageStd; float[] floatValues = new float[(INPUT_SIZE * INPUT_SIZE) * 3]; for (int i = 0; i < pic.Length; i++) { var color = pic[i]; floatValues[i * 3] = (color.r - IMAGE_MEAN) / IMAGE_STD; floatValues[i * 3 + 1] = (color.g - IMAGE_MEAN) / IMAGE_STD; floatValues[i * 3 + 2] = (color.b - IMAGE_MEAN) / IMAGE_STD; } var shape = new TensorShape(1, INPUT_SIZE, INPUT_SIZE, 3); return(new Tensor(shape, floatValues)); }
public static Tensor TensorFromTexture(Texture2D image, ImageDims dims) { image = image.Scale(dims.InputSize, dims.InputSize); return(image.ToTensor(dims)); }