code: def _convert_padding(source_node, IR_node):
      source_node_layer = source_node.layer
  
      if source_node_layer.HasField("convolution"):
          # padding in conv
  
          source_node_conv = source_node_layer.convolution
  
          if source_node_conv.HasField("valid"):
              # pad in IR is [x1_b, x2_b, ..., x1_e, x2_e, ...]
  
              dim = []
              for (
                  i
              ) in source_node_conv.valid.paddingAmounts.borderAmounts:
                  dim.extend([i.startEdgeSize, i.endEdgeSize])
  
              if dim == []:
                  assign_IRnode_values(IR_node, {"auto_pad": "VALID"})
                  pad_dim = [0] * 8
                  pad_dim = convert_tf_pad_to_onnx(pad_dim)
  
                  assign_IRnode_values(IR_node, {"pads": pad_dim})
              else:
                  # padding
                  pad_dim = [0, 0]
  
                  pad_dim.extend(dim)
  
                  pad_dim += [0, 0]
  
                  pad_dim = convert_tf_pad_to_onnx(pad_dim)
  
                  assign_IRnode_values(IR_node, {"pads": pad_dim})
  
          elif source_node_conv.HasField("same"):
              # compute padding for 'same'
              assign_IRnode_values(IR_node, {"auto_pad": "SAME"})
  
              kernel = list(source_node_conv.kernelSize)
              dilation = list(source_node_conv.dilationFactor)
              if dilation == []:
                  dilation = [1, 1]
              stride = list(source_node_conv.stride)
              if stride == []:
                  stride = [1, 1]
  
              kernel[0] = dilation[0] * (kernel[0] - 1) + 1
              kernel[1] = dilation[1] * (kernel[1] - 1) + 1
  
              if stride == [1, 1]:
                  # https://discuss.mxnet.io/t/pooling-and-convolution-with-same-mode/528/3
  
                  p0 = (kernel[0] - 1) // 2
                  p1 = (kernel[1] - 1) // 2
  
                  if kernel[0] % 2 == 0:
                      p00 = p0
                      p01 = p0 + 1
                  else:
                      p00 = p0
                      p01 = p0
  
                  if kernel[1] % 2 == 0:
                      p10 = p1
                      p11 = p1 + 1
                  else:
                      p10 = p1
                      p11 = p1
  
                  pad_dim = [0, 0, p00, p01, p10, p11, 0, 0]
  
                  pad_dim = convert_tf_pad_to_onnx(pad_dim)
  
                  assign_IRnode_values(IR_node, {"pads": pad_dim})
              else:
                  # https://www.jianshu.com/p/05c4f1621c7e
                  pad_dim = [0, 0, 0, 0, 0, 0, 0, 0]
  
                  pad_dim = convert_tf_pad_to_onnx(pad_dim)
  
                  assign_IRnode_values(IR_node, {"pads": pad_dim})
  
          else:
              assert False
  
      elif source_node_layer.HasField("pooling"):
          # padding in pooling
          source_node_pool = source_node_layer.pooling
          if source_node_pool.HasField("valid"):
              dim = []
              for (
                  i
              ) in source_node_pool.valid.paddingAmounts.borderAmounts:
                  dim.extend([i.startEdgeSize, i.endEdgeSize])
  
              if dim == []:
                  assign_IRnode_values(IR_node, {"auto_pad": "VALID"})
                  pad_dim = [0] * 8
                  pad_dim = convert_tf_pad_to_onnx(pad_dim)
  
                  assign_IRnode_values(IR_node, {"pads": pad_dim})
              else:
                  # padding
                  pad_dim = [0, 0]
  
                  pad_dim.extend(dim)
  
                  pad_dim += [0, 0]
                  pad_dim = convert_tf_pad_to_onnx(pad_dim)
                  assign_IRnode_values(IR_node, {"pads": pad_dim})
  
          elif source_node_pool.HasField("same"):
              assign_IRnode_values(IR_node, {"auto_pad": "SAME"})
  
              kernel = list(source_node_pool.kernelSize)
              stride = list(source_node_pool.stride)
              if stride == []:
                  stride = [1, 1]
  
              if stride == [1, 1]:
                  # https://discuss.mxnet.io/t/pooling-and-convolution-with-same-mode/528/3
                  p0 = (kernel[0] - 1) // 2
                  p1 = (kernel[1] - 1) // 2
  
                  if kernel[0] % 2 == 0:
                      p00 = p0
                      p01 = p0 + 1
                  else:
                      p00 = p0
                      p01 = p0
  
                  if kernel[1] % 2 == 0:
                      p10 = p1
                      p11 = p1 + 1
                  else:
                      p10 = p1
                      p11 = p1
  
                  pad_dim = [0, 0, p00, p01, p10, p11, 0, 0]
  
                  pad_dim = convert_tf_pad_to_onnx(pad_dim)
  
                  assign_IRnode_values(IR_node, {"pads": pad_dim})
              else:
                  # TODO
                  pad_dim = [0, 0, 0, 0, 0, 0, 0, 0]
  
                  pad_dim = convert_tf_pad_to_onnx(pad_dim)
  
                  assign_IRnode_values(IR_node, {"pads": pad_dim})
  
          elif source_node_pool.HasField("includeLastPixel"):
              # symmetric padding
              h, w = source_node_pool.includeLastPixel.paddingAmounts
              assign_IRnode_values(
                  IR_node, {"pads": [0, h, h, 0, 0, w, w, 0]}
              )
          else:
              assert False
  
      else:
          assert False
  
imports: import os
  from six import string_types
  import numpy
  import math
  from coremltools.models.neural_network import NeuralNetworkBuilder
  from coremltools.models import datatypes
  from coremltools.models import MLModel
  from coremltools.models.utils import save_spec
  from coremltools.models._infer_shapes_nn_mlmodel import infer_shapes
  from coremltools.proto import Model_pb2, NeuralNetwork_pb2
  from mmdnn.conversion.coreml.coreml_graph import CoremlGraph
  import mmdnn.conversion.common.IR.graph_pb2
  from mmdnn.conversion.common.IR.graph_pb2 import NodeDef, GraphDef, DataType
  from mmdnn.conversion.common.DataStructure.parser import Parser
  from mmdnn.conversion.common.utils import *
label_desc: Use image node_id 10 for calling the CoremlParser obj's underlying member method code with example usage: obj._convert_padding(source_node, IR_node) without return types