run method
Runs this signature with named inputs, writing results to outputs.
Automatically resizes input tensors if shapes differ, allocates tensors if needed, and copies output data into the provided objects.
inputs, map from input tensor name to data (List, Uint8List, etc.)
outputs, map from output tensor name to a pre-allocated object that
will receive the output data (List, Float32List, etc.).
Pass an empty map if the signature produces no outputs you
need to read (e.g. the set_weights signature).
Example, training step:
final lossBuffer = Float32List(1);
trainRunner.run(
{'x': imageData, 'y': labels},
{'loss': lossBuffer},
);
Example, restore weights:
setWeightsRunner.run({'w': w, 'b': b}, {});
Implementation
void run(Map<String, Object> inputs, Map<String, Object> outputs) {
checkState(!_closed, message: 'SignatureRunner is already closed.');
// Resize input tensors if shapes differ.
for (final entry in inputs.entries) {
final tensor = getInputTensor(entry.key);
final newShape = tensor.getInputShapeIfDifferent(entry.value);
if (newShape != null) {
resizeInputTensor(entry.key, newShape);
}
}
if (!_allocated) {
allocateTensors();
}
// Copy input data into native tensors.
for (final entry in inputs.entries) {
getInputTensor(entry.key).setTo(entry.value);
}
_inferenceStopwatch
..reset()
..start();
invoke();
_inferenceStopwatch.stop();
_lastInferenceDurationMicroseconds =
_inferenceStopwatch.elapsedMicroseconds;
// Copy output data back to Dart objects.
for (final entry in outputs.entries) {
getOutputTensor(entry.key).copyTo(entry.value);
}
}