tf中list和tensor的转换

得到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:\data\178955836273545-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()
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值