private static bool CheckLeaveStatus(TreeNodeStruct NodeStruct) //判断是否返回上级目录 { if (MapCount[MapCount.Count - 1] > 0 && MapKey == true & NodeStruct.CurrentNode.Text.Contains("Map")) { MapKey = false; MapCount[MapCount.Count - 1] = MapCount[MapCount.Count - 1] - 1; if (MapCount[MapCount.Count - 1] == 0) { MapCount.RemoveAt(MapCount.Count - 1); return(true); } } else if (MapCount[MapCount.Count - 1] > 0 && MapKey == false & NodeStruct.CurrentNode.Text.Contains("Map")) { MapKey = true; } else if (ListCount[ListCount.Count - 1] > 0 && NodeStruct.CurrentNode.Text.Contains("STRUCT_BEGIN") == false && NodeStruct.CurrentNode.Text.Contains("STRUCT_END") == false & NodeStruct.CurrentNode.Text.Contains("(List)")) { ListCount[ListCount.Count - 1] = ListCount[ListCount.Count - 1] - 1; if (ListCount[ListCount.Count - 1] == 0) { if (ListCount.Count > 1) { ListCount.RemoveAt(ListCount.Count - 1); } return(true); } } return(false); }
void PrepareWeightsWindows() { int maps = (MapCount == null) ? 1 : MapCount.Aggregate(1, (acc, val) => acc * val); weightWindows = new IVector[maps]; for (int m = 0; m < maps; m++) { var w = Offsets.Select(offset => ElementAt(Weights, null, offset, KernelShape, m * kernelSize)); weightWindows[m] = Factory.GetPlainVector(Vector <double> .Build.DenseOfEnumerable(w), EVectorFormat.sparse, WeightsScale); } }
public override int OutputDimension() { if (!layerPrepared) { Prepare(); } int count = Corners.Length; if (Weights == null) { return(count); } int maps = (MapCount == null) ? 1 : MapCount.Aggregate(1, (acc, val) => acc * val); return(count * maps); }
public override void Prepare() { if (!layerPrepared) { convolutionEngine.Prepare(); kernelSize = KernelShape.Aggregate(1, (acc, val) => acc * val); if (Bias == null) { kernelSize++; } if (Weights == null) { return; } PrepareWeightsWindows(); double BiasScale = GetOutputScale(); int maps = (MapCount == null) ? 1 : MapCount.Aggregate(1, (acc, val) => acc * val); if (HotIndices == null) { HotIndices = Vector <double> .Build.Dense(Corners.Length) + 1; } if (Bias != null) { biasVectors = new IVector[maps]; ParallelProcessInEnv(maps, (env, taskIndex, mapIndex) => { biasVectors[mapIndex] = Factory.GetPlainVector(HotIndices * Bias[mapIndex], EVectorFormat.dense, Source.GetOutputScale() * WeightsScale); }); } else { biasVectors = new IVector[maps]; ParallelProcessInEnv(maps, (env, taskIndex, mapIndex) => { biasVectors[mapIndex] = Factory.GetPlainVector(HotIndices * Weights[(mapIndex + 1) * kernelSize - 1], EVectorFormat.dense, Source.GetOutputScale() * WeightsScale); }); } layerPrepared = true; } }
public void Prepare() { if (!prepared) { if (Upperpadding == null) { Upperpadding = new int[InputShape.Length]; } if (Lowerpadding == null) { Lowerpadding = new int[InputShape.Length]; } if (Padding == null) { Padding = new bool[InputShape.Length]; } maps = (MapCount == null) ? 1 : MapCount.Aggregate(1, (acc, val) => acc * val); Corners = CornerGenerator().Select(c => (int[])c.Clone()).ToArray(); prepared = true; } }
public override IMatrix Apply(IMatrix m) { if (Weights == null) // pool without convolve { var res = Factory.GetMatrix(Corners.Select(Corner => SumOnce(m, Corner)).ToArray(), EMatrixFormat.ColumnMajor, CopyVectors: false); ReleaseTemp(); return(res); } else { int maps = (MapCount == null) ? 1 : MapCount.Aggregate(1, (acc, val) => acc * val); if (Bias != null) { if (biasVectors == null || biasVectors[0].Dim != m.RowCount) { if (biasVectors != null) { foreach (var b in biasVectors) { if (b != null) { b.Dispose(); } } } biasVectors = new IVector[maps]; ParallelProcessInEnv(maps, (env, taskIndex, mapIndex) => { var t = Enumerable.Range(0, (int)m.RowCount).Select(i => Bias[mapIndex]); biasVectors[mapIndex] = Factory.GetPlainVector(Vector <double> .Build.DenseOfEnumerable(t), EVectorFormat.dense, Source.GetOutputScale() * WeightsScale); }); } var res = new IVector[maps * Corners.Length]; Console.WriteLine("Pool (bias) layer with {0} maps and {1} locations (total size {2}) kernel size {3}", maps, Corners.Length, res.Length, kernelSize); ParallelProcessInEnv(maps * Corners.Length, (env, currentTask, k) => { int mapIndex = k / Corners.Length; int CornerIndex = k - (mapIndex * Corners.Length); using (var conv = ConvolveOnce(m, CornerIndex, mapIndex, env)) res[k] = conv.Add(biasVectors[mapIndex], env); if (k % 17 == 0) { Console.Write("Done (bias) {0}\r", k); } }); var mat = Factory.GetMatrix(res, EMatrixFormat.ColumnMajor, CopyVectors: false); ReleaseTemp(); return(mat); } else { if (biasVectors == null || biasVectors[0].Dim != m.RowCount) { if (biasVectors != null) { foreach (var b in biasVectors) { if (b != null) { b.Dispose(); } } } biasVectors = new IVector[maps]; ParallelProcessInEnv(maps, (env, taskIndex, mapIndex) => { var t = Enumerable.Range(0, (int)m.RowCount).Select(i => Weights[(mapIndex + 1) * kernelSize - 1]); biasVectors[mapIndex] = Factory.GetPlainVector(Vector <double> .Build.DenseOfEnumerable(t), EVectorFormat.dense, Source.GetOutputScale() * WeightsScale); }); } var res = new IVector[maps * Corners.Length]; Console.WriteLine("Pool (no-bias) layer with {0} maps and {1} locations (total size {2}) kernel size {3}", maps, Corners.Length, res.Length, kernelSize); ParallelProcessInEnv(res.Length, (env, currentTask, k) => { int mapIndex = k / Corners.Length; int CornerIndex = k - (mapIndex * Corners.Length); using (var conv = ConvolveOnce(m, CornerIndex, mapIndex, env)) res[k] = conv.Add(biasVectors[mapIndex], env); if (k % 17 == 0) { Console.Write("Done (bias) {0}\r", k); } }); IMatrix mat = Factory.GetMatrix(res, EMatrixFormat.ColumnMajor, CopyVectors: false); ReleaseTemp(); return(mat); } } }
public static TreeNodeStruct QuickDecodeJce(string RootKey, TreeNodeStruct NodeStruct) { var NodeList = new List <TreeNode>(); if (!string.IsNullOrEmpty(RootKey)) { NodeList.Add(NodeStruct.CurrentNode); var NodeCollection = NodeStruct.CurrentNode.Nodes; TreeNode[] Nodes = NodeCollection.Find(RootKey, true); if (Nodes.Count() > 0) { NodeStruct.CurrentNode = Nodes[0]; } } ///////////貌似得缓存一下,不然有些分叉出现错位现象,这个对大型数据解析的速度影响很大,慢了很多,不知道该怎么处理 ///Thread.Sleep(20); var now = DateTime.Now; while (DateTime.Now < now.AddMilliseconds(15)) { } /////////// string subNode = null; byte[] jceData = null; HeadDataStruct HeadData = new HeadDataStruct(); try { while (NodeStruct.BytesIn.Length > 0) { var len = readHead(NodeStruct.BytesIn, ref HeadData); var Hex = NodeStruct.BytesIn[0].ToString("x2").ToUpper(); NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(len).ToArray(); var typ = HeadData.typ; var tag = HeadData.tag; switch ((int)typ) { case (int)JceType.TYPE_BYTE: { jceData = NodeStruct.BytesIn.Take(1).ToArray(); NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(1).ToArray(); subNode = "Field #" + tag.ToString() + " [" + Hex + "] (Byte) Value=" + jceData[0].ToString() + " Hex=" + BitConverter.ToString(jceData).Replace("-", " "); NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); break; } case (int)JceType.TYPE_SHORT: { jceData = NodeStruct.BytesIn.Take(2).ToArray(); NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(2).ToArray(); subNode = "Field #" + tag.ToString() + " [" + Hex + "] (Short) Value=" + BitConverter.ToInt16(jceData.Reverse().ToArray(), 0).ToString() + " Hex=" + BitConverter.ToString(jceData).Replace("-", " "); NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); break; } case (int)JceType.TYPE_INT: { jceData = NodeStruct.BytesIn.Take(4).ToArray(); NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(4).ToArray(); subNode = "Field #" + tag.ToString() + " [" + Hex + "] (Int) Value=" + BitConverter.ToInt32(jceData.Reverse().ToArray(), 0).ToString() + " Hex=" + BitConverter.ToString(jceData).Replace("-", " "); NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); break; } case (int)JceType.TYPE_LONG: { jceData = NodeStruct.BytesIn.Take(8).ToArray(); NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(8).ToArray(); subNode = "Field #" + tag.ToString() + " [" + Hex + "] (Long) Value=" + BitConverter.ToInt64(jceData.Reverse().ToArray(), 0).ToString() + " Hex=" + BitConverter.ToString(jceData).Replace("-", " "); NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); break; } case (int)JceType.TYPE_FLOAT: { jceData = NodeStruct.BytesIn.Take(4).ToArray(); NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(4).ToArray(); subNode = "Field #" + tag.ToString() + " [" + Hex + "] (Single) Value=" + BitConverter.ToSingle(jceData.Reverse().ToArray(), 0).ToString() + " Hex=" + BitConverter.ToString(jceData).Replace("-", " "); NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); break; } case (int)JceType.TYPE_DOUBLE: { jceData = NodeStruct.BytesIn.Take(8).ToArray(); NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(8).ToArray(); subNode = "Field #" + tag.ToString() + " [" + Hex + "] (Double) Value=" + BitConverter.ToDouble(jceData.Reverse().ToArray(), 0).ToString() + " Hex=" + BitConverter.ToString(jceData).Replace("-", " "); NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); break; } case (int)JceType.TYPE_STRING1: { var jceDatalen = int.Parse(Convert.ToString(NodeStruct.BytesIn.Take(1).ToArray()[0])); NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(1).ToArray(); if (jceDatalen > 0) { if (NodeStruct.BytesIn.Length < jceDatalen) { jceDatalen = NodeStruct.BytesIn.Length; } jceData = NodeStruct.BytesIn.Take(jceDatalen).ToArray(); NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(jceDatalen).ToArray(); var str = Encoding.UTF8.GetString(jceData).Replace("\0", "").Replace("\n", "").Replace("\r\n", "").Replace("\r", "").Replace("\b", "").Replace("\f", "").Replace("\v", ""); subNode = "Field #" + tag.ToString() + " [" + Hex + "] (String) Length=" + str.Length.ToString() + " UTF8=" + str; } else { subNode = "Field #" + tag.ToString() + " [" + Hex + "] (LongString) Length=0 Hex=00"; } NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); break; } case (int)JceType.TYPE_STRING4: { var jceDatalen = BitConverter.ToInt32(NodeStruct.BytesIn.Take(4).Reverse().ToArray(), 0); NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(4).ToArray(); if (jceDatalen > 0) { if (NodeStruct.BytesIn.Length < jceDatalen) { jceDatalen = NodeStruct.BytesIn.Length; } jceData = NodeStruct.BytesIn.Take(jceDatalen).ToArray(); NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(jceDatalen).ToArray(); var str = Encoding.UTF8.GetString(jceData).Replace("\0", "").Replace("\n", "").Replace("\r\n", "").Replace("\r", "").Replace("\b", "").Replace("\f", "").Replace("\v", ""); subNode = "Field #" + tag.ToString() + " [" + Hex + "] (LongString) Length=" + str.Length.ToString() + " UTF8=" + str; } else { subNode = "Field #" + tag.ToString() + " [" + Hex + "] (LongString) Length=0 Hex=00"; } NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); break; } case (int)JceType.TYPE_MAP: { var count = 0; HeadDataStruct HD = new HeadDataStruct(); NodeStruct.BytesIn = SkipLength(NodeStruct.BytesIn, ref count, ref HD); subNode = "Field #" + tag.ToString() + " [" + Hex + "] (Map) Count=" + count.ToString(); if (count > 0) { MapCount.Add(count); MapKey = false; var RandKey = new Random().Next().ToString(); NodeStruct.CurrentNode.Nodes.Add(subNode + RandKey, subNode); NodeStruct = QuickDecodeJce(subNode + RandKey, NodeStruct); } else { NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); } break; } case (int)JceType.TYPE_LIST: { var count = 0; HeadDataStruct HD = new HeadDataStruct(); NodeStruct.BytesIn = SkipLength(NodeStruct.BytesIn, ref count, ref HD); subNode = "Field #" + tag.ToString() + " [" + Hex + "] (List) Count=" + count.ToString(); if (count > 0) { ListCount.Add(count); var RandKey = new Random().Next().ToString(); NodeStruct.CurrentNode.Nodes.Add(subNode + RandKey, subNode); NodeStruct = QuickDecodeJce(subNode + RandKey, NodeStruct); } else { NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); } break; } case (int)JceType.TYPE_STRUCT_BEGIN: { if (tag.ToString() != "0") { subNode = "Field #" + tag.ToString() + " [STRUCT_BEGIN]"; } else { subNode = "[STRUCT_BEGIN]"; } var RandKey = new Random().Next().ToString(); NodeStruct.CurrentNode.Nodes.Add(subNode + RandKey, subNode); NodeStruct = QuickDecodeJce(subNode + RandKey, NodeStruct); NodeStruct.CurrentNode.Nodes.Add("STRUCT_END"); break; } case (int)JceType.TYPE_STRUCT_END: { goto ExitLabel1; } case (int)JceType.TYPE_ZERO_TAG: { subNode = "Field #" + tag.ToString() + " [" + Hex + "] (Zero) Value=0"; NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); break; } case (int)JceType.TYPE_SIMPLE_LIST: { HeadDataStruct HD = new HeadDataStruct(); readHead(NodeStruct.BytesIn, ref HD); NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(1).ToArray(); var jceDatalen = 0; NodeStruct.BytesIn = SkipLength(NodeStruct.BytesIn, ref jceDatalen, ref HD); if (jceDatalen > 0) { subNode = "Field #" + tag.ToString() + " [" + Hex + "] (SimpleList) Length=" + jceDatalen.ToString(); jceData = NodeStruct.BytesIn.Take(jceDatalen).ToArray(); if (jceData[0] == (byte)JceType.TYPE_STRUCT_BEGIN || jceData[0] == (byte)JceType.TYPE_LIST || jceData[0] == (byte)JceType.TYPE_SIMPLE_LIST || (jceData[0] == (byte)JceType.TYPE_MAP && jceData[1] == 0)) { var RandKey = new Random().Next().ToString(); NodeStruct.CurrentNode.Nodes.Add(subNode + RandKey, subNode); var DataRemain = NodeStruct.BytesIn.Skip(jceDatalen).ToArray(); NodeStruct.BytesIn = jceData; QuickDecodeJce(subNode + RandKey, NodeStruct); NodeStruct.BytesIn = DataRemain; } else { NodeStruct.BytesIn = NodeStruct.BytesIn.Skip(jceDatalen).ToArray(); subNode = "Field #" + tag.ToString() + " [" + Hex + "] (SimpleList) Length=" + jceData.Length.ToString() + " Hex=" + BitConverter.ToString(jceData).Replace("-", ""); NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); } } else { subNode = "Field #" + tag.ToString() + " [" + Hex + "] (SimpleList) Length=0"; NodeStruct.CurrentNode.Nodes.Add(new TreeNode(subNode)); } MapKey = true; break; } } if (CheckLeaveStatus(NodeStruct) == true) { break; } } ExitLabel1 :; } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } if (!string.IsNullOrEmpty(RootKey)) { TreeNode Parent = NodeStruct.CurrentNode.Parent; if (Parent != null) { NodeStruct.CurrentNode = Parent; } } return(NodeStruct); }