将张量流转换为统一后端的问题(梭子鱼)

时间:2019-04-15 13:49:31

标签: unity3d tensorflow machine-learning barracuda

我正在尝试将用Keras创建的UNet模型转换为.nn,以用于unity的神经网络后端。但是我收到此错误。对于我的模型导出,我导出了一个“ .h5”,然后将其转换为二进制文件“ .pb”,后来我使用了tensorflow_to_barracuda.py。可能有人团结一致地制定了有效的细分计划吗?

Converting unet_person.bytes to unet_person.nn
IGNORED: PlaceholderWithDefault unknown layer
IGNORED: Switch unknown layer
IGNORED: Switch unknown layer
IGNORED: Shape unknown layer
IGNORED: Switch unknown layer
IGNORED: Merge unknown layer
IGNORED: Shape unknown layer
IGNORED: Shape unknown layer
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-22-d09d8c6d2c1a> in <module>
      1 from mlagents.trainers import tensorflow_to_barracuda as tb
      2 
----> 3 tb.convert('unet_person.bytes', 'unet_person.nn')

/anaconda3/lib/python3.6/site-packages/mlagents/trainers/tensorflow_to_barracuda.py in convert(source_file, target_file, trim_unused_by_output, verbose, compress_f16)
938     o_model = barracuda.Model()
939     o_model.layers, o_input_shapes, o_model.tensors, o_model.memories = \
--> 940         process_model(i_model, args)
941 
942     # Cleanup unconnected Identities (they might linger after processing complex node patterns like LSTM)

/anaconda3/lib/python3.6/site-packages/mlagents/trainers/tensorflow_to_barracuda.py in process_model(model, args)
870                 nodes = nodes_as_array[node_index:pattern_end]
871                 name = nodes[-1].name
--> 872                 var_tensors, const_tensors = get_tensors(nodes)
873                 if args.print_patterns or args.verbose:
874                     print('PATTERN:', name, '~~', pattern_name, pattern, '<-', var_tensors, '+', [t.name for t in const_tensors])

/anaconda3/lib/python3.6/site-packages/mlagents/trainers/tensorflow_to_barracuda.py in get_tensors(pattern_nodes)
845                 tensor_nodes = [n for n in pattern_nodes if n.op == 'Const']
846                 tensors = [Struct(name = n.name, obj = n.attr["value"].tensor, shape = get_tensor_dims(n.attr["value"].tensor), data = get_tensor_data(n.attr["value"].tensor))
--> 847                     for n in tensor_nodes]
848 
849                 # TODO: unify / reuse code from process_layer

/anaconda3/lib/python3.6/site-packages/mlagents/trainers/tensorflow_to_barracuda.py in <listcomp>(.0)
845                 tensor_nodes = [n for n in pattern_nodes if n.op == 'Const']
846                 tensors = [Struct(name = n.name, obj = n.attr["value"].tensor, shape = get_tensor_dims(n.attr["value"].tensor), data = get_tensor_data(n.attr["value"].tensor))
--> 847                     for n in tensor_nodes]
848 
849                 # TODO: unify / reuse code from process_layer

/anaconda3/lib/python3.6/site-packages/mlagents/trainers/tensorflow_to_barracuda.py in get_tensor_data(tensor)
492     if tensor.bool_val:
493         data = np.array(tensor.bool_val, dtype=float)
--> 494     return np.array(data).reshape(dims)
495 
496 def flatten(items,enter=lambda x:isinstance(x, list)):

UnboundLocalError: local variable 'data' referenced before assignment

2 个答案:

答案 0 :(得分:1)

在梭子鱼1.0中,有一种使用Keras2ONNX pip软件包将Keras(.h5)模型转换为ONNX模型的方法。

您安装keras2ONNX,然后运行

import keras2onnx
onnx_model = keras2onnx.convert_keras(unet, name='unet')
keras2onnx.save_model(onnx_model, "unet.onnx")

请注意,您需要以下标志:channel_first_inputs = [unet.layers [0] .layers [0]]

onnx_model = keras2onnx.convert_keras(unet, name='unet')

由于梭子鱼输入首先是通道输入,这意味着对于batch_size x width x height x rgb图像,顺序为rgb x width x height x batch_size。

答案 1 :(得分:0)

我发现该框架的开发程度还不够高。 对我有用的是为所有平台编译Tensorflow Lite源并使用该后端。转换为Tensorflow Lite仍然有些棘手,因为仅支持某些图层。最后,您需要将C二进制文件包装在C#中,这里已经为您完成了部分工作:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/experimental/examples/unity

编译相对容易。