예제 #1
0
    override public void CalcMesh()
    {
        Debug.Log(name);
        if (input == null)
        {
            base.CalcMesh();
            return;
        }

        _mesh.subMeshCount = 3;

        List <Vector3> verts       = new List <Vector3>();
        List <Color>   cols        = new List <Color>();
        List <float>   activations = new List <float>();
        List <int>     inds        = new List <int>();
        List <int>     lineInds    = new List <int>();
        List <int>     polyInds    = new List <int>();

        Vector3 posDiff = new Vector3(0, 0, -zOffset);
        Vector3 zPos    = new Vector3(0, 0, ZPosition());

        AddNodes(verts, inds);
        for (int i = 0; i < verts.Count; i++)
        {
            if (_activationTensorPerEpoch.ContainsKey(epoch))
            {
                int[] index = Util.GetSampleMultiDimIndices(_activationTensorShape, i, GlobalManager.Instance.testSample);

                Array activationTensor = _activationTensorPerEpoch[epoch];
                float tensorVal        = (float)activationTensor.GetValue(index);

                if (allCalculations > 0)
                {
                    activations.Add(tensorVal);
                }

                tensorVal *= pointBrightness;

                cols.Add(new Color(tensorVal, tensorVal, tensorVal, 1f));

                //cols.Add(Color.white);
            }
            else
            {
                cols.Add(Color.black);
            }
        }


        List <List <Shape> > inputFilterPoints = _inputLayer.GetLineStartShapes(convShape, _featureMapResolution, _featureMapTheoreticalResolution, stride, allCalculations);

        inputFilterPoints = _inputLayer.GetLineStartShapes(convShape, _featureMapResolution, _featureMapTheoreticalResolution, stride, allCalculations, this.convLocation);

        //TODO: reuse generated vert positions

        //for each output feature map
        for (int h = 0; h < _featureMaps.Count; h++)
        {
            //line endpoints
            GridShape inputGrid = (GridShape)_featureMaps[h].GetInputGrid(allCalculations);
            Vector3[] end_verts = inputGrid.GetVertices(true);

            Vector3[] edgeBundleCenters = new Vector3[end_verts.Length];

            for (int i = 0; i < edgeBundleCenters.Length; i++)
            {
                edgeBundleCenters[i] = GetEdgeBundleCenter(end_verts[i], edgeBundle);
            }


            //for each input feature map
            for (int i = 0; i < inputFilterPoints.Count; i++)
            {
                //for each input conv grid
                List <Shape> featureMapGrids = inputFilterPoints[i];
                for (int j = 0; j < featureMapGrids.Count; j++)
                {
                    GridShape gridShape   = (GridShape)featureMapGrids[j];
                    Vector3[] start_verts = gridShape.GetVertices(true);

                    verts.Add(end_verts[j] + zPos);
                    cols.Add(Color.black);
                    int start_ind         = verts.Count - 1;
                    int bundle_center_ind = 0;
                    if (edgeBundle > 0)
                    {
                        verts.Add(edgeBundleCenters[j]);
                        cols.Add(Color.black);
                        bundle_center_ind = verts.Count - 1;
                    }
                    for (int k = 0; k < start_verts.Length; k++)
                    {
                        verts.Add(start_verts[k] + zPos + posDiff);

                        if (_weightTensorPerEpoch.ContainsKey(epoch))
                        {
                            Array tensor     = _weightTensorPerEpoch[epoch];
                            int   kernelInd1 = k % convShape.x;
                            int   kernelInd2 = k / convShape.y;

                            int[] index = { kernelInd1, kernelInd2, 0, h };

                            float activationMult = 1f;
                            if (allCalculations > 0 && GlobalManager.Instance.multWeightsByActivations)
                            {
                                activationMult = activations[j];
                            }

                            float tensorVal = (float)tensor.GetValue(index) * weightBrightness * activationMult;
                            cols.Add(new Color(tensorVal, tensorVal, tensorVal, 1f));
                        }
                        else
                        {
                            cols.Add(Color.black);
                        }

                        lineInds.Add(start_ind);
                        if (edgeBundle > 0)
                        {
                            lineInds.Add(bundle_center_ind);
                            lineInds.Add(bundle_center_ind);
                        }
                        lineInds.Add(verts.Count - 1);
                    }
                }
            }
        }

        if (showOriginalDepth)
        {
            AllFeatureMapsDisplay allFeatureMapsDisplay = new AllFeatureMapsDisplay(new Vector3(0, fullResHeight, ZPosition()), fullDepth, lineCircleGrid, new Vector2(allFiltersSpacing, allFiltersSpacing));
            allFeatureMapsDisplay.AddPolysToLists(verts, polyInds);
            GridShape firstFeatureMapGrid = _featureMaps[_featureMaps.Count - 1].GetPixelGrid();
            allFeatureMapsDisplay.AddLinesToLists(verts, lineInds, firstFeatureMapGrid.BboxV(zPos.z));
        }


        //fill with dummy colors
        int diff = verts.Count - cols.Count;

        Debug.Log("diff " + diff + " polyInds " + polyInds.Count + " inds " + inds.Count + " lineInds " + lineInds.Count + " cols " + cols.Count + " verts " + verts.Count);
        for (int i = 0; i < diff; i++)
        {
            cols.Add(Color.white);
        }

        _mesh.SetVertices(verts);
        _mesh.SetColors(cols);
        _mesh.SetIndices(inds.ToArray(), MeshTopology.Points, 0);
        _mesh.SetIndices(lineInds.ToArray(), MeshTopology.Lines, 1);
        _mesh.SetIndices(polyInds.ToArray(), MeshTopology.Triangles, 2);
    }