【问题标题】:Neural Style Transfer, using Python and iteration error?神经风格转移,使用 Python 和迭代错误?
【发布时间】:2022-03-03 15:59:30
【问题描述】:

我正在学习神经风格迁移教程,并且是该主题的新手,该论文可在 here 获得,并且我正在遵循可用的确切代码 here

我通过安装我需要的东西、TensorFlow 环境等解决了一些问题,但是在迭代部分,这个示例代码的最后几行,屏幕上出现了一个错误,我不明白为什么我看到了。

请从GitHub找到完整代码

这是迭代部分:

iterations = 4000
for i in range(1, iterations + 1):
    loss, grads = compute_loss_and_grads(combination_image, base_image, style_reference_image)
    optimizer.apply_gradients([(grads, combination_image)])
    if i % 100 == 0:
        print("Iteration %d: loss=%.2f" % (i, loss))
        img = deprocess_image(combination_image.numpy())
        fname = result_prefix + "_at_iteration_%d.png" % i
        keras.preprocessing.image.save_img(fname, img)

下面是我在运行迭代后收到的大错误。 如果您能告诉我如何解决这个问题,我们将不胜感激。

Traceback (most recent call last):

File "C:\Users\sohei\AppData\Local\Temp\ipykernel_2748\3038039193.py", line 4, in <module>
combination_image, base_image, style_reference_image

File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\tensorflow\python\eager\def_function.py", line 414, in __call__
self._initialize(args, kwds, add_initializers_to=initializer_map)

File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\tensorflow\python\eager\def_function.py", line 357, in _initialize
*args, **kwds))

File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\tensorflow\python\eager\function.py", line 1349, in _get_concrete_function_internal_garbage_collected
graph_function, _, _ = self._maybe_define_function(args, kwargs)

File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\tensorflow\python\eager\function.py", line 1652, in _maybe_define_function
graph_function = self._create_graph_function(args, kwargs)

File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\tensorflow\python\eager\function.py", line 1545, in _create_graph_function
capture_by_value=self._capture_by_value),

File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\tensorflow\python\framework\func_graph.py", line 715, in func_graph_from_py_func
func_outputs = python_func(*func_args, **func_kwargs)

File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\tensorflow\python\eager\def_functi## Heading ##on.py", line 307, in wrapped_fn
return weak_wrapped_fn().__wrapped__(*args, **kwds)

File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\tensorflow\python\framework\func_graph.py", line 705, in wrapper
raise e.ag_error_metadata.to_exception(type(e))

AttributeError: in converted code:
relative to C:\Users\sohei:

AppData\Local\Temp\ipykernel_2748\3231027975.py:171 compute_loss_and_grads *
    grads = tape.gradient(loss, combination_image)
anaconda3\envs\tf\lib\site-packages\tensorflow\python\eager\backprop.py:980 gradient
    unconnected_gradients=unconnected_gradients)
anaconda3\envs\tf\lib\site-packages\tensorflow\python\eager\imperative_grad.py:76 imperative_grad
    compat.as_str(unconnected_gradients.value))

AttributeError: 'RefVariable' object has no attribute '_id'



2022-02-08 12:17:34.494127: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
[SpyderKernelApp] ERROR | Error in message handler

Traceback (most recent call last):
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 461, in dispatch_queue
await self.process_one()
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 450, in process_one
await dispatch(*args)
TypeError: object NoneType can't be used in 'await' expression
[SpyderKernelApp] ERROR | Error in message handler
Traceback (most recent call last):
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 461, in dispatch_queue
await self.process_one()
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 450, in process_one
await dispatch(*args)
TypeError: object NoneType can't be used in 'await' expression
[SpyderKernelApp] ERROR | Error in message handler
Traceback (most recent call last):
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 461, in dispatch_queue
await self.process_one()
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 450, in process_one
await dispatch(*args)
TypeError: object NoneType can't be used in 'await' expression
[SpyderKernelApp] ERROR | Error in message handler
Traceback (most recent call last):
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 461, in dispatch_queue
await self.process_one()
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 450, in process_one
await dispatch(*args)
TypeError: object NoneType can't be used in 'await' expression
[SpyderKernelApp] ERROR | Error in message handler
Traceback (most recent call last):
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 461, in dispatch_queue
await self.process_one()
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 450, in process_one
await dispatch(*args)
TypeError: object NoneType can't be used in 'await' expression
[SpyderKernelApp] ERROR | Error in message handler
Traceback (most recent call last):
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 461, in dispatch_queue
await self.process_one()
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 450, in process_one
await dispatch(*args)
TypeError: object NoneType can't be used in 'await' expression
[SpyderKernelApp] ERROR | Error in message handler
Traceback (most recent call last):
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 461, in dispatch_queue
await self.process_one()
File "C:\Users\sohei\anaconda3\envs\tf\lib\site-packages\ipykernel\kernelbase.py", line 450, in process_one
await dispatch(*args)
TypeError: object NoneType can't be used in 'await' expression
[SpyderKernelApp] ERROR | Error in message handler

TypeError: object NoneType can't be used in 'await' expression*emphasized text*

【问题讨论】:

  • 您的实际错误似乎是这个:AttributeError: 'RefVariable' object has no attribute '_id'this question 有帮助吗?
  • @aaossa :非常感谢您的评论!实际上,这解决了问题并帮助了很多!这是我笔记本电脑的处理器:Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz 2.30 GHz,仅 200 次迭代就花了 ~ 55 分钟!!!非常感谢任何建议! #NeuralStyleTransfer #TensorFlow
  • 这能回答你的问题吗? 'RefVariable' object has no attribute '_id'

标签: python tensorflow iteration


【解决方案1】:

当您尝试使用 TF 1.x 运行代码时,此 'AttributeError: 'RefVariable' object has no attribute '_id'' 会显示。

我尝试在Google ColabTensorflow 2.7 中复制相同的内容,但没有显示任何错误。

尝试使用Tensorflow 2.x(急切模式)运行此代码(由 aaossa 解释)并使用gpu 运行代码,相对而言可能需要更少的时间来执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多