我有一个图片数据集作为张量,每个像素的值在0到1之间,我有一组“bin”。
bins = [0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95]我想返回一个张量,每个像素值都是它最近的bin。例如,如果像素是0.03,它将变成0.05,如果像素是0.79,它将变成0.75。
我希望这是用张量完成的,而不是numpy。
这就是它在numpy中的工作方式。然而,当涉及到迭代时,张量流似乎是一个完全不同的野兽。我已经尝试了tf.map_fn和tf.scan来遍历,但是我不能让它工作。
def valueQuant(picture, splitSize):
#This is the Picture that will be returned
Quant_Pic = np.zeros((picture.shape[0], picture.shape[1]))
#go through each pixel of the image
for y_col in range(picture.shape[0]):
for x_row in range(picture.shape[1]):
#isolate regions based on value
for i in range(splitSize):
#low and high values to isolate
lowFloatRange = float((1/splitSize)*i)
highFloatRange = float((1/splitSize)*(i+1))
#value to turn entire clustor
midRange = lowFloatRange + ((highFloatRange - lowFloatRange)/2)
#current value of current pixel
curVal = picture[y_col][x_row]
#if the current value is within the range of interest
if(curVal >= lowFloatRange and curVal <= highFloatRange):
Quant_Pic[y_col][x_row] = midRange
return Quant_Pic 仅使用张量流方法,我就能找到一种单元方法。
def quant_val(current_input):
bins = tf.constant([0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95])
dist = tf.tile(current_input, [10])
dist = tf.math.subtract(bins, current_input)
absDist = tf.math.abs(dist)
idx = tf.math.argmin(absDist)
output = bins[idx]
output = tf.expand_dims(output, 0)
print("output", output)
return output
current_input = tf.constant([0.53])
quant_val(current_input)这能够返回具有单个值的张量的正确答案,但我不确定如何将其外推到更大的图像张量结构。任何帮助都将不胜感激!谢谢你们,仁慈的智者们。
发布于 2019-06-30 13:12:14
圆周方法:
这非常简单,但是一些.5值是向上舍入的,其他值是向下舍入的。如果这不是问题:
def quant_val(images): #0 to 1
images = (images - 0.05) * 10 #-0.5 to 9.5
bins = tf.round(images) #0 to 9
bins = tf.clip_by_value(bins, 0, 9) #possible -1 and 10 due to the remark on top
return (bins/10) + 0.05 #0.05 to 0.95https://stackoverflow.com/questions/56821446
复制相似问题