parameters: (self, batch_size, input_format)
docstring: Remove all the pre-processing nodes in the ONNX graph and leave only the image normalization essentials.
          :param batch_size: The batch size to use for the ONNX graph.
          :param input_format: Input tensor format, either NCHW or NHWC.
docstring_summary: Remove all the pre-processing nodes in the ONNX graph and leave only the image normalization essentials.
          :param batch_size: The batch size to use for the ONNX graph.
          :param input_format: Input tensor format, either NCHW or NHWC.
docstring_tokens: Remove all the pre - processing nodes in the ONNX graph and leave only the image normalization essentials . : param batch_size : The batch size to use for the ONNX graph . : param input_format : Input tensor format either NCHW or NHWC .
function: def update_preprocessor(self, batch_size, input_format):
          """
          Remove all the pre-processing nodes in the ONNX graph and leave only the image normalization essentials.
          :param batch_size: The batch size to use for the ONNX graph.
          :param input_format: Input tensor format, either NCHW or NHWC.
          """
          # Update batch size.
          self.batch_size = batch_size
  
          # Set input tensor shape.
          assert input_format in ["NCHW", "NHWC"]
          input_shape = [None] * 4
          if input_format == "NHWC":
            input_shape = [self.batch_size, self.height, self.width, 3]
          if input_format == "NCHW":
            input_shape = [self.batch_size, 3, self.height, self.width]
          self.graph.inputs[0].shape = input_shape
          self.graph.inputs[0].dtype = np.float32
          self.graph.inputs[0].name = "input_tensor"
  
          self.sanitize()
          log.info("ONNX graph input shape: {} [NCHW format set]".format(self.graph.inputs[0].shape))
  
          # Find the initial nodes of the graph, whatever the input is first connected to, and disconnect them.
          for node in [node for node in self.graph.nodes if self.graph.inputs[0] in node.inputs]:
              node.inputs.clear()
  
          # Get input tensor.
          # Convert to NCHW format if needed.
          input_tensor = self.graph.inputs[0]
          if input_format == "NHWC":
              input_tensor = self.graph.transpose("preprocessor/transpose", input_tensor, [0, 3, 1, 2])
  
          # Mobilenets' and inception's backbones preprocessor.
          if 'mobilenet' in self.model or 'inception_resnet' in self.model:
              mul_const = np.expand_dims(np.asarray([2 / 255], dtype=np.float32), axis=(0, 2, 3))
              sub_const = np.expand_dims(np.asarray([1], dtype=np.float32), axis=(0, 2, 3))
              mul_out = self.graph.op_with_const("Mul", "preprocessor/scale", input_tensor, mul_const)
              sub_out = self.graph.op_with_const("Sub", "preprocessor/mean", mul_out, sub_const)
  
          # Resnet backbones' preprocessor.
          elif 'resnet' in self.model:
              sub_const = np.expand_dims(np.asarray([255 * 0.485, 255 * 0.456, 255 * 0.406], dtype=np.float32), axis=(0, 2, 3))
              sub_out = self.graph.op_with_const("Sub", "preprocessor/mean", input_tensor, sub_const)
          
          # Backbone is not supported.
          else:
              log.info("Given model's backbone is not supported, pre-processor algorithm can't be generated")
              sys.exit(1)
  
          # Find first Conv node and connect preprocessor directly to it.
          conv_node = self.graph.find_node_by_op("Conv")
          log.info("Found {} node '{}' as stem entry".format(conv_node.op, conv_node.name))
          conv_node.inputs[0] = sub_out[0]
  
          # Disconnect the last node in one of the preprocessing branches with first TensorListStack parent node.
          concat_node = self.graph.find_node_by_op("Concat")
          concat_node.outputs = []
  
          # Disconnect the last node in second preprocessing branch with parent second TensorListStack node.
          tile_node = self.graph.find_node_by_op("Tile")
          tile_node.outputs = []
  
          # Reshape nodes tend to update the batch dimension to a fixed value of 1, they should use the batch size instead.
          for node in [node for node in self.graph.nodes if node.op == "Reshape"]:
              if type(node.inputs[1]) == gs.Constant and node.inputs[1].values[0] == 1:
                  node.inputs[1].values[0] = self.batch_size
  
          self.sanitize()
function_tokens: def update_preprocessor ( self , batch_size , input_format ) : # Update batch size. self . batch_size = batch_size # Set input tensor shape. assert input_format in [ "NCHW" , "NHWC" ] input_shape = [ None ] * 4 if input_format == "NHWC" : input_shape = [ self . batch_size , self . height , self . width , 3 ] if input_format == "NCHW" : input_shape = [ self . batch_size , 3 , self . height , self . width ] self . graph . inputs [ 0 ] . shape = input_shape self . graph . inputs [ 0 ] . dtype = np . float32 self . graph . inputs [ 0 ] . name = "input_tensor" self . sanitize ( ) log . info ( "ONNX graph input shape: {} [NCHW format set]" . format ( self . graph . inputs [ 0 ] . shape ) ) # Find the initial nodes of the graph, whatever the input is first connected to, and disconnect them. for node in [ node for node in self . graph . nodes if self . graph . inputs [ 0 ] in node . inputs ] : node . inputs . clear ( ) # Get input tensor. # Convert to NCHW format if needed. input_tensor = self . graph . inputs [ 0 ] if input_format == "NHWC" : input_tensor = self . graph . transpose ( "preprocessor/transpose" , input_tensor , [ 0 , 3 , 1 , 2 ] ) # Mobilenets' and inception's backbones preprocessor. if 'mobilenet' in self . model or 'inception_resnet' in self . model : mul_const = np . expand_dims ( np . asarray ( [ 2 / 255 ] , dtype = np . float32 ) , axis = ( 0 , 2 , 3 ) ) sub_const = np . expand_dims ( np . asarray ( [ 1 ] , dtype = np . float32 ) , axis = ( 0 , 2 , 3 ) ) mul_out = self . graph . op_with_const ( "Mul" , "preprocessor/scale" , input_tensor , mul_const ) sub_out = self . graph . op_with_const ( "Sub" , "preprocessor/mean" , mul_out , sub_const ) # Resnet backbones' preprocessor. elif 'resnet' in self . model : sub_const = np . expand_dims ( np . asarray ( [ 255 * 0.485 , 255 * 0.456 , 255 * 0.406 ] , dtype = np . float32 ) , axis = ( 0 , 2 , 3 ) ) sub_out = self . graph . op_with_const ( "Sub" , "preprocessor/mean" , input_tensor , sub_const ) # Backbone is not supported. else : log . info ( "Given model's backbone is not supported, pre-processor algorithm can't be generated" ) sys . exit ( 1 ) # Find first Conv node and connect preprocessor directly to it. conv_node = self . graph . find_node_by_op ( "Conv" ) log . info ( "Found {} node '{}' as stem entry" . format ( conv_node . op , conv_node . name ) ) conv_node . inputs [ 0 ] = sub_out [ 0 ] # Disconnect the last node in one of the preprocessing branches with first TensorListStack parent node. concat_node = self . graph . find_node_by_op ( "Concat" ) concat_node . outputs = [ ] # Disconnect the last node in second preprocessing branch with parent second TensorListStack node. tile_node = self . graph . find_node_by_op ( "Tile" ) tile_node . outputs = [ ] # Reshape nodes tend to update the batch dimension to a fixed value of 1, they should use the batch size instead. for node in [ node for node in self . graph . nodes if node . op == "Reshape" ] : if type ( node . inputs [ 1 ] ) == gs . Constant and node . inputs [ 1 ] . values [ 0 ] == 1 : node . inputs [ 1 ] . values [ 0 ] = self . batch_size self . sanitize ( )