public void StructSerialize() { PoolingInfo info = PoolingInfo.New(PoolingMode.AverageIncludingPadding, 3, 3, 1, 1); using (MemoryStream stream = new MemoryStream()) { stream.Write(info); stream.Seek(0, SeekOrigin.Begin); Assert.IsTrue(stream.TryRead(out PoolingInfo copy)); Assert.IsTrue(info.Equals(copy)); } }
public void NetworkSerialization() { INeuralNetwork network = NetworkManager.NewSequential(TensorInfo.Image <Rgb24>(120, 120), CuDnnNetworkLayers.Convolutional(ConvolutionInfo.New(ConvolutionMode.CrossCorrelation), (10, 10), 20, ActivationType.AbsoluteReLU), CuDnnNetworkLayers.Convolutional(ConvolutionInfo.New(ConvolutionMode.Convolution, 2, 2), (5, 5), 20, ActivationType.ELU), CuDnnNetworkLayers.Convolutional(ConvolutionInfo.Default, (10, 10), 20, ActivationType.Identity), CuDnnNetworkLayers.Pooling(PoolingInfo.New(PoolingMode.AverageIncludingPadding, 2, 2, 1, 1), ActivationType.ReLU), CuDnnNetworkLayers.Convolutional(ConvolutionInfo.Default, (10, 10), 20, ActivationType.Identity), CuDnnNetworkLayers.Pooling(PoolingInfo.Default, ActivationType.ReLU), CuDnnNetworkLayers.FullyConnected(125, ActivationType.Tanh), CuDnnNetworkLayers.FullyConnected(27, ActivationType.Tanh), CuDnnNetworkLayers.Softmax(133)); using (MemoryStream stream = new MemoryStream()) { network.Save(stream); stream.Seek(0, SeekOrigin.Begin); INeuralNetwork copy = NetworkLoader.TryLoad(stream, ExecutionModePreference.Cuda); Assert.IsTrue(network.Equals(copy)); } }
public unsafe void InceptionPoolPipeline() { float[,] x = WeightsProvider.NewFullyConnectedWeights(TensorInfo.Linear(10), 12 * 12 * 3, WeightsInitializationMode.GlorotNormal).AsSpan().AsMatrix(10, 12 * 12 * 3); CuDnnPoolingLayer pool = new CuDnnPoolingLayer(TensorInfo.Image <Rgb24>(12, 12), PoolingInfo.New(PoolingMode.Max, 3, 3, 1, 1, 1, 1), ActivationType.ReLU); CuDnnConvolutionalLayer conv = new CuDnnConvolutionalLayer(pool.OutputInfo, ConvolutionInfo.New(ConvolutionMode.CrossCorrelation), (1, 1), 10, ActivationType.ReLU, BiasInitializationMode.Gaussian); CuDnnInceptionLayer inception = new CuDnnInceptionLayer(TensorInfo.Image <Rgb24>(12, 12), InceptionInfo.New(3, 2, 2, 2, 2, PoolingMode.Max, 10)); fixed(float *pw = inception.Weights) Unsafe.InitBlock(pw, 0, (uint)(sizeof(float) * inception.Weights.Length)); Buffer.BlockCopy(conv.Weights, 0, inception.Weights, sizeof(float) * (3 * 3 + 3 * 2 + 3 * 3 * 2 * 2 + 3 * 2 + 5 * 5 * 2 * 2), sizeof(float) * conv.Weights.Length); Buffer.BlockCopy(conv.Biases, 0, inception.Biases, sizeof(float) * (3 + 2 + 2 + 2 + 2), sizeof(float) * conv.Biases.Length); fixed(float *px = x) { // Forward + Z Tensor.Reshape(px, x.GetLength(0), x.GetLength(1), out Tensor xTensor); pool.Forward(xTensor, out Tensor zTemp, out Tensor aTemp); conv.Forward(aTemp, out Tensor zConv, out Tensor aConv); inception.Forward(xTensor, out Tensor zInc, out Tensor aInc); Tensor.New(zConv.Entities, zConv.Length, out Tensor reshaped); float *pzInc = (float *)zInc.Ptr.ToPointer() + 12 * 12 * (3 + 2 + 2), preshaped = (float *)reshaped.Ptr.ToPointer(); for (int i = 0; i < zConv.Entities; i++) { Buffer.MemoryCopy(pzInc + i * zInc.Length, preshaped + i * zConv.Length, sizeof(float) * zConv.Length, sizeof(float) * zConv.Length); } Assert.IsTrue(reshaped.ContentEquals(zConv)); // A float *paInc = (float *)aInc.Ptr.ToPointer() + 12 * 12 * (3 + 2 + 2); for (int i = 0; i < aConv.Entities; i++) { Buffer.MemoryCopy(paInc + i * aInc.Length, preshaped + i * aConv.Length, sizeof(float) * aConv.Length, sizeof(float) * aConv.Length); } Assert.IsTrue(reshaped.ContentEquals(aConv)); // Backpropagation Tensor.Like(aTemp, out Tensor convdx); Tensor.Like(xTensor, out Tensor pooldx); Tensor.Like(xTensor, out Tensor incdx); conv.Backpropagate(aTemp, zConv, aConv, convdx, out Tensor convdJdw, out Tensor convdJdb); pool.Backpropagate(xTensor, zTemp, convdx, pooldx); inception.Backpropagate(xTensor, zInc, aInc, incdx, out Tensor incdJdw, out Tensor incdJdb); Assert.IsTrue(incdx.ContentEquals(pooldx)); // Gradient Tensor.Reshape((float *)incdJdw.Ptr.ToPointer() + (3 * 3 + 3 * 2 + 3 * 3 * 2 * 2 + 3 * 2 + 5 * 5 * 2 * 2), 1, convdJdw.Size, out Tensor dJdwInc0); Tensor.Reshape((float *)incdJdb.Ptr.ToPointer() + 11, 1, convdJdb.Size, out Tensor dJdbInc0); Assert.IsTrue(convdJdw.ContentEquals(dJdwInc0, 1e-5f)); Assert.IsTrue(convdJdb.ContentEquals(dJdbInc0, 1e-5f)); // Cleanup Tensor.Free(zTemp, aTemp, zConv, aConv, zInc, aInc, reshaped, convdx, pooldx, incdx, convdJdw, convdJdb, incdJdw, incdJdb); } }