private void Awake() { _rng = new Rng(1234); DataManager.LoadFloatData(); // Create convolution layers _layers = new List <ConvLayer2D>(); var l1 = ConvLayer2D.Create(DataManager.Width, DataManager.Channels, 5, 1, 0, 4).Value; _layers.Add(l1); var l2 = ConvLayer2D.Create(l1.OutWidth, l1.NumFilters, 3, 3, 0, 4).Value; _layers.Add(l2); var l3 = ConvLayer2D.Create(l2.OutWidth, l2.NumFilters, 3, 1, 0, 4).Value; _layers.Add(l3); var last = l3; int convOutCount = last.OutWidth * last.OutWidth * last.NumFilters; Debug.Log("Conv out neuron count: " + convOutCount); _fcLayer = new FCLayer(10, convOutCount); // Parameter initialization for (int i = 0; i < _layers.Count; i++) { NeuralMath.RandomGaussian(ref _rng, _layers[i].Kernel, 0f, 0.25f); NeuralMath.RandomGaussian(ref _rng, _layers[i].Bias, 0f, 0.1f); } NeuralMath.RandomGaussian(ref _rng, _fcLayer.Biases, 0f, 0.1f); NeuralMath.RandomGaussian(ref _rng, _fcLayer.Weights, 0f, 0.1f); // Create debug textures _layerTex = new List <Conv2DLayerTexture>(_layers.Count); for (int i = 0; i < _layers.Count; i++) { _layerTex.Add(new Conv2DLayerTexture(_layers[i])); } // Create the training structure _batch = new NativeArray <int>(BatchSize, Allocator.Persistent, NativeArrayOptions.ClearMemory); _targetOutputs = new NativeArray <float>(OutputClassCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); _dCdO = new NativeArray <float>(OutputClassCount, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); _input = new NativeArray <float>(DataManager.ImgDims * DataManager.Channels, Allocator.Persistent, NativeArrayOptions.UninitializedMemory); }
// Todo: support for the multiple filter slices public static void KernelToTexture(ConvLayer2D layer, int filter, int fslice, Texture2D tex) { var colors = new Color[layer.KWidth * layer.KWidth]; int start = layer.KWidth * layer.KWidth * layer.InDepth * filter + layer.KWidth * layer.KWidth * fslice; for (int y = 0; y < layer.KWidth; y++) { for (int x = 0; x < layer.KWidth; x++) { float pix = layer.Kernel[start + y * layer.KWidth + x]; colors[y * layer.KWidth + x] = new Color(pix, pix, pix, 1f); } } tex.SetPixels(0, 0, tex.width, tex.height, colors); tex.Apply(false); }
public Conv2DLayerTexture(ConvLayer2D layer) { Layer = layer; ActTex = TextureUtils.CreateTexture2DArray(layer.OutWidth, layer.OutWidth, layer.NumFilters); KernTex = TextureUtils.CreateTexture2DArray(layer.KWidth, layer.KWidth, layer.NumFilters * layer.InDepth); }