code: def quantize_buffer(self):
      # Quantize buffer by calibrator
      quantization_param = self.calibrator.run()
      for buffer_name in quantization_param:
          for tensor in self.models["subgraphs"][0]["tensors"]:
              if tensor["name"] == buffer_name:
                  tensor["quantization"] = {
                      "min": [quantization_param[buffer_name]["min"]],
                      "max": [quantization_param[buffer_name]["max"]],
                      "scale": [
                          quantization_param[buffer_name]["scale"]
                      ],
                      "zero_point": [
                          int(
                              quantization_param[buffer_name][
                                  "zero_point"
                              ]
                          )
                      ],
                  }
                  tensor["type"] = "INT8"
      for op in self.models["subgraphs"][0]["operators"]:
          opcode_index = op.get("opcode_index", 0)
          op_name = self.models["operator_codes"][opcode_index][
              "builtin_code"
          ]
          outputs = op["outputs"]
          requirements = QUANTIZATIONREQUIREMENTS[op_name]
          for output_idx in range(len(outputs)):
              outputs_idx = "Output_{}".format(output_idx)
              requirement = requirements.get(outputs_idx)
              if not requirement:
                  continue
              if (
                  requirement["restriction"]
                  == Restriction.SAME_WITH_INPUT
              ):
                  input_tensor_idx = op["inputs"][0]
                  input_tensor = self.get_tensor_by_index(
                      input_tensor_idx
                  )
                  output_tensor_idx = outputs[output_idx]
                  output_tensor = self.get_tensor_by_index(
                      output_tensor_idx
                  )
                  output_tensor["quantization"] = input_tensor[
                      "quantization"
                  ]
              if requirement["restriction"] == Restriction.SOFTMAX:
                  scale = 1.0 / 256.0
                  zero_point = -128
                  output_tensor_idx = outputs[output_idx]
                  output_tensor = self.get_tensor_by_index(
                      output_tensor_idx
                  )
                  output_tensor["quantization"]["scale"] = [scale]
                  output_tensor["quantization"]["zero_point"] = [
                      zero_point
                  ]
              if requirement["restriction"] == Restriction.LOGISTIC:
                  scale = 1.0 / 256.0
                  zero_point = 0
                  output_tensor_idx = outputs[output_idx]
                  output_tensor = self.get_tensor_by_index(
                      output_tensor_idx
                  )
                  output_tensor["quantization"]["scale"] = [scale]
                  output_tensor["quantization"]["zero_point"] = [
                      zero_point
                  ]
              if requirement["restriction"] == Restriction.CONCAT:
                  self.models["operator_codes"].append(
                      {"builtin_code": "QUANTIZE", "version": 2}
                  )
                  inputs = op["inputs"]
                  min_of_input = 9e25
                  max_of_input = -9e25
                  for input_idx in inputs:
                      input_tensor = self.get_tensor_by_index(input_idx)
                      min_of_input = min(
                          min_of_input,
                          input_tensor["quantization"]["min"][0],
                      )
                      max_of_input = max(
                          max_of_input,
                          input_tensor["quantization"]["max"][0],
                      )
                  scale = (max_of_input - min_of_input) / 255
                  zero_point = np.round(127 - max_of_input / scale)
  
                  for input_idx in inputs:
                      input_tensor = self.get_tensor_by_index(input_idx)
                      buffer_data = {}
                      self.models["buffers"].append(buffer_data)
                      tensor_info = {
                          "shape": input_tensor["shape"],
                          "type": "INT8",
                          "buffer": len(self.models["buffers"]) - 1,
                          "name": input_tensor["name"] + "_requantized",
                          "quantization": {
                              "min": [
                                  np.around(min_of_input, 6).tolist()
                              ],
                              "max": [
                                  np.around(max_of_input, 6).tolist()
                              ],
                              "scale": [np.around(scale, 6).tolist()],
                              "zero_point": [int(zero_point.tolist())],
                          },
                      }
                      self.models["subgraphs"][0]["tensors"].append(
                          tensor_info
                      )
                      quantize_op = {
                          "opcode_index": len(
                              self.models["operator_codes"]
                          )
                          - 1,
                          "inputs": [input_idx],
                          "outputs": [
                              len(
                                  self.models["subgraphs"][0]["tensors"]
                              )
                              - 1
                          ],
                      }
                      for operator in self.models["subgraphs"][0][
                          "operators"
                      ]:
                          inps = operator["inputs"]
                          new_inps = []
                          for inp in inps:
                              if inp == input_idx:
                                  new_inps.append(
                                      quantize_op["outputs"][0]
                                  )
                              else:
                                  new_inps.append(inp)
                          operator["inputs"] = new_inps
                      self.models["subgraphs"][0]["operators"].append(
                          quantize_op
                      )
                  input_tensor_idx = op["inputs"][0]
                  input_tensor = self.get_tensor_by_index(
                      input_tensor_idx
                  )
                  output_tensor_idx = outputs[output_idx]
                  output_tensor = self.get_tensor_by_index(
                      output_tensor_idx
                  )
                  output_tensor["quantization"] = input_tensor[
                      "quantization"
                  ]
  
imports: import numpy
  import tensorflow
  import tqdm
label_desc: Use image node_id 7 for calling the QuantizationConverter obj's underlying member method code with example usage: obj.quantize_buffer() without return types