squareSideFromFloats function

int squareSideFromFloats(
  1. int floats, {
  2. int channels = 3,
  3. String label = 'input',
})

The side length S of a square [1, S, S, channels] tensor that holds floats float32 values.

Throws UnsupportedError if floats is not S*S*channels for a positive integer S.

Implementation

int squareSideFromFloats(
  int floats, {
  int channels = 3,
  String label = 'input',
}) {
  if (channels <= 0 || floats <= 0 || floats % channels != 0) {
    throw UnsupportedError(
      'Compiled $label has $floats floats, not a positive multiple of '
      '$channels channels.',
    );
  }
  final int area = floats ~/ channels;
  final int side = math.sqrt(area).round();
  if (side * side != area) {
    throw UnsupportedError(
      'Compiled $label area $area is not a perfect square '
      '(expected [1, S, S, $channels]).',
    );
  }
  return side;
}