I'm trying to parallelize this chunk of code using Tensorflow -
import numpy as np
import tensorflow as tf
import time
#Importing a generic dataset from Keras
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(
path='mnist.npz'
)
#I would like to compare a reference image to a bunch of images
#This is my reference image
x_reference = np.expand_dims(x_train[0],axis = 2)
start = time.time()
for index, image in enumerate(x_train):
# The tf.image.ssim is a similarity metric
tf.image.ssim(np.expand_dims(x_train[index],axis = 2), x_reference, 255)
print("Total Time=", time.time() - start)
I've decided to use tf.while_loop for parallelization -
import numpy as np
import tensorflow as tf
#Importing a generic dataset from Keras
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(
path='mnist.npz'
)
#This is my reference image
x_reference = np.expand_dims(x_train[0],axis = 2)
t1 = tf.constant(x_train)
t2 = tf.constant(x_reference)
iters = tf.constant(60000)
def cond(t1, t2, i, iters):
return tf.less(i, iters)
def body(t1, t2, i, iters):
return [tf.image.ssim(np.expand_dims(t1[i],axis = 2), t2, 255), t2, tf.add(i,1), iters]
res = tf.while_loop(cond, body, [t1, t2, 0 , iters], parallel_iterations=60000)
However, I am running into errors. I'm quite new with tensorflow and therefore I understand that this code is going to be broken at a range of points. All forms of guidance (even through comments) are highly appreciated!