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

Commit

Permalink
Fix #56
Browse files Browse the repository at this point in the history
  • Loading branch information
olokobayusuf committed Jun 30, 2023
0 parents commit 92f8da8
Show file tree
Hide file tree
Showing 186 changed files with 10,200 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.
295 changes: 295 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.

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.

19 changes: 19 additions & 0 deletions Editor/NatML.ML.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "NatML.ML.Editor",
"references": [
"NatML.Hub",
"NatML.Hub.Editor",
"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.

38 changes: 38 additions & 0 deletions Editor/NatMLMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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/Explore Predictors", false, BasePriority + 1)]
private static void OpenHub () => Help.BrowseURL(@"https://hub.natml.ai");

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

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

[MenuItem(@"NatML/Clear Predictor Cache", false, BasePriority + 4)]
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.

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

namespace NatML.Editor {

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEngine;
using API;
using API.Types;
using Hub.Editor;
using Hub.Internal;
using Internal;

internal sealed class NatMLSettingsEmbed : BuildEmbedHelper<NatMLSettings> {

protected override BuildTarget[] SupportedTargets => new [] {
BuildTarget.Android,
BuildTarget.iOS,
BuildTarget.StandaloneOSX,
BuildTarget.StandaloneWindows,
BuildTarget.StandaloneWindows64,
BuildTarget.WebGL,
};
private const string CachePath = @"Assets/NMLBuildCache";

protected override NatMLSettings[] CreateEmbeds (BuildReport report) {
// Check platform // We don't support embeds on Linux editor (#54)
if (Application.platform == RuntimePlatform.LinuxEditor)
return new NatMLSettings[0];
// Create cache path
Directory.CreateDirectory(CachePath);
// Create settings
var settings = CreateSettings();
// Get embeds
var embeds = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(assembly => assembly
.GetTypes()
.SelectMany(type => Attribute.GetCustomAttributes(type, typeof(MLEdgeModel.EmbedAttribute)))
)
.Cast<MLEdgeModel.EmbedAttribute>()
.ToArray();
var defaultAccessKey = HubSettings.Instance.AccessKey;
var format = GetFormat(report.summary.platform);
var secret = MLEdgeModel.CreateSecret().Result; // this completes immediately everywhere except web
settings.embeds = Task.WhenAll(embeds.Select(e => Task.Run(async () => {
var accessKey = !string.IsNullOrEmpty(e.accessKey) ? e.accessKey : defaultAccessKey;
var client = new NatMLClient(accessKey);
var session = await client.PredictorSessions.Create(e.tag, format, secret);
var graphStream = await client.Storage.Download(session.graph);
var graph = graphStream.ToArray();
var embed = new NatMLSettings.Embed { fingerprint = session.fingerprint, data = graph };
return embed;
}))).Result;
// Write settings
AssetDatabase.CreateAsset(settings, $"{CachePath}/NatML.asset");
return new [] { settings };
}

protected override void ClearEmbeds (BuildReport report) {
base.ClearEmbeds(report);
AssetDatabase.DeleteAsset(CachePath);
}

internal static NatMLSettings CreateSettings () {
var settings = ScriptableObject.CreateInstance<NatMLSettings>();
settings.accessKey = HubSettings.Instance.AccessKey;
return settings;
}

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
private static void OnLoad () => NatMLSettings.Instance = CreateSettings();

private static GraphFormat GetFormat (BuildTarget target) => target switch {
BuildTarget.Android => GraphFormat.TFLite,
BuildTarget.iOS => GraphFormat.CoreML,
BuildTarget.StandaloneOSX => GraphFormat.CoreML,
BuildTarget.StandaloneWindows => GraphFormat.ONNX,
BuildTarget.StandaloneWindows64 => GraphFormat.ONNX,
BuildTarget.WebGL => GraphFormat.ONNX,
_ => 0,
};
}
}
11 changes: 11 additions & 0 deletions Editor/NatMLSettingsEmbed.cs.meta

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

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

namespace NatML.Editor {

using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;

internal sealed class WebGLBuildHelper : IPreprocessBuildWithReport {

public int callbackOrder => 0;
private readonly string[] EM_ARGS = new [] {
@"--bind",
};

void IPreprocessBuildWithReport.OnPreprocessBuild (BuildReport report) {
if (report.summary.platform != BuildTarget.WebGL)
return;
foreach (var arg in EM_ARGS) {
var standaloneArg = $" {arg} ";
if (!PlayerSettings.WebGL.emscriptenArgs.Contains(standaloneArg))
PlayerSettings.WebGL.emscriptenArgs += standaloneArg;
}
}
}
}
11 changes: 11 additions & 0 deletions Editor/WebGLBuildHelper.cs.meta

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

Loading

0 comments on commit 92f8da8

Please sign in to comment.