我是靠谱客的博主 传统外套,这篇文章主要介绍tf中list和tensor的转换[4,224,224,4],现在分享给大家,希望可以做个参考。

得到shape
input_shape = [4,224,224,4]
input = tf.random_normal(input_shape) #转成tensor

inputs_shape = input.get_shape().as_list() #转成list
print(inputs_shape)

[4,224,224,4]

tensor转ndarray

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

image_path = r"E:data178955836273545-Back.jpg"

#image_string = open(image_path,'rb').read()
#image_string = tf.gfile.FastGFile(image_path, 'rb').read()  #  字节
image_string = tf.io.read_file(image_path)

image = tf.image.decode_jpeg(image_string)  # 2.图片解码

with tf.Session() as sess:

    print(sess.run(image).shape)

    image = image.eval()  # 将tensor对象转成数组
    print(image.shape)
    plt.imshow(image)
    plt.show()

    image = tf.expand_dims(image, 0)
    #image = np.asarray(sess.run(image), dtype='uint8')  # TF处理过的图片自动转换了类型,需要调整回uint8才能正常显示,将tensor转为ndarray
    image = tf.image.convert_image_dtype(image,dtype=tf.uint8)
    image = image.eval()

    height, width, channels = image.shape[1:]
    image = image.reshape(height, width, channels)
    plt.imshow(image)
    plt.show()

最后

以上就是传统外套最近收集整理的关于tf中list和tensor的转换[4,224,224,4]的全部内容,更多相关tf中list和tensor内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(126)

评论列表共有 0 条评论

立即
投稿
返回
顶部