Example #1
0
    /// <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);
    }