Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
Fix edge model from MLModelData
Browse files Browse the repository at this point in the history
  • Loading branch information
olokobayusuf committed Jul 10, 2023
0 parents commit 3d599fe
Show file tree
Hide file tree
Showing 178 changed files with 9,569 additions and 0 deletions.
Binary file added .media/hub.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/mobilenet.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/wall.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
322 changes: 322 additions & 0 deletions Changelog.md

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Changelog.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions Editor/BuildEmbedHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* NatML
* Copyright © 2023 NatML Inc. All rights reserved.
*/

namespace NatML.Editor {

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;

/// <summary>
/// Lightweight utility for embedding scriptable objects into builds.
/// </summary>
public abstract class BuildEmbedHelper<T> : IPreprocessBuildWithReport, IPostprocessBuildWithReport where T : ScriptableObject {

#region --Client API--
/// <summary>
/// Callback order for prioritization.
/// </summary>
public virtual int callbackOrder => 0;

/// <summary>
/// Supported targets for this build embed.
/// If `null` the helper will embed for all build targets.
/// </summary>
protected virtual BuildTarget[] SupportedTargets => null;

/// <summary>
/// Create embeds for the build.
/// </summary>
protected abstract T[] CreateEmbeds (BuildReport report);

/// <summary>
/// Clear embeds after the build.
/// </summary>
protected virtual void ClearEmbeds (BuildReport report) => ClearEmbeds<T>();

/// <summary>
/// Clear any existing data embedded in the build.
/// </summary>
protected static void ClearEmbeds<U> () {
var assets = PlayerSettings.GetPreloadedAssets()?.ToList();
if (assets == null)
return;
assets.RemoveAll(asset => asset && asset.GetType() == typeof(U));
PlayerSettings.SetPreloadedAssets(assets.ToArray());
}
#endregion


#region --Operations--

void IPreprocessBuildWithReport.OnPreprocessBuild (BuildReport report) {
// Clear
ClearEmbeds<T>();
// Check target
var targets = SupportedTargets;
if (!targets?.Contains(report.summary.platform) ?? false)
return;
// Create
var data = CreateEmbeds(report);
if (data == null)
return;
// Embed
var assets = PlayerSettings.GetPreloadedAssets()?.ToList() ?? new List<UnityEngine.Object>();
assets.AddRange(data);
PlayerSettings.SetPreloadedAssets(assets.ToArray());
}

void IPostprocessBuildWithReport.OnPostprocessBuild (BuildReport report) => ClearEmbeds(report);
#endregion
}
}
11 changes: 11 additions & 0 deletions Editor/BuildEmbedHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Editor/Importers.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Editor/Importers/CoreMLImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* NatML
* Copyright © 2023 NatML Inc. All rights reserved.
*/

namespace NatML.Editor.Importers {

using UnityEditor.AssetImporters;
using API.Types;

[ScriptedImporter(1, @"mlmodel")]
internal sealed class CoreMLImporter : GraphImporter {

protected override PredictorSession CreateSession () => new PredictorSession {
format = GraphFormat.CoreML
};
}
}
11 changes: 11 additions & 0 deletions Editor/Importers/CoreMLImporter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Editor/Importers/GraphImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* NatML
* Copyright © 2023 NatML Inc. All rights reserved.
*/

namespace NatML.Editor.Importers {

using System.IO;
using UnityEngine;
using UnityEditor.AssetImporters;
using API.Types;

public abstract class GraphImporter : ScriptedImporter {

public sealed override void OnImportAsset (AssetImportContext ctx) {
var modelData = ScriptableObject.CreateInstance<MLModelData>();
modelData.session = CreateSession();
modelData.graph = File.ReadAllBytes(ctx.assetPath);
ctx.AddObjectToAsset("MLModelData", modelData);
ctx.SetMainObject(modelData);
}

protected abstract PredictorSession CreateSession ();
}
}
11 changes: 11 additions & 0 deletions Editor/Importers/GraphImporter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Editor/Importers/ONNXImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* NatML
* Copyright © 2023 NatML Inc. All rights reserved.
*/

namespace NatML.Editor.Importers {

using UnityEditor.AssetImporters;
using API.Types;

[ScriptedImporter(1, @"onnx")]
internal sealed class ONNXImporter : GraphImporter {

protected override PredictorSession CreateSession () => new PredictorSession {
format = GraphFormat.ONNX
};
}
}
11 changes: 11 additions & 0 deletions Editor/Importers/ONNXImporter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Editor/Importers/TFLiteImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* NatML
* Copyright © 2023 NatML Inc. All rights reserved.
*/

namespace NatML.Editor.Importers {

using UnityEditor.AssetImporters;
using API.Types;

[ScriptedImporter(1, @"tflite")]
internal sealed class TFLiteImporter : GraphImporter {

protected override PredictorSession CreateSession () => new PredictorSession {
format = GraphFormat.TFLite
};
}
}
11 changes: 11 additions & 0 deletions Editor/Importers/TFLiteImporter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Editor/NatML.ML.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "NatML.ML.Editor",
"references": [
"NatML.ML"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Editor/NatML.ML.Editor.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions Editor/NatMLMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* NatML
* Copyright © 2023 NatML Inc. All rights reserved.
*/

namespace NatML.Editor {

using System.IO;
using UnityEditor;
using UnityEngine;
using Internal;

internal static class NatMLMenu {

private const int BasePriority = -50;

[MenuItem(@"NatML/NatML " + NatML.Version, false, BasePriority)]
private static void Version () { }

[MenuItem(@"NatML/NatML " + NatML.Version, true, BasePriority)]
private static bool EnableVersion () => false;

[MenuItem(@"NatML/Get Access Key", false, BasePriority + 1)]
private static void OpenAccessKey () => Help.BrowseURL(@"https://hub.natml.ai/account/developers");

[MenuItem(@"NatML/Explore Predictors", false, BasePriority + 2)]
private static void OpenHub () => Help.BrowseURL(@"https://hub.natml.ai");

[MenuItem(@"NatML/Join Discord Community", false, BasePriority + 3)]
private static void OpenDiscord () => Help.BrowseURL(@"https://natml.ai/community");

[MenuItem(@"NatML/View NatML Docs", false, BasePriority + 4)]
private static void OpenDocs () => Help.BrowseURL(@"https://docs.natml.ai/unity");

[MenuItem(@"NatML/Open a NatML Issue", false, BasePriority + 5)]
private static void OpenIssue () => Help.BrowseURL(@"https://github.com/natmlx/natml-unity");

[MenuItem(@"NatML/Clear Predictor Cache", false, BasePriority + 6)]
private static void ClearCache () {
MLEdgeModel.ClearCache();
Debug.Log("NatML: Cleared predictor cache");
}
}
}
11 changes: 11 additions & 0 deletions Editor/NatMLMenu.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions Editor/NatMLProjectSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* NatML
* Copyright © 2023 NatML Inc. All rights reserved.
*/

namespace NatML.Editor {

using System.Threading.Tasks;
using UnityEngine;
using UnityEditor;
using Internal;

[FilePath(@"ProjectSettings/NatMLHub.asset", FilePathAttribute.Location.ProjectFolder)]
internal sealed class NatMLProjectSettings : ScriptableSingleton<NatMLProjectSettings> {

#region --Data--
[SerializeField]
private string accessKey;
#endregion


#region --Client API--
/// <summary>
/// NatML access key.
/// </summary>
internal string AccessKey {
get => accessKey;
set {
// Check
if (value == accessKey)
return;
// Update
accessKey = value;
Save(false);
NatMLSettings.Instance = CreateSettings();
}
}

/// <summary>
/// NatML settings from the current project settings.
/// </summary>
internal static NatMLSettings CreateSettings () {
var settings = ScriptableObject.CreateInstance<NatMLSettings>();
settings.accessKey = instance.AccessKey;
return settings;
}
#endregion


#region --Operations--

[InitializeOnLoadMethod]
private static void OnLoad () => NatMLSettings.Instance = CreateSettings();

[InitializeOnEnterPlayMode]
private static void OnEnterPlaymodeInEditor (EnterPlayModeOptions options) => NatMLSettings.Instance = CreateSettings();
#endregion
}
}
Loading

0 comments on commit 3d599fe

Please sign in to comment.