tf2.0:tf.image.resize“U with”pad失败,原因是;使用'tf.Tensor'作为Python'bool'quot;使用tf.keras.Input
使用tf2.0:tf.image.resize“U with”pad失败,原因是;使用'tf.Tensor'作为Python'bool'quot;使用tf.keras.Input,python,tensorflow,machine-learning,keras,tensorflow2.0,Python,Tensorflow,Machine Learning,Keras,Tensorflow2.0,使用tensorflow 2.0,resize\u With_pad在tf.keras.Input作为输入时似乎不起作用,但resize效果很好。比如说, import tensorflow as tf # with tensorflow constant img_arr = tf.zeros([1,100,100,3]) tf.image.resize(img_arr, [224, 224]) # works tf.image.resize_with_pad(img_arr, 224, 22
tensorflow 2.0
,resize\u With_pad
在tf.keras.Input
作为输入时似乎不起作用,但resize
效果很好。比如说,
import tensorflow as tf
# with tensorflow constant
img_arr = tf.zeros([1,100,100,3])
tf.image.resize(img_arr, [224, 224]) # works
tf.image.resize_with_pad(img_arr, 224, 224) # works
# with keras input
img_arr = tf.keras.Input(shape = (100,100,3))
tf.image.resize(img_arr, [224, 224]) # works
tf.image.resize_with_pad(img_arr, 224, 224) # doesn't work
抛出错误
---------------------------------------------------------------------------
OperatorNotAllowedInGraphError Traceback (most recent call last)
<ipython-input-29-aee2cbd13944> in <module>
9 img_arr = tf.keras.Input(shape = (100,100,3))
10 tf.image.resize(img_arr, [224, 224]) # works
---> 11 tf.image.resize_with_pad(img_arr, 224, 224) # doesn't work
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/ops/image_ops_impl.py in resize_image_with_pad_v2(image, target_height, target_width, method, antialias)
1472
1473 return _resize_image_with_pad_common(image, target_height, target_width,
-> 1474 _resize_fn)
1475
1476
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/ops/image_ops_impl.py in _resize_image_with_pad_common(image, target_height, target_width, resize_fn)
1337 raise ValueError('\'image\' must have either 3 or 4 dimensions.')
1338
-> 1339 assert_ops = _CheckAtLeast3DImage(image, require_static=False)
1340 assert_ops += _assert(target_width > 0, ValueError,
1341 'target_width must be > 0.')
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/ops/image_ops_impl.py in _CheckAtLeast3DImage(image, require_static)
226 check_ops.assert_positive(
227 array_ops.shape(image),
--> 228 ["all dims of 'image.shape' "
229 'must be > 0.']),
230 check_ops.assert_greater_equal(
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/ops/check_ops.py in assert_positive(x, data, summarize, message, name)
266 'x (%s) = ' % name, x]
267 zero = ops.convert_to_tensor(0, dtype=x.dtype)
--> 268 return assert_less(zero, x, data=data, summarize=summarize)
269
270
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/ops/check_ops.py in assert_less(x, y, data, summarize, message, name)
865 ]
866 condition = math_ops.reduce_all(math_ops.less(x, y))
--> 867 return control_flow_ops.Assert(condition, data, summarize=summarize)
868
869
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/util/tf_should_use.py in wrapped(*args, **kwargs)
196 """
197 def wrapped(*args, **kwargs):
--> 198 return _add_should_use_warning(fn(*args, **kwargs))
199 return tf_decorator.make_decorator(
200 fn, wrapped, 'should_use_result',
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/ops/control_flow_ops.py in Assert(condition, data, summarize, name)
147 """
148 if context.executing_eagerly():
--> 149 if not condition:
150 xs = ops.convert_n_to_tensor(data)
151 data_str = [_summarize_eager(x, summarize) for x in xs]
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in __bool__(self)
763 `TypeError`.
764 """
--> 765 self._disallow_bool_casting()
766
767 def __nonzero__(self):
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _disallow_bool_casting(self)
532 else:
533 # Default: V1-style Graph execution.
--> 534 self._disallow_in_graph_mode("using a `tf.Tensor` as a Python `bool`")
535
536 def _disallow_iteration(self):
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in _disallow_in_graph_mode(self, task)
521 raise errors.OperatorNotAllowedInGraphError(
522 "{} is not allowed in Graph execution. Use Eager execution or decorate"
--> 523 " this function with @tf.function.".format(task))
524
525 def _disallow_bool_casting(self):
OperatorNotAllowedInGraphError: using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function.
---------------------------------------------------------------------------
OperatorNotAllowedInGraphError回溯(最近一次呼叫最后一次)
在里面
9 img_arr=tf.keras.Input(形状=(100100,3))
10 tf.image.resize(img_arr[224224])#有效
--->11 tf.image.使用_pad(img_arr,224,224)调整#的大小#不起作用
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow\u core/python/ops/image\u ops\u impl.py,使用pad\u v2调整图像大小(图像、目标高度、目标宽度、方法、抗锯齿)
1472
1473返回_resize_image_和_pad_common(图像、目标高度、目标宽度、,
->1474(调整大小)
1475
1476
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/ops/image_ops_impl.py in_resize_image_与_pad_common(图像、目标高度、目标宽度、调整大小_fn)
1337 raise VALUE ERROR(“\'image\”必须具有3或4个维度。“)
1338
->1339 assert_ops=_CheckAtLeast3DImage(image,require_static=False)
1340 assert_ops+=_assert(目标宽度>0,值错误,
1341'目标宽度必须大于0')
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow\u core/python/ops/image\u ops\u impl.py in\u checkAtlast3Dimage(图像,需要静态)
226检查操作。断言为阳性(
227阵列操作形状(图像),
-->228[“图像形状”的所有阴影”
229'必须大于0'],
230检查操作断言大于等于(
/断言中的anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/ops/check_ops.py(x,数据,摘要,消息,名称)
266'x(%s)='%name,x]
267零=运算。转换为张量(0,dtype=x.dtype)
-->268返回assert_less(零,x,data=data,summary=summary)
269
270
/assert_less中的anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/ops/check_ops.py(x,y,数据,摘要,消息,名称)
865 ]
866条件=数学运算减少所有(数学运算减少(x,y))
-->867返回控制流操作断言(条件、数据、汇总=汇总)
868
869
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/util/tf_应使用包装中的.py(*args,**kwargs)
196 """
197 def包裹(*args,**kwargs):
-->198返回_添加_应使用_警告(fn(*args,**kwargs))
199返回tf_decorator.make_decorator(
200 fn,包装为“应使用结果”,
/Assert中的anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/ops/control_flow_ops.py(条件、数据、摘要、名称)
147 """
148如果上下文。急切地执行_():
-->149如果不是条件:
150 xs=运算。将n_转换为张量(数据)
151 data_str=[[u summary_eager(x,summary)表示x在xs中的位置]
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow\u core/python/framework/ops.py in\uuuuu bool\uuu(self)
763“TypeError”。
764 """
-->765 self.\u不允许\u bool\u铸造()
766
767定义非零(自):
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/framework/ops.py in_disallow_bool_casting(self)
532其他:
533#默认值:V1样式的图形执行。
-->534 self.\u不允许在“图形”模式下使用(“使用`tf.Tensor`作为Python`bool`”)
535
536定义不允许迭代(自):
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow\u core/python/framework/ops.py处于图形模式(self,task)
521引发错误。操作员或不允许引发错误(
522在图形执行中不允许“{}。请使用渴望执行或装饰”
-->523“此函数带有@tf.function.”.format(任务))
524
525 def_不允许铸造(自身):
OperatorNotAllowedInGraphError:在图形执行中不允许将`tf.Tensor`用作Python`bool`。请使用渴望执行或使用@tf.function修饰此函数。
这可能是Tensorflow 2.0版中的一个错误,但在Tensorflow 2.1版中已修复
因此,请将Tensorflow版本升级到2.1
或2.2
,问题将得到解决
工作代码如下所述:
!pip install tensorflow==2.2
import tensorflow as tf
print(tf.__version__)
img_arr = tf.keras.Input(shape = (100,100,3))
tf.image.resize(img_arr, [224, 224]) # works
tf.image.resize_with_pad(img_arr, 224, 224) # # works now in TF >= 2.1
输出:
2.2.0
<tf.Tensor 'Pad_1:0' shape=(None, 224, 224, 3) dtype=float32>
2.2.0
这可能是Tensorflow 2.0版中的一个错误,但在Tensorflow 2.1版中已修复
因此,请将Tensorflow版本升级到2.1
或2.2
,问题将得到解决
工作代码如下所述:
!pip install tensorflow==2.2
import tensorflow as tf
print(tf.__version__)
img_arr = tf.keras.Input(shape = (100,100,3))
tf.image.resize(img_arr, [224, 224]) # works
tf.image.resize_with_pad(img_arr, 224, 224) # # works now in TF >= 2.1
输出:
2.2.0
<tf.Tensor 'Pad_1:0' shape=(None, 224, 224, 3) dtype=float32>
2.2.0