Reputation: 794
I need to find median of a image read as a tensor via tensorflow.js in angular application. Can anyone suggest how to find median in tfjs?
Like -
let x = tf.tensor([1, 2, 3, 4, 5, 8, 9]);
console.log(x.median());
This should print 4.
I tried finding equivalent of tensorflow probability stats in javascript but no luck so far.
Upvotes: 2
Views: 278
Reputation: 18401
The median is the middle number after the series have been arranged. The tensor needs to first sorted. The value at the index sizeTensor / 2
will be the median value.
t= tf.tensor([1, 2, 3, 4, 5, 8, 9]);
tf.topk(t, t.size).values.slice(t.size / 2, 1).print() // will print 4
For the above to work with high dimension tensors - images for example, we will need to first reshape to 1d tensor
Given img
the tensor of an image
t = img.reshape([-1])
// do the same as above
Upvotes: 2