UnityPaul commited on
Commit
05abce4
1 Parent(s): 0730c1b

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +4 -4
  2. RunMobileNet.cs +39 -37
  3. info.json +1 -1
  4. mobilenet_v2.sentis +2 -2
README.md CHANGED
@@ -4,16 +4,16 @@ library_name: unity-sentis
4
  pipeline_tag: image-classification
5
  ---
6
 
7
- ## MobileNet V2 in Unity Sentis Format (Version 1.3.0-pre.3*)
8
- *Version 1.3.0 Sentis files are not compatible with Sentis 1.4.0 and need to be recreated/downloaded
9
 
10
  This is a small image classification model that works in Unity 2023. It is based on [MobileNet V2](https://arxiv.org/abs/1801.04381)
11
 
12
  ## How to Use
13
  * Create a new scene in Unity 2023
14
- * Install `com.unity.sentis` version `1.3.0-pre.3` from the package manager
15
  * Add the C# script to the Main Camera
16
- * Put the `mobilenet_v2.sentis` model in the `Assets/StreamingAssets` folder
17
  * Drag the `class_desc.txt` on to the `labelsAsset` field
18
  * Drag one of the sample images on to the inputImage field in the inspector.
19
  * Press play and the result of the prediction will print to the console window.
 
4
  pipeline_tag: image-classification
5
  ---
6
 
7
+ ## MobileNet V2 in Unity Sentis Format (Version 1.4.0-pre.2*)
8
+ *Version 1.3.0 Sentis files are not compatible with 1.4.0 and need to be recreated/downloaded
9
 
10
  This is a small image classification model that works in Unity 2023. It is based on [MobileNet V2](https://arxiv.org/abs/1801.04381)
11
 
12
  ## How to Use
13
  * Create a new scene in Unity 2023
14
+ * Install `com.unity.sentis` version `1.4.0-pre.2` from the package manager
15
  * Add the C# script to the Main Camera
16
+ * Drag the `mobilenet_v2.sentis` model onto the `modelAsset `field
17
  * Drag the `class_desc.txt` on to the `labelsAsset` field
18
  * Drag one of the sample images on to the inputImage field in the inspector.
19
  * Press play and the result of the prediction will print to the console window.
RunMobileNet.cs CHANGED
@@ -1,7 +1,8 @@
1
- using System.Collections.Generic;
2
  using Unity.Sentis;
3
  using UnityEngine;
4
-
 
5
  /*
6
  * MovileNetV2 Inference Script
7
  * ============================
@@ -18,6 +19,9 @@ using UnityEngine;
18
 
19
  public class RunMobileNet : MonoBehaviour
20
  {
 
 
 
21
  const string modelName = "mobilenet_v2.sentis";
22
 
23
  //The image to classify here:
@@ -26,32 +30,42 @@ public class RunMobileNet : MonoBehaviour
26
  //Link class_desc.txt here:
27
  public TextAsset labelsAsset;
28
 
29
- //All images are resized to these values:
30
  const int imageHeight = 224;
31
  const int imageWidth = 224;
32
 
33
  const BackendType backend = BackendType.GPUCompute;
34
- private Model model;
35
  private IWorker engine;
36
  private string[] labels;
37
 
38
- static Ops ops;
39
- ITensorAllocator allocator;
 
40
 
41
  void Start()
42
  {
43
- //These are used for tensor operations
44
- allocator = new TensorCachingAllocator();
45
- ops = WorkerFactory.CreateOps(backend, allocator);
46
 
47
  //Parse neural net labels
48
  labels = labelsAsset.text.Split('\n');
49
 
50
- //Load model
51
- model = ModelLoader.Load(Application.streamingAssetsPath + "/" + modelName);
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  //Setup the engine to run the model
54
- engine = WorkerFactory.CreateWorker(backend, model);
55
 
56
  //Execute inference
57
  ExecuteML();
@@ -60,51 +74,39 @@ public class RunMobileNet : MonoBehaviour
60
  public void ExecuteML()
61
  {
62
  //Preprocess image for input
63
- using var rawinput = TextureConverter.ToTensor(inputImage, 224, 224, 3);
64
- using var input = Normalise(rawinput);
65
 
66
  //Execute neural net
67
  engine.Execute(input);
68
 
69
  //Read output tensor
70
- var output = engine.PeekOutput() as TensorFloat;
71
- var argmax = ops.ArgMax(output, 1, false);
72
- argmax.MakeReadable();
 
73
 
74
  //Select the best output class and print the results
75
- var res = argmax[0];
76
- var label = labels[res];
77
-
78
- output.MakeReadable();
79
- var accuracy = output[res];
80
 
81
  //The result is output to the console window
82
  int percent = Mathf.FloorToInt(accuracy * 100f + 0.5f);
83
- Debug.Log($"{label} {percent}﹪");
84
 
85
  //Clean memory
86
  Resources.UnloadUnusedAssets();
87
  }
88
 
89
  //This scales and shifts the RGB values for input into the model
90
- TensorFloat Normalise(TensorFloat image)
91
  {
92
- using var M = new TensorFloat(new TensorShape(1, 3, 1, 1), new float[]
93
- {
94
- 1/0.229f, 1/0.224f, 1/0.225f
95
- });
96
- using var P = new TensorFloat(new TensorShape(1, 3, 1, 1), new float[]
97
- {
98
- 0.485f, 0.456f, 0.406f
99
- });
100
- using var image2 = ops.Sub(image, P);
101
- return ops.Mul(image2, M);
102
  }
103
 
104
  private void OnDestroy()
105
  {
106
- engine?.Dispose();
107
- ops?.Dispose();
108
- allocator?.Dispose();
109
  }
110
  }
 
1
+ using System.Collections.Generic;
2
  using Unity.Sentis;
3
  using UnityEngine;
4
+ using System.IO;
5
+ using FF = Unity.Sentis.Functional;
6
  /*
7
  * MovileNetV2 Inference Script
8
  * ============================
 
19
 
20
  public class RunMobileNet : MonoBehaviour
21
  {
22
+ //draw the sentis file here:
23
+ public ModelAsset modelAsset;
24
+
25
  const string modelName = "mobilenet_v2.sentis";
26
 
27
  //The image to classify here:
 
30
  //Link class_desc.txt here:
31
  public TextAsset labelsAsset;
32
 
33
+ //All images are resized to these values to go into the model
34
  const int imageHeight = 224;
35
  const int imageWidth = 224;
36
 
37
  const BackendType backend = BackendType.GPUCompute;
38
+
39
  private IWorker engine;
40
  private string[] labels;
41
 
42
+ //Used to normalise the input RGB values
43
+ TensorFloat mulRGB = new TensorFloat(new TensorShape(1, 3, 1, 1), new float[] { 1 / 0.229f, 1 / 0.224f, 1 / 0.225f });
44
+ TensorFloat shiftRGB = new TensorFloat(new TensorShape(1, 3, 1, 1), new float[] { 0.485f, 0.456f, 0.406f });
45
 
46
  void Start()
47
  {
 
 
 
48
 
49
  //Parse neural net labels
50
  labels = labelsAsset.text.Split('\n');
51
 
52
+ //Load model from file or asset
53
+ //var model = ModelLoader.Load(Path.Join(Application.streamingAssetsPath, modelName));
54
+ var model = ModelLoader.Load(modelAsset);
55
+
56
+ //We modify the model to normalise the input RGB values and select the highest prediction
57
+ //probability and item number
58
+ var model2 = FF.Compile(
59
+ input =>
60
+ {
61
+ var probability = model.Forward(NormaliseRGB(input))[0];
62
+ return (FF.ReduceMax(probability, 1), FF.ArgMax(probability, 1));
63
+ },
64
+ model.inputs[0]
65
+ );
66
 
67
  //Setup the engine to run the model
68
+ engine = WorkerFactory.CreateWorker(backend, model2);
69
 
70
  //Execute inference
71
  ExecuteML();
 
74
  public void ExecuteML()
75
  {
76
  //Preprocess image for input
77
+ using var input = TextureConverter.ToTensor(inputImage, imageWidth, imageHeight, 3);
 
78
 
79
  //Execute neural net
80
  engine.Execute(input);
81
 
82
  //Read output tensor
83
+ var probability = engine.PeekOutput("output_0") as TensorFloat;
84
+ var item = engine.PeekOutput("output_1") as TensorInt;
85
+ item.CompleteOperationsAndDownload();
86
+ probability.CompleteOperationsAndDownload();
87
 
88
  //Select the best output class and print the results
89
+ var ID = item[0];
90
+ var accuracy = probability[0];
 
 
 
91
 
92
  //The result is output to the console window
93
  int percent = Mathf.FloorToInt(accuracy * 100f + 0.5f);
94
+ Debug.Log($"Prediction: {labels[ID]} {percent}﹪");
95
 
96
  //Clean memory
97
  Resources.UnloadUnusedAssets();
98
  }
99
 
100
  //This scales and shifts the RGB values for input into the model
101
+ FunctionalTensor NormaliseRGB(FunctionalTensor image)
102
  {
103
+ return (image - FunctionalTensor.FromTensor(shiftRGB)) * FunctionalTensor.FromTensor(mulRGB);
 
 
 
 
 
 
 
 
 
104
  }
105
 
106
  private void OnDestroy()
107
  {
108
+ mulRGB?.Dispose();
109
+ shiftRGB?.Dispose();
110
+ engine?.Dispose();
111
  }
112
  }
info.json CHANGED
@@ -9,6 +9,6 @@
9
  "class_desc.txt"
10
  ],
11
  "version":[
12
- "1.3.0-pre.3"
13
  ]
14
  }
 
9
  "class_desc.txt"
10
  ],
11
  "version":[
12
+ "1.4.0"
13
  ]
14
  }
mobilenet_v2.sentis CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:81e2e51fad533a32b721454cad79341da8457f8b361f2868319eac93d6b9c5e7
3
- size 14046825
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:907d42cf325f7d2457b8ccdd12fabb5bea882d2757c3bb5bc57042e5ec6533bc
3
+ size 13989036