namespace ZeroLevel.NN.Models { public class ImagePreprocessorOptions { private const float PIXEL_NORMALIZATION_SCALE = 1f / 255f; public ImagePreprocessorOptions(int inputWidth, int inputHeight, PredictorChannelType channelType) { this.InputWidth = inputWidth; this.InputHeight = inputHeight; this.ChannelType = channelType; } public ImagePreprocessorOptions UseCrop(int width, int height, bool saveOriginal, bool overlap) { Crop.Enabled = true; Crop.Height = height; Crop.Width = width; Crop.Overlap = overlap; Crop.SaveOriginal = saveOriginal; return this; } public ImagePreprocessorOptions ApplyNormilization(float? multiplier = null) { if (multiplier.HasValue) { NormalizationMultiplier = multiplier.Value; } this.Normalize = true; return this; } public ImagePreprocessorOptions ApplyAxeInversion() { this.InvertXY = true; return this; } public ImagePreprocessorOptions ApplyCorrection(float[] mean, float[] std) { if (this.Correction) { throw new InvalidOperationException("Correction setup already"); } this.Correction = true; this.Mean = mean; this.Std = std; return this; } public ImagePreprocessorOptions ApplyCorrection(Func correctionFunc) { if (this.Correction) { throw new InvalidOperationException("Correction setup already"); } this.Correction = true; this.CorrectionFunc = correctionFunc; return this; } public ImagePreprocessorOptions UseBGR() { this.BGR = true; return this; } public float NormalizationMultiplier { get; private set; } = PIXEL_NORMALIZATION_SCALE; /// /// Channel type, if first tensor dims = [batch_index, channel, x, y], if last, dims = dims = [batch_index, x, y, channel] /// public PredictorChannelType ChannelType { get; private set; } /// /// Ctop image options /// public ImagePreprocessorCropOptions Crop { get; } = new ImagePreprocessorCropOptions(); /// /// NN model input height /// public int InputHeight { get; private set; } /// /// NN model input width /// public int InputWidth { get; private set; } /// /// Transfrom pixel values to (0-1) range /// public bool Normalize { get; private set; } = false; /// /// Transform pixel value with mean/std values v=(v-mean)/std /// public bool Correction { get; private set; } = false; /// /// Mean values if Correction parameter is true /// public Func CorrectionFunc { get; private set; } = null; public float[] Mean { get; private set; } /// /// Std values if Correction parameter is true /// public float[] Std { get; private set; } /// /// Put pixel values to tensor in BGR order /// public bool BGR { get; set; } = false; /// /// Invert width and height in input tensor /// public bool InvertXY { get; set; } = false; /// /// Channel count (auto calculate) /// public int Channels { get; set; } /// /// Maximum batch size, decrease if video memory overflow /// public int MaxBatchSize { get; set; } = 13; } }