public ILayer GetLayer() { var activation = GetActivationFunction(); ILayer toReturn; switch (LayerType) { case NeuralLayerType.BasicLayer: toReturn = new BasicLayer(activation, HasBias, NeuronsPerDimension.All.ToArray()); break; case NeuralLayerType.Conv2D: toReturn = new Conv2DLayer(activation, ConvNbFilters, ConvNbRows, ConvNbColumns); break; case NeuralLayerType.Dropout: toReturn = new DropoutLayer(activation, HasBias, NeuronsPerDimension.One, DropOut); break; case NeuralLayerType.MaxPool: toReturn = new MaxPoolLayer(NeuronsPerDimension.All.ToArray()); break; default: throw new ArgumentOutOfRangeException(); } return(toReturn); }
private static Network CreateNewNetwork() { var num_inputs = 27; // 9 eyes, each sees 3 numbers (wall, green, red thing proximity) var num_actions = 5; // 5 possible angles agent can turn var temporal_window = 1; // amount of temporal memory. 0 = agent lives in-the-moment :) var network_size = num_inputs * temporal_window + num_actions * temporal_window + num_inputs; Network net = new Network(); InputLayer il = new InputLayer(); il.OutputWidth = 1; il.OutputHeight = 1; il.OutputDepth = network_size; net.Layers.Add(il); ConvLayer conv = new ConvLayer(16, 5, 5, il.OutputDepth, il.OutputWidth, il.OutputHeight, 1, 2, 0, 1, 0.1); net.Layers.Add(conv); ReluLayer rlv = new ReluLayer(conv.OutputDepth, conv.OutputWidth, conv.OutputHeight); net.Layers.Add(rlv); MaxPoolLayer pl = new MaxPoolLayer(2, 2, rlv.OutputDepth, rlv.OutputWidth, rlv.OutputHeight, 2, 0, 0); net.Layers.Add(pl); FullyConnLayer fc = new FullyConnLayer(50, pl.OutputDepth, pl.OutputWidth, pl.OutputHeight, 0, 1, 0); net.Layers.Add(fc); ReluLayer rl = new ReluLayer(fc.OutputDepth, fc.OutputWidth, fc.OutputHeight); net.Layers.Add(rl); FullyConnLayer fc2 = new FullyConnLayer(50, rl.OutputDepth, rl.OutputWidth, rl.OutputHeight, 0, 1, 0); net.Layers.Add(fc2); ReluLayer rl2 = new ReluLayer(fc2.OutputDepth, fc2.OutputWidth, fc2.OutputHeight); net.Layers.Add(rl2); FullyConnLayer fc8 = new FullyConnLayer(5, rl2.OutputDepth, rl2.OutputWidth, rl2.OutputHeight, 0, 1, 0); net.Layers.Add(fc8); RegressionLayer sl = new RegressionLayer(fc8.OutputDepth, fc8.OutputWidth, fc8.OutputHeight); net.LossLayer = sl; return(net); }
private static Network CreateNewNetwork() { Network net = new Network(); InputLayer il = new InputLayer(); il.OutputWidth = 32; il.OutputHeight = 32; il.OutputDepth = 3; net.Layers.Add(il); ConvLayer conv = new ConvLayer(16, 5, 5, 3, 32, 32, 1, 2, 0, 1, 0.1); net.Layers.Add(conv); ReluLayer rl = new ReluLayer(conv.OutputDepth, conv.OutputWidth, conv.OutputHeight); net.Layers.Add(rl); MaxPoolLayer pl = new MaxPoolLayer(2, 2, rl.OutputDepth, rl.OutputWidth, rl.OutputHeight, 2, 0, 0); net.Layers.Add(pl); ConvLayer conv2 = new ConvLayer(20, 5, 5, pl.OutputDepth, pl.OutputWidth, pl.OutputHeight, 1, 2, 0, 1, 0.1); net.Layers.Add(conv2); ReluLayer rl2 = new ReluLayer(conv2.OutputDepth, conv2.OutputWidth, conv2.OutputHeight); net.Layers.Add(rl2); MaxPoolLayer pl2 = new MaxPoolLayer(2, 2, rl2.OutputDepth, rl2.OutputWidth, rl2.OutputHeight, 2, 0, 0); net.Layers.Add(pl2); ConvLayer conv3 = new ConvLayer(20, 5, 5, pl2.OutputDepth, pl2.OutputWidth, pl2.OutputHeight, 1, 2, 0, 1, 0.1); net.Layers.Add(conv3); ReluLayer rl3 = new ReluLayer(conv3.OutputDepth, conv3.OutputWidth, conv3.OutputHeight); net.Layers.Add(rl3); MaxPoolLayer pl3 = new MaxPoolLayer(2, 2, rl3.OutputDepth, rl3.OutputWidth, rl3.OutputHeight, 2, 0, 0); net.Layers.Add(pl3); FullyConnLayer fc = new FullyConnLayer(10, pl3.OutputDepth, pl3.OutputWidth, pl3.OutputHeight, 0, 1, 0); net.Layers.Add(fc); SoftmaxLayer sl = new SoftmaxLayer(fc.OutputDepth, fc.OutputWidth, fc.OutputHeight); net.LossLayer = sl; return(net); }
/// <summary> /// Instantiates Layer from LayerData and sets initial values. /// </summary> /// <param name="l"></param> /// <param name="input"></param> /// <param name="isOutput"></param> /// <returns></returns> GameObject InstantiateLayer(LayerData l, GameObject input, bool isOutput) { switch (l.type) { case LayerType.IMAGE: { GameObject go = GetImageLayerPrefab(); GameObject inst = Instantiate(go); ImageLayer imLayer = inst.GetComponent <ImageLayer>(); imLayer.reducedResolution = new Vector2Int(l.activationShape[1], l.activationShape[2]); imLayer.fullResolution = new Vector2Int(l.activationShape[1], l.activationShape[2]); imLayer.pixelSpacing = 0.025f; imLayer.depth = l.activationShape[3]; GameObject canvas = GameObject.FindGameObjectWithTag("Canvas"); Material pixelMaterial = canvas.GetComponent <GuiManager>().pixelMaterial; imLayer.rgb = false; MeshRenderer meshRenderer = inst.GetComponent <MeshRenderer>(); meshRenderer.sharedMaterials[0] = pixelMaterial; imLayer.SetActivationTensorShape(l.activationShape); for (int i = 0; i < l.activationTensors.Count; i++) { imLayer.SetActivationTensorForEpoch(l.activationTensors[i], i); } return(inst); } case LayerType.CONV: { GameObject go = GetConvLayerPrefab(); GameObject inst = Instantiate(go); ConvLayer convLayer = inst.GetComponent <ConvLayer>(); convLayer.convShape = new Vector2Int(l.weightShape[0], l.weightShape[1]); convLayer.reducedDepth = l.weightShape[3]; convLayer.fullDepth = l.weightShape[3]; convLayer.input = input; convLayer.filterSpread = 1.0f; convLayer.lineCircleGrid = 2.0f; convLayer.filterSpacing = 0.025f; convLayer.SetWeightTensorShape(l.weightShape); for (int i = 0; i < l.weightTensors.Count; i++) { convLayer.SetWeightTensorForEpoch(l.weightTensors[i], i); } convLayer.SetActivationTensorShape(l.activationShape); for (int i = 0; i < l.activationTensors.Count; i++) { convLayer.SetActivationTensorForEpoch(l.activationTensors[i], i); } return(inst); } case LayerType.MAXPOOL: { GameObject go = GetMaxPoolLayerPrefab(); GameObject inst = Instantiate(go); MaxPoolLayer mpLayer = inst.GetComponent <MaxPoolLayer>(); mpLayer.filterSpacing = 0.025f; mpLayer.zOffset = 0.25f; mpLayer.input = input; mpLayer.SetActivationTensorShape(l.activationShape); for (int i = 0; i < l.activationTensors.Count; i++) { mpLayer.SetActivationTensorForEpoch(l.activationTensors[i], i); } return(inst); } case LayerType.FC: { GameObject go = GetFCLayerPrefab(); GameObject inst = Instantiate(go); FCLayer fcLayer = inst.GetComponent <FCLayer>(); fcLayer.input = input; fcLayer.filterSpacing = 0.025f; fcLayer.reducedDepth = l.weightShape[1]; fcLayer.fullDepth = l.weightShape[1]; //TODO: here loading is reducing non output fc layers automatically by 4 if (!l.name.Contains("out")) { fcLayer.reducedDepth = l.weightShape[1]; } else { fcLayer.lineCircleGrid = 0; } if (l.name.Contains("0")) { fcLayer.collapseInput = 1f; } //fcLayer.edgeBundle = 1.0f; fcLayer.SetTensorShape(l.weightShape); for (int i = 0; i < l.weightTensors.Count; i++) { fcLayer.SetTensorForEpoch(l.weightTensors[i], i); } fcLayer.SetActivationTensorShape(l.activationShape); for (int i = 0; i < l.activationTensors.Count; i++) { fcLayer.SetActivationTensorForEpoch(l.activationTensors[i], i); } return(inst); } default: break; } return(null); }
public static Network CreateVGG16Network(int imageWidth, int imageHeight, int LabelsCount) { Network net = new Network(); InputLayer il = new InputLayer(); il.OutputWidth = imageWidth; il.OutputHeight = imageHeight; il.OutputDepth = 3; net.Layers.Add(il); ConvLayer conv1_1 = new ConvLayer(64, 3, 3, 3, imageWidth, imageHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv1_1); ReluLayer rl1 = new ReluLayer(conv1_1.OutputDepth, conv1_1.OutputWidth, conv1_1.OutputHeight); net.Layers.Add(rl1); ConvLayer conv1_2 = new ConvLayer(64, 3, 3, rl1.OutputDepth, rl1.OutputWidth, rl1.OutputHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv1_2); ReluLayer rl2 = new ReluLayer(conv1_2.OutputDepth, conv1_2.OutputWidth, conv1_2.OutputHeight); net.Layers.Add(rl2); MaxPoolLayer pl1 = new MaxPoolLayer(2, 2, rl2.OutputDepth, rl2.OutputWidth, rl2.OutputHeight, 2, 0); net.Layers.Add(pl1); ConvLayer conv2_1 = new ConvLayer(128, 3, 3, pl1.OutputDepth, pl1.OutputWidth, pl1.OutputHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv2_1); ReluLayer rl3 = new ReluLayer(conv2_1.OutputDepth, conv2_1.OutputWidth, conv2_1.OutputHeight); net.Layers.Add(rl3); ConvLayer conv2_2 = new ConvLayer(128, 3, 3, rl3.OutputDepth, rl3.OutputWidth, rl3.OutputHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv2_2); ReluLayer rl4 = new ReluLayer(conv2_2.OutputDepth, conv2_2.OutputWidth, conv2_2.OutputHeight); net.Layers.Add(rl4); MaxPoolLayer pl2 = new MaxPoolLayer(2, 2, rl4.OutputDepth, rl4.OutputWidth, rl4.OutputHeight, 2, 0); net.Layers.Add(pl2); ConvLayer conv3_1 = new ConvLayer(256, 3, 3, pl2.OutputDepth, pl2.OutputWidth, pl2.OutputHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv3_1); ReluLayer rl5 = new ReluLayer(conv3_1.OutputDepth, conv3_1.OutputWidth, conv3_1.OutputHeight); net.Layers.Add(rl5); ConvLayer conv3_2 = new ConvLayer(256, 3, 3, rl5.OutputDepth, rl5.OutputWidth, rl5.OutputHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv3_2); ReluLayer rl6 = new ReluLayer(conv3_2.OutputDepth, conv3_2.OutputWidth, conv3_2.OutputHeight); net.Layers.Add(rl6); ConvLayer conv3_3 = new ConvLayer(256, 3, 3, rl6.OutputDepth, rl6.OutputWidth, rl6.OutputHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv3_3); ReluLayer rl7 = new ReluLayer(conv3_3.OutputDepth, conv3_3.OutputWidth, conv3_3.OutputHeight); net.Layers.Add(rl7); MaxPoolLayer pl3 = new MaxPoolLayer(2, 2, rl7.OutputDepth, rl7.OutputWidth, rl7.OutputHeight, 2, 0); net.Layers.Add(pl3); ConvLayer conv4_1 = new ConvLayer(512, 3, 3, pl3.OutputDepth, pl3.OutputWidth, pl3.OutputHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv4_1); ReluLayer rl8 = new ReluLayer(conv4_1.OutputDepth, conv4_1.OutputWidth, conv4_1.OutputHeight); net.Layers.Add(rl8); ConvLayer conv4_2 = new ConvLayer(512, 3, 3, rl8.OutputDepth, rl8.OutputWidth, rl8.OutputHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv4_2); ReluLayer rl9 = new ReluLayer(conv4_2.OutputDepth, conv4_2.OutputWidth, conv4_2.OutputHeight); net.Layers.Add(rl9); ConvLayer conv4_3 = new ConvLayer(512, 3, 3, rl9.OutputDepth, rl9.OutputWidth, rl9.OutputHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv4_3); ReluLayer rl10 = new ReluLayer(conv4_3.OutputDepth, conv4_3.OutputWidth, conv4_3.OutputHeight); net.Layers.Add(rl10); MaxPoolLayer pl4 = new MaxPoolLayer(2, 2, rl10.OutputDepth, rl10.OutputWidth, rl10.OutputHeight, 2, 0); net.Layers.Add(pl4); ConvLayer conv5_1 = new ConvLayer(512, 3, 3, pl4.OutputDepth, pl4.OutputWidth, pl4.OutputHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv5_1); ReluLayer rl11 = new ReluLayer(conv5_1.OutputDepth, conv5_1.OutputWidth, conv5_1.OutputHeight); net.Layers.Add(rl11); ConvLayer conv5_2 = new ConvLayer(512, 3, 3, rl11.OutputDepth, rl11.OutputWidth, rl11.OutputHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv5_2); ReluLayer rl12 = new ReluLayer(conv5_2.OutputDepth, conv5_2.OutputWidth, conv5_2.OutputHeight); net.Layers.Add(rl12); ConvLayer conv5_3 = new ConvLayer(512, 3, 3, rl12.OutputDepth, rl12.OutputWidth, rl12.OutputHeight, 1, 1, 0, 1, 0.1f); net.Layers.Add(conv5_3); ReluLayer rl13 = new ReluLayer(conv5_3.OutputDepth, conv5_3.OutputWidth, conv5_3.OutputHeight); net.Layers.Add(rl13); MaxPoolLayer pl5 = new MaxPoolLayer(2, 2, rl13.OutputDepth, rl13.OutputWidth, rl13.OutputHeight, 2, 0); net.Layers.Add(pl5); FullyConnLayer fc = new FullyConnLayer(4096, pl5.OutputDepth, pl5.OutputWidth, pl5.OutputHeight, 0, 1, 0); net.Layers.Add(fc); ReluLayer rl14 = new ReluLayer(fc.OutputDepth, fc.OutputWidth, fc.OutputHeight); net.Layers.Add(rl14); DropoutLayer d = new DropoutLayer(rl14.OutputDepth, rl14.OutputWidth, rl14.OutputHeight, 0.5f); net.Layers.Add(d); FullyConnLayer fc2 = new FullyConnLayer(4096, d.OutputDepth, d.OutputWidth, d.OutputHeight, 0, 1, 0); net.Layers.Add(fc2); ReluLayer rl15 = new ReluLayer(fc2.OutputDepth, fc2.OutputWidth, fc2.OutputHeight); net.Layers.Add(rl15); DropoutLayer d2 = new DropoutLayer(rl15.OutputDepth, rl15.OutputWidth, rl15.OutputHeight, 0.5f); net.Layers.Add(d2); FullyConnLayer fc3 = new FullyConnLayer(LabelsCount, d2.OutputDepth, d2.OutputWidth, d2.OutputHeight, 0, 1, 0); net.Layers.Add(fc3); SoftmaxLayer sl = new SoftmaxLayer(fc3.OutputDepth, fc3.OutputWidth, fc3.OutputHeight); net.LossLayer = sl; return(net); }