Global input problem when I use barracuda in Unity to execute a model trained by ML-agents

44 views Asked by At

I already trained a .onnx model by ML-agents, but when I used barracuda to execute it, something went wrong. It said “KeyNotFoundException: The given key 'obs_0' was not present in the dictionary.” and warned as following : 1.Global input is missing: obs_0 2.Global input is missing: action_masks 3.GenericVars missing variable: obs_0

The code with problem is shown as following, but actually, when I deleted "worker.Execute(inputs);", everything returned to normal, so I guess it's where problem happened.

    private void Update()
    {
        //constructing the input array
        float[] positionArray =
        {
            transform.localPosition.x, transform.localPosition.y, transform.localPosition.z,
            target.localPosition.x, target.localPosition.y,target.localPosition.z
        };
        Tensor tensor = new Tensor(1,1,6,1,positionArray);
        var inputs = new Dictionary<string, Tensor> {
            { "in", tensor }
        }; 
        worker.Execute(inputs);
        Tensor output = worker.PeekOutput("in");
        float[] temp = output.ToReadOnlyArray();
        float moveSpeed = 4f;
        transform.localPosition += new Vector3(temp[0], 0f) * Time.deltaTime * moveSpeed;
        tensor.Dispose();
        output.Dispose();
    }

Theoretically using barracuda to execute a model is very easy, so I really don't have any clue to fix it, the only thing I did was adding this code:

        var inputs = new Dictionary<string, Tensor> {
            { "in", tensor }
        }; 

Before that, the error is"KeyNotFoundException: The given key 'action_masks' was not present in the dictionary." just a difference in the key name.

1

There are 1 answers

0
flyingDumpling On

Oops, I see. Inputs and outputs of ONNX models are defined with specific names to identify and access corresponding data. When exporting models to ONNX format using ML-agents, default names are automatically assigned to inputs and outputs. Therefore, to use models exported with ML-agents, it's necessary to use their predefined naming conventions. So the Key of input should be "action_masks" and "obs_0", and the output Key can be "continuous_actions", Just as the visualization of ML-agents models following. enter image description here This might be basic knowledge, but as a newcomer in this field, it took me a while to grasp it :(.