python - type object 'Image' has no attribute 'fromarray' -
when compile code, request me result below:
d:\python\anaconda3\envs\tensorflow\python.exe d:/python/pycharm_project/test/mnist_chuji 2017-08-15 14:07:37.587932: w c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] tensorflow library wasn't compiled use sse instructions, these available on machine , speed cpu computations. 2017-08-15 14:07:37.588611: w c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] tensorflow library wasn't compiled use sse2 instructions, these available on machine , speed cpu computations. 2017-08-15 14:07:37.589142: w c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] tensorflow library wasn't compiled use sse3 instructions, these available on machine , speed cpu computations. 2017-08-15 14:07:37.589598: w c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] tensorflow library wasn't compiled use sse4.1 instructions, these available on machine , speed cpu computations. 2017-08-15 14:07:37.590038: w c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] tensorflow library wasn't compiled use sse4.2 instructions, these available on machine , speed cpu computations. 2017-08-15 14:07:37.590437: w c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] tensorflow library wasn't compiled use avx instructions, these available on machine , speed cpu computations. 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 image.fromarray(a).save(f, fmt) attributeerror: type object 'image' has no attribute 'fromarray' process finished exit code 1
and here code( have marked line numbers happened in error list):
#导入模拟仿真需要的库 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() 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])
i don't why 'image' has no attribute 'fromarray'. have installed lib pillow.
at beginning, thought maybe because there 2 versions of python(2.7 , 3.5) in computer makes problem. then, uninstall python environment , install py3.5 again installing pillow. there no help...
try
#导入模拟仿真需要的库 import tensorflow tf import numpy np #导入可视化需要的库 pil import image io import stringio #python3 使用了io代替了sstringio ipython.display import clear_output, image displayimage, 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() image.fromarray(a).save(f, fmt) #line 15 display(displayimage(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])
Comments
Post a Comment