Rehab games keep stroke patients practising, but only if they don’t lose too often. Losing again and again is demotivating, and patients give up. Winning only because the game plays itself isn’t much better, because the win doesn’t feel earned.
The idea I was working towards at CMC Vellore was an AI agent that knows how to win the game, and steps in to help the patient only when they need it. Unity is a good place to build this. The game is a known environment, so an agent can learn its goal with reinforcement learning, and the same game can then be played by a patient.
This script is one piece of that: running a trained agent’s model by hand, so its actions are available to blend with the player’s input instead of just driving the game on its own.
Code: Zilch123/Custom-Barracuda-Implementation
The test game
I used RollerBall, the starter example from Unity ML-Agents. A ball on a platform has to roll to a target cube without falling off. The agent observes the target’s position, its own position and its velocity (8 numbers), and outputs two forces, one along x and one along z.
The agent was trained with ML-Agents’ Python trainer, which exports the trained model as a .nn file.
Why run the model by hand
Normally you hand the .nn file to an ML-Agents Brain, and it runs the model and moves the agent for you. That’s convenient, but it hides the agent’s decision. For assistance you need the opposite: get the agent’s suggested action every frame, so you can compare it with what the patient is doing and decide how much help to give.
Barracuda is Unity’s neural network inference library, the engine ML-Agents used underneath. Using it directly gives you the raw model outputs.
Loading the model
model_ = ModelLoader.LoadFromStreamingAssets("RollerBallBrain.nn");
worker = BarracudaWorkerFactory.CreateWorker(BarracudaWorkerFactory.Type.ComputePrecompiled, model_);The worker runs the model. ComputePrecompiled runs it on the GPU with compute shaders.
Running it every frame
Each frame, the observations go into a tensor with the same layout the model was trained on, and the worker returns the action:
var input_vector_obs = new Tensor(1, 1, 1, 8, new float[] {
Target.position.x, Target.position.y, Target.position.z,
this.transform.position.x, this.transform.position.y, this.transform.position.z,
rBody.velocity.x, rBody.velocity.z });
var input_eps = new Tensor(1, 1, 1, 1, new float[] { 0.2f });
var dict = new Dictionary<string, Tensor>();
dict["vector_observation"] = input_vector_obs;
dict["epsilon"] = input_eps;
worker.Execute(dict);
var out_ = TensorUtils.BarracudaToFloatArray(worker.Fetch("action"));The input names (vector_observation, epsilon) have to match the names inside the exported model. The epsilon input is the noise the trained policy uses when sampling continuous actions.
The two outputs are the forces along x and z, which then move the ball:
controlSignal.x = vectorAction[0];
controlSignal.z = vectorAction[1];
rBody.AddForce(controlSignal * speed);In this script the agent’s action drives the ball directly, to check that the hand-run model behaves the same as it did under the Brain. The next step is to stop applying it straight away, and mix it with the player’s input instead.
Where this was going
With the agent’s action available every frame, assistance becomes a blending problem: how much of the agent’s action to add to the player’s, and when. I explored that trade-off later in a simulation, in Assist As Needed, where the assistant weighs the agent’s best move against staying close to what the player chose. The player’s input itself came from controllers like the wrist encoder setup.
A note on versions
This was written for ML-Agents and Barracuda as they were in 2019. Both have changed a lot since. Barracuda has been replaced by Unity’s newer inference engine (Sentis), and ML-Agents now exports ONNX models. The idea is the same: load the model yourself, feed it observations, and read the actions.
Thanks to the Unity ML-Agents team, whose Barracuda documentation made this possible.