How to interpolate data of mnist digits set?

Viewed 196

I want to reshape the MNIST digits dataset from (28, 28) to (32, 32). One way is to interpolate the data. I use custom Radial Basis Function for interpolation. How to do it??

Here is the RBF function

def RBF(x, c, s):
return np.exp(-np.sum((x-c)**2, axis=1)/(2*s**2))

where x is the actual value, c is the centre(assumed as mean) and s is standard deviation.

Here is how I load the data from tensorflow

import tensorflow as tf
data = tf.contrib.learn.datasets.mnist.load_mnist()
1 Answers

You can use, tf.image.resize_with_pad which resizes the image to a target width and height by keeping the aspect ration the same without distortion.

Below is the code for the same.

import tensorflow as tf
from matplotlib import pyplot as plt
(x_train, y_train), (x_test, y_test) =tf.keras.datasets.mnist.load_data(path='mnist.npz')

original_image = x_train[0].reshape(28,28,1)
resized_image = tf.image.resize_with_pad(original_image, 32,32) 

plt.imshow(original_image.reshape(28,28)) 

Original Image:

enter image description here

Resized Image:

plt.imshow(resized_image.numpy().reshape(32,32))

enter image description here

You can see the scale range in both the images and the aspect ratio is also maintained.

Related