python - How to apply interp1d to each element of a tensor in Tensorflow -
let say, have interpolation function.
def mymap(): x = np.arange(256) y = np.random.rand(x.size)*255.0 return interp1d(x, y)
this guy maps number in [0,255] number following profile given x
, y
(now y
random, though). when following, each value in image gets mapped nicely.
x = imread('...') x_ = mymap()(x)
however, how can in tensorflow? want like
img = tf.placeholder(tf.float32, [64, 64, 1], name="img") distorted_image = tf.map_fn(mymap(), img)
but results in error saying
valueerror: setting array element sequence.
for information, checked if function map simple below, works well
mymap2 = lambda x: x+10 distorted_image = tf.map_fn(mymap2, img)
how can map each number in tensor? help?
the function input of tf.map_fn
needs function written tensorflow ops. instance, 1 work:
def this_will_work(x): return tf.square(x) img = tf.placeholder(tf.float32, [64, 64, 1]) res = tf.map_fn(this_will_work, img)
this 1 not work:
def this_will_not_work(x): return np.sinh(x) img = tf.placeholder(tf.float32, [64, 64, 1]) res = tf.map_fn(this_will_not_work, img)
because np.sinh
cannot applied tensorflow tensor (np.sinh(tf.constant(1))
returns error).
solutions
you can write interpolation function in tensorflow, , maybe ask in stackoverflow question.
if absolutely want use scipy.interpolate.interp1d
, need keep code encapsulated in python. that, can use tf.py_func
, , use scipy function inside.
Comments
Post a Comment