public static ndarray Normalize(ndarray x, int axis = -1, int order = 2) { var l2 = np.atleast_1d(nd.Norm(x, order, axis)).FirstOrDefault(); l2[l2 == 0] = 1; return((ndarray)(x / np.expand_dims(l2, axis))); }
public static ndarray ToCategorial(ndarray y, int?num_classes = null, dtype dtype = null) { var input_shape = y.shape; List <long> new_shape = new List <long>(); if (input_shape.lastDim == 1 && input_shape.iDims.Length > 1) { new_shape = input_shape.iDims.Take(input_shape.iDims.Length - 1).ToList(); } else { new_shape = input_shape.iDims.ToList(); } y = y.ravel(); if (num_classes.HasValue) { num_classes = (int)np.max(y) + 1; } var n = y.shape.iDims[0]; var categorical = np.zeros((n, num_classes), dtype: dtype); categorical[np.arange(n), y] = 1; new_shape.Add(num_classes.Value); var output_shape = new shape(new_shape); categorical = np.reshape(categorical, output_shape); return(categorical); }
private static ndarray to_channels_first_helper(ndarray np_data) { var data_format = MxNetBackend.ImageDataFormat(); if (!new string[] { "channels_first", "channels_last" }.Contains(data_format)) { throw new ValueError("Unknown data_format " + data_format.ToString()); } var shape = np_data.shape; if (shape.iDims.Length == 5) { np_data = np.transpose(np_data, new long[] { 0, 4, 1, 2, 3 }); } else if (shape.iDims.Length == 4) { np_data = np.transpose(np_data, new long[] { 0, 3, 1, 2 }); } else if (shape.iDims.Length == 3) { throw new ValueError("Your data is either a textual data of shape `(num_sample, step, feature)` or a grey scale image of shape `(num_sample, rows, cols)`. Case 1: If your data is time-series or a textual data(probably you are using Conv1D), then there is no need of channel conversion.Case 2: If your data is image(probably you are using Conv2D), then you need to reshape the tension dimensions as follows:`shape = x_input.shape``x_input = x_input.reshape(shape[0], 1, shape[1], shape[2])`Note: Do not use `to_channels_fir()` in above cases."); } else { throw new ValueError("Your input dimension tensor is incorrect."); } return(np_data); }
public static ObjectDetectionResult[] Detect(dynamic detector, Size supportedSize, Image <Rgb24> image) { if (detector is null) { throw new ArgumentNullException(nameof(detector)); } if (image is null) { throw new ArgumentNullException(nameof(image)); } var input = ImageTools.YoloPreprocess(new ObjectDetectionDataset.ClrEntry { Image = image.Clone(), }, supportedSize); var images = input.Image[np.newaxis, np.rest_of_the_axes].AsArray(); IDictionary <string, Tensor> prediction = detector(tf.constant(images)); _ArrayLike Get(string name) => prediction["tf_op_layer_" + name].numpy(); ndarray <float> boxs = Get(nameof(SelectedBoxesOutput.Boxes)).AsArray <float>(); ndarray <float> scores = Get(nameof(SelectedBoxesOutput.Scores)).AsArray <float>(); ndarray <long> classes = Get(nameof(SelectedBoxesOutput.Classes)).AsArray <long>(); ndarray <int> detections = Get(nameof(SelectedBoxesOutput.Detections)).AsArray <int>(); return(ObjectDetectionResult.FromCombinedNonMaxSuppressionBatch( boxs, scores, classes, detections[0].AsScalar())); }
private void UpdateCodeWindows(int newStart) { var cursorPos = GetCursorPos(this.codeDisplay.Text, newStart); byte[] codeBytes = new byte[CSharpOrNot.Width * CSharpOrNot.Height]; CSharpOrNot.RenderTextBlockToGreyscaleBytes(this.code, cursorPos, CSharpOrNot.Size, codeBytes); this.codeWindow.Text = string.Join(Environment.NewLine, codeBytes.Select(b => b == 255 ? ' ' : (char)b) .Batch(CSharpOrNot.Width) .Select(line => new string(line.ToArray()))); BitmapTools.ToBitmap(codeBytes, this.renderTarget); BitmapTools.Upscale(this.renderTarget, this.output); this.codeImage.Source?.Dispose(); this.output.Save("code.png", ImageFormat.Png); this.codeImage.Source = new Avalonia.Media.Imaging.Bitmap("code.png"); ndarray @in = GreyscaleImageBytesToNumPy(codeBytes, imageCount: 1, width: CSharpOrNot.Width, height: CSharpOrNot.Height); var prediction = this.model.predict(@in); int extensionIndex = (int)prediction.argmax(); string extension = IncludeExtensions[extensionIndex].Substring(1); bool csharp = extension == "cs"; this.language.Text = csharp ? "C#" : $"Not C#! ({extension}?)"; this.languageBox.Background = csharp ? Brushes.Green : Brushes.Red; }
public void test_full_4() { var x = np.arange(0, 100, 1, np.Float32).reshape(new shape(10, 10)); print(x); print("Update sixth value to 11"); x[6] = 55; print(x); print(x.shape); print(x.strides); ndarray y = x[5] as ndarray; AssertArray(y, new float[] { 50, 51, 52, 53, 54, 55, 56, 57, 58, 59 }); AssertShape(y, 10); AssertStrides(y, sizeof(float)); y = x[6] as ndarray; AssertArray(y, new float[] { 55, 55, 55, 55, 55, 55, 55, 55, 55, 55 }); AssertShape(y, 10); AssertStrides(y, sizeof(float)); y = x[7] as ndarray; AssertArray(y, new float[] { 70, 71, 72, 73, 74, 75, 76, 77, 78, 79 }); AssertShape(y, 10); AssertStrides(y, sizeof(float)); }
public ndarray BuildCPPN(int width, int height, ndarray x_dat, ndarray y_dat, ndarray r_dat, ndarray hid_vec) { Num_Points = width * height; // Scale the hidden vector ndarray hid_vec_scaled = np.reshape(hid_vec, new shape(BatchSize, 1, HSize)) * np.ones((Num_Points, 1), dtype: np.Float32) * Scaling; //Unwrap the grid matrices ndarray x_dat_unwrapped = np.reshape(x_dat, new shape(BatchSize * Num_Points, 1)); ndarray y_dat_unwrapped = np.reshape(y_dat, new shape(BatchSize * Num_Points, 1)); ndarray r_dat_unwrapped = np.reshape(r_dat, new shape(BatchSize * Num_Points, 1)); ndarray h_vec_unwrapped = np.reshape(hid_vec_scaled, new shape(BatchSize * Num_Points, HSize)); //Build the network Art_Net = FullyConnected(h_vec_unwrapped, NetSize) + FullyConnected(x_dat_unwrapped, NetSize, false) + FullyConnected(y_dat_unwrapped, NetSize, true) + FullyConnected(r_dat_unwrapped, NetSize, false); //Set Activation function var output = TanhSig(); var model = np.reshape(output, new shape(BatchSize, width, height, C_Dim)); return(model); }
public List <ndarray> CreateGrid(int width = 32, int height = 32, float scaling = 1.0f) { Num_Points = width * height; double ret_step = 0; ndarray x_range = np.linspace(-1 * scaling, scaling, ref ret_step, width); ndarray y_range = np.linspace(-1 * scaling, scaling, ref ret_step, height); ndarray x_mat = np.matmul(np.ones(new shape(height, 1)), x_range.reshape(1, width)); ndarray y_mat = np.matmul(y_range.reshape(height, 1), np.ones(new shape(1, width))); ndarray r_mat = np.sqrt((x_mat * x_mat) + (y_mat * y_mat)); x_mat = np.tile(x_mat.flatten(), BatchSize).reshape(BatchSize, Num_Points, 1); y_mat = np.tile(y_mat.flatten(), BatchSize).reshape(BatchSize, Num_Points, 1); r_mat = np.tile(r_mat.flatten(), BatchSize).reshape(BatchSize, Num_Points, 1); return(new List <ndarray> { x_mat, y_mat, r_mat }); }
public static ndarray _get_t_vector(ndarray r_quat, ndarray s_quat) { var r_w_matrix = _get_w_matrix(r_quat); var t_vector = 2 * (ndarray)np.matmul(r_w_matrix.T, s_quat)[":3"]; return(t_vector); }
public void test_tri_1() { ndarray a = np.tri(3, 5, 2, dtype: np.Int32); print(a); var ExpectedDataA = new Int32[, ] { { 1, 1, 1, 0, 0 }, { 1, 1, 1, 1, 0 }, { 1, 1, 1, 1, 1 } }; AssertArray(a, ExpectedDataA); print("***********"); ndarray b = np.tri(3, 5, -1); print(b); var ExpectedDataB = new float[, ] { { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 0.0f, 0.0f, 0.0f } }; AssertArray(b, ExpectedDataB); }
public void test_var_1() { ndarray a = np.array(new int[, ] { { 1, 2 }, { 3, 4 } }); ndarray b = np.var(a); Assert.AreEqual(1.25, b.GetItem(0)); print(b); ndarray c = np.var(a, axis: 0); AssertArray(c, new double[] { 1.0, 1.0 }); print(c); ndarray d = np.var(a, axis: 1); AssertArray(d, new double[] { 1.25, 1.25 }); // NOTES: TODO: slightly different than python. keepdims issue print(d); // In single precision, std() can be inaccurate: a = np.zeros(new shape(2, 512 * 512), dtype: np.Float32); a[0, ":"] = 1.0; a[1, ":"] = 0.1; b = np.var(a); Assert.AreEqual((double)0.202509764960269, Convert.ToDouble(b.GetItem(0)), 0.00000001); print(b); // Computing the standard deviation in float64 is more accurate: c = np.var(a, dtype: np.Float64); Assert.AreEqual((double)0.202499999329974, Convert.ToDouble(c.GetItem(0)), 0.00000001); print(c); }
private ndarray correct_dimensions(ndarray s, int targetlength) { // """checks the dimensionality of some numeric argument s, broadcasts it // to the specified length if possible. // Args: // s: None, scalar or 1D array // targetlength: expected length of s // Returns: // None if s is None, else numpy vector of length targetlength // """ if (s != null) { s = np.array(s); if (s.ndim == 0) { s = np.array(s * targetlength); } else if (s.ndim == 1) { if (s.dims[0] != targetlength) { throw new Exception("arg must have length " + targetlength); } } else { throw new Exception("Invalid argument"); } } return(s); }
public void test_std_1() { ndarray a = np.array(new int[, ] { { 1, 2 }, { 3, 4 } }); ndarray b = np.std(a); print(b); Assert.AreEqual(1.11803398874989, (double)b.GetItem(0), 0.0000001); ndarray c = np.std(a, axis: 0); print(c); AssertArray(c, new double[] { 1.0, 1.0 }); ndarray d = np.std(a, axis: 1); print(d); AssertArray(d, new double[] { 1.1180339887498949, 1.1180339887498949 }); // NOTES: TODO: slightly different than python. keepdims issue // In single precision, std() can be inaccurate: a = np.zeros(new shape(2, 512 * 512), dtype: np.Float32); a[0, ":"] = 1.0; a[1, ":"] = 0.1; b = np.std(a); print(b); Assert.AreEqual(0.450010849825055, (double)b.GetItem(0), 0.0000001); // Computing the standard deviation in float64 is more accurate: c = np.std(a, dtype: np.Float64); print(c); Assert.AreEqual(0.449999999255527, (double)c.GetItem(0), 0.0000001); }
public void test_largearray_inner_Float64() { var a = np.arange(0, 2000 * 100, 1, dtype: np.Float64).reshape((2000, -1)); var b = np.arange(0, 2000 * 100, 1, dtype: np.Float64).reshape((2000, -1)); //var a = np.arange(0, 2000 * 1, 1, dtype: np.Float64); //var b = np.arange(0, 2000 * 1, 1, dtype: np.Float64); ndarray c = null; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); for (int i = 0; i < 10; i++) { c = np.inner(a, b); } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds.ToString()); var sum = np.sum(c); Assert.AreEqual((double)(3.9999603333936118E+18), (double)sum); }
public void test_diag_1() { ndarray m = np.arange(9); var n = np.diag(m); print(m); print(n); var ExpectedDataN = new Int32[, ] { { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 2, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 3, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 4, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 5, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 6, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 7, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 8 } }; AssertArray(n, ExpectedDataN); m = np.arange(9).reshape(new shape(3, 3)); n = np.diag(m); print(m); print(n); AssertArray(n, new int[] { 0, 4, 8 }); }
static void Test3() { int LoopCount = 500; ndarray matrixOrig = np.arange(16000000, dtype: np.Float64).reshape((40, -1)); ndarray matrix = null; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); for (int i = 0; i < LoopCount; i++) { matrix = matrixOrig["1:40:2", "1:-2:3"] as ndarray; matrix = matrix / 3; //matrix = matrix + i; } //Assert.AreEqual(7290200441084.1943, np.sum(matrix).GetItem(0)); var output = matrix[new Slice(15, 25, 2), new Slice(15, 25, 2)]; sw.Stop(); Console.WriteLine(string.Format("DOUBLE calculations took {0} milliseconds\n", sw.ElapsedMilliseconds)); Console.WriteLine(output.ToString()); Console.WriteLine("************\n"); Console.ReadLine(); }
public static ndarray _get_r_matrix(ndarray r_quat) { var r_w_matrix = _get_w_matrix(r_quat); var r_q_matrix = _get_q_matrix(r_quat); var r_matrix = np.matmul(r_w_matrix.T, r_q_matrix)[":3", ":3"]; return((ndarray)r_matrix); }
internal void AssertStrides(ndarray a, int s0, int s1, int s2, int s3) { Assert.AreEqual(a.strides.Length, 4); Assert.AreEqual(a.strides[0], s0); Assert.AreEqual(a.strides[1], s1); Assert.AreEqual(a.strides[2], s2); Assert.AreEqual(a.strides[3], s3); }
public static (ndarray, ndarray) _get_r_quat2(ndarray d_matrix) { ndarray eigvals = null, eigvects = null; //(eigvals, eigvects) = np.linalg.eig(d_matrix); ndarray beta_1 = np.argmax(eigvals); var r_quat = eigvects[":", beta_1]; return(beta_1, (ndarray)r_quat); }
void CreateBatches() { this.batchCount = (int)(tensor.size / (this.batchSize * this.seqLength)); if (this.batchCount == 0) { throw new ArgumentException(); } this.tensor = this.tensor[..(this.batchCount * this.batchSize * this.seqLength)];
public static double _get_lambda_next(ndarray am, ndarray bs, ndarray bm, ndarray cs, ndarray cm, ndarray rq) { var expr_1 = np.matmul(np.matmul(rq.T, am), rq); var expr_2 = (1 / cs) * np.matmul(np.matmul(np.matmul(rq.T, bm.T), cm), rq); var expr_3 = (1 / cs) * np.matmul(np.matmul(np.matmul(rq.T, cm.T), cm), rq); var lambda_next = (expr_1 - expr_2) / (bs - expr_3); return(getFloatValue((ndarray)lambda_next)); }
private void AssertDataTypes <T>(ndarray arrayData, T[,,,,] expectedData) { if (expectedData[0, 0, 0, 0, 0] == null) { return; } Assert.IsInstanceOfType(arrayData.GetItem(0), expectedData[0, 0, 0, 0, 0].GetType(), "ndarray type is not a match for expectedData"); }
public void test_mean_3() { ndarray x = np.array(new int[] { 1, 2, 2, 2, 1 }); var mean = x.Mean <int>(); ndarray y = x.A("1:3"); mean = y.Mean <int>(); }
public void test_randint8_UD_1() { var random = new np.random(new UserDefinedRandomGenerator()); random.seed(9292); ndarray arr = random.randint(2, 3, new shape(4), dtype: np.Int8); Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_BYTE); AssertShape(arr, 4); print(arr); AssertArray(arr, new SByte[] { 2, 2, 2, 2 }); arr = random.randint(2, 8, new shape(5000000), dtype: np.Int8); Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_BYTE); var amax = np.amax(arr); print(amax); Assert.AreEqual((sbyte)7, (sbyte)amax); var amin = np.amin(arr); print(amin); Assert.AreEqual((sbyte)2, (sbyte)amin); var avg = np.average(arr); print(avg); Assert.AreEqual(2.4605506, (double)avg); var first10 = arr["0:10:1"] as ndarray; print(first10); AssertArray(first10, new sbyte[] { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }); arr = random.randint(-2, 3, new shape(5000000), dtype: np.Int8); Assert.AreEqual(arr.TypeNum, NPY_TYPES.NPY_BYTE); amax = np.amax(arr); print(amax); Assert.AreEqual((sbyte)2, (sbyte)amax); amin = np.amin(arr); print(amin); Assert.AreEqual((sbyte)(-2), (sbyte)amin); avg = np.average(arr); print(avg); Assert.AreEqual(-1.6648296, (double)avg); first10 = arr["0:10:1"] as ndarray; print(first10); AssertArray(first10, new sbyte[] { -2, -2, 0, -1, -2, -2, -2, -2, -2, -2 }); }
public ObjectDetectionDataset(string[] annotations, string[] classNames, int[] strides, int inputSize, ndarray <float> anchors, int anchorsPerScale, int maxBBoxPerScale) { this.classNames = classNames ?? throw new ArgumentNullException(nameof(classNames)); if (classNames.Length == 0) { throw new ArgumentException(message: "List of class names must not be empty"); } this.annotations = annotations ?? throw new ArgumentNullException(nameof(annotations)); if (annotations.Length == 0) { throw new ArgumentException(message: "List of annotations must not be empty"); } if (strides is null || strides.Length == 0) { throw new ArgumentNullException(nameof(strides)); } if (strides.Any(NotPositive)) { throw new ArgumentOutOfRangeException(nameof(strides)); } this.strides = strides.ToArray(); if (anchors is null) { throw new ArgumentNullException(nameof(anchors)); } if (anchors.ndim != 3) { throw new ArgumentException("Bad shape", paramName: nameof(anchors)); } this.anchors = anchors; if (anchorsPerScale <= 0) { throw new ArgumentOutOfRangeException(nameof(anchorsPerScale)); } this.anchorsPerScale = anchorsPerScale; if (inputSize <= 0) { throw new ArgumentOutOfRangeException(nameof(inputSize)); } this.inputSize = inputSize; if (maxBBoxPerScale <= 0) { throw new ArgumentOutOfRangeException(nameof(maxBBoxPerScale)); } this.maxBBoxPerScale = maxBBoxPerScale; }
// from https://stackoverflow.com/questions/4601373/better-way-to-shuffle-two-numpy-arrays-in-unison static void Shuffle <T1, T2>(ndarray <T1> array1, ndarray <T2> array2) { using var _ = Py.GIL(); var random = numPy.GetAttr("random"); var randomState = random.InvokeMethod("get_state"); random.InvokeMethod("shuffle", array1.PythonObject); random.InvokeMethod("set_state", randomState); random.InvokeMethod("shuffle", array2.PythonObject); }
public void test_largearray_append_BIGINT() { var matrix = np.arange(16000000, dtype: np.BigInt).reshape(40, -1); Assert.AreEqual((System.Numerics.BigInteger) 127999992000000, np.sum(matrix).GetItem(0)); ndarray m1 = np.append(matrix, new System.Numerics.BigInteger[] { 999, 100, 101 }); Assert.AreEqual((System.Numerics.BigInteger) 127999992001200, np.sum(m1).GetItem(0)); }
private static double getFloatValue(ndarray array) { double res = 0; if (!double.TryParse(array.Real.ToString(), System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out res)) { throw new Exception(); } return(res); }
public string Sample(Session session, dynamic chars, IReadOnlyDictionary <char, int> vocabulary, int num = 200, string prime = "The ", int samplingType = 1) { dynamic state = this.CreateInitialState(session, vocabulary, prime); int WeightedPick(IEnumerable <float32> weights) { double[] sums = weights.Aggregate((sum: 0.0, sums: new List <double>()), (acc, value) => { acc.sum += (double)value; acc.sums.Add(acc.sum); return(acc.sum, acc.sums); }).sums.ToArray(); int index = Array.BinarySearch(sums, this.random.NextDouble() * sums.Last()); return(index < 0 ? ~index : index); } string ret = prime; char chr = prime.Last(); for (int i = 0; i < num; i++) { var x = np.zeros(new TensorShape(1, 1)); x[0, 0] = vocabulary[chr]; var feed = new PythonDict <dynamic, dynamic> { [this.inputData] = x, [this.initialState] = state, }; var outputs = session.run(new dynamic[] { this.probs, this.finalState }, feed); var probs = outputs[0]; state = outputs[1]; ndarray computedProbabilities = probs[0]; dynamic sample; switch (samplingType) { case 1: case 2 when chr == ' ': sample = WeightedPick(computedProbabilities.Cast <ndarray>().SelectMany(s => s.Cast <float32>())); break; case 0: case 2: sample = computedProbabilities.argmax(); break; default: throw new NotSupportedException(); } var pred = chars[sample]; ret += pred; chr = pred; } return(ret); }
public override void Update(NDArray labels, NDArray preds) { CheckLabelShapes(labels, preds, true); ndarray pearson_corr = (ndarray)nd.Correlation(labels.Ravel(), preds.Ravel()).AsNumpy()[0, 1]; sum_metric += pearson_corr.asscalar <float>(); global_sum_metric += pearson_corr.asscalar <float>(); num_inst += 1; global_num_inst += 1; }