apply method

  1. @override
TensorBuffer apply(
  1. TensorBuffer input
)
override

Applies the defined normalization on given input tensor and returns the result.

Note: input is possibly the same instance with the output.

Returns output tensor.

Implementation

@override
TensorBuffer apply(TensorBuffer input) {
  if (isIdentityOp) {
    return input;
  }
  List<int> shape = input.getShape();
  SupportPreconditions.checkArgument(
      numChannels == 1 ||
          (shape.length != 0 && shape[shape.length - 1] == numChannels),
      errorMessage:
          "Number of means (stddevs) is not same with number of channels (size of last axis).");

  int flatSize = input.getFlatSize();
  List<double> values = List.filled(flatSize, 0);
  int j = 0;
  for (int i = 0; i < flatSize; i++) {
    values[i] = (input.getDoubleValue(i) - mean[j]) / stddev[j];
    j = (j + 1) % numChannels;
  }
  TensorBuffer output;
  if (input.isDynamic) {
    output = TensorBuffer.createDynamic(TfLiteType.float32);
  } else {
    output = TensorBuffer.createFixedSize(shape, TfLiteType.float32);
  }
  output.loadList(values, shape: shape);
  return output;
}