如何在张量流中制作张量柱的直方图

问题描述:

我有一批图像作为尺寸为[batch_size, w, h]的张量。如何在张量流中制作张量柱的直方图

我希望得到每列中值的直方图。

这是我想出了(但它仅适用于第一个图像中的批次和其也非常慢):

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) 

是否有一个有效的方式来做到这一点?

好吧,我找到了一个解决方案(虽然它相当慢,不允许修复垃圾箱边缘),但有人可能会发现这个有用的。

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])]) 

另外,我发现这个链接是非常有用的 https://*.com/questions/41764199/row-wise-histogram/41768777#41768777