如何在Mandlebrot tensorflow程序中显示图像。当前输出是<ipython.core.display.image object =“”>

时间:2017-04-05 09:54:02

标签: python tensorflow

&#39;&#39;&#39;导入用于模拟的库&#39;&#39;

import tensorflow as tf
import numpy as np

&#39;&#39;&#39;进口可视化&#39;&#39;&#39;

from PIL.Image
from io import BytesIO
from IPython.display import Image, display

&#39;&#39;&#39;现在我们定义一个功能,以便在我们拥有图像后实际显示图像     迭代计数&#39;&#39;

def DisplayFractal(a, fmt='jpeg'):

    img =np.concatenate([10+20*np.cos(a_cyclic),30+50*np.sin(a_cyclic),155-
    80*np.cos(a_cyclic)], 2)
    img[a==a.max()] = 0
    a = img
    a = np.uint8(np.clip(a, 0, 255))
     f = BytesIO()
    PIL.Image.fromarray(a).save(f, fmt)
    display(Image(data=f.getvalue()))


sess = tf.InteractiveSession()
# Use NumPy to create a 2D array of complex numbers

Y, X = np.mgrid[-1.3:1.3:0.005, -2:1:0.005]
Z = X+1j*Y
print(Z)
#Now we define and initialize TensorFlow tensors.

xs = tf.constant(Z.astype(np.complex64))
zs = tf.Variable(xs)
ns = tf.Variable(tf.zeros_like(xs, tf.float32))


tf.global_variables_initializer().run()

zs_ = zs*zs + xs
print(zs)

# Have we diverged with this new value?
not_diverged = tf.abs(zs_) < 4

&#39;&#39;&#39; 更新zs和迭代计数的操作。 注意:我们在分歧后继续计算zs!这个 非常浪费!如果有一点,那就更好了 不太简单,方法这样做。 &#39;&#39;&#39;     step = tf.group(zs.assign(zs_),ns.assign_add(tf.cast(not_diverged,     tf.float32)))

for i in range(200): step.run()

DisplayFractal(ns.eval())

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。您必须在Jupyter笔记本中运行TensorFlow示例: http://jupyter.org/

如果您从其他IDE(Spyder)运行它,您将在控制台中看到<IPython.core.display.Image object>

答案 1 :(得分:0)

但真正的问题是如果没有jupyter就可以做到......

答案 2 :(得分:-2)

嗯,我已经破坏了这个问题,你可以看看我的功能:

def displayFractal(a,fmt='jpeg'):
    a_cyclic=(6.28*a/200.0).reshape(list(a.shape)+[1])

    # emmm I have changed the number. you can just continue your number        
    img=np.concatenate([5+10*np.cos(a_cyclic),15+25*np.sin(a_cyclic),70-40*np.cos(a_cyclic)],2)

    img[a==a.max()]=0
    a=img
    a=np.uint8(np.clip(a,0,255))
    plt.imshow(PIL.Image.fromarray(a))
    plt.show()

当然你应该首先将matplotlib .pyplot导入为plt。