我正在尝试在 Unity 中运行以下 ONNX 模型,使用梭子鱼和以下预训练模型: https ://github.com/onnx/models/tree/master/vision/classification/mobilenet 使用我的网络摄像头作为摄像头,并使用以下脚本测试系统:
using UnityEngine;
using UnityEngine.UI;
using Unity.Barracuda;
using System.IO;
using System.Linq;
public class Webcam : MonoBehaviour
{
public NNModel mob_net;
public Model model;
private IWorker worker;
int current_cam_index = 0;
WebCamTexture tex;
public RawImage display;
private bool brain_on = false;
private const int SIZE = 224;
public void start_cam()
{
model = ModelLoader.Load(mob_net);
worker = WorkerFactory.CreateWorker(WorkerFactory.Type.ComputePrecompiled, model);
if (tex != null)
{
stop_cam();
}
else
{
WebCamDevice device = WebCamTexture.devices[current_cam_index];
tex = new WebCamTexture(device.name);
display.texture = tex;
tex.Play();
}
}
public void stop_cam()
{
display.texture = null;
tex.Stop();
tex = null;
}
void crop__normalize_inference(WebCamTexture src)
{
int x = Mathf.FloorToInt(display.transform.position.x);
int y = Mathf.FloorToInt(display.transform.position.y);
Color[] pix = src.GetPixels(x, y, SIZE, SIZE);
Texture2D dest = new Texture2D(SIZE, SIZE);
dest.SetPixels(pix);
dest.Apply();
float[] floats = new float[224 * 224 * 3];
for (int i = 0; i < pix.Length; ++i)
{
var color = pix[i];
floats[i * 3 + 0] = (color.r - 127) / 127.5f;
floats[i * 3 + 1] = (color.g - 127) / 127.5f;
floats[i * 3 + 2] = (color.b - 127) / 127.5f;
}
Tensor in_tensor = new Tensor(1, 224, 224, 3, floats);
worker.Execute(in_tensor);
Tensor out_tensor = worker.PeekOutput("MobilenetV2/Predictions/Reshape_1");
var max = Mathf.Max(out_tensor.ToReadOnlyArray());
var arr = out_tensor.ToReadOnlyArray();
var index = System.Array.IndexOf(arr, max);
string line = File.ReadLines(@"D:\Unity\WebCam\Cam\Assets\Scenes\mobile_net.txt").Skip(index).Take(1).First();
Debug.Log(line);
in_tensor.Dispose();
out_tensor.Dispose();
worker.Dispose();
}
public void brain()
{
brain_on = !brain_on;
}
private void Update()
{
if (brain_on)
{
crop_and_normalize(tex);
brain_on = false;
}
}
}
但是当我运行它时,我得到一个错误,上面写着:
ArgumentException: Can only specify one unknown dimension
我的猜测是 Unity 不支持该模型,或者由于某种原因我的输入张量形式不正确。. .
任何帮助将不胜感激,
ķ