Upgrade to Barracuda 2.4.0-preview

Added PixelShader backend for Single and Multi image recognition demos
Minor fixes and UI updates
This commit is contained in:
Florent Guinier 2022-02-04 17:34:41 +01:00
Родитель 27101dbe56
Коммит 7db1660148
8 изменённых файлов: 1169 добавлений и 40 удалений

Просмотреть файл

@ -2,7 +2,7 @@
This is a [Barracuda](https://github.com/Unity-Technologies/barracuda-release) demo library containing usage examples. All examples are also compatible with mobile platforms (iOS and Android) and WebGL (except the Face Tracking demo).
Each example is placed in a separate folder with a demo scene and all the required resources. If you want to add your images, make to sure to check the Texture Import Settings on the example images.
Each example is placed in a separate folder with a demo scene and all the required resources. If you want to add your images, make sure to check the Texture Import Settings on the example images.
## Static Image Recognition

Просмотреть файл

@ -16,9 +16,9 @@ public class RunInferenceMobileNet : MonoBehaviour
public TextAsset labelsAsset;
public Text resultClassText;
public Material preprocessMaterial;
public bool useGPU = true;
public Dropdown backendDropdown;
private string inferenceBackend = "CSharpBurst";
private Model model;
private IWorker engine;
private Dictionary<string, Tensor> inputs = new Dictionary<string, Tensor>();
@ -34,10 +34,6 @@ public class RunInferenceMobileNet : MonoBehaviour
labels = labelsAsset.text.Split('\n');
//load model
model = ModelLoader.Load(srcModel);
#if UNITY_WEBGL
useGPU = false;
backendDropdown.enabled = false;
#endif
//format input texture variable
targetRT = RenderTexture.GetTemporary(inputResolutionX, inputResolutionY, 0, RenderTextureFormat.ARGBHalf);
//execute inference
@ -46,14 +42,24 @@ public class RunInferenceMobileNet : MonoBehaviour
public void ExecuteML(int imageID)
{
#if UNITY_WEBGL
useGPU = false;
#endif
selectedImage = imageID;
displayImage.texture = inputImage[selectedImage];
engine = WorkerFactory.CreateWorker(model, useGPU ? WorkerFactory.Device.GPU : WorkerFactory.Device.CPU);
if (inferenceBackend == "CSharpBurst")
{
engine = WorkerFactory.CreateWorker(WorkerFactory.Type.CSharpBurst, model);
}
else if (inferenceBackend == "ComputePrecompiled")
{
engine = WorkerFactory.CreateWorker(WorkerFactory.Type.ComputePrecompiled, model);
}
else if (inferenceBackend == "PixelShader")
{
engine = WorkerFactory.CreateWorker(WorkerFactory.Type.PixelShader, model);
}
//preprocess image for input
var input = new Tensor(PrepareTextureForInput(inputImage[selectedImage], !useGPU), 3);
var input = new Tensor(PrepareTextureForInput(inputImage[selectedImage]), 3);
//execute neural net
engine.Execute(input);
//read output tensor
@ -69,15 +75,12 @@ public class RunInferenceMobileNet : MonoBehaviour
Resources.UnloadUnusedAssets();
}
Texture PrepareTextureForInput(Texture2D src, bool needsCPUcopy)
Texture PrepareTextureForInput(Texture2D src)
{
RenderTexture.active = targetRT;
//normalization is applied in the NormalizeInput shader
Graphics.Blit(src, targetRT, preprocessMaterial);
if (!needsCPUcopy)
return targetRT;
var result = new Texture2D(targetRT.width, targetRT.height, TextureFormat.RGBAHalf, false);
result.ReadPixels(new Rect(0,0, targetRT.width, targetRT.height), 0, 0);
result.Apply();
@ -88,21 +91,28 @@ public class RunInferenceMobileNet : MonoBehaviour
{
List<string> options = new List<string> ();
options.Add("CSharpBurst");
#if !UNITY_WEBGL
options.Add("ComputePrecompiled");
#endif
options.Add("PixelShader");
backendDropdown.ClearOptions ();
backendDropdown.AddOptions(options);
}
public void SelectBackendAndExecuteML()
{
if (backendDropdown.options[backendDropdown.value].text == "CSharpBurst")
{
useGPU = false;
inferenceBackend = "CSharpBurst";
}
if (backendDropdown.options[backendDropdown.value].text == "ComputePrecompiled")
else if (backendDropdown.options[backendDropdown.value].text == "ComputePrecompiled")
{
useGPU = true;
inferenceBackend = "ComputePrecompiled";
}
else if (backendDropdown.options[backendDropdown.value].text == "PixelShader")
{
inferenceBackend = "PixelShader";
}
ExecuteML(selectedImage);
}

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -39,6 +39,7 @@ public sealed class RunInferenceBlazeFace : MonoBehaviour
public GameObject webglWarning;
public UI.Dropdown _cameraDropdown;
public UI.Dropdown _backendDropdown;
//
// Detection structure. The layout of this structure must be matched with
@ -98,6 +99,7 @@ public sealed class RunInferenceBlazeFace : MonoBehaviour
Screen.orientation = ScreenOrientation.LandscapeLeft;
AddCameraOptions();
AddBackendOptions();
//initialization
AllocateObjects();
@ -142,6 +144,18 @@ public sealed class RunInferenceBlazeFace : MonoBehaviour
_cameraDropdown.AddOptions(options);
}
public void AddBackendOptions()
{
List<string> options = new List<string> ();
#if !UNITY_WEBGL
options.Add("ComputePrecompiled");
#endif
//options.Add("PixelShader"); //not supported with this demo
//options.Add("CSharpBurst"); //not supported with this demo
_backendDropdown.ClearOptions();
_backendDropdown.AddOptions(options);
}
public void SwapCamera()
{
_webcam.Stop();
@ -152,7 +166,7 @@ public sealed class RunInferenceBlazeFace : MonoBehaviour
public void ProcessImage(Texture image, float threshold = 0.75f)
=> ExecuteML(image, threshold);
void AllocateObjects()
public void AllocateObjects()
{
var model = ModelLoader.Load(_model);
_size = model.inputs[0].shape[6]; // Input tensor width
@ -168,7 +182,18 @@ public sealed class RunInferenceBlazeFace : MonoBehaviour
_countBuffer = new ComputeBuffer
(1, sizeof(uint), ComputeBufferType.Raw);
_worker = model.CreateWorker();
if (_backendDropdown.options[_backendDropdown.value].text == "ComputePrecompiled")
{
_worker = WorkerFactory.CreateWorker(WorkerFactory.Type.ComputePrecompiled, model);
}
else if (_backendDropdown.options[_backendDropdown.value].text == "PixelShader")
{
_worker = WorkerFactory.CreateWorker(WorkerFactory.Type.PixelShader, model);
}
else if (_backendDropdown.options[_backendDropdown.value].text == "CSharpBurst")
{
_worker = WorkerFactory.CreateWorker(WorkerFactory.Type.CSharpBurst, model);
}
}
void ExecuteML(Texture source, float threshold)

Просмотреть файл

@ -13,7 +13,6 @@ public class RunInferenceYOLO : MonoBehaviour
public RawImage displayImage;
public NNModel srcModel;
public TextAsset labelsAsset;
public bool useGPU = true;
public Dropdown backendDropdown;
public Transform displayLocation;
public Font font;
@ -25,6 +24,7 @@ public class RunInferenceYOLO : MonoBehaviour
private Dictionary<string, Tensor> inputs = new Dictionary<string, Tensor>();
private string[] labels;
private RenderTexture targetRT;
private string inferenceBackend = "CSharpBurst";
private const int amountOfClasses = 80;
private const int box20Sections = 20;
private const int box40Sections = 40;
@ -67,20 +67,11 @@ public class RunInferenceYOLO : MonoBehaviour
labels = labelsAsset.text.Split('\n');
//load model
model = ModelLoader.Load(srcModel);
#if UNITY_WEBGL
useGPU = false;
backendDropdown.enabled = false;
#endif
SelectBackendAndExecuteML();
}
public void ExecuteML(int imageID)
{
#if UNITY_WEBGL
useGPU = false;
#endif
ClearAnnotations();
selectedImage = imageID;
if (inputImage[selectedImage].width != 640 || inputImage[selectedImage].height != 640)
@ -88,7 +79,19 @@ public class RunInferenceYOLO : MonoBehaviour
Debug.LogError("Image resolution must be 640x640. Make sure Texture Import Settings are similar to the example images");
}
displayImage.texture = inputImage[selectedImage];
engine = WorkerFactory.CreateWorker(model, useGPU ? WorkerFactory.Device.GPU : WorkerFactory.Device.CPU);
if (inferenceBackend == "CSharpBurst")
{
engine = WorkerFactory.CreateWorker(WorkerFactory.Type.CSharpBurst, model);
}
else if (inferenceBackend == "ComputePrecompiled")
{
engine = WorkerFactory.CreateWorker(WorkerFactory.Type.ComputePrecompiled, model);
}
else if (inferenceBackend == "PixelShader")
{
engine = WorkerFactory.CreateWorker(WorkerFactory.Type.PixelShader, model);
}
//preprocess image for input
var input = new Tensor((inputImage[imageID]), 3);
@ -129,7 +132,10 @@ public class RunInferenceYOLO : MonoBehaviour
{
List<string> options = new List<string> ();
options.Add("CSharpBurst");
#if !UNITY_WEBGL
options.Add("ComputePrecompiled");
#endif
options.Add("PixelShader");
backendDropdown.ClearOptions ();
backendDropdown.AddOptions(options);
}
@ -138,12 +144,15 @@ public class RunInferenceYOLO : MonoBehaviour
{
if (backendDropdown.options[backendDropdown.value].text == "CSharpBurst")
{
useGPU = false;
inferenceBackend = "CSharpBurst";
}
if (backendDropdown.options[backendDropdown.value].text == "ComputePrecompiled")
else if (backendDropdown.options[backendDropdown.value].text == "ComputePrecompiled")
{
useGPU = true;
inferenceBackend = "ComputePrecompiled";
}
else if (backendDropdown.options[backendDropdown.value].text == "PixelShader")
{
inferenceBackend = "PixelShader";
}
ExecuteML(selectedImage);
}

Просмотреть файл

@ -1,6 +1,6 @@
{
"dependencies": {
"com.unity.barracuda": "2.3.1-preview",
"com.unity.barracuda": "2.4.0-preview",
"com.unity.ide.rider": "1.2.1",
"com.unity.ide.visualstudio": "2.0.9",
"com.unity.ide.vscode": "1.2.3",

Просмотреть файл

@ -1,7 +1,7 @@
{
"dependencies": {
"com.unity.barracuda": {
"version": "2.3.1-preview",
"version": "2.4.0-preview",
"depth": 0,
"source": "registry",
"dependencies": {

Просмотреть файл

@ -417,7 +417,7 @@ PlayerSettings:
m_Automatic: 0
- m_BuildTarget: WebGLSupport
m_APIs: 0b000000
m_Automatic: 1
m_Automatic: 0
m_BuildTargetVRSettings:
- m_BuildTarget: Standalone
m_Enabled: 0
@ -466,6 +466,7 @@ PlayerSettings:
switchTitleNames_12:
switchTitleNames_13:
switchTitleNames_14:
switchTitleNames_15:
switchPublisherNames_0:
switchPublisherNames_1:
switchPublisherNames_2:
@ -481,6 +482,7 @@ PlayerSettings:
switchPublisherNames_12:
switchPublisherNames_13:
switchPublisherNames_14:
switchPublisherNames_15:
switchIcons_0: {fileID: 0}
switchIcons_1: {fileID: 0}
switchIcons_2: {fileID: 0}
@ -496,6 +498,7 @@ PlayerSettings:
switchIcons_12: {fileID: 0}
switchIcons_13: {fileID: 0}
switchIcons_14: {fileID: 0}
switchIcons_15: {fileID: 0}
switchSmallIcons_0: {fileID: 0}
switchSmallIcons_1: {fileID: 0}
switchSmallIcons_2: {fileID: 0}
@ -511,6 +514,7 @@ PlayerSettings:
switchSmallIcons_12: {fileID: 0}
switchSmallIcons_13: {fileID: 0}
switchSmallIcons_14: {fileID: 0}
switchSmallIcons_15: {fileID: 0}
switchManualHTML:
switchAccessibleURLs:
switchLegalInformation:
@ -573,6 +577,8 @@ PlayerSettings:
switchSocketInitializeEnabled: 1
switchNetworkInterfaceManagerInitializeEnabled: 1
switchPlayerConnectionEnabled: 1
switchUseMicroSleepForYield: 1
switchMicroSleepForYieldTime: 25
ps4NPAgeRating: 12
ps4NPTitleSecret:
ps4NPTrophyPackPath:
@ -643,6 +649,7 @@ PlayerSettings:
ps4videoRecordingFeaturesUsed: 0
ps4contentSearchFeaturesUsed: 0
ps4CompatibilityPS5: 0
ps4AllowPS5Detection: 0
ps4GPU800MHz: 1
ps4attribEyeToEyeDistanceSettingVR: 0
ps4IncludedModules: []
@ -670,6 +677,8 @@ PlayerSettings:
ps5disableAutoHideSplash: 0
ps5OperatingSystemCanDisableSplashScreen: 0
ps5IncludedModules: []
ps5SharedBinaryContentLabels: []
ps5SharedBinarySystemFolders: []
monoEnv:
splashScreenBackgroundSourceLandscape: {fileID: 0}
splashScreenBackgroundSourcePortrait: {fileID: 0}
@ -696,6 +705,7 @@ PlayerSettings:
il2cppCompilerConfiguration: {}
managedStrippingLevel: {}
incrementalIl2cppBuild: {}
suppressCommonWarnings: 1
allowUnsafeCode: 0
additionalIl2CppArgs:
scriptingRuntimeVersion: 1