Beispiel #1
0
        public static bool Prefix(PlayerControlGizmo __instance, EObjectType tarType, int tarId)
        {
            if (__instance.mouseOverTargetType == tarType && __instance.mouseOverTargetId == tarId)
            {
                return(true);
            }
            if (tarId == 0 || tarType != EObjectType.Entity)
            {
                return(true);
            }

            PlanetFactory factory    = __instance.player.factory;
            EntityData    entityData = factory.entityPool[tarId];

            if (entityData.id == 0)
            {
                return(true);
            }

            ItemProto itemProto = LDB.items.Select(entityData.protoId);

            if (itemProto.ModelCount > 1)
            {
                __instance.mouseOverTargetType = tarType;
                __instance.mouseOverTargetId   = tarId;
                if (__instance.mouseOverTargetGizmo != null)
                {
                    __instance.mouseOverTargetGizmo.Close();
                    __instance.mouseOverTargetGizmo = null;
                }

                if (__instance.mouseOverBuildingGizmo != null)
                {
                    __instance.mouseOverBuildingGizmo.Close();
                    __instance.mouseOverBuildingGizmo = null;
                }

                if (__instance.mouseOverMinerGizmo != null)
                {
                    __instance.mouseOverMinerGizmo.Close();
                    __instance.mouseOverMinerGizmo = null;
                }

                ModelProto proto = LDB.models.modelArray[entityData.modelIndex];
                __instance.mouseOverBuildingGizmo                 = BoxGizmo.Create(entityData.pos, entityData.rot, proto.prefabDesc.selectCenter, proto.prefabDesc.selectSize);
                __instance.mouseOverBuildingGizmo.multiplier      = 1f;
                __instance.mouseOverBuildingGizmo.alphaMultiplier = proto.prefabDesc.selectAlpha;
                __instance.mouseOverBuildingGizmo.fadeInScale     = 1.3f;
                __instance.mouseOverBuildingGizmo.fadeInTime      = 0.05f;
                __instance.mouseOverBuildingGizmo.fadeInFalloff   = 0.5f;
                __instance.mouseOverBuildingGizmo.fadeOutScale    = 1.3f;
                __instance.mouseOverBuildingGizmo.fadeOutTime     = 0.05f;
                __instance.mouseOverBuildingGizmo.fadeOutFalloff  = 0.5f;
                __instance.mouseOverBuildingGizmo.color           = Color.white;
                __instance.mouseOverBuildingGizmo.Open();
                return(false);
            }

            return(true);
        }
        // Asset importer callback
        public override void OnImportAsset(AssetImportContext ctx)
        {
            var onnxModel = new ModelProto();

            using (var readStream = new FileStream(ctx.assetPath, FileMode.Open))
                using (var inputStream = new CodedInputStream(readStream))
                    onnxModel.MergeFrom(inputStream);

            var model = ConvertOnnxModel(onnxModel);

            D.Log($"ONNX v{model.IrVersion}. Producer: {model.ProducerName}. Asset path: {ctx.assetPath}");
            D.Log($"Barracuda model: {model}");

            NNModelData assetData = ScriptableObject.CreateInstance <NNModelData>();

            using (var memoryStream = new MemoryStream())
                using (var writer = new BinaryWriter(memoryStream))
                {
                    ModelWriter.Save(writer, model);
                    assetData.Value = memoryStream.ToArray();
                }
            assetData.name      = "Data";
            assetData.hideFlags = HideFlags.HideInHierarchy;

            NNModel asset = ScriptableObject.CreateInstance <NNModel>();

            asset.modelData = assetData;

            ctx.AddObjectToAsset("main obj", asset, LoadIconTexture());
            ctx.AddObjectToAsset("model data", assetData);

            ctx.SetMainObject(asset);
        }
Beispiel #3
0
        //     Call to set all tensors with raw data as external data. This call should preceed 'save_model'.
        //     'save_model' saves all the tensors data as external data after calling this function.
        //     @params
        //     model: ModelProto to be converted.
        //     all_tensors_to_one_file: If true, save all tensors to one external file specified by location.
        //                              If false, save each tensor to a file named with the tensor name.
        //     location: specify the external file that all tensors to save to.
        //               If not specified, will use the model name.
        //     size_threshold: Threshold for size of data. Only when tensor's data is >= the size_threshold
        //     it will be converted to external data. To convert every tensor with raw data to external data set size_threshold=0.
        //
        public static void ConvertModelToExternalData(ModelProto model, bool all_tensors_to_one_file = true, string location = null, int size_threshold = 1024)
        {
            if (all_tensors_to_one_file)
            {
                var file_name = Guid.NewGuid().ToString();
                if (location != null)
                {
                    file_name = location;
                }
                foreach (var tensor in _get_all_tensors(model))
                {
                    if (tensor.RawData != null && tensor.RawData.Length >= size_threshold)
                    {
                        SetExternalData(tensor, file_name);
                    }
                }
            }
            else
            {
                foreach (var tensor in _get_all_tensors(model))
                {
                    if (tensor.RawData != null && tensor.RawData.Length >= size_threshold)
                    {
                        var tensor_location = tensor.Name;
                        if (!_is_valid_filename(tensor_location))
                        {
                            tensor_location = Guid.NewGuid().ToString();
                        }

                        SetExternalData(tensor, tensor_location);
                    }
                }
            }
        }
Beispiel #4
0
        private static List <TensorProto> _get_all_tensors(ModelProto onnx_model_proto)
        {
            List <TensorProto> result = _get_initializer_tensors(onnx_model_proto).ToList();

            result.AddRange(_get_attribute_tensors(onnx_model_proto));
            return(result);
        }
Beispiel #5
0
        public static ModelProto ConvertVersion(ModelProto model, int target_version)
        {
            var model_str           = model.ToByteArray();
            var converted_model_str = C.convert_version(model_str, target_version);

            return(onnx.LoadModelFromString(converted_model_str));
        }
Beispiel #6
0
 public Extractor(ModelProto model)
 {
     this.model = ShapeInference.infer_shapes(model);
     this.graph = this.model.Graph;
     this.wmap  = this._build_name2obj_dict(this.graph.Initializer.ToArray());
     this.vimap = this._build_name2obj_dict(this.graph.ValueInfo.ToArray());
 }
        private void SaveOnnxModel(ModelProto model, string binaryFormatPath, string textFormatPath)
        {
            DeleteOutputPath(binaryFormatPath); // Clean if such a file exists.
            DeleteOutputPath(textFormatPath);

            if (binaryFormatPath != null)
            {
                using (var file = Env.CreateOutputFile(binaryFormatPath))
                    using (var stream = file.CreateWriteStream())
                        model.WriteTo(stream);
            }

            if (textFormatPath != null)
            {
                using (var file = Env.CreateOutputFile(textFormatPath))
                    using (var stream = file.CreateWriteStream())
                        using (var writer = new StreamWriter(stream))
                        {
                            var parsedJson = JsonConvert.DeserializeObject(model.ToString());
                            writer.Write(JsonConvert.SerializeObject(parsedJson, Formatting.Indented));
                        }

                // Strip the version information.
                var fileText = File.ReadAllText(textFormatPath);

                fileText = Regex.Replace(fileText, "\"producerVersion\": \".*\"", "\"producerVersion\": \"##VERSION##\"");
                File.WriteAllText(textFormatPath, fileText);
            }
        }
Beispiel #8
0
        private ModelProto ToProto(string[] parameters)
        {
            int modelId       = int.Parse(parameters[0]);
            int inputTensorId = int.Parse(parameters[1]);

            Debug.Log("<color=yellow>Serialize to ONNX, modelId:" + modelId.ToString() + ", inputTensorId:" + inputTensorId.ToString() + "</color>");

            Model      model = this.GetModel(modelId);
            ModelProto m     = new ModelProto
            {
                IrVersion   = 2,
                OpsetImport = { new OperatorSetIdProto
                                {
                                    Domain  = "", // operator set that is defined as part of the ONNX specification
                                    Version = 2
                                } },
                ProducerName    = "openmined",
                ProducerVersion = "0.0.2",
                Domain          = "org.openmined",
                ModelVersion    = 0,
                DocString       = "",
                Graph           = model.GetProto(inputTensorId, this),
            };

            return(m);
        }
Beispiel #9
0
        private static bool PreloadPatch(ItemProto __instance, int _index)
        {
            ModelProto modelProto = LDB.models.modelArray[__instance.ModelIndex];

            if (modelProto != null)
            {
                Vector3 lapJoint = Vector3.zero;
                if (__instance.ID == 2303 || __instance.ID == 2304 || __instance.ID == 2305)
                {
                    lapJoint = new Vector3(0, 5.1f, 0);
                }
                else if (__instance.ID == 2302)
                {
                    lapJoint = new Vector3(0, 4.3f, 0);
                }
                else if (__instance.ID == 2309)
                {
                    lapJoint = new Vector3(0, 7.0f, 0);
                }
                else if (__instance.ID == 2308)
                {
                    lapJoint = new Vector3(0, 16.0f, 0);
                }
                if (lapJoint != Vector3.zero)
                {
                    LDB.models.modelArray[__instance.ModelIndex].prefabDesc.multiLevel = true;
                    LDB.models.modelArray[__instance.ModelIndex].prefabDesc.multiLevelAllowInserter = true;
                    LDB.models.modelArray[__instance.ModelIndex].prefabDesc.lapJoint = lapJoint;
                }
            }
            return(true);
        }
        public static ModelProto infer_shapes(ModelProto model, bool check_type = false, bool strict_mode = false)
        {
            var model_str          = model.ToByteArray();
            var inferred_model_str = C.infer_shapes(model_str, check_type, strict_mode);

            return(onnx.LoadModelFromString(inferred_model_str));
        }
Beispiel #11
0
        public static ModelProto MakeModel(List <NodeProto> nodes, string producerName, string name,
                                           string domain, string producerVersion, long modelVersion, List <ModelArgs> inputs,
                                           List <ModelArgs> outputs, List <ModelArgs> intermediateValues, List <TensorProto> initializers)
        {
            Contracts.CheckValue(nodes, nameof(nodes));
            Contracts.CheckValue(inputs, nameof(inputs));
            Contracts.CheckValue(outputs, nameof(outputs));
            Contracts.CheckValue(intermediateValues, nameof(intermediateValues));
            Contracts.CheckValue(initializers, nameof(initializers));
            Contracts.CheckNonEmpty(producerName, nameof(producerName));
            Contracts.CheckNonEmpty(name, nameof(name));
            Contracts.CheckNonEmpty(domain, nameof(domain));
            Contracts.CheckNonEmpty(producerVersion, nameof(producerVersion));

            var model = new ModelProto();

            model.Domain          = domain;
            model.ProducerName    = producerName;
            model.ProducerVersion = producerVersion;
            model.IrVersion       = (long)UniversalModelFormat.Onnx.Version.IrVersion;
            model.ModelVersion    = modelVersion;
            model.OpsetImport.Add(new OperatorSetIdProto()
            {
                Domain = "ai.onnx.ml", Version = 1
            });
            model.OpsetImport.Add(new OperatorSetIdProto()
            {
                Domain = "", Version = 7
            });
            model.Graph = new GraphProto();
            var graph = model.Graph;

            graph.Node.Add(nodes);
            graph.Name = name;
            foreach (var arg in inputs)
            {
                var val = new ValueInfoProto();
                graph.Input.Add(val);
                MakeValue(val, arg.Name, arg.DataType, arg.Dims, arg.DimParams);
            }

            foreach (var arg in outputs)
            {
                var val = new ValueInfoProto();
                graph.Output.Add(val);
                MakeValue(val, arg.Name, arg.DataType, arg.Dims, arg.DimParams);
            }

            foreach (var arg in intermediateValues)
            {
                var val = new ValueInfoProto();
                graph.ValueInfo.Add(val);
                MakeValue(val, arg.Name, arg.DataType, arg.Dims, arg.DimParams);
            }

            graph.Initializer.AddRange(initializers);

            return(model);
        }
        public void AddBPGPUIModel(BuildPreview preview)
        {
            if (preview == null || preview.bpgpuiModelId <= 0)
            {
                return;
            }

            if (!preview.needModel)
            {
                return;
            }

            ModelProto modelProto = LDB.models.Select(preview.desc.modelIndex);
            Color32    color      = Configs.builtin.copyErrorColor;

            if (modelProto.RendererType == 2)
            {
                GetInserterT1T2(preview.objId, out bool flag, out bool flag2);

                if (preview.objId > 0)
                {
                    animPool[preview.bpgpuiModelId] = factory.entityAnimPool[preview.objId];
                }

                animPool[preview.bpgpuiModelId].state = (uint)((color.r << 24) + (color.g << 16) + (color.b << 8) + color.a);
                planet.factoryModel.bpgpuiManager.AddBuildPreviewModel(preview.desc.modelIndex, out preview.bpgpuiModelInstIndex, preview.bpgpuiModelId,
                                                                       preview.lpos, preview.lrot, preview.lpos2, preview.lrot2, flag ? 1 : 0, flag2 ? 1 : 0, false);
                return;
            }

            if (modelProto.RendererType == 3)
            {
                factory.ReadObjectConn(preview.objId, 14, out bool _, out int num, out int _);

                if (preview.objId > 0)
                {
                    animPool[preview.bpgpuiModelId] = factory.entityAnimPool[preview.objId];
                }

                animPool[preview.bpgpuiModelId].state = (uint)((color.r << 24) + (color.g << 16) + (color.b << 8) + color.a);
                planet.factoryModel.bpgpuiManager.AddBuildPreviewModel(preview.desc.modelIndex, out preview.bpgpuiModelInstIndex, preview.bpgpuiModelId,
                                                                       preview.lpos, preview.lrot, num != 0 ? 1U : 0U, false);
                return;
            }

            if (preview.objId > 0)
            {
                animPool[preview.bpgpuiModelId] = factory.entityAnimPool[preview.objId];
            }

            animPool[preview.bpgpuiModelId].state = (uint)((color.r << 24) + (color.g << 16) + (color.b << 8) + color.a);
            if (preview.objId > 0 && preview.desc.isEjector)
            {
                animPool[preview.bpgpuiModelId].power = factory.factorySystem.ejectorPool[factory.entityPool[preview.objId].ejectorId].localDir.z;
            }

            planet.factoryModel.bpgpuiManager.AddBuildPreviewModel(preview.desc.modelIndex, out preview.bpgpuiModelInstIndex, preview.bpgpuiModelId,
                                                                   preview.lpos, preview.lrot, false);
        }
        static void AssertModelBytesEqualToEmbeddedExpected(ModelProto model, string expectedName)
        {
            var actualBytes = model.ToByteArray();
            //model.WriteToFile(expectedName);
            var expectedBytes = AssemblyResourceLoader.GetBytes(expectedName);

            CollectionAssert.AreEqual(expectedBytes, actualBytes);
        }
Beispiel #14
0
    protected override void Run(ModelProto model)
    {
        var writer = _console.Out;

        writer.WriteLine($"# {Input}");
        writer.WriteLine();

        model.Graph.Info(writer);
    }
Beispiel #15
0
        public static ModelProto WriteFile(this ModelProto Model, string filename)
        {
            using (var output = File.Create(filename))
            {
                Model.WriteTo(output);
            }

            return(Model);
        }
Beispiel #16
0
 public void Init(ModelProto model, NodeProto proto, GraphNode gnode)
 {
     this.model = model;
     Text       = "Edit node: " + proto.Name;
     this.node  = proto;
     updateList();
     textBox3.Text = proto.Name;
     this.gnode    = gnode;
 }
Beispiel #17
0
    protected override void Run(ModelProto model)
    {
        // Should this not be before loading input? Is the abstract base really that good?

        var dimParamOrValue = int.TryParse(Dim, out var dimValue)
            ? DimParamOrValue.New(dimValue)
            : DimParamOrValue.New(Dim);

        _console.WriteLine($"Setting dimension at {Index} to '{dimParamOrValue}'");

        model.Graph.SetDim(Index, dimParamOrValue);
    }
Beispiel #18
0
        public static ModelProto WriteExternalDataTensors(ModelProto model, string filepath)
        {
            foreach (var tensor in _get_all_tensors(model))
            {
                if (UsesExternalData(tensor))
                {
                    SaveExternalData(tensor, filepath);
                    tensor.RawData = null;
                }
            }

            return(model);
        }
Beispiel #19
0
        public void AfterLoad()
        {
            ItemProto item = LDB.items.Select(2212);

            item.ModelIndex = firstSubstationId;
            item.ModelCount = 3;
            ModelProto modelProto = LDB.models.modelArray[item.ModelIndex];

            if (modelProto != null)
            {
                item.prefabDesc = modelProto.prefabDesc;
            }
        }
Beispiel #20
0
        public static PrefabDesc GetPrefabDesc(BuildingCopy copiedBuilding)
        {
            ModelProto modelProto = LDB.models.Select(copiedBuilding.modelIndex);

            if (modelProto != null)
            {
                return(modelProto.prefabDesc);
            }
            else
            {
                return(copiedBuilding.itemProto.prefabDesc);
            }
        }
Beispiel #21
0
 public static void LoadExternalDataForModel(ModelProto model, string base_dir)
 {
     foreach (var tensor in _get_all_tensors(model))
     {
         if (UsesExternalData(tensor))
         {
             LoadExternalDataForTensor(tensor, base_dir);
             // After loading raw_data from external_data, change the state of tensors
             tensor.DataLocation = TensorProto.Types.DataLocation.Default;
             // and remove external data
             tensor.ExternalData.Remove(tensor.ExternalData.FirstOrDefault(x => x.Key == ":"));
         }
     }
 }
Beispiel #22
0
        public static void SaveModel(ModelProto proto, string f, object format = null)
        {
            var model_filepath = _get_file_path(f);

            if (!string.IsNullOrWhiteSpace(model_filepath))
            {
                var basepath = Path.GetDirectoryName(model_filepath);
                proto = ExternalDataInfo.WriteExternalDataTensors(proto, basepath);
            }

            var s = _serialize(proto);

            _save_bytes(s, f);
        }
Beispiel #23
0
        //All of these register a specified proto in LDBTool
        public static int registerModel(String prefabPath)
        {
            int id = findAvailableID(100, LDB.models, models);

            ModelProto proto = new ModelProto
            {
                Name       = id.ToString(),
                PrefabPath = prefabPath,
                ID         = id
            };

            LDBTool.PreAddProto(ProtoType.Model, proto);
            models.Add(proto.ID, proto);
            return(proto.ID);
        }
Beispiel #24
0
        public static void ConvertModelFromExternalData(ModelProto model)
        {
            foreach (var tensor in _get_all_tensors(model))
            {
                if (UsesExternalData(tensor))
                {
                    if (tensor.RawData == null)
                    {
                        throw new Exception("raw_data field doesn't exist.");
                    }

                    tensor.ExternalData.Remove(tensor.ExternalData.FirstOrDefault(x => x.Key == ":"));
                    tensor.DataLocation = TensorProto.Types.DataLocation.Default;
                }
            }
        }
Beispiel #25
0
        public static ModelProto Initialize(this ModelProto Model, List <NodeProto> nodes, string name, List <ValueInfoProto> inputs, List <ValueInfoProto> outputs)
        {
            Model.IrVersion = 3;
            Model.OpsetImport.Add(new OperatorSetIdProto()
            {
                Domain = "", Version = 9
            });
            Model.ProducerName    = "C# OnnxProtoBuf";
            Model.ProducerVersion = "";
            Model.Graph           = new GraphProto();
            Model.Graph           = MakeGraph(nodes, name, inputs, outputs);
            Model.Graph.DocString = "";
            Model.Graph.Name      = name;


            return(Model);
        }
Beispiel #26
0
        //All of these register a specified proto in LDBTool

        /// <summary>
        /// Registers a ModelProto
        /// </summary>
        /// <param name="id">UNIQUE id of your model</param>
        /// <param name="prefabPath">Path to the prefab, starting from asset folder in your unity project</param>
        /// <param name="mats">List of materials to use</param>
        public static ModelProto registerModel(int id, string prefabPath, Material[] mats)
        {
            //int id = findAvailableID(100, LDB.models, models);

            ModelProto proto = new ModelProto
            {
                Name       = id.ToString(),
                PrefabPath = prefabPath,
                ID         = id
            };

            LDBTool.PreAddProto(ProtoType.Model, proto);
            models.Add(proto.ID, proto);
            modelMats.Add(prefabPath, mats);

            return(proto);
        }
Beispiel #27
0
 private static IEnumerable <TensorProto> _get_attribute_tensors(ModelProto onnx_model_proto)
 {
     foreach (var node in onnx_model_proto.Graph.Node)
     {
         foreach (var attribute in node.Attribute)
         {
             if (attribute.T != null)
             {
                 yield return(attribute.T);
             }
             foreach (var tensor in attribute.Tensors)
             {
                 yield return(tensor);
             }
         }
     }
 }
Beispiel #28
0
        static float Run(ModelProto model)
        {
            model.WriteFile(model.Graph.Name);
            SessionOptions options = new SessionOptions();

            options.SetSessionGraphOptimizationLevel(2);

            using (var session = new InferenceSession(model.Graph.Name, options))
            {
                var inputMeta = session.InputMetadata;
                var container = new List <NamedOnnxValue>();
                foreach (var key in inputMeta.Keys)
                {
                    if (inputMeta[key].ElementType == typeof(Int64))
                    {
                        container.Add(NamedOnnxValue.CreateFromTensor <Int64>(key,
                                                                              GetIntTensor(inputMeta[key].Dimensions, key)));
                    }
                    else if (inputMeta[key].ElementType == typeof(double))
                    {
                        container.Add(NamedOnnxValue.CreateFromTensor <double>(key,
                                                                               GetRandomDoubleTensor(inputMeta[key].Dimensions)));
                    }
                    else
                    {
                        container.Add(NamedOnnxValue.CreateFromTensor <float>(key,
                                                                              GetRandomTensor(inputMeta[key].Dimensions)));
                    }
                }

                if (!warmed.Contains(model.Graph.Name))
                {
                    session.Run(container);
                    warmed.Add(model.Graph.Name);
                }

                stopWatch.Reset();
                stopWatch.Start();
                var results = session.Run(container);
                stopWatch.Stop();
                results.Dispose();
                return
                    ((float)stopWatch.Elapsed.Ticks / TimeSpan.TicksPerMillisecond);
            }
        }
        public static void WriteIndentedJsonToFile(this ModelProto model, string filePath)
        {
            var jsonText    = JsonFormatter.Default.Format(model);
            var jsonElement = JsonSerializer.Deserialize <JsonElement>(jsonText);

            var options = new JsonSerializerOptions()
            {
                WriteIndented = true
            };

            var jsonTextPretty = JsonSerializer.Serialize(jsonElement, options);

            File.WriteAllText(filePath, jsonTextPretty);
            // Below does not indent
            //using var stream = File.Open(filePath, FileMode.Create);
            //using var writer = new Utf8JsonWriter(stream);
            //JsonSerializer.Serialize(writer, jsonElement, options);
        }
Beispiel #30
0
        private static void GenerateUndoData(PlanetFactory factory, List <int> targetIds)
        {
            if (NebulaModAPI.IsMultiplayerActive)
            {
                if (NebulaModAPI.MultiplayerSession.LocalPlayer.IsClient)
                {
                    return;
                }
            }

            try
            {
                List <int> filteredIds = targetIds.Where(objId =>
                {
                    if (objId < 0)
                    {
                        int modelIndex        = factory.prebuildPool[-objId].modelIndex;
                        ModelProto modelProto = LDB.models.Select(modelIndex);
                        return(modelProto != null);
                    }

                    if (objId > 0)
                    {
                        int modelIndex        = factory.entityPool[objId].modelIndex;
                        ModelProto modelProto = LDB.models.Select(modelIndex);
                        return(modelProto != null);
                    }

                    return(false);
                }).ToList();

                BlueprintData blueprint = UndoUtils.GenerateBlueprint(filteredIds, out Vector3 position);
                if (blueprint.buildings.Length > 0 && !position.Equals(Vector3.zero))
                {
                    PlayerUndo data = UndoManager.GetCurrentPlayerData();

                    data.AddUndoItem(new UndoDismantle(data, filteredIds, blueprint, new[] { position }, 0));
                }
            }
            catch (Exception e)
            {
                BlueprintTweaksPlugin.logger.LogWarning($"Failed to generate undo for drag dismantle: {e.Message}, stacktrace:\n{e.StackTrace}");
            }
        }