python - About "PIL" error, NameError: name 'PIL' is not defined -
i new python user , new 1 in "stack overflow", when try compile tensorflow code met question, , can't found answer website, want helps here, thank in advance!
and compiling result:
d:\python\anaconda2\envs\tensorflow\python.exe d:/python/pycharm_project/test/mnist_chuji traceback (most recent call last): file "d:/python/pycharm_project/test/mnist_chuji", line 52, in <module> displayarray(u_init, rng=[-0.1, 0.1]) file "d:/python/pycharm_project/test/mnist_chuji", line 15, in displayarray pil.image.fromarray(a).save(f, fmt) nameerror: name 'pil' not defined process finished exit code 1
here code, , marked line number errors happened make finding easily:
#导入模拟仿真需要的库 import tensorflow tf import numpy np #导入可视化需要的库 pil import image io import stringio #python3 使用了io代替了sstringio ipython.display import clear_output, image, display def displayarray(a, fmt='jpeg', rng=[0,1]): """display array picture.""" = (a - rng[0])/float(rng[1] - rng[0])*255 = np.uint8(np.clip(a, 0, 255)) f = stringio() pil.image.fromarray(a).save(f, fmt) #line 15 display(image(data=f.getvalue())) sess = tf.interactivesession() def make_kernel(a): """transform 2d array convolution kernel""" = np.asarray(a) = a.reshape(list(a.shape) + [1,1]) return tf.constant(a, dtype=1) def simple_conv(x, k): """a simplified 2d convolution operation""" x = tf.expand_dims(tf.expand_dims(x, 0), -1) y = tf.nn.depthwise_conv2d(x, k, [1, 1, 1, 1], padding='same') return y[0, :, :, 0] def laplace(x): """compute 2d laplacian of array""" laplace_k = make_kernel([[0.5, 1.0, 0.5], [1.0, -6., 1.0], [0.5, 1.0, 0.5]]) return simple_conv(x, laplace_k) n = 500 # initial conditions -- rain drops hit pond # set 0 u_init = np.zeros([n, n], dtype="float32") ut_init = np.zeros([n, n], dtype="float32") # rain drops hit pond @ random points n in range(40): a,b = np.random.randint(0, n, 2) u_init[a,b] = np.random.uniform() displayarray(u_init, rng=[-0.1, 0.1]) #line 52 # parameters: # eps -- time resolution # damping -- wave damping eps = tf.placeholder(tf.float32, shape=()) damping = tf.placeholder(tf.float32, shape=()) # create variables simulation state u = tf.variable(u_init) ut = tf.variable(ut_init) # discretized pde update rules u_ = u + eps * ut ut_ = ut + eps * (laplace(u) - damping * ut) # operation update state step = tf.group( u.assign(u_), ut.assign(ut_)) # initialize state initial conditions tf.initialize_all_variables().run() # run 1000 steps of pde in range(1000): # step simulation step.run({eps: 0.03, damping: 0.04}) # visualize every 50 steps if % 50 == 0: clear_output() displayarray(u.eval(), rng=[-0.1, 0.1])
and have install pillow in tensorflow environment(python 3.5.2).
thank much!
use image.fromarray
, since image
imported pil
pil never imported.
Comments
Post a Comment