private void InitGrids() { _pixelGrid = new GridShape(position, _shape, Get2DSpacing()); _outputGrid = new GridShape(_outputPosition, _shape, Get2DSpacing()); _allCalcFilterGrids = new List <Shape>(); Vector2 safeSpacing = new Vector2(0.0f, 0.0f); if (_convShape.x > 1) { safeSpacing = (_shape.x - 1) / (float)(_convShape.x - 1) * Get2DSpacing(); } _filterGrid = new GridShape(_outputPosition, _convShape, safeSpacing); Vector3[] allCalcPositions; if (_outputShape == _shape) //means stride == 1 { allCalcPositions = _pixelGrid.GetVertices(true); } else { _outputGrid = new GridShape(_outputPosition, _outputShape, Get2DSpacing() * stride.x); allCalcPositions = _outputGrid.GetVertices(true); } for (int i = 0; i < allCalcPositions.Length; i++) { _allCalcFilterGrids.Add(new GridShape(allCalcPositions[i], _convShape, safeSpacing)); } }
/// <summary> /// Returns the vertices of the grid used for the full res visualization /// </summary> /// <returns></returns> private Vector3[] fullResGridVerts() { int root = Mathf.CeilToInt(Mathf.Sqrt(fullDepth)); GridShape grid = new GridShape(CenterPosition(), new Vector2Int(root, root), new Vector2(filterSpread, filterSpread)); return(grid.GetVertices(false)); }
/// <summary> /// As we require much less vertices and line polygons when the edge bundling is turned up to 1.0, the mesh is calculated differently (in this function). /// </summary> /// <param name="verts"></param> /// <param name="lineInds"></param> /// <param name="inputFilterPoints"></param> void AddFullyBundledLines(List <Vector3> verts, List <int> lineInds, List <List <Shape> > inputFilterPoints) { Vector3 posDiff = new Vector3(0, 0, -zOffset); Vector3 zPos = new Vector3(0, 0, ZPosition()); Vector3 end_vert0 = _nodePositions[0]; Vector3 edgeBundleCenter = GetEdgeBundleCenter(end_vert0, 1f); verts.Add(edgeBundleCenter); int bundle_center_ind = verts.Count - 1; //for each of this Layers Points for (int h = 0; h < _nodePositions.Count; h++) { //line endpoint Vector3 end_vert = _nodePositions[h]; verts.Add(end_vert + zPos); int end_ind = verts.Count - 1; lineInds.Add(end_ind); lineInds.Add(bundle_center_ind); } //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]; //scale shape spacing by collapse input gridShape.spacing *= (1.0f - collapseInput); Vector3[] start_verts = gridShape.GetVertices(true); //for each conv point for (int k = 0; k < start_verts.Length; k++) { lineInds.Add(bundle_center_ind); verts.Add(start_verts[k] + zPos + posDiff); lineInds.Add(verts.Count - 1); } } } }
private void UpdateGrids() { _pixelGrid.position = position; _pixelGrid.resolution = _shape; _pixelGrid.spacing = Get2DSpacing(); _outputGrid.position = _outputPosition; _outputGrid.resolution = _outputShape; _outputGrid.spacing = Get2DSpacing() * stride; _filterGrid.position = _outputPosition; Vector2 safeSpacing = new Vector2(0.0f, 0.0f); if (_convShape.x > 1) { safeSpacing = (_shape.x - 1) / (float)(_convShape.x - 1) * Get2DSpacing(); } ((GridShape)_filterGrid).spacing = safeSpacing; Vector3[] allCalcPositions; if (_outputShape == _shape) //means stride == 1 { allCalcPositions = _pixelGrid.GetVertices(true); } else { _outputGrid.position = _outputPosition + new Vector3(0, 1.0f, 0); _outputGrid.resolution = _outputShape; _outputGrid.spacing = safeSpacing; allCalcPositions = _outputGrid.GetVertices(true); } for (int i = 0; i < allCalcPositions.Length; i++) { ((GridShape)_allCalcFilterGrids[i]).position = allCalcPositions[i]; ((GridShape)_allCalcFilterGrids[i]).resolution = _convShape; ((GridShape)_allCalcFilterGrids[i]).spacing = safeSpacing; } }
protected void AddNodes(List <Vector3> verts, List <int> inds, List <int> fullResInds, List <int> lineInds) { base.AddNodes(verts, inds); if (showOriginalResolution) { Vector3[] v = _fullResGrid.GetVertices(true); verts.AddRange(v); for (int i = 0; i < v.Length; i++) { fullResInds.Add(verts.Count - v.Length + i); } GridShape centerGrid = _featureMaps[_featureMaps.Count / 2].GetPixelGrid(); float[] bbox = centerGrid.GetBbox(); float[] zpos = { 0, -fullresOffset }; List <int> lineStartEndInds = new List <int>(); foreach (float z in zpos) { verts.Add(new Vector3(bbox[0], bbox[1], centerGrid.position.z + z)); lineStartEndInds.Add(verts.Count - 1); verts.Add(new Vector3(bbox[0], bbox[3], centerGrid.position.z + z)); lineStartEndInds.Add(verts.Count - 1); verts.Add(new Vector3(bbox[2], bbox[1], centerGrid.position.z + z)); lineStartEndInds.Add(verts.Count - 1); verts.Add(new Vector3(bbox[2], bbox[3], centerGrid.position.z + z)); lineStartEndInds.Add(verts.Count - 1); } for (int i = 0; i < 4; i++) { lineInds.Add(lineStartEndInds[i]); lineInds.Add(lineStartEndInds[i + 4]); } } }
override public void CalcMesh() { if (input == null) { base.CalcMesh(); return; } _mesh.subMeshCount = 2; //POINTS List <Vector3> verts = new List <Vector3>(); List <Color> cols = new List <Color>(); List <float> activations = new List <float>(); List <int> inds = 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); activations.Add(tensorVal); tensorVal *= pointBrightness; cols.Add(new Color(tensorVal, tensorVal, tensorVal, 1f)); //cols.Add(Color.white); } else { cols.Add(Color.black); } } if (showOriginalDepth) { AddFullResNodes(verts, inds); } int diff = verts.Count - cols.Count; for (int i = 0; i < diff; i++) { cols.Add(Color.black); } //LINES Vector2Int inputShape = new Vector2Int(1, 1); if (collapseInput != 1.0f) { inputShape = new Vector2Int(_inputLayer.GetOutputShape().x, _inputLayer.GetOutputShape().y); } //Input Layer Points, each inner List only contains one point List <List <Shape> > inputFilterPoints = _inputLayer.GetLineStartShapes(inputShape, new Vector2Int(1, 1), new Vector2(1, 1), new Vector2Int(1, 1), 0.0f); List <int> lineInds = new List <int>(); if (edgeBundle == 1f) { AddFullyBundledLines(verts, lineInds, inputFilterPoints); diff = verts.Count - cols.Count; for (int i = 0; i < diff; i++) { cols.Add(Color.black); } } else { //for each of this Layers Points for (int h = 0; h < _nodePositions.Count; h++) { //line endpoint Vector3 end_vert = _nodePositions[h]; verts.Add(end_vert + zPos); cols.Add(Color.black); int end_ind = verts.Count - 1; Vector3 edgeBundleCenter = GetEdgeBundleCenter(end_vert, edgeBundle); int bundle_center_ind = 0; if (edgeBundle > 0) { verts.Add(edgeBundleCenter); cols.Add(Color.black); bundle_center_ind = verts.Count - 1; } //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]; //scale shape spacing by collapse input gridShape.spacing *= (1.0f - collapseInput); Vector3[] start_verts = gridShape.GetVertices(true); //for each conv point for (int k = 0; k < start_verts.Length; k++) { lineInds.Add(end_ind); if (edgeBundle > 0) { lineInds.Add(bundle_center_ind); lineInds.Add(bundle_center_ind); } verts.Add(start_verts[k] + zPos + posDiff); if (_tensorPerEpoch.ContainsKey(epoch)) { Array tensor = _tensorPerEpoch[epoch]; int[] index = { i *k, h }; if (_inputLayer.GetType().Equals(typeof(FCLayer))) { index[0] = j; } float activationMult = 1f; if (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(verts.Count - 1); } } } } } _mesh.SetVertices(verts); _mesh.SetColors(cols); _mesh.SetIndices(inds.ToArray(), MeshTopology.Points, 0); _mesh.SetIndices(lineInds.ToArray(), MeshTopology.Lines, 1); Debug.Log("vertcount: " + verts.Count); }
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); }
override public void CalcMesh() { if (input == null) { base.CalcMesh(); return; } _mesh.subMeshCount = 2; List <Vector3> verts = new List <Vector3>(); List <Color> cols = new List <Color>(); List <int> inds = 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) * 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); List <int> lineInds = new List <int>(); //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 conv grid List <Shape> featureMapGrids = inputFilterPoints[h]; for (int j = 0; j < featureMapGrids.Count; j++) { GridShape gridShape = (GridShape)featureMapGrids[j]; Vector3[] start_verts = gridShape.GetVertices(true); if (j >= end_verts.Length) { continue; } verts.Add(end_verts[j] + zPos); int start_ind = verts.Count - 1; int bundle_center_ind = 0; if (edgeBundle > 0) { verts.Add(edgeBundleCenters[j]); bundle_center_ind = verts.Count - 1; } for (int k = 0; k < start_verts.Length; k++) { verts.Add(start_verts[k] + zPos + posDiff); lineInds.Add(start_ind); if (edgeBundle > 0) { lineInds.Add(bundle_center_ind); lineInds.Add(bundle_center_ind); } lineInds.Add(verts.Count - 1); } } } _mesh.SetVertices(verts); int diff = verts.Count - cols.Count; for (int i = 0; i < diff; i++) { cols.Add(Color.black); } _mesh.SetColors(cols); _mesh.SetIndices(inds.ToArray(), MeshTopology.Points, 0); _mesh.SetIndices(lineInds.ToArray(), MeshTopology.Lines, 1); }