public override void Process(TransformContext context) { var space = (SpaceToBatchNd)context.MatchedLayers[0]; var dwConv2d = (DepthwiseConv2d)context.MatchedLayers[1]; var conv2d = (Conv2d)context.MatchedLayers[2]; var input = space.Input.Connection.From; var output = conv2d.Output; space.Input.ClearConnection(); var newDwConv2d = new DepthwiseConv2d(input.Dimensions, dwConv2d.Weights, dwConv2d.Bias, Padding.Same, 1, 1, dwConv2d.FusedActivationFunction); var quantize = new Quantize(newDwConv2d.Output.Dimensions); var upload = new K210Upload(quantize.Output.Dimensions); var newConv2d = new K210Conv2d(upload.Output.Dimensions, K210Conv2dType.Conv2d, conv2d.Weights, conv2d.Bias, K210PoolType.LeftTop, conv2d.FusedActivationFunction, null); var dequantize = new Dequantize(newConv2d.Output.Dimensions); newDwConv2d.Input.SetConnection(input); quantize.Input.SetConnection(newDwConv2d.Output); upload.Input.SetConnection(quantize.Output); newConv2d.Input.SetConnection(upload.Output); dequantize.Input.SetConnection(newConv2d.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); foreach (var oldOut in oldOuts) { oldOut.SetConnection(dequantize.Output); } }
public override void Process(TransformContext context) { K210Conv2d newLayer; OutputConnector output; OutputConnector input; var conv = context.MatchedLayers[0]; if (conv is Conv2d conv2d) { newLayer = new K210Conv2d(conv2d.Input.Dimensions, K210Conv2dType.Conv2d, conv2d.Weights, conv2d.Bias, K210PoolType.LeftTop, conv2d.FusedActivationFunction, null); input = conv2d.Input.Connection.From; output = conv2d.Output; } else { throw new InvalidOperationException(); } var quantize = new Quantize(input.Dimensions); var upload = new K210Upload(input.Dimensions); var dequantize = new Dequantize(newLayer.Output.Dimensions); quantize.Input.SetConnection(input); upload.Input.SetConnection(quantize.Output); newLayer.Input.SetConnection(upload.Output); dequantize.Input.SetConnection(newLayer.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); foreach (var oldOut in oldOuts) { oldOut.SetConnection(dequantize.Output); } }
public void Infer(K210Upload layer, K210UploadLayerArgument argument, InferenceContext context) { var inputAlloc = context.MainMemoryMap[layer.Input.Connection.From]; var outputAlloc = context.KPUMemoryMap[layer.Output]; argument.MainMemoryInputAddress = inputAlloc.GetAddress(); argument.KPUMemoryOutputAddress = outputAlloc.GetAddress(); }
public K210UploadLayerArgument Convert(K210Upload layer, ConvertContext context) { return(new K210UploadLayerArgument { Width = (uint)layer.Input.Dimensions[3], Height = (uint)layer.Input.Dimensions[2], Channels = (uint)layer.Input.Dimensions[1] }); }
public override void Process(TransformContext context) { var space = (SpaceToBatchNd)context.MatchedLayers[0]; var input = space.Input.Connection.From; space.Input.ClearConnection(); K210Conv2d newLayer; OutputConnector output; var conv = context.MatchedLayers[1]; if (conv is Conv2d conv2d) { newLayer = new K210Conv2d(input.Dimensions, K210Conv2dType.Conv2d, conv2d.Weights, conv2d.Bias, conv2d.StrideWidth == 2 ? K210PoolType.LeftTop : K210PoolType.None, conv2d.FusedActivationFunction, null); output = conv2d.Output; } else if (conv is DepthwiseConv2d dwConv2d) { newLayer = new K210Conv2d(input.Dimensions, K210Conv2dType.DepthwiseConv2d, dwConv2d.Weights, dwConv2d.Bias, K210PoolType.None, dwConv2d.FusedActivationFunction, null); output = dwConv2d.Output; } else { throw new InvalidOperationException(); } var quantize = new Quantize(input.Dimensions); var upload = new K210Upload(input.Dimensions); var dequantize = new Dequantize(newLayer.Output.Dimensions); quantize.Input.SetConnection(input); upload.Input.SetConnection(quantize.Output); newLayer.Input.SetConnection(upload.Output); dequantize.Input.SetConnection(newLayer.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); foreach (var oldOut in oldOuts) { oldOut.SetConnection(dequantize.Output); } }
public override void Process(TransformContext context) { var space = context.MatchedLayers[0]; var dwConv2d = (DepthwiseConv2d)context.MatchedLayers[1]; var conv2d = (Conv2d)context.MatchedLayers.Last(); var input = space.InputConnectors[0].Connection.From; var output = conv2d.Output; space.InputConnectors[0].ClearConnection(); var newDwConv2d = new DepthwiseConv2d(input.Dimensions, dwConv2d.Weights, dwConv2d.Bias, Padding.Same, 1, 1, dwConv2d.FusedActivationFunction); var quantize = new Quantize(newDwConv2d.Output.Dimensions); var upload = new K210Upload(quantize.Output.Dimensions); var newConv2d = new K210Conv2d(upload.Output.Dimensions, K210Conv2dType.Conv2d, conv2d.Weights, conv2d.Bias, K210PoolType.LeftTop, conv2d.FusedActivationFunction, null); var dequantize = new Dequantize(newConv2d.Output.Dimensions); newDwConv2d.Input.SetConnection(input); quantize.Input.SetConnection(newDwConv2d.Output); upload.Input.SetConnection(quantize.Output); newConv2d.Input.SetConnection(upload.Output); dequantize.Input.SetConnection(newConv2d.Output); var oldOuts = output.Connections.Select(o => o.To).ToList(); if (context.MatchedLayers.Count == 3) { foreach (var oldOut in oldOuts) { oldOut.SetConnection(dequantize.Output); } } else { var newOutput = dequantize.Output; foreach (var middleLayer in context.MatchedLayers.Skip(2).Take(context.MatchedLayers.Count - 3)) { Layer newLayer; switch (middleLayer) { case Quantize _: newLayer = new Quantize(newOutput.Dimensions); break; case Dequantize _: newLayer = new Dequantize(newOutput.Dimensions); break; case LeakyRelu l: newLayer = new LeakyRelu(newOutput.Dimensions, l.Slope); break; default: throw new NotImplementedException(); } newLayer.InputConnectors[0].SetConnection(newOutput); newOutput = newLayer.OutputConnectors[0]; } foreach (var oldOut in oldOuts) { oldOut.SetConnection(newOutput); } } }