std.constant'op требует, чтобы тип атрибута соответствовал типу возврата op
Я пытаюсь преобразовать модель keras, которую я обучил и точно настроил с помощью учебного пособия по квантованию на их официальном сайте, в модель intflite. Я могу следовать их шагам, пока мне не придется преобразовать модель в формат tflite. Затем он дает мне такой результат:
`Traceback (most recent call last):
File "/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/lite/python/convert.py", line 185, in toco_convert_protos
enable_mlir_converter)
File "/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/lite/python/wrap_toco.py", line 38, in wrapped_toco_convert
enable_mlir_converter)
Exception: /home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/layers/ops/core.py:56:1: error: 'std.constant' op requires attribute's type ('tensor<48x64xf32>') to match op's return type ('tensor<*xf32>')
outputs = standard_ops.tensordot(inputs, kernel, [[rank - 1], [0]])
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py:1194:1: note: called from
dtype=self._compute_dtype_object)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize_wrapper.py:162:1: note: called from
outputs = self.layer.call(inputs)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/autograph/impl/api.py:302:1: note: called from
return func(*args, **kwargs)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py:961:1: note: called from
outputs = call_fn(inputs, *args, **kwargs)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/engine/functional.py:507:1: note: called from
outputs = node.layer(*args, **kwargs)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/engine/functional.py:385:1: note: called from
inputs, training=training, mask=mask)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py:961:1: note: called from
outputs = call_fn(inputs, *args, **kwargs)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/saving/saving_utils.py:132:1: note: called from
outputs = model(inputs, training=False)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py:600:1: note: called from
return weak_wrapped_fn().__wrapped__(*args, **kwds)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/layers/ops/core.py:56:1: note: see current operation: %cst_8 = "std.constant"() {value = dense<"0x38211AB .. A3E"> : tensor<48x64xf32>} : () -> tensor<*xf32>
outputs = standard_ops.tensordot(inputs, kernel, [[rank - 1], [0]])
^
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/student1/kvantizacija/tensorflow_example.py", line 58, in <module>
tflite_model_quant = converter.convert()
File "/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/lite/python/lite.py", line 778, in convert
self).convert(graph_def, input_tensors, output_tensors)
File "/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/lite/python/lite.py", line 595, in convert
**converter_kwargs)
File "/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/lite/python/convert.py", line 560, in toco_convert_impl
enable_mlir_converter=enable_mlir_converter)
File "/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/lite/python/convert.py", line 188, in toco_convert_protos
raise ConverterError(str(e))
tensorflow.lite.python.convert.ConverterError: /home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/layers/ops/core.py:56:1: error: 'std.constant' op requires attribute's type ('tensor<48x64xf32>') to match op's return type ('tensor<*xf32>')
outputs = standard_ops.tensordot(inputs, kernel, [[rank - 1], [0]])
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py:1194:1: note: called from
dtype=self._compute_dtype_object)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow_model_optimization/python/core/quantization/keras/quantize_wrapper.py:162:1: note: called from
outputs = self.layer.call(inputs)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/autograph/impl/api.py:302:1: note: called from
return func(*args, **kwargs)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py:961:1: note: called from
outputs = call_fn(inputs, *args, **kwargs)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/engine/functional.py:507:1: note: called from
outputs = node.layer(*args, **kwargs)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/engine/functional.py:385:1: note: called from
inputs, training=training, mask=mask)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py:961:1: note: called from
outputs = call_fn(inputs, *args, **kwargs)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/saving/saving_utils.py:132:1: note: called from
outputs = model(inputs, training=False)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/eager/def_function.py:600:1: note: called from
return weak_wrapped_fn().__wrapped__(*args, **kwds)
^
/home/student1/venv_kvant/lib/python3.6/site-packages/tensorflow/python/keras/layers/ops/core.py:56:1: note: see current operation: %cst_8 = "std.constant"() {value = dense<"0x38211ABEE ... 6D3DE88D49BE40211A3E"> : tensor<48x64xf32>} : () -> tensor<*xf32>
outputs = standard_ops.tensordot(inputs, kernel, [[rank - 1], [0]])
^
Process finished with exit code 1
Если я удалю флаг для оптимизации, это даст мне модель tflite, но не даст мне нужную мне модель int8. Я могу успешно провести квантование поезда той же модели, которую я точно настраиваю с помощью обучения с квантованием, но по какой-то причине, когда я оборачиваю модель в оболочки Quantize и пытаюсь ее преобразовать, она не работает. Я использую последнюю ночную версию и пробовал запускать скрипт как с доступом к графическому процессору, так и без него. `
Если вам нужна дополнительная информация, не стесняйтесь спрашивать. Еще одна модель - это 4 блока CNN + Max_pool с несколькими слоями Dense в конце. При необходимости могу предоставить визуализацию модели.
PS. Вот резюме:
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input (InputLayer) [(None, 48, 48, 3)] 0
__________________________________________________________________________________________________
quantize_layer (QuantizeLayer) (None, 48, 48, 3) 3 input[0][0]
__________________________________________________________________________________________________
quant_conv_1 (QuantizeWrapper) (None, 46, 46, 16) 483 quantize_layer[0][0]
__________________________________________________________________________________________________
quant_relu_1 (QuantizeWrapper) (None, 46, 46, 16) 3 quant_conv_1[0][0]
__________________________________________________________________________________________________
quant_pool_1 (QuantizeWrapper) (None, 22, 22, 16) 1 quant_relu_1[0][0]
__________________________________________________________________________________________________
quant_conv_2 (QuantizeWrapper) (None, 20, 20, 32) 4707 quant_pool_1[0][0]
__________________________________________________________________________________________________
quant_relu_2 (QuantizeWrapper) (None, 20, 20, 32) 3 quant_conv_2[0][0]
__________________________________________________________________________________________________
quant_pool_2 (QuantizeWrapper) (None, 9, 9, 32) 1 quant_relu_2[0][0]
__________________________________________________________________________________________________
quant_conv_3 (QuantizeWrapper) (None, 7, 7, 32) 9315 quant_pool_2[0][0]
__________________________________________________________________________________________________
quant_relu_3 (QuantizeWrapper) (None, 7, 7, 32) 3 quant_conv_3[0][0]
__________________________________________________________________________________________________
quant_pool_3 (QuantizeWrapper) (None, 3, 3, 32) 1 quant_relu_3[0][0]
__________________________________________________________________________________________________
quant_conv_4 (QuantizeWrapper) (None, 2, 2, 64) 8387 quant_pool_3[0][0]
__________________________________________________________________________________________________
quant_pool_4 (QuantizeWrapper) (None, 1, 1, 64) 1 quant_conv_4[0][0]
__________________________________________________________________________________________________
quant_relu_4 (QuantizeWrapper) (None, 1, 1, 64) 3 quant_pool_4[0][0]
__________________________________________________________________________________________________
quant_fc_yaw (QuantizeWrapper) (None, 1, 1, 48) 3125 quant_relu_4[0][0]
__________________________________________________________________________________________________
quant_fc_pitch (QuantizeWrapper (None, 1, 1, 48) 3125 quant_relu_4[0][0]
__________________________________________________________________________________________________
quant_fc_roll (QuantizeWrapper) (None, 1, 1, 48) 3125 quant_relu_4[0][0]
__________________________________________________________________________________________________
quant_relu_yaw (QuantizeWrapper (None, 1, 1, 48) 3 quant_fc_yaw[0][0]
__________________________________________________________________________________________________
quant_relu_pitch (QuantizeWrapp (None, 1, 1, 48) 3 quant_fc_pitch[0][0]
__________________________________________________________________________________________________
quant_relu_roll (QuantizeWrappe (None, 1, 1, 48) 3 quant_fc_roll[0][0]
__________________________________________________________________________________________________
quant_flatten_yaw (QuantizeWrap (None, 48) 1 quant_relu_yaw[0][0]
__________________________________________________________________________________________________
quant_flatten_pitch (QuantizeWr (None, 48) 1 quant_relu_pitch[0][0]
__________________________________________________________________________________________________
quant_flatten_roll (QuantizeWra (None, 48) 1 quant_relu_roll[0][0]
__________________________________________________________________________________________________
quant_output_yaw (QuantizeWrapp (None, 61) 2994 quant_flatten_yaw[0][0]
__________________________________________________________________________________________________
quant_output_pitch (QuantizeWra (None, 61) 2994 quant_flatten_pitch[0][0]
__________________________________________________________________________________________________
quant_output_roll (QuantizeWrap (None, 61) 2994 quant_flatten_roll[0][0]
__________________________________________________________________________________________________
quant_yaw (QuantizeWrapper) (None, 1) 54 quant_flatten_yaw[0][0]
__________________________________________________________________________________________________
quant_pitch (QuantizeWrapper) (None, 1) 54 quant_flatten_pitch[0][0]
__________________________________________________________________________________________________
quant_roll (QuantizeWrapper) (None, 1) 54 quant_flatten_roll[0][0]
==================================================================================================
Total params: 41,442
Trainable params: 41,066
Non-trainable params: 376
1 ответ
Приветствую, это закрыто в связи с поиском другого способа решения проблемы. Проблема заключалась в том, что была последовательность слоев: -> Плотный -> Плоский -> Плотный, и из-за этого возникала эта ошибка. На данный момент решение, которое я использую, состоит в том, чтобы поменять местами слой Flatten и 1st Dense. Если кто-нибудь знает, как решить проблему с использованием исходной последовательности, дайте мне знать.