예제 #1
0
def optimize(model: onnx.ModelProto, skip_fuse_bn: bool,
             skipped_optimizers: Optional[Sequence[str]]) -> onnx.ModelProto:
    """
    :param model: The onnx model.
    :return: The optimized onnx model.
    Before simplifying, use this method to generate value_info, which is used in `forward_all`
    After simplifying, use this method to fold constants generated in previous step into initializer,
    and eliminate unused constants.
    """

    onnx.checker.check_model(model)
    onnx.helper.strip_doc_string(model)
    optimizers_list = onnxoptimizer.get_fuse_and_elimination_passes()
    if not skip_fuse_bn:
        optimizers_list.append('fuse_bn_into_conv')
    if skipped_optimizers is not None:
        for opt in skipped_optimizers:
            try:
                optimizers_list.remove(opt)
            except ValueError:
                pass

    model = onnxoptimizer.optimize(model, optimizers_list, fixed_point=True)
    onnx.checker.check_model(model)
    return model
예제 #2
0
    def optimize(self, optimizations: List[str] = None, fixed_point: bool = False):
        """
        Use ONNX optimizer to optimize the ONNX model. The optimizations supported can be seen by calling
        'onnxoptimizer.get_available_passes()'

        :param optimizations: List of possible optimizations. If None, all of the optimizations will be used. Defaulted
                              to None.
        :param fixed_point:   Optimize the weights using fixed point. Defaulted to False.
        """
        # Set the ONNX optimizations list:
        onnx_optimizations = onnxoptimizer.get_fuse_and_elimination_passes()
        if optimizations is None:
            # Set to all optimizations:
            optimizations = onnx_optimizations

        # Optimize the model:
        self._model = onnxoptimizer.optimize(
            self._model, passes=optimizations, fixed_point=fixed_point
        )
예제 #3
0
def optimize(model: onnx.ModelProto, skip_fuse_bn: bool,
             skipped_optimizers: Optional[Sequence[str]]) -> onnx.ModelProto:
    """
    :model参数: 待优化的ONXX模型.
    :return: 优化之后的ONNX模型.
    简化之前, 使用这个方法产生会在'forward_all'用到的ValueInfo
    简化之后,使用这个方法去折叠前一步产生的常量到initializer中并且消除没被使用的常量
    """

    onnx.checker.check_model(model)
    onnx.helper.strip_doc_string(model)
    optimizers_list = onnxoptimizer.get_fuse_and_elimination_passes()
    if not skip_fuse_bn:
        optimizers_list.append('fuse_bn_into_conv')
    if skipped_optimizers is not None:
        for opt in skipped_optimizers:
            try:
                optimizers_list.remove(opt)
            except ValueError:
                pass

    model = onnxoptimizer.optimize(model, optimizers_list, fixed_point=True)
    onnx.checker.check_model(model)
    return model