1

I have successfully performed 2d interpolation in python using the RectBivariateSpline method from scipy.interpolate. However, it is performed on numpy arrays. I want to perform it on tensors solely using tensorflow.

This is what I have right now: It works if all are numpy arrays. However, I am having a hard time to rewrite it in tensorflow.

x_old = np.arange(0,256)
y_old = np.arange(0,256)

#x = tensor of shape [256,256]
#y = tensor of shape [256,256]
#in_im = tensor of shape [256,256,3]
#out_im = tensor of shape [256,256,3]

for d in range(0,3):
    interpf = RectBivariateSpline( x_old, y_old, in_im[:,:,d])
    out_im[:,:,d] = interpf.ev(x[:,:], y[:,:])
TSimron
  • 73
  • 11

2 Answers2

0

The resize operators in tf.image might be what you are looking for, e.g. tf.image.resize_bicubic (https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/image/resize_bicubic)

Christoph Henkelmann
  • 1,437
  • 1
  • 12
  • 17
-1

To convert tensors into numpy array is the solution.
This question about conversion might be helpful.

In short, Any tensor returned by Session.run or eval is a NumPy array.

Example code is below.

import tensorflow as tf
import numpy as np
from scipy.interpolate import RectBivariateSpline

x = tf.constant([1,2,3,4])
y = tf.constant([1,2,3,4,5])
vals = tf.constant([
    [4,1,4,4,2],
    [4,2,3,2,6],
    [3,7,4,3,5],
    [2,4,5,3,4]
])

sess = tf.Session()
x, y, vals = sess.run([x, y, vals]) # x, y vals are now ndarray

rect_B_spline = RectBivariateSpline(x, y, vals)

a = tf.constant([3.2, 3.8, 2.2])
b = tf.constant([2.4, 4.3, 3.3])

a = sess.run([a, b])

print(rect_B_spline.ev(a, b))
Sihyeon Kim
  • 161
  • 7
  • 1
    This does not work for me because i am using this interpolation inside a loss function which has to be strictly done in tensors. Please let me know if there is another way to do that, but I tried to used the session and numpy calculations in the loss function but that didn't work. – TSimron Aug 14 '19 at 15:33