问题
I am currently trying to better understand Tensorflows CustomLayer feature. While implementing such a custom layer, I ran into the following error:
/usr/lib/python3/dist-packages/skimage/util/dtype.py:110: UserWarning: Possible precision loss when converting from float64 to uint16
"%s to %s" % (dtypeobj_in, dtypeobj))
/usr/lib/python3/dist-packages/skimage/exposure/exposure.py:307: RuntimeWarning: invalid value encountered in true_divide
image = (image - imin) / float(imax - imin)
Traceback (most recent call last):
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/client/session.py", line 1323, in _do_call
return fn(*args)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/client/session.py", line 1302, in _run_fn
status, run_metadata)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: 0-th value returned by pyfunc_0 is double, but expects float
[[Node: model/pretrained/custom_layer_1/map/while/custom_image_op = PyFuncStateless[Tin=[DT_FLOAT], Tout=[DT_FLOAT], token="pyfunc_0", _device="/job:localhost/replica:0/task:0/device:CPU:0"](model/pretrained/custom_layer_1/map/while/TensorArrayReadV3/_49)]]
[[Node: model/pretrained/custom_layer_1/map/while/TensorArrayWrite/TensorArrayWriteV3/_55 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_126_model/pretrained/custom_layer_1/map/while/TensorArrayWrite/TensorArrayWriteV3", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](^_cloopmodel/pretrained/custom_layer_1/map/while/NextIteration_1/_20)]]
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "train.py", line 94, in <module>
trainer.run(sess)
File "/home//Downloads/dl_ss_19-master/Ex4/train/trainer.py", line 128, in run
self._train_epoch(sess)
File "/home/train/trainer.py", line 49, in _train_epoch
self._model_is_training: True})
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/client/session.py", line 889, in run
run_metadata_ptr)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/client/session.py", line 1120, in _run
feed_dict_tensor, options, run_metadata)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/client/session.py", line 1317, in _do_run
options, run_metadata)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/client/session.py", line 1336, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: 0-th value returned by pyfunc_0 is double, but expects float
[[Node: model/pretrained/custom_layer_1/map/while/custom_image_op = PyFuncStateless[Tin=[DT_FLOAT], Tout=[DT_FLOAT], token="pyfunc_0", _device="/job:localhost/replica:0/task:0/device:CPU:0"](model/pretrained/custom_layer_1/map/while/TensorArrayReadV3/_49)]]
[[Node: model/pretrained/custom_layer_1/map/while/TensorArrayWrite/TensorArrayWriteV3/_55 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_126_model/pretrained/custom_layer_1/map/while/TensorArrayWrite/TensorArrayWriteV3", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](^_cloopmodel/pretrained/custom_layer_1/map/while/NextIteration_1/_20)]]
Caused by op 'model/pretrained/custom_layer_1/map/while/custom_image_op', defined at:
File "train.py", line 61, in <module>
prediction_logits = model.pretrained(img_conc)
File "/home/model/pretrained.py", line 56, in create
x = CustomLayer(output_dim=(224, 224, 3))(inputs)
File "/local/tensorflow-1.4.0/python3.5/keras/engine/topology.py", line 603, in __call__
output = self.call(inputs, **kwargs)
File "/home/model/pretrained.py", line 41, in call
res = tf.map_fn(preprocess_input, x)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/ops/functional_ops.py", line 389, in map_fn
swap_memory=swap_memory)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/ops/control_flow_ops.py", line 2816, in while_loop
result = loop_context.BuildLoop(cond, body, loop_vars, shape_invariants)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/ops/control_flow_ops.py", line 2640, in BuildLoop
pred, body, original_loop_vars, loop_vars, shape_invariants)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/ops/control_flow_ops.py", line 2590, in _BuildLoop
body_result = body(*packed_vars_for_body)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/ops/functional_ops.py", line 379, in compute
packed_fn_values = fn(packed_values)
File "/home/model/pretrained.py", line 30, in preprocess_input
name='custom_image_op')
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/ops/script_ops.py", line 215, in py_func
input=inp, token=token, Tout=Tout, name=name)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/ops/gen_script_ops.py", line 90, in _py_func_stateless
"PyFuncStateless", input=input, token=token, Tout=Tout, name=name)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/framework/ops.py", line 2956, in create_op
op_def=op_def)
File "/local/tensorflow-1.4.0/python3.5/tensorflow/python/framework/ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access
InvalidArgumentError (see above for traceback): 0-th value returned by pyfunc_0 is double, but expects float
[[Node: model/pretrained/custom_layer_1/map/while/custom_image_op = PyFuncStateless[Tin=[DT_FLOAT], Tout=[DT_FLOAT], token="pyfunc_0", _device="/job:localhost/replica:0/task:0/device:CPU:0"](model/pretrained/custom_layer_1/map/while/TensorArrayReadV3/_49)]]
[[Node: model/pretrained/custom_layer_1/map/while/TensorArrayWrite/TensorArrayWriteV3/_55 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device_incarnation=1, tensor_name="edge_126_model/pretrained/custom_layer_1/map/while/TensorArrayWrite/TensorArrayWriteV3", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](^_cloopmodel/pretrained/custom_layer_1/map/while/NextIteration_1/_20)]]
Thus far I tried to cast the various parameters and change the arguments of the function. This however didn't influence the result at all. I am at a loss. Does someone have experience with pyfunc_0? If yes, how can I fix below's code?
Links I found useful:
https://github.com/cesc-park/attend2u/issues/2
https://github.com/rstudio/tensorflow/issues/169
"0-th value returned by pyfunc_0 is double, but expects float" though I think it returns float
Including advanced computation (scikit-like) in a keras custom layer
def equalize(img):
img_adapteq = 255 * exposure.equalize_adapthist(img // 255)
return img_adapteq.astype(np.float)
def preprocess_input(img):
x = tf.py_func(equalize,
[img],
'float32',
stateful=False,
name='custom_image_op')
return tf.cast(x, tf.float32)
class CustomLayer(Layer):
def __init__(self, output_dim, **kwargs):
self.output_dim = output_dim
self.trainable = False
super(CustomLayer, self).__init__(**kwargs)
def call(self, x):
res = tf.map_fn(preprocess_input, x)
res.set_shape([x.shape[0],
self.output_dim[1],
self.output_dim[0],
x.shape[-1]])
return res
inputs = Input(shape=(224, 224, 3), tensor=x)
x = CustomLayer(output_dim=(224, 224, 3))(inputs)
来源:https://stackoverflow.com/questions/57628958/return-value-of-pyfunc-0-is-double-but-expects-float