示例#1
0
文件: Form1.cs 项目: fel88/Dendrite
        public void ExportAsZip(string zipPath, GraphModel model)
        {
            int cntr = 0;

            if (File.Exists(zipPath))
            {
                File.Delete(zipPath);
            }
            using (FileStream zipToOpen = new FileStream(zipPath, FileMode.CreateNew))
            {
                using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("<?xml version=\"1.0\"?>");
                    sb.AppendLine("<net>");

                    ZipArchiveEntry wg = archive.CreateEntry("weights/");
                    foreach (var item in model.Nodes)
                    {
                        sb.AppendLine($"<node id=\"{item.Id}\" name=\"{item.Name}\" opType=\"{item.LayerType.ToString()}\">");
                        sb.AppendLine("<attributes>");
                        foreach (var attr in item.Attributes)
                        {
                            sb.AppendLine($"<attr name=\"{attr.Name}\" value=\"{(attr.Tag as AttributeProto).AsString()}\"/>");
                        }
                        sb.AppendLine("</attributes>");
                        sb.AppendLine("<childs>");
                        foreach (var attr in item.Childs)
                        {
                            sb.AppendLine($"<child id=\"{attr.Id}\"/>");
                        }
                        sb.AppendLine("</childs>");
                        sb.AppendLine("<parents>");
                        foreach (var attr in item.Parents)
                        {
                            sb.AppendLine($"<parent id=\"{attr.Id}\"/>");
                        }
                        sb.AppendLine("</parents>");
                        foreach (var w in item.Data)
                        {
                            if (w.Weights == null || w.Weights.Length == 0)
                            {
                                continue;
                            }
                            sb.AppendLine($"<weights file=\"{cntr}.dat\" name=\"{w.Name}\" dims=\"{w.Dims.Aggregate("", (x, y) => x + y + ",").TrimEnd(',')}\"/>");


                            List <byte>  bb = new List <byte>();
                            MemoryStream ms = new MemoryStream();

                            foreach (var bitem in w.Weights)
                            {
                                foreach (var b in BitConverter.GetBytes(bitem))
                                {
                                    ms.WriteByte(b);
                                }
                            }
                            ms.Seek(0, SeekOrigin.Begin);
                            ZipArchiveEntry w1 = archive.CreateEntry($"weights/{cntr++}.dat");

                            using (StreamWriter writer = new StreamWriter(w1.Open()))
                            {
                                ms.CopyTo(writer.BaseStream);
                            }
                        }
                        sb.AppendLine($"</node>");
                    }



                    sb.AppendLine("</net>");
                    var xpath = "info.xml";

                    ZipArchiveEntry ann = archive.CreateEntry(xpath);

                    using (StreamWriter writer = new StreamWriter(ann.Open()))
                    {
                        writer.Write(sb.ToString());
                    }
                }
            }
        }
示例#2
0
文件: Form1.cs 项目: fel88/Dendrite
        public bool LoadModel(string path, bool _fitAll = true)
        {
            if (ParentForm != null)
            {
                ParentForm.FormClosing += ParentForm_FormClosing;
            }
            _lastPath = path;
            var fr = Providers.FirstOrDefault(z => z.IsSuitableFile(path));

            if (fr == null)
            {
                MessageBox.Show("Unsupported file format.", WindowCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            Stopwatch  sw = new Stopwatch();
            WaitDialog wd = new WaitDialog();

            timer1.Enabled = false;
            Action loadAct = () =>
            {
                sw.Start();
                var model = fr.LoadFromFile(path);
                Model = model;
                if (!loadedModels.Any(z => z.ToLower() == path.ToLower()))
                {
                    loadedModels.Add(path);
                }

                listView1.Items.Clear();

                foreach (var item in model.Nodes)
                {
                    //listView1.Items.Add(new ListViewItem(new string[] { item.Name, ss, item.Output[0] }) { Tag = nodes[i] });
                }

                //var cnt2 = res2.Graph.Output[0].Name;
                //nodes.InsertRange(0, res2.Graph.Input.Select(z => outs[z.Name]));

                updateNodesSizes();
                CurrentLayout.GetRenderTextWidth = renderTextWidth;
                CurrentLayout.Layout(Model);

                //Text = $"{WindowCaption}: {Path.GetFileName(path)}";
                if (ParentForm != null)
                {
                    ParentForm.Invoke((Action)(() =>
                    {
                        ParentForm.Text = Path.GetFileName(path);
                    }));
                }
                drawEnabled = true;
                reset.Set();
                if (_fitAll)
                {
                    fitAll();
                }
                sw.Stop();
            };

            drawEnabled = false;

            wd.Init(loadAct);
            wd.ShowDialog();
            timer1.Enabled = true;
            if (wd.Exception != null)
            {
                Helpers.ShowError(wd.Exception.Message, Program.MainForm.Text);
            }
            Program.MainForm.SetStatusMessage($"Load time: {sw.ElapsedMilliseconds} ms");

            return(true);
        }
示例#3
0
 public abstract void Layout(GraphModel model);
示例#4
0
 internal void SetModel(GraphModel graph, ITag model)
 {
     this.graph = graph;
     this.model = model;
 }
示例#5
0
 public override void UpdateIntAttributeValue(GraphModel model, GraphNode parentNode, string name, int val)
 {
     throw new NotImplementedException();
 }
示例#6
0
 public override void AppendToOutput(GraphModel model, GraphNode node)
 {
     throw new NotImplementedException();
 }
示例#7
0
 public override void UpdateFloatTensor(GraphModel model, GraphNode parentNode, string name, float[] data, long[] dims)
 {
     throw new NotImplementedException();
 }
示例#8
0
 public override void SaveModel(GraphModel model, string path)
 {
     throw new NotImplementedException();
 }
示例#9
0
 public void SetModel(GraphModel model)
 {
     this.model = model;
 }