public override InternalArray Forward(InternalArray ar) { #if PROFILER LogItem = new CalcLogItem(this, "avgPool2D"); var sw = Stopwatch.StartNew(); #endif var hin = ar.Shape[2]; var win = ar.Shape[3]; var n = ar.Shape[0]; var c = ar.Shape[1]; var hout = ((hin + 2 * padding[0] - kernelSize[0]) / stride[0]) + 1; var wout = ((win + 2 * padding[1] - kernelSize[1]) / stride[1]) + 1; InternalArray ret = new InternalArray(new int[] { n, c, hout, wout }); //get all 2d images //append them together to [n,c] List <double> data = new List <double>(); for (int i = 0; i < n; i++) { for (int j = 0; j < c; j++) { var img = Helpers.Get2DImageFrom4DArray(ar, i, j); var img2 = Process2DImage(img); data.AddRange(img2.Data); } } ret.Data = data.ToArray(); #if PROFILER sw.Stop(); Profiler.AddLog(LogItem, this, "exec", sw.ElapsedMilliseconds, true); #endif return(ret); }
public override InternalArray Forward(InternalArray ar) { #if PROFILER LogItem = new CalcLogItem(this, "batchNorm2d"); var sw = Stopwatch.StartNew(); #endif InternalArray ret = new InternalArray(ar.Shape); var n = ar.Shape[0]; var c = ar.Shape[1]; List <double> data = new List <double>(); for (int i = 0; i < n; i++) { for (int j = 0; j < c; j++) { var img = Helpers.Get2DImageFrom4DArray(ar, i, j); for (int zi = 0; zi < img.Data.Length; zi++) { img.Data[zi] = ((img.Data[zi] - RunningMean.Data[j]) / Math.Sqrt(RunningVar.Data[j] + eps)) * Weight.Data[j] + Bias.Data[j]; } data.AddRange(img.Data); } } ret.Data = data.ToArray(); #if PROFILER sw.Stop(); if (Parent != null) { Profiler.AddLog(Parent.LogItem, LogItem); } #endif return(ret); }
public override InternalArray Forward(InternalArray ar) { //Profiler.PushCurrent(new CalcLogItem(this, "conv2d")); #if PROFILER LogItem = new CalcLogItem(this, "conv2d"); Profiler.AddLog(LogItem); var sw = Stopwatch.StartNew(); #endif var hin = ar.Shape[2]; var win = ar.Shape[3]; var n = ar.Shape[0]; var c = ar.Shape[1]; var hout = ((hin + 2 * padding[0] - dilation[0] * (kernelSize[0] - 1) - 1) / stride[0]) + 1; var wout = ((win + 2 * padding[1] - dilation[1] * (kernelSize[1] - 1) - 1) / stride[1]) + 1; var cout = Weight.Shape[0]; InternalArray ret = new InternalArray(new int[] { n, cout, hout, wout }); //get all 3d images int pos = 0; int pos0 = 0; for (int i = 0; i < n; i++) { var img = ar.GetNext3DImageFrom4DArray(ref pos0); InternalArray img2 = null; if (AllowOptimized) { if (AllowOptimizedViaDot) { img2 = ProcessImageViaDot(img); #if PROFILER method = "dot"; #endif } else { /*if (dilation[0] == 1 && kernelSize[0] == 3 && padding[0] > 0) * { #if PROFILER * * method = "+pad"; #endif * img2 = ProcessImageOptimized2(img, hout, wout, c, hin, win); * var img4 = Helpers.Pad3d(img, padding[0]); * var img3 = ProcessImageOptimizedNoPadding(img4, hout + padding[0] * 2, wout + padding[0] * 2, c, img.Shape[1], img.Shape[2]); * var img5 = Helpers.Unpad3d(img, padding[0]); * * } * else*/ { if (padding[0] == 0 && dilation[1] == 1 && kernelSize[0] == 1 && stride[0] == 1) { #if PROFILER method = "nopad.k1"; #endif img2 = ProcessImageOptimizedNoPaddingKernel1(img, hout, wout, c, hin, win); /*var img3 = ProcessImageOptimized2(img, hout, wout, c, hin, win); * if (!img2.IsEqual(img3)) * { * throw new Exception("11"); * }*/ } else if (padding[0] == 0 && dilation[0] == 1 && kernelSize[0] == 3) { #if PROFILER method = "no pad"; //Helpers.Pad2d() #endif img2 = ProcessImageOptimizedNoPadding(img, hout, wout, c, hin, win); /*var img3 = ProcessImageOptimized2(img, hout, wout, c, hin, win); * if (!img2.IsEqual(img3)) * { * throw new Exception("11"); * } */ } else { #if PROFILER method = "opt2"; #endif img2 = ProcessImageOptimized2(img, hout, wout, c, hin, win); } } } } else { #if PROFILER method = "native"; #endif //img2 = ProcessImage(img); img2 = ProcessImageBySubRegions(img); /*var res1 = ProcessImageBySubRegions(img); * if (!img2.IsEqual(res1)) * { * throw new Exception("11"); * }*/ } Array.Copy(img2.Data, 0, ret.Data, pos, img2.Data.Length); pos += img2.Data.Length; } //sw.Stop(); // LastMs = sw.ElapsedMilliseconds; #if PROFILER Profiler.AddLog(LogItem, this, method + "; hin: " + hin + "; win: ;" + win + "; padding: " + padding[0] + "; dilation: " + dilation[0] + "; ksize: " + kernelSize[0] + "; stride: " + stride[0] + "; nIn: " + inChannels + "; nOut: " + outChannels, LastMs); Profiler.AddLog(LogItem, this, $"ar: {ar}; w: {Weight}"); Profiler.AddLog(LogItem, this, "out shape: " + ret.Shape.Aggregate("", (x, y) => x + y + "; ")); //Profiler.PopCurrent(); if (Parent != null) { Profiler.AddLog(Parent.LogItem, LogItem); } #endif return(ret); }
public void AddChild(CalcLogItem l) { l.Parent = this; childs.Add(l); }