protected ValueTuple <Tensor, List <Tensor> > CreateVisualEncoder(Tensor visualInput, List <SimpleDenseLayerDef> denseLayers, string scope) { //use the same encoder as in UnityML's python codes Tensor temp; List <Tensor> returnWeights = new List <Tensor>(); using (Current.K.name_scope(scope)) { var conv1 = new Conv2D(16, new int[] { 8, 8 }, new int[] { 4, 4 }, use_bias: visualEncoderBias, kernel_initializer: new GlorotUniform(scale: visualEncoderInitialScale), activation: new ELU()); var conv2 = new Conv2D(32, new int[] { 4, 4 }, new int[] { 2, 2 }, use_bias: visualEncoderBias, kernel_initializer: new GlorotUniform(scale: visualEncoderInitialScale), activation: new ELU()); temp = conv1.Call(visualInput)[0]; temp = conv2.Call(temp)[0]; var flatten = new Flatten(); //temp = Current.K.batch_flatten(temp); temp = flatten.Call(temp)[0]; returnWeights.AddRange(conv1.weights); returnWeights.AddRange(conv2.weights); } var output = BuildSequentialLayers(denseLayers, temp, scope); var hiddenFlat = output.Item1; returnWeights.AddRange(output.Item2); return(ValueTuple.Create(hiddenFlat, returnWeights)); }
public override NDArrayOrSymbol HybridForward(NDArrayOrSymbol x, params NDArrayOrSymbol[] args) { var residual = x; x = bn1.Call(x, args); if (x.IsNDArray) { x = nd.Activation(x.NdX, ActivationType.Relu); } else { x = sym.Activation(x.SymX, ActivationType.Relu); } if (ds != null) { residual = ds.Call(x, args); } x = conv1.Call(x, args); x = bn2.Call(x, args); if (x.IsNDArray) { x = nd.Activation(x.NdX, ActivationType.Relu); } else { x = sym.Activation(x.SymX, ActivationType.Relu); } x = conv2.Call(x, args); x = bn3.Call(x, args); if (x.IsNDArray) { x = nd.Activation(x.NdX, ActivationType.Relu); } else { x = sym.Activation(x.SymX, ActivationType.Relu); } x = conv3.Call(x, args); if (x.IsNDArray) { return(x.NdX + residual.NdX); } return(x.SymX + residual.SymX); }
public void TestConv2D() { var inputLayer = UnityTFUtils.Input(shape: new int?[] { 32, 32, 3 }); var conv1 = new Conv2D(16, new int[] { 3, 3 }, padding: PaddingType.Same, activation: new ReLU()); var conv2 = new Conv2D(3, new int[] { 3, 3 }, padding: PaddingType.Same, activation: new ReLU()); var target = UnityTFUtils.Input(shape: new int?[] { 32, 32, 3 }); var pred = conv2.Call(conv1.Call(inputLayer[0])[0])[0]; var lossM = new MeanSquareError(); lossM.Call(target[0], pred); ((UnityTFBackend)K).ExportGraphDef("SavedGraph/convLayer.pb"); }