如何仅在应用程序的特定部分使用 tensorflow eager execution?
How to use tensorflow eager execution only in specific parts of the application?
我有几个不同文件的文件:
- main.py
- watch.py
- read.py
- detect.py <-- 使用基于 tensorflow 的库
darkflow
依赖图形模式
- translate.py <-- 使用 tf
急于执行
在 darkflow 的 TFNet 初始化过程中我得到这个错误:
Traceback (most recent call last):
File "/home/justin/Projects/comp3931/main.py", line 6, in <module>
watcher = Watcher('res/vid/planet_earth_s01e01/video.mp4', 'res/vid/planet_earth_s01e01/english.srt')
File "/home/justin/Projects/comp3931/watch.py", line 9, in __init__
self.detector = Detector()
File "/home/justin/Projects/comp3931/detect.py", line 6, in __init__
self.tfnet = TFNet(self.options)
File "/usr/local/lib64/python3.6/site-packages/darkflow/net/build.py", line 75, in __init__
self.build_forward()
File "/usr/local/lib64/python3.6/site-packages/darkflow/net/build.py", line 105, in build_forward
self.inp = tf.placeholder(tf.float32, inp_size, 'input')
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1677, in placeholder
raise RuntimeError("tf.placeholder() is not compatible with "
RuntimeError: tf.placeholder() is not compatible with eager execution.
所以,我假设当我从 translate.py
文件实例化 Translator
class 时,它会调用整个程序的急切执行,这与对 darkflow 的 TFNet
class 用于 Dectector
class 来自 detect.py
如果我 运行 translate.py
独立于其他人它工作正常,其他模块如果 运行 它们没有 translate.py
参与也工作正常。
我猜他们使用不同的上下文 (graph/eager),整个事情不能 运行 在同一个程序中。我试过查看文档,但找不到在需要时切换回图形模式的方法。
有什么方法可以 运行 在同一个应用程序的不同位置同时使用 eager 和 graph 模式?
https://www.tensorflow.org/programmers_guide/eager
(向下滚动到 "Use eager execution in a graph environment")。
也许这有帮助...
最好编写同时兼容图形模式和即时执行的代码。来自 documentation:
- Use tf.data for input processing instead of queues. It's faster and easier.
- Use object-oriented layer APIs—like tf.keras.layers and tf.keras.Model—since they have explicit storage for variables.
- Most model code works the same during eager and graph execution, but there are exceptions. (For example, dynamic models using Python
control flow to change the computation based on inputs.)
- Once eager execution is enabled with tf.enable_eager_execution, it cannot be turned off. Start a new Python session to return to graph
execution.
也就是说,可以通过使用 tfe.py_func()
在图形模式下使用即时执行。这是文档中的代码示例(我刚刚添加了导入和断言):
import tensorflow as tf
import tensorflow.contrib.eager as tfe
def my_py_func(x):
assert tf.executing_eagerly()
x = tf.matmul(x, x) # You can use tf ops
print(x) # but it's eager!
return x
assert not tf.executing_eagerly()
with tf.Session() as sess:
x = tf.placeholder(dtype=tf.float32)
# Call eager function in graph!
pf = tfe.py_func(my_py_func, [x], tf.float32)
sess.run(pf, feed_dict={x: [[2.0]]}) # [[4.0]]
反过来也是可能的,正如 Alex Passos 在 this video 中解释的那样。这是一个受视频中启发的示例:
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tf.enable_eager_execution()
def my_graph_func(x):
assert not tf.executing_eagerly()
w = tfe.Variable(2.0)
b = tfe.Variable(4.0)
return x * w + b
assert tf.executing_eagerly()
g = tfe.make_template("g", my_graph_func, create_graph_function_=True)
print(g(3))
还有一种非官方的方式来切换模式,使用 tensorflow.python.eager.context
中定义的 eager_mode
和 graph_mode
上下文,如下所示:
import tensorflow as tf
import tensorflow.contrib.eager as tfe
from tensorflow.python.eager.context import eager_mode, graph_mode
with eager_mode():
print("Eager mode")
assert tf.executing_eagerly()
x1 = tfe.Variable(5.0)
print(x1.numpy())
print()
with graph_mode():
print("Graph mode")
assert not tf.executing_eagerly()
x2 = tfe.Variable(5.0)
with tf.Session():
x2.initializer.run()
print(x2.eval())
由于它不是官方的,您可能应该在生产代码中避免使用它,但在调试时或在 Jupyter notebook 中它可能会派上用场。最后一种选择是使用此 switch_to()
函数:
import tensorflow as tf
import tensorflow.contrib.eager as tfe
from tensorflow.python.eager.context import context, EAGER_MODE, GRAPH_MODE
def switch_to(mode):
ctx = context()._eager_context
ctx.mode = mode
ctx.is_eager = mode == EAGER_MODE
switch_to(EAGER_MODE)
assert tf.executing_eagerly()
v = tfe.Variable(3.0)
print(v.numpy())
assert tf.get_default_graph().get_operations() == []
switch_to(GRAPH_MODE)
assert not tf.executing_eagerly()
v = tfe.Variable(3.0)
init = tf.global_variables_initializer()
assert len(tf.get_default_graph().get_operations()) > 0
with tf.Session():
init.run()
print(v.eval())
这确实是一个 hack,但如果您不喜欢将所有代码嵌套在 with
块中,它可能在 Jupyter notebook 中很有用。
我有几个不同文件的文件:
- main.py
- watch.py
- read.py
- detect.py <-- 使用基于 tensorflow 的库
darkflow
依赖图形模式 - translate.py <-- 使用 tf 急于执行
在 darkflow 的 TFNet 初始化过程中我得到这个错误:
Traceback (most recent call last):
File "/home/justin/Projects/comp3931/main.py", line 6, in <module>
watcher = Watcher('res/vid/planet_earth_s01e01/video.mp4', 'res/vid/planet_earth_s01e01/english.srt')
File "/home/justin/Projects/comp3931/watch.py", line 9, in __init__
self.detector = Detector()
File "/home/justin/Projects/comp3931/detect.py", line 6, in __init__
self.tfnet = TFNet(self.options)
File "/usr/local/lib64/python3.6/site-packages/darkflow/net/build.py", line 75, in __init__
self.build_forward()
File "/usr/local/lib64/python3.6/site-packages/darkflow/net/build.py", line 105, in build_forward
self.inp = tf.placeholder(tf.float32, inp_size, 'input')
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 1677, in placeholder
raise RuntimeError("tf.placeholder() is not compatible with "
RuntimeError: tf.placeholder() is not compatible with eager execution.
所以,我假设当我从 translate.py
文件实例化 Translator
class 时,它会调用整个程序的急切执行,这与对 darkflow 的 TFNet
class 用于 Dectector
class 来自 detect.py
如果我 运行 translate.py
独立于其他人它工作正常,其他模块如果 运行 它们没有 translate.py
参与也工作正常。
我猜他们使用不同的上下文 (graph/eager),整个事情不能 运行 在同一个程序中。我试过查看文档,但找不到在需要时切换回图形模式的方法。
有什么方法可以 运行 在同一个应用程序的不同位置同时使用 eager 和 graph 模式?
https://www.tensorflow.org/programmers_guide/eager (向下滚动到 "Use eager execution in a graph environment")。
也许这有帮助...
最好编写同时兼容图形模式和即时执行的代码。来自 documentation:
- Use tf.data for input processing instead of queues. It's faster and easier.
- Use object-oriented layer APIs—like tf.keras.layers and tf.keras.Model—since they have explicit storage for variables.
- Most model code works the same during eager and graph execution, but there are exceptions. (For example, dynamic models using Python control flow to change the computation based on inputs.)
- Once eager execution is enabled with tf.enable_eager_execution, it cannot be turned off. Start a new Python session to return to graph execution.
也就是说,可以通过使用 tfe.py_func()
在图形模式下使用即时执行。这是文档中的代码示例(我刚刚添加了导入和断言):
import tensorflow as tf
import tensorflow.contrib.eager as tfe
def my_py_func(x):
assert tf.executing_eagerly()
x = tf.matmul(x, x) # You can use tf ops
print(x) # but it's eager!
return x
assert not tf.executing_eagerly()
with tf.Session() as sess:
x = tf.placeholder(dtype=tf.float32)
# Call eager function in graph!
pf = tfe.py_func(my_py_func, [x], tf.float32)
sess.run(pf, feed_dict={x: [[2.0]]}) # [[4.0]]
反过来也是可能的,正如 Alex Passos 在 this video 中解释的那样。这是一个受视频中启发的示例:
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tf.enable_eager_execution()
def my_graph_func(x):
assert not tf.executing_eagerly()
w = tfe.Variable(2.0)
b = tfe.Variable(4.0)
return x * w + b
assert tf.executing_eagerly()
g = tfe.make_template("g", my_graph_func, create_graph_function_=True)
print(g(3))
还有一种非官方的方式来切换模式,使用 tensorflow.python.eager.context
中定义的 eager_mode
和 graph_mode
上下文,如下所示:
import tensorflow as tf
import tensorflow.contrib.eager as tfe
from tensorflow.python.eager.context import eager_mode, graph_mode
with eager_mode():
print("Eager mode")
assert tf.executing_eagerly()
x1 = tfe.Variable(5.0)
print(x1.numpy())
print()
with graph_mode():
print("Graph mode")
assert not tf.executing_eagerly()
x2 = tfe.Variable(5.0)
with tf.Session():
x2.initializer.run()
print(x2.eval())
由于它不是官方的,您可能应该在生产代码中避免使用它,但在调试时或在 Jupyter notebook 中它可能会派上用场。最后一种选择是使用此 switch_to()
函数:
import tensorflow as tf
import tensorflow.contrib.eager as tfe
from tensorflow.python.eager.context import context, EAGER_MODE, GRAPH_MODE
def switch_to(mode):
ctx = context()._eager_context
ctx.mode = mode
ctx.is_eager = mode == EAGER_MODE
switch_to(EAGER_MODE)
assert tf.executing_eagerly()
v = tfe.Variable(3.0)
print(v.numpy())
assert tf.get_default_graph().get_operations() == []
switch_to(GRAPH_MODE)
assert not tf.executing_eagerly()
v = tfe.Variable(3.0)
init = tf.global_variables_initializer()
assert len(tf.get_default_graph().get_operations()) > 0
with tf.Session():
init.run()
print(v.eval())
这确实是一个 hack,但如果您不喜欢将所有代码嵌套在 with
块中,它可能在 Jupyter notebook 中很有用。