TFLite转换不支持的操作:CropAndResize

时间:2018-12-06 07:42:24

标签: python-3.x tensorflow-lite

我尝试通过TFLite将我的模型(pb)转换为lite。这是我的代码:

import tensorflow as tf

graph_def_file = "./graph.pb"
input_arrays = ['image', 'sp', 'Hsp_boxes', 'O_boxes']

output_arrays = ["classification/op_store"]

converter = tf.lite.TFLiteConverter.from_frozen_graph(
  graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

这是我在操作过程中打印的终端:

ConverterError: TOCO failed. See console for info.
2018-12-06 07:18:35.310490: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: CropAndResize
2018-12-06 07:18:35.322497: I tensorflow/lite/toco/import_tensorflow.cc:1324] Converting unsupported operation: CropAndResize
2018-12-06 07:18:35.478205: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before Removing unused ops: 1104 operators, 1685 arrays (0 quantized)
2018-12-06 07:18:35.505520: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before general graph transformations: 1104 operators, 1685 arrays (0 quantized)
2018-12-06 07:18:35.820303: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] After general graph transformations pass 1: 293 operators, 583 arrays (0 quantized)
2018-12-06 07:18:35.828260: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] After general graph transformations pass 2: 287 operators, 577 arrays (0 quantized)
2018-12-06 07:18:35.835681: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] After general graph transformations pass 3: 287 operators, 577 arrays (0 quantized)
2018-12-06 07:18:35.842342: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before dequantization graph transformations: 287 operators, 577 arrays (0 quantized)
2018-12-06 07:18:35.852020: I tensorflow/lite/toco/allocate_transient_arrays.cc:345] Total transient array allocated size: 250675200 bytes, theoretical optimal value: 176947200 bytes.
2018-12-06 07:18:35.853832: E tensorflow/lite/toco/toco_tooling.cc:421] We are continually in the process of adding support to TensorFlow Lite for more ops. It would be helpful if you could inform us of how this conversion went by opening a github issue at https://github.com/tensorflow/tensorflow/issues/new?template=40-tflite-op-request.md
 and pasting the following:

Some of the operators in the model are not supported by the standard TensorFlow Lite runtime. If those are native TensorFlow operators, you might be able to use the extended runtime by passing --enable_select_tf_ops, or by setting target_ops=TFLITE_BUILTINS,SELECT_TF_OPS when calling tf.lite.TFLiteConverter(). Otherwise, if you have a custom implementation for them you can disable this error with --allow_custom_ops, or by setting allow_custom_ops=True when calling tf.lite.TFLiteConverter(). Here is a list of builtin operators you are using: ADD, CAST, CONCATENATION, CONV_2D, DIV, FLOOR, FULLY_CONNECTED, LOGISTIC, MAX_POOL_2D, MEAN, MUL, PACK, PAD, RESHAPE, SHAPE, SLICE, SOFTMAX, STRIDED_SLICE, TRANSPOSE. Here is a list of operators for which you will need custom implementations: CropAndResize, RandomUniform.
/opt/conda/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Traceback (most recent call last):
  File "/opt/conda/bin/toco_from_protos", line 11, in <module>
    sys.exit(main())
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/lite/toco/python/toco_from_protos.py", line 59, in main
    app.run(main=execute, argv=[sys.argv[0]] + unparsed)
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/lite/toco/python/toco_from_protos.py", line 33, in execute
    output_str = tensorflow_wrap_toco.TocoConvert(model_str, toco_str, input_str)
Exception: We are continually in the process of adding support to TensorFlow Lite for more ops. It would be helpful if you could inform us of how this conversion went by opening a github issue at https://github.com/tensorflow/tensorflow/issues/new?template=40-tflite-op-request.md
 and pasting the following:

Some of the operators in the model are not supported by the standard TensorFlow Lite runtime. If those are native TensorFlow operators, you might be able to use the extended runtime by passing --enable_select_tf_ops, or by setting target_ops=TFLITE_BUILTINS,SELECT_TF_OPS when calling tf.lite.TFLiteConverter(). Otherwise, if you have a custom implementation for them you can disable this error with --allow_custom_ops, or by setting allow_custom_ops=True when calling tf.lite.TFLiteConverter(). Here is a list of builtin operators you are using: ADD, CAST, CONCATENATION, CONV_2D, DIV, FLOOR, FULLY_CONNECTED, LOGISTIC, MAX_POOL_2D, MEAN, MUL, PACK, PAD, RESHAPE, SHAPE, SLICE, SOFTMAX, STRIDED_SLICE, TRANSPOSE. Here is a list of operators for which you will need custom implementations: CropAndResize, RandomUniform.

我发现未分配的操作CropAndResize导致TOCO失败。我的模型的一部分包括:

def crop_pool_layer(self, bottom, rois, name):
    with tf.variable_scope(name) as scope:

    batch_ids    = tf.squeeze(tf.slice(rois, [0, 0], [-1, 1], name="batch_id"), [1])
    bottom_shape = tf.shape(bottom)
    height       = (tf.to_float(bottom_shape[1]) - 1.) * np.float32(self.stride[0])
    width        = (tf.to_float(bottom_shape[2]) - 1.) * np.float32(self.stride[0])
    x1 = tf.slice(rois, [0, 1], [-1, 1], name="x1") / width
    y1 = tf.slice(rois, [0, 2], [-1, 1], name="y1") / height
    x2 = tf.slice(rois, [0, 3], [-1, 1], name="x2") / width
    y2 = tf.slice(rois, [0, 4], [-1, 1], name="y2") / height

    bboxes        = tf.stop_gradient(tf.concat([y1, x1, y2, x2], axis=1))
    if cfg.RESNET.MAX_POOL:
        pre_pool_size = cfg.POOLING_SIZE * 2
        crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [pre_pool_size, pre_pool_size], name="crops")
        crops = slim.max_pool2d(crops, [2, 2], padding='SAME')
    else:
        crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [cfg.POOLING_SIZE, cfg.POOLING_SIZE], name="crops")
return crops

然后我尝试按照custom_operators

的说明进行操作

但是不知道自定义操作符并实现。

1 个答案:

答案 0 :(得分:0)

tflite支持将CropAndResize分为两个操作,一个Slice操作和一个Resize操作。

要调整大小,请使用: https://www.tensorflow.org/api_docs/python/tf/image/resize