Exemple #1
0
def converto_fp16():# Update the input name and path for your ONNX model
    import onnxmltools
    from onnxmltools.utils.float16_converter import convert_float_to_float16
    input_onnx_model = '/home/sobey/model.onnx'

    # Change this path to the output name and path for your float16 ONNX model
    output_onnx_model = '/home/sobey/model_f16.onnx'


    # Load your model

    onnx_model = onnxmltools.utils.load_model(input_onnx_model)

    # Convert tensor float type from your input ONNX model to tensor float16
    onnx_model = convert_float_to_float16(onnx_model)

    # Save as protobuf
    onnxmltools.utils.save_model(onnx_model, output_onnx_model)
    print("saved model in ",output_onnx_model)
Exemple #2
0
            r_hyps_pad_sos_eos)
    ort_outs = ort_session.run(None, ort_inputs)

    # check encoder output
    test(to_numpy(o0), ort_outs[0], rtol=1e-03, atol=1e-05)
    logger.info("export to onnx decoder succeed!")

    if args.fp16:
        try:
            import onnxmltools
            from onnxmltools.utils.float16_converter import convert_float_to_float16
        except ImportError:
            print('Please install onnxmltools!')
            sys.exit(1)
        encoder_onnx_model = onnxmltools.utils.load_model(encoder_onnx_path)
        encoder_onnx_model = convert_float_to_float16(encoder_onnx_model)
        encoder_onnx_path = os.path.join(args.output_onnx_dir,
                                         'encoder_fp16.onnx')
        onnxmltools.utils.save_model(encoder_onnx_model, encoder_onnx_path)
        decoder_onnx_model = onnxmltools.utils.load_model(decoder_onnx_path)
        decoder_onnx_model = convert_float_to_float16(decoder_onnx_model)
        decoder_onnx_path = os.path.join(args.output_onnx_dir,
                                         'decoder_fp16.onnx')
        onnxmltools.utils.save_model(decoder_onnx_model, decoder_onnx_path)
    # dump configurations
    onnx_config = {
        "beam_size": args.beam_size,
        "reverse_weight": args.reverse_weight,
        "ctc_weight": args.ctc_weight,
        "fp16": args.fp16
    }
import onnxmltools
from onnxmltools.utils.float16_converter import convert_float_to_float16

input_onnx_model = './hfnet_github.onnx'  # fp32 onnx model
output_onnx_model = './hfnet_github_fp16.onnx'  # fp16 onnx model

onnx_model = onnxmltools.utils.load_model(input_onnx_model)

onnx_model = convert_float_to_float16(onnx_model)
# onnx_model.graph.output.pop()
# onnx_model.graph.output.pop()
# onnx_model.graph.output.pop()
onnxmltools.utils.save_model(onnx_model, output_onnx_model)
# usage: python convert_fp16_onnx.py <fp32 ONNX file> <fp16 ONNX file>
#
# This script calls the onnxmltools to convert a fp32 model to fp16.
# Note that for some models, this can take a long time, e.g. measured 8 hours
# to convert resnet50i64 model from torchvision.  Other models are done within
# seconds.
#
import onnx
import sys
from onnx import optimizer
from onnxmltools.utils.float16_converter import convert_float_to_float16
if len(sys.argv) == 3:
    original_model = onnx.load(sys.argv[1])

    new_model = convert_float_to_float16(original_model)
    onnx.save_model(new_model,sys.argv[2])