Bilinear interpolation by convolution

1.2k Views Asked by At

I tried to do the upsampling by transposed convolution.Since I was unable to figure out the kernel weights,I tried the following way.

Upsample image by a factor of (using nearest neighbour method) Slide a 2x2 filter with 0.25 values over it. This should yield all the values except last col and row. The following code works correctly only when input and output filters are unit sized.

from tensorflow.keras import layers
import numpy as np

nof = 9
# The input data (could be an image).
temp = tf.constant([i for i in range(2*2*nof)], tf.float64)
temp2 = tf.reshape(temp, [1, 2, 2,nof])

# A filter (affects the convolution).
filter = tf.constant([0.25 for i in range(2*2*nof*nof)], tf.float64)
filter2 = tf.reshape(filter, [2, 2, nof, nof])


img = layers.UpSampling2D(2)(temp2)
print(img.shape)


# Use convolution layer on 4D matrices.
#convolution = tf.nn.conv2d(img, filter2, [1, 1, 1, 1], padding="SAME")
convolution1 = tf.nn.conv2d(img, filter2, [1, 1, 1, 1], padding="VALID")
# Initialize session.
session = tf.Session()
tf.global_variables_initializer()


print("FILTER")
print([i for i in np.ravel(session.run(filter2))])
print('\n')


print(session.run(temp2),'\n')
image = session.run(img)
print(image)

print("CONVOLUTION")
c1 = session.run(convolution1)
print(c1)



0

There are 0 best solutions below