File size: 1,749 Bytes
d353048 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | using Unity.InferenceEngine;
namespace Unity.MLAgents.Inference
{
internal static class TensorExtensions
{
// assumes NCHW (channel first) but might be NHWC
public static int Batch(this Tensor tensor)
{
return tensor.shape.Batch();
}
public static int Height(this Tensor tensor)
{
return tensor.shape.Height();
}
public static int Width(this Tensor tensor)
{
return tensor.shape.Width();
}
public static int Channels(this Tensor tensor)
{
return tensor.shape.Channels();
}
public static int Length(this Tensor tensor)
{
return tensor.shape.length;
}
}
internal static class TensorShapeExtensions
{
public static int Batch(this TensorShape shape)
{
return shape.rank >= 1 ? shape[0] : 0;
}
public static int Height(this TensorShape shape)
{
return shape.rank >= 4 ? shape[shape.rank - 2] : 0;
}
public static int Width(this TensorShape shape)
{
return shape.rank >= 3 ? shape[shape.rank - 1] : 0;
}
public static int Channels(this TensorShape shape)
{
return shape.rank is >= 2 and < 4 ? shape[1] : shape.rank >= 4 ? shape[shape.rank - 3] : 0;
}
public static int Index(this TensorShape shape, int n, int c, int h, int w)
{
int index =
n * shape.Height() * shape.Width() * shape.Channels() +
c * shape.Height() * shape.Width() +
h * shape.Width() +
w;
return index;
}
}
}
|