public override void Forward(SuperArray x) { base.Forward(x); var(n, c, h, w) = x.GetConv2DShape(); int pad = 0; if (Padding == PaddingType.Same) { pad = 1; } else if (Padding == PaddingType.Full) { pad = 2; } var h_out = (h - PoolSize.Item1 + 2 * pad) / Strides + 1; var w_out = (w - PoolSize.Item2 + 2 * pad) / Strides + 1; var x_reshaped = x.Reshape(n * c, 1, h, w); xCols = ImUtil.Im2Col(x_reshaped, PoolSize, pad, Strides); Output = Ops.Max(xCols, 0); Output = Output.Reshape(h_out, w_out, n, c).Transpose(2, 3, 0, 1); }
public static SuperArray Im2Col(SuperArray x, Tuple <int, int> kernalSize, int padding = 1, int stride = 1, Tuple <int, int> dialation = null) { if (dialation == null) { dialation = Tuple.Create <int, int>(1, 1); } var(n, c, h, w) = x.GetConv2DShape(); var out_height = (h + 2 * padding - kernalSize.Item1) / stride + 1; var out_width = (w + 2 * padding - kernalSize.Item2) / stride + 1; SuperArray cols = new SuperArray((c * kernalSize.Item1 * kernalSize.Item2), (n * out_height * out_width)); return(cols); }
public override void Forward(SuperArray x) { base.Forward(x); var(n, c, h, w) = x.GetConv2DShape(); Parameter weight = BuildParam("w", new Shape(Filters, c, KernalSize.Item1, KernalSize.Item2), KernalInitializer, KernalConstraint, KernalRegularizer); Parameter bias = null; if (UseBias) { bias = BuildParam("b", new Shape(Filters, 1), BiasInitializer, BiasConstraint, BiasRegularizer); } int pad = 0; if (Padding == PaddingType.Same) { pad = 1; } else if (Padding == PaddingType.Full) { pad = 2; } var dialatedKernel = Tuple.Create(((KernalSize.Item1 - 1) * DialationRate.Item1 + 1), ((KernalSize.Item2 - 1) * DialationRate.Item2 + 1)); var h_out = (h - dialatedKernel.Item1 + 2 * pad) / Strides + 1; var w_out = (w - dialatedKernel.Item2 + 2 * pad) / Strides + 1; var wRows = weight.Data.Reshape(Filters, -1); xCols = ImUtil.Im2Col(x, dialatedKernel, pad, Strides); Output = Ops.Dot(wRows, xCols); if (UseBias) { Output = Output + bias.Data; } Output = Output.Reshape(Filters, h_out, w_out, n); Output = Output.Transpose(3, 0, 1, 2); }