예제 #1
0
    private IEnumerator Generate2()
    {
        Debug.Log("Generate2");
        var nodes = (from node in FindObjectsOfType <BaseNode>()
                     where node.gameObject.layer == 0
                     select node).ToArray();

        yield return(null);

        StringBuilder script = new StringBuilder();

        script.AppendLine("command = TrainConvNet");
        script.AppendLine("makeMode = false ; traceLevel = 0; deviceId = \"auto\"");
        script.AppendLine("rootDir = \".\"; dataDir = \"$rootDir$\"; modelDir = \"$rootDir$/Models\"");
        script.AppendLine("modelPath = \"$modelDir$/test.cmf\"");
        script.AppendLine("TrainConvNet = {");
        script.AppendLine("\taction = \"train\"");
        script.AppendLine("\tBrainScriptNetworkBuilder = {");

        InputNode     reader = null;
        StringBuilder model  = new StringBuilder();
        string        ce     = string.Empty;
        string        errs   = string.Empty;

        model.AppendLine("model(features) = {");

        foreach (var node in nodes)
        {
            if (node.Last.Count == 0 && node.Next.Count == 0)
            {
                continue;
            }
            if (node.Last.Count == 0 && node is InputNode)
            {
                reader = node as InputNode;
            }
            else if (node is EvaluationNode)
            {
                errs = string.Format("errs = {0} ({1}, {2})", node.GetParameters(), "labels", "z");
            }
            else if (node is CriterionNode)
            {
                ce = string.Format("ce = {0} ({1}, {2})", node.GetParameters(), "labels", "z");
            }
            else
            {
                string        layerParam = node.GetParameters();
                StringBuilder convLast   = new StringBuilder();
                if (node.Last.Count > 0)
                {
                    if (node.Last[0] is InputNode)
                    {
                        convLast.Append("features");
                    }
                    else
                    {
                        convLast.Append(node.Last[0].ShortName);
                    }
                    for (int i = 1; i < node.Last.Count; i++)
                    {
                        if (node.Last[i] is InputNode)
                        {
                            convLast.Append(", features");
                            continue;
                        }
                        else
                        {
                            convLast.AppendFormat(", {0}", node.Last[i].ShortName);
                        }
                    }
                }
                model.AppendFormat("{0} = {1}{{ {2} }}({3}){4}", node.ShortName, node.GetType().Name, layerParam, convLast.ToString(), Environment.NewLine);
            }
            yield return(null);
        }
        script.AppendLine(model.ToString() + "}.z");
        script.AppendFormat("\t{0}{1}", reader?.GetFeatures(), Environment.NewLine);
        script.AppendFormat("\t{0}{1}", reader?.GetLabels(), Environment.NewLine);
        script.AppendLine("\tz = model (features)");

        script.AppendLine(ce);
        script.AppendLine(errs);
        script.AppendLine("\tfeaturesNodes = (features)");
        script.AppendLine("\tlabelNodes = (labels)");
        script.AppendLine("\tcriterionNodes = (ce)");
        script.AppendLine("\tevaluationNodes = (errs)");
        script.AppendLine("\toutputNodes = (z)");
        script.AppendLine("}");
        script.AppendLine("reader = {");
        script.AppendLine(reader?.GetParameters());
        script.AppendLine("}");
        script.Append("}");
        yield return(null);

        Debug.Log("Generate complate");
        //Debug.Log(script.ToString());

        codeScreen.GetComponentInChildren <TMPro.TextMeshPro>().text = script.ToString();
        generating = false;
    }