The code shown below takes the partial derivative of each pixel of the output fused image of a neural network with respect to input image of the neural network using tensorflow's tf.gradients(Y, X) and compute a Jacobian matrix for each of the two input images MRI and PET. However, the code is really slow since I currently define a for loop by iterating through each pixel to compute the Jacobian matrix.

jacob_matrix_mri = np.zeros((65536,256,256))
jacob_matrix_pet = np.zeros((65536,256,256))

count = 0
saver = tf.train.Saver()
with tf.Session() as sess:
   saver.restore(sess,'/home/nora/Desktop/MBIA 2019/Checkpoint/')
   for i in range(image_width):
        for j in range(image_length):
            grad_mri = tf.gradients(fused_image[0,i,j,0],images_mri)
            grad_pet = tf.gradients(fused_image[0,i,j,0],images_pet)
            gradients_mri, gradients_pet, _ =sess.run([grad_mri,grad_pet,fused_image],                                                                     

            jacob_matrix_mri[count,:,:] = np.squeeze(gradients_mri[0][0,:])
            jacob_matrix_pet[count,:,:] = np.squeeze(gradients_pet[0][0,:])
            count = count+1
            if count % 1000 == 0:
               print count
               t2 = time.time()
               print (t2-t1)
t3 = time.time()
  • \$\begingroup\$ What are you looking to get out of a review? The “however it’s very slow” bit makes it sound like you’d like tips on speeding up the code, but you don’t actually ask this... Wherever you post a question, the more explicit you are, the more likely you are of getting a useful answer! \$\endgroup\$ – Cris Luengo Jun 19 '19 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.