Instructions to use unity/inference-engine-mobilenet-v2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- unity-sentis
How to use unity/inference-engine-mobilenet-v2 with unity-sentis:
string modelName = "[Your model name here].sentis"; Model model = ModelLoader.Load(Application.streamingAssetsPath + "/" + modelName); IWorker engine = WorkerFactory.CreateWorker(BackendType.GPUCompute, model); // Please see provided C# file for more details
- Notebooks
- Google Colab
- Kaggle
| using Unity.InferenceEngine; | |
| using UnityEngine; | |
| public class RunMobileNet : MonoBehaviour | |
| { | |
| public ModelAsset modelAsset; | |
| //The image to classify here: | |
| public Texture2D inputImage; | |
| //Link class_desc.txt here: | |
| public TextAsset labelsAsset; | |
| const BackendType backend = BackendType.GPUCompute; | |
| Worker worker; | |
| string[] labels; | |
| void Start() | |
| { | |
| //Parse neural net labels | |
| labels = labelsAsset.text.Split('\n'); | |
| //Load model from asset | |
| var model = ModelLoader.Load(modelAsset); | |
| //We modify the model to normalise the input RGB values and select the highest prediction | |
| //probability and item number | |
| var graph = new FunctionalGraph(); | |
| var image = graph.AddInput(model, 0); | |
| var normalizedInput = (image - Functional.Constant(new TensorShape(1, 3, 1, 1), new[] { 0.485f, 0.456f, 0.406f })) * Functional.Constant(new TensorShape(1, 3, 1, 1), new[] { 1 / 0.229f, 1 / 0.224f, 1 / 0.225f }); | |
| var probability = Functional.Forward(model, normalizedInput)[0]; | |
| var value = Functional.ReduceMax(probability, 1); | |
| var index = Functional.ArgMax(probability, 1); | |
| graph.AddOutput(value, "value"); | |
| graph.AddOutput(index, "index"); | |
| var model2 = graph.Compile(); | |
| //Set up the worker to run the model | |
| worker = new Worker(model2, backend); | |
| //Execute inference | |
| ExecuteML(); | |
| } | |
| public void ExecuteML() | |
| { | |
| using var input = new Tensor<float>(new TensorShape(1, 3, 224, 224)); | |
| //Preprocess image for input | |
| TextureConverter.ToTensor(inputImage, input); | |
| //Schedule neural net | |
| worker.Schedule(input); | |
| //Read output tensors | |
| using var value = (worker.PeekOutput("value") as Tensor<float>).ReadbackAndClone(); | |
| using var index = (worker.PeekOutput("index") as Tensor<int>).ReadbackAndClone(); | |
| //Select the best output class and print the results | |
| var accuracy = value[0]; | |
| var ID = index[0]; | |
| //The result is output to the console window | |
| int percent = Mathf.FloorToInt(accuracy * 100f + 0.5f); | |
| Debug.Log($"Prediction: {labels[ID]} {percent}﹪"); | |
| } | |
| void OnDestroy() | |
| { | |
| worker?.Dispose(); | |
| } | |
| } | |