/// <summary> /// Given an original image and a transformed one, we get back one that /// has the same dimensions the original and if we transform it we get back image, i.e: /// <code> UnTransform(orig, Transform(orig)) = orig </code> /// </summary> /// <param name="orig">The original image.</param> /// <param name="image">The transformed image.</param> /// <returns>An image satisfying the above equation.</returns> public Vector <double> UnTransform(Vector <double> orig, Vector <double> image) { Vector <double> ret = DenseVector.Create(orig.Count(), 0.0); orig.CopyTo(ret); // Now ret is a copy of orig, so simply overwrite the relevant pixels int center_row = inputCoordinates_.RowCount / 2; int center_col = inputCoordinates_.ColumnCount / 2; int topleft_row = center_row - cropSize_ / 2; int topleft_col = center_col - cropSize_ / 2; for (int channel = 0; channel < inputCoordinates_.ChannelCount; channel++) { for (int i = 0; i < cropSize_; i++) { for (int j = 0; j < cropSize_; j++) { int input_idx = inputCoordinates_.GetIndex(channel, topleft_row + i, topleft_col + j); int output_idx = outputCoordinates_.GetIndex(channel, i, j); if (input_idx >= 0 && input_idx < inputDimension_ && output_idx >= 0 && output_idx < outputDimension_) { ret[input_idx] = image[output_idx]; } } } } return(ret); }
public List <double[]> Augment(double[] datum) { List <double[]> newdatums = new List <double[]>(); for (int i = 0; i < _how_many; i++) { var newdatum = new double[datum.Length]; // Sample epsilon double eps = (_typ == RANDTYPE.UNIFORM) ? (_random.NextDouble() * 2.0 - 1.0) : Utils.URand.NextGaussian(_random); for (int c = 0; c < _coords.ChannelCount; c++) { for (int x = 0; x < _coords.RowCount; x++) { for (int y = 0; y < _coords.ColumnCount; y++) { int xnew = (int)(x + eps * _xoffset); int ynew = (int)(y + eps * _yoffset); if (xnew < 0 || xnew >= _coords.RowCount) { xnew = x; } if (ynew < 0 || ynew >= _coords.ColumnCount) { ynew = y; } int idx = _coords.GetIndex(c, x, y); int newidx = _coords.GetIndex(c, xnew, ynew); newdatum[newidx] = datum[idx]; } } } // Utils.UDraw.DisplayImageAndPause(Utils.UArray.ToRGBArray(datum, 1.0, 0.0), 32, 32, true); // Utils.UDraw.DisplayImageAndPause(Utils.UArray.ToRGBArray(newdatum, 1.0, 0.0), 32, 32, true); newdatums.Add(newdatum); } return(newdatums); }
public List <double[]> Augment(double[] datum) { List <double[]> newdatums = new List <double[]>(); // How many to generate for (int i = 0; i < _how_many; i++) { // Allocate data var newdatum = new double[datum.Length]; // Sample epsilon double eps = (_typ == RANDTYPE.UNIFORM) ? (_random.NextDouble() * 2.0 - 1.0) * _max_eps: Utils.URand.NextGaussian(_random) * _max_eps; // Add constant epsilon to all channels. for (int c = 0; c < _coords.ChannelCount; c++) { for (int x = 0; x < _coords.RowCount; x++) { for (int y = 0; y < _coords.ColumnCount; y++) { int idx = _coords.GetIndex(c, x, y); newdatum[idx] = Utils.UMath.Clamp(datum[idx] + eps, Utils.RobustnessOptions.MinValue, Utils.RobustnessOptions.MaxValue); } } } newdatums.Add(newdatum); } return(newdatums); }
public double[] ChannelAverages(double[] datum) { double[] rets = new double[_coords.ChannelCount]; for (int c = 0; c < _coords.ChannelCount; c++) { for (int x = 0; x < _coords.RowCount; x++) { for (int y = 0; y < _coords.ColumnCount; y++) { int idx = _coords.GetIndex(c, x, y); rets[c] += datum[idx]; } } } return(rets); }