【问题标题】:How do I convert from a tensorflow tensor to an image without using numpy?如何在不使用 numpy 的情况下将张量流张量转换为图像?
【发布时间】:2022-01-08 07:04:14
【问题描述】:

我试图使用numpy.asarray(tensor) 将张量转换为 ndarray。然后,我打算使用 PIL 将该 ndarray 转换为可下载的图像。但是,由于我在 TPU 上运行此代码,因此它阻止了 numpy 转换,无法继续执行 PIL 步骤。有没有办法直接从张量转换为图像,而不转换为ndarray?

【问题讨论】:

    标签: python image numpy tensorflow tensor


    【解决方案1】:

    也许尝试使用tf.keras.preprocessing.image.array_to_img 将张量转换为PIL 图像:

    import tensorflow as tf
    import matplotlib.pyplot as plt
    
    img = tf.random.normal((100, 100, 3))
    pil_img = tf.keras.preprocessing.image.array_to_img(img)
    plt.imshow(pil_img)
    
    <matplotlib.image.AxesImage at 0x7f3483d71310>
    

    【讨论】:

    • 谢谢,我终于弄清楚了真正的问题:通过TPU执行图表后,输出处于某种错误状态。我修复了它,然后它开始工作得更好了。
    猜你喜欢
    • 2021-04-28
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 2018-02-13
    相关资源
    最近更新 更多