numpy和tensor的转换
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了numpy和tensor的转换相关的知识,希望对你有一定的参考价值。
参考技术A numpy转tensortensor转numpy
(数组张量相互转换)numpy2tensor and tensor2numpy
import tensorflow as tf
import numpy as np
path2='results/HXresults/1.png'
path3='test_imgs/mri/1.png'
MRI=(imread(path3)/255.0).astype(np.float32)
X=(imread(path2)/255.0).astype(np.float32)
MRI = np.expand_dims(MRI, axis = 0)
MRI = np.expand_dims(MRI, axis = -1)
X = np.expand_dims(X, axis = 0)
# numpy2tensor
X = tf.constant(X)
X_YCBCR = rgb2ycbcr(X)
# numpy2tensor
sess=tf.Session()
MRI =MRI.eval(session=sess)
X_YCBCR=X_YCBCR.eval(session=sess)
np_img = vifp_tf(MRI, X_YCBCR[:,:,:,0])
print(np_img)
# tensor2numpy
tf_img = tf.convert_to_tensor(a)
print(tf_img)
with tf.Session() as sess: # 查看张量内容
print(sess.run(tf_img))
以上是关于numpy和tensor的转换的主要内容,如果未能解决你的问题,请参考以下文章
服务器系列(23):tensor、numpy.array、list三者之间互相转换