[tflchef/rev] support quantization (#2402)
author박세희/동작제어Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Tue, 27 Nov 2018 06:00:18 +0000 (15:00 +0900)
committer박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 27 Nov 2018 06:00:18 +0000 (15:00 +0900)
* [tflchef/rev] support quantization

This will enable reading quantization from tflite and store to tflchef

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
* remove space

* add note

* fix typo

* remove invalid '> 0'

contrib/tflchef/tflite/src/RecipeChef.cpp

index 836c12e..0a26ae0 100644 (file)
@@ -94,8 +94,6 @@ std::unique_ptr<ModelRecipe> generate_recipe(const tflite::Model *model)
   for (uint32_t i = 0; i < tensors->Length(); ++i)
   {
     auto tensor = tensors->Get(i);
-    // TODO support quantization
-    assert(tensor->quantization() == nullptr);
 
     // check buffer
     if (tensor->buffer() >= buffers->size())
@@ -134,6 +132,37 @@ std::unique_ptr<ModelRecipe> generate_recipe(const tflite::Model *model)
         filler->add_arg(ss.str());
       }
     }
+
+    auto quant = tensor->quantization();
+    if (quant != nullptr)
+    {
+      // Note: Calling 'operand->mutable_quant()' will create empty 'quant' node
+      // in the recipe file. We want this only when valid parameter exist.
+      if (quant->min() != nullptr && quant->min()->size() > 0)
+      {
+        tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
+        for (uint32_t idx = 0; idx < quant->min()->size(); ++idx)
+          chef_quant->add_min(quant->min()->Get(idx));
+      }
+      if (quant->max() != nullptr && quant->max()->size() > 0)
+      {
+        tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
+        for (uint32_t idx = 0; idx < quant->max()->size(); idx++)
+          chef_quant->add_max(quant->max()->Get(idx));
+      }
+      if (quant->scale() != nullptr && quant->scale()->size() > 0)
+      {
+        tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
+        for (uint32_t idx = 0; idx < quant->scale()->size(); ++idx)
+          chef_quant->add_scale(quant->scale()->Get(idx));
+      }
+      if (quant->zero_point() != nullptr && quant->zero_point()->size() > 0)
+      {
+        tflchef::TensorQuantization *chef_quant = operand->mutable_quant();
+        for (uint32_t idx = 0; idx < quant->zero_point()->size(); ++idx)
+          chef_quant->add_zero_point(quant->zero_point()->Get(idx));
+      }
+    }
   }
 
   // add all operators