/// <summary> /// Scripted importer callback /// </summary> /// <param name="ctx">Asset import context</param> public override void OnImportAsset(AssetImportContext ctx) { var converter = new ONNXModelConverter(optimizeModel, treatErrorsAsWarnings, forceArbitraryBatchSize, importMode); var model = converter.Convert(ctx.assetPath); 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); }
/// <summary> /// Scripted importer callback /// </summary> /// <param name="ctx">Asset import context</param> public override void OnImportAsset(AssetImportContext ctx) { ONNXModelConverter.ModelImported += BarracudaAnalytics.SendBarracudaImportEvent; var converter = new ONNXModelConverter(optimizeModel, treatErrorsAsWarnings, forceArbitraryBatchSize, importMode); var model = converter.Convert(ctx.assetPath); if (weightsTypeMode == ONNXModelConverter.DataTypeMode.ForceHalf) { model.ConvertWeights(DataType.Half); } else if (weightsTypeMode == ONNXModelConverter.DataTypeMode.ForceFloat) { model.ConvertWeights(DataType.Float); } 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); }