2

I have a batch of images as a tensor of size [batch_size, w, h].

I wish to get a histogram of the values in each column.

This is what I came up with (but it works only for the first image in the batch and its also very slow):

global_hist = []
net = tf.squeeze(net)
for i in range(batch_size):
    for j in range(1024):
        hist = tf.histogram_fixed_width(tf.slice(net,[i,0,j],[1,1024,1]), [0.0, 0.2, 0.4, 0.6, 0.8, 1.0], nbins=10)
        global_hist[i].append(hist)

Is there an efficient way to do this?

itzik Ben Shabat
  • 927
  • 11
  • 24

1 Answers1

0

ok so I found a solution (though its rather slow and does not allow to fix the bins edges), but someone may find this usefull.

nbins=10
net = tf.squeeze(net)
for i in range(batch_size):
    local_hist = tf.expand_dims(tf.histogram_fixed_width(tf.slice(net,[i,0,0],[1,1024,1]), [0.0, 1.0], nbins=nbins, dtype=tf.float32),[-1])
    for j in range(1,1024):
        hist = tf.histogram_fixed_width(tf.slice(net,[i,0,j],[1,1024,1]), [0.0, 1.0], nbins=nbins, dtype=tf.float32)
        hist = tf.expand_dims(hist,[-1])
        local_hist = tf.concat(1, [local_hist, hist])
    if i==0:
        global_hist = tf.expand_dims(local_hist, [0])
    else:
        global_hist = tf.concat(0, [global_hist, tf.expand_dims(local_hist,[0])])

In addition, I found this link to be very usefull https://stackoverflow.com/questions/41764199/row-wise-histogram/41768777#41768777

Radu
  • 713
  • 1
  • 10
  • 13
itzik Ben Shabat
  • 927
  • 11
  • 24