Exemple #1
0
def main(argv=sys.argv):
    args = parse_args(argv)
    seed_all(12345)
    init_algorithms(deterministic=True)

    # Load the model into system memory (CPU, not GPU)
    model_state = torch.load(args.input, map_location='cpu')
    model_desc = model_state['model_desc']
    model = create_model(model_desc)
    model.load_state_dict(model_state['state_dict'])

    if args.format == 'pytorch':
        new_model_state = {
            'state_dict': model.state_dict(),
            'model_desc': model_desc,
            'train_datasets': model_state.get('train_datasets', []),
        }
        torch.save(new_model_state, args.output)
    elif args.format == 'onnx':
        image_height = model.data_specs.input_specs.height
        image_width = model.data_specs.input_specs.width
        dummy_input = torch.randn(1, 3, image_height, image_width)
        onnx.export(model, (dummy_input, ), args.output, verbose=False)
    else:
        raise Exception('Unrecognised model format: {}'.format(args.format))
Exemple #2
0
def convert_pytorch(nlp: Pipeline, opset: int, output: str,
                    use_external_format: bool):
    if not is_torch_available():
        raise Exception(
            "Cannot convert because PyTorch is not installed. Please install torch first."
        )

    import torch
    from torch.onnx import export

    print("Using framework PyTorch: {}".format(torch.__version__))

    with torch.no_grad():
        input_names, output_names, dynamic_axes, tokens = infer_shapes(
            nlp, "pt")
        ordered_input_names, model_args = ensure_valid_input(
            nlp.model, tokens, input_names)

        export(
            nlp.model,
            model_args,
            f=output,
            input_names=ordered_input_names,
            output_names=output_names,
            dynamic_axes=dynamic_axes,
            do_constant_folding=True,
            use_external_data_format=use_external_format,
            enable_onnx_checker=True,
            opset_version=opset,
        )
def main():
    args = parse_args()
    cfg = mmcv.Config.fromfile(args.config)
    output_path = './test.onnx'

    torch.backends.cudnn.benchmark = True
    cfg.model.pretrained = None
    cfg.data.test.test_mode = True

    # build the model and load checkpoint
    model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg)
    load_checkpoint(model, args.checkpoint, map_location='cpu')
    model.eval()

    output_h, output_w = 512, 512
    for k, v in cfg.items():
        if k.startswith('test'):
            for k_, v_ in v.items():
                if k_.endswith('img_scale'):
                    output_w, output_h = v_

    assert 'forward_export' in model.__dir__()
    model.forward = model.forward_export
    with torch.no_grad():
        export(model,
               torch.zeros((1, 3, output_h, output_w), dtype=torch.float32),
               output_path,
               opset_version=9,
               do_constant_folding=True)
    similarity_test(model, args.out, height_width=(output_h, output_w))
Exemple #4
0
def save_onnx(path, model, *input_shapes, device="cpu"):
    model = model.eval()
    args = [
        torch.ones([1, *shape], requires_grad=True, device=device)
        for shape in input_shapes
    ]
    outputs = model(*args)

    if torch.is_tensor(outputs):
        outputs = [outputs]

    input_names = [f"input_{i}" for i, _ in enumerate(args)]
    output_names = [f"output_{i}" for i, _ in enumerate(outputs)]

    onnx.export(
        model,
        tuple(args),
        path,
        export_params=True,
        verbose=True,
        do_constant_folding=True,
        input_names=input_names,
        output_names=output_names,
        dynamic_axes={
            n: {
                0: "batch_size"
            }
            for n in input_names + output_names
        },
        opset_version=10,
    )
Exemple #5
0
def toonnx(args):
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    if not args.full_model:
        model = darknet.DarkNet().to(device)
        model_state_dict = model.state_dict()
        state_dict = torch.load(args.pytorch_model, map_location=device)
        state_dict = {
            k: v
            for k, v in state_dict.items() if k in model_state_dict
        }
        state_dict = collections.OrderedDict(state_dict)
        model_state_dict.update(state_dict)
        model.load_state_dict(model_state_dict)
    else:
        print('Warning: this function has not been tested yet!')
        model = torch.load(args.pytorch_model)

    dummy_input = torch.rand(1,
                             3,
                             args.insize[0],
                             args.insize[1],
                             device=device)
    onnx.export(model,
                dummy_input,
                args.onnx_model,
                verbose=True,
                input_names=['data'])

    session = ort.InferenceSession(args.onnx_model)
    outputs = session.run(None, {'data': dummy_input.cpu().numpy()})
    for i, output in enumerate(outputs):
        print('branch {} output size is {}'.format(i, output.shape))
Exemple #6
0
def main():
    x_data, char_matrix = prepare_moses(return_matrix=True, limit=DATA_LIMIT)

    model = GRU(input_size=INPUT_SIZE, hidden_size=HIDDEN_SIZE, batch_size=BATCH_SIZE,
                output_size=OUTPUT_SIZE, n_layers=N_LAYERS, bidirectional=BIDIRECTIONAL)

    global MODEL_NAME
    if MODEL_NAME is None:
        MODEL_NAME = 'GRU-Model-{}-{}.onnx'.format(N_LAYERS, HIDDEN_SIZE)

    criterion = nn.CrossEntropyLoss()
    for epoch in range(1):

        for iteration, element in enumerate(x_data):
            element = torch.Tensor([element])

            sequence, target = element[0][:-1], element[0][1:]

            loss = train(model, sequence, target, criterion=criterion)

            smile_str = evaluate(model, char_matrix, prime_str='CC(=O)')
            print('{}\t Loss: {}\n'.format(smile_str, loss))

            if iteration >= ITERATION_LIMIT:
                break

    onnx.export(model, sequence[0], MODEL_NAME)
    print(evaluate(model, prime_str='CC(=O)', embedding_matrix=char_matrix))
def deploy_onnx_quantized(dataloader: DataLoader, model: nn.Module, fuse: bool,
                          name: str):
    model = deepcopy(model)
    path = f'./{name}'

    model = model.eval()
    if fuse:
        model.fuse()
        path += '_fused'

    float_path = path + '_float.onnx'
    quantized_path = path + '_quant.onnx'
    example_input = torch.rand(1, 3, 224, 224)
    onnx.export(model=model,
                args=(example_input, ),
                f=float_path,
                input_names=['input_image'],
                output_names=['logits'],
                opset_version=12)
    onnx_q_loader = ONNXQuantizationDataReader(quant_loader=dataloader,
                                               input_name='input_image')
    quantize_static(model_input=float_path,
                    model_output=quantized_path,
                    calibration_data_reader=onnx_q_loader)

    avg_time = benchmark_onnx_model(rt.InferenceSession(float_path))
    size = Path(float_path).stat().st_size / 1e6
    print(
        f'Benchmarking {float_path}: Avg. inference@CPU: {avg_time:3.2f} ms, Size: {size:2.2f} MB'
    )
    avg_time = benchmark_onnx_model(rt.InferenceSession(quantized_path))
    size = Path(quantized_path).stat().st_size / 1e6
    print(
        f'Benchmarking {quantized_path}: Avg. inference@CPU: {avg_time:3.2f} ms, Size: {size:2.2f} MB'
    )
def convert_pytorch(nlp: Pipeline, opset: int, output: Path,
                    use_external_format: bool):
    """
    Export a PyTorch backed pipeline to ONNX Intermediate Representation (IR

    Args:
        nlp: The pipeline to be exported
        opset: The actual version of the ONNX operator set to use
        output: Path where will be stored the generated ONNX model
        use_external_format: Split the model definition from its parameters to allow model bigger than 2GB

    Returns:

    """
    if not is_torch_available():
        raise Exception(
            "Cannot convert because PyTorch is not installed. Please install torch first."
        )

    import torch
    from torch.onnx import export

    from .pytorch_utils import is_torch_less_than_1_11

    print(f"Using framework PyTorch: {torch.__version__}")

    with torch.no_grad():
        input_names, output_names, dynamic_axes, tokens = infer_shapes(
            nlp, "pt")
        ordered_input_names, model_args = ensure_valid_input(
            nlp.model, tokens, input_names)

        # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11,
        # so we check the torch version for backwards compatibility
        if is_torch_less_than_1_11:
            export(
                nlp.model,
                model_args,
                f=output.as_posix(),
                input_names=ordered_input_names,
                output_names=output_names,
                dynamic_axes=dynamic_axes,
                do_constant_folding=True,
                use_external_data_format=use_external_format,
                enable_onnx_checker=True,
                opset_version=opset,
            )
        else:
            export(
                nlp.model,
                model_args,
                f=output.as_posix(),
                input_names=ordered_input_names,
                output_names=output_names,
                dynamic_axes=dynamic_axes,
                do_constant_folding=True,
                opset_version=opset,
            )
 def to_onnx(self, outdir="model/openvino"):
     os.makedirs(outdir, exist_ok=True)
     mfcc = torch.rand(1, 1, self.n_mfcc, self.max_frame).to(self.device)
     onnx.export(self.model,
                 mfcc,
                 f"{outdir}/diarization.onnx",
                 input_names=["input"],
                 output_names=["output"])
     print(f"model is exported as {outdir}/diarization.onnx")
 def export(model, device, path):
     dummy_input = randn(1, 1, 28, 28, device=device)
     onnx.export(
         model,
         dummy_input,
         path,
         input_names=["Input3"],
         output_names=["Plus214_Output_0"],
     )
Exemple #11
0
def export(dir):
    dummy_input = Variable(torch.randn(1, 3, 4, 4))
    model = broadcast_add()
    model.eval()
    torch.save(model.state_dict(), os.path.join(dir, "broadcast_add.pth"))
    onnx.export(model,
                dummy_input,
                os.path.join(dir, "broadcast_add.onnx"),
                verbose=True)
def export(dir):
    file_path = os.path.realpath(__file__)
    file_dir = os.path.dirname(file_path)
    dummy_input = Variable(torch.randn(1, 3, 32, 32))
    model = ResNet34()
    # model = load_network(model,os.path.join(file_dir,'..','model','pose_v02.pth'))
    model.eval()
    torch.save(model.state_dict(),os.path.join(dir,"resnet.pth"))
    onnx.export(model, dummy_input,os.path.join(dir,"resnet.onnx"), verbose=True)
Exemple #13
0
 def export_roihead(self, input_shape, export_name):
     inputs = torch.randn(*input_shape)
     mask = self.forward('centerhead', inputs)
     export(self, ('centerhead', inputs),
            export_name,
            input_names=['roipool'],
            output_names=['mask'],
            opset_version=11)
     return mask
Exemple #14
0
 def export_fpn(self, input_shape, export_name):
     inputs = torch.randn(*input_shape)
     features = self.forward('fpn', inputs)
     export(self, ('fpn', inputs),
            export_name,
            input_names=['images'],
            output_names=list(features.keys()),
            opset_version=11)
     return features
def export(dir):
    dummy_input = Variable(torch.randn(1, 3, 224, 224))
    model = MobileNetV2()
    model.eval()
    torch.save(model.state_dict(), os.path.join(dir, "MobileNetV2.pth"))
    onnx.export(model,
                dummy_input,
                os.path.join(dir, "MobileNetV2.onnx"),
                verbose=True)
Exemple #16
0
 def to_onnx(self, fname="speaker_diarization.onnx", outdir="model"):
     os.makedirs(outdir, exist_ok=True)
     audio = np.ones(self.chunk_len)
     audio = self._to_melspec(audio)
     hidden = torch.rand(self.n_layer, 1, self.n_hidden)
     export(self.model, (audio, hidden),
            f"{outdir}/{fname}",
            input_names=["input"],
            output_names=["output"])
def save_model(model, device, path, name, no_cuda):
    if not no_cuda:
        x = torch.randint(255, (1, 28*28), dtype=torch.float).to(device) / 255
    else:
        x = torch.randint(255, (1, 28*28), dtype=torch.float) / 255

    onnx.export(model, x, "./outputs/{}.onnx".format(name))
    print('Saved onnx model to model.onnx')

    torch.save(model.state_dict(), "./outputs/{}.pth".format(name))
    print('Saved PyTorch Model to model.pth')
Exemple #18
0
def test_onnx_export():
    dummy_input = torch.randn(1, 3, 256, 256)
    model = MargiPoseModel(CanonicalSkeletonDesc,
                           n_stages=1,
                           axis_permutation=True,
                           feature_extractor='inceptionv4',
                           pixelwise_loss='jsd')
    model.eval()
    with TemporaryDirectory() as d:
        onnx_file = os.path.join(d, 'model.onnx')
        onnx.export(model, (dummy_input, ), onnx_file, verbose=False)
 def torch2onnx(self, model, weight_path=None):
     if not os.path.exists(self.save_dir):
         os.makedirs(self.save_dir)
     if weight_path is not None:
         self.model_process.loadLatestModelWeight(weight_path, model)
     save_onnx_path = os.path.join(self.save_dir,
                                   "%s.onnx" % model.get_name())
     onnx.export(model,
                 self.input_x,
                 save_onnx_path,
                 export_params=True,
                 verbose=False)
     return save_onnx_path
Exemple #20
0
def export(dir):
    file_path = os.path.realpath(__file__)
    file_dir = os.path.dirname(file_path)
    dummy_input = torch.randn(1, 3, 224, 224)
    model = resnet50()
    # model = load_network(model,os.path.join(file_dir,'..','model','pose_v02.pth'))
    model.eval()
    torch.save(model.state_dict(), os.path.join(dir, "resnet50.pth"))
    onnx.export(model,
                dummy_input,
                os.path.join(dir, "resnet50.onnx"),
                opset_version=9,
                verbose=True)
Exemple #21
0
def save_model(model, device, path, name):
    base = Path(path)
    onnx_file = base.joinpath('{}.onnx'.format(name)).resolve()
    pth_file = base.joinpath('{}.pth'.format(name)).resolve()

    # create dummy variable to traverse graph
    x = torch.randint(255, (1, 28 * 28), dtype=torch.float).to(device) / 255
    onnx.export(model, x, onnx_file)
    print('Saved onnx model to {}'.format(onnx_file))

    # saving PyTorch Model Dictionary
    torch.save(model.state_dict(), pth_file)
    print('Saved PyTorch Model to {}'.format(pth_file))
Exemple #22
0
 def export_fcoshead(self,
                     features,
                     export_name,
                     input_names=None,
                     output_names=None):
     self.precal_locations = self.proposal_generator.compute_locations(
         features)
     proposals = self.forward('fcoshead', features)
     export(self, ('fcoshead', features),
            export_name,
            input_names=input_names,
            output_names=output_names,
            opset_version=11)
     return proposals
Exemple #23
0
def test_onnx():
    class SimpleModel(nn.Module):
        def forward(self, x):
            x = flat_softmax(x)
            x = dsnt(x, normalized_coordinates=True)
            return x

    dummy_input = torch.randn((2, 4, 32, 32))
    model = SimpleModel()
    model.eval()
    with TemporaryDirectory() as d:
        onnx_file = os.path.join(d, 'model.onnx')
        onnx.export(model, (dummy_input, ), onnx_file, verbose=False)
        assert os.path.isfile(onnx_file)
Exemple #24
0
def convert_pytorch(nlp: Pipeline, opset: int, output: Path,
                    use_external_format: bool):
    """
    Export a PyTorch backed pipeline to ONNX Intermediate Representation (IR

    Args:
        nlp: The pipeline to be exported
        opset: The actual version of the ONNX operator set to use
        output: Path where will be stored the generated ONNX model
        use_external_format: Split the model definition from its parameters to allow model bigger than 2GB

    Returns:

    """
    if not is_torch_available():
        raise Exception(
            "Cannot convert because PyTorch is not installed. Please install torch first."
        )

    import torch
    from torch.onnx import export

    print(f"Using framework PyTorch: {torch.__version__}")

    with torch.no_grad():
        input_names, output_names, dynamic_axes, tokens = infer_shapes(
            nlp, "pt")
        ordered_input_names, model_args = ensure_valid_input(
            nlp.model, tokens, input_names)

        print('Exporting from PyTorch to ONNX...')
        print('input_names', input_names)
        print('output_names', output_names)
        print('dynamic_axes', dynamic_axes)
        print('tokens', tokens)
        print('model_args', model_args)

        export(nlp.model,
               model_args,
               f=output.as_posix(),
               input_names=ordered_input_names,
               output_names=output_names,
               dynamic_axes=dynamic_axes,
               do_constant_folding=True,
               use_external_data_format=use_external_format,
               enable_onnx_checker=True,
               opset_version=opset,
               verbose=True)
Exemple #25
0
def ptconversion():
    model = torch.load('entire_model.pt')
    x = Variable(torch.randn(1, 10, 6))
    dot = make_dot(model(x), params=dict(model.named_parameters()))
    dot.format = 'png'
    dot.render('torchviz-sample')
    model.eval()
    input_shape = (1, 10, 6)
    model_onnx_path = "torch_model.onnx"
    dummy_input = Variable(torch.randn(1, *input_shape).type(dtype),
                           requires_grad=True)
    # plot graph of variable, not of a nn.Module
    output = torch_onnx.export(net, dummy_input, model_onnx_path, verbose=True)
    # plot graph of variable, not of a nn.Module
    print("Export of torch_model.onnx complete!")
    onnx_model = onnx.load("torch_model.onnx")
    s = MessageToJson(onnx_model)
    onnx_json = json.loads(s)
    # Convert JSON to String
    onnx_str = json.dumps(onnx_json)
    with open("model2.json", "w") as json_file:
        json_file.write(onnx_str)
    resp = make_response(onnx_str)
    resp.headers['Access-Control-Allow-Origin'] = '*'
    return resp
def train(cfg, args):
    #logging.basicConfig(filename='./output/LOG/'+__name__+'.log',format='[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]', level = logging.INFO,filemode='a',datefmt='%Y-%m-%d%I:%M:%S %p')
    logging.basicConfig(
        filename='./output/LOG/' + __name__ + '.log',
        format='[%(asctime)s-%(filename)s-%(levelname)s:%(message)s]',
        level=logging.INFO)
    logger = logging.getLogger('SSD.trainer')
    # -----------------------------------------------------------------------------
    # Model
    # -----------------------------------------------------------------------------
    ssd_model = build_ssd_model(cfg)
    ssd_model.init_from_base_net(args.vgg)
    ssd_model = torch.nn.DataParallel(ssd_model,
                                      device_ids=range(
                                          torch.cuda.device_count()))
    device = torch.device(cfg.MODEL.DEVICE)
    print(ssd_model)
    logger.info(ssd_model)
    model = torchvision.models.AlexNet(num_classes=10)
    logger.info(model)
    writer = tensorboardX.SummaryWriter(log_dir="./output/model_graph/",
                                        comment="myresnet")
    #dummy_input = torch.autograd.Variable(torch.rand(1, 3, 227, 227))
    dummy_input = torch.autograd.Variable(torch.rand(1, 3, 300, 300))
    #writer.add_graph(model=ssd_model, input_to_model=(dummy_input, ))
    model_onnx_path = 'torch_model.onnx'
    output = torch_onnx.export(ssd_model,
                               dummy_input,
                               model_onnx_path,
                               verbose=False)

    #ssd_model.to(device)
    print('----------------')
Exemple #27
0
def train(epoch, dataloader, module):
    for i in range(epoch):
        for batch_idx, (data, target) in enumerate(dataloader):
            data, target = Variable(data), Variable(target)
            optimizer = optim.SGD(module.parameters(), lr=0.01, momentum=0.9)
            optimizer.zero_grad()
            output = module(data)
            loss = F.kl_div(output, target)
            loss.backward()
            optimizer.step()
            if batch_idx % 200 == 0:
                print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                    i, batch_idx * len(data), len(dataloader.dataset),
                    100. * batch_idx / len(dataloader), loss.data))
    torch.save(module, "./module/my_model.pkl")
    dummy_input = torch.randn(40)
    torch_onnx.export(module, args=dummy_input, f="./module/my_model.onnx")
    def export(self, output_dir):
        assert self.model is not None, "Only a trained model can be exported."

        self.model.eval().cpu()

        dummy_input = torch.randn((1, 1, 1)).float()
        output_filename = path.join(output_dir, f'{self.name()}.onnx')
        onnx.export(self.model,
                    dummy_input,
                    output_filename,
                    input_names=['input'],
                    output_names=['output'],
                    do_constant_folding=True,
                    dynamic_axes={
                        'input': [0, 2],
                        'output': [0, 2]
                    })
Exemple #29
0
def toonnx(args):
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    if not args.full_model:
        if args.backbone == 'darknet':
            model = darknet.DarkNet(np.random.randint(0, 100,
                                                      (12, 2))).to(device)
        elif args.backbone == 'shufflenetv2':
            model = shufflenetv2.ShuffleNetV2(np.random.randint(
                0, 100, (12, 2)),
                                              model_size=args.thin).to(device)
        else:
            print('unknown backbone architecture!')
            sys.exit(0)
        if args.pytorch_model:
            model_state_dict = model.state_dict()
            state_dict = torch.load(args.pytorch_model, map_location=device)
            state_dict = {
                k: v
                for k, v in state_dict.items() if k in model_state_dict
            }
            state_dict = collections.OrderedDict(state_dict)
            model_state_dict.update(state_dict)
            model.load_state_dict(model_state_dict)
    else:
        print('Warning: this function has not been tested yet!')
        model = torch.load(args.pytorch_model)

    model.eval()
    dummy_input = torch.rand(1,
                             3,
                             args.insize[0],
                             args.insize[1],
                             device=device)
    onnx.export(model,
                dummy_input,
                args.onnx_model,
                verbose=True,
                input_names=['data'],
                output_names=['out1', 'out2', 'out3'],
                opset_version=11)

    session = ort.InferenceSession(args.onnx_model)
    outputs = session.run(None, {'data': dummy_input.cpu().numpy()})
    for i, output in enumerate(outputs):
        print('branch {} output size is {}'.format(i, output.shape))
def stylization(p_wct, content_image_path, style_image_path, content_seg_path, style_seg_path, output_image_path, cuda, args):
    # Load image
    cont_img = Image.open(content_image_path).convert('RGB')
    styl_img = Image.open(style_image_path).convert('RGB')
    try:
        cont_seg = Image.open(content_seg_path)
        styl_seg = Image.open(style_seg_path)
    except:
        cont_seg = []
        styl_seg = []
    
    cont_img = transforms.ToTensor()(cont_img).unsqueeze(0)
    styl_img = transforms.ToTensor()(styl_img).unsqueeze(0)
    
    if cuda:
        cont_img = cont_img.cuda(0)
        styl_img = styl_img.cuda(0)
        p_wct.cuda(0)
    
    cont_img = Variable(cont_img, requires_grad=False)
    styl_img = Variable(styl_img, requires_grad=False)
    cont_seg = torch.FloatTensor(np.asarray(cont_seg))
    styl_seg = torch.FloatTensor(np.asarray(styl_seg))

    if args.export_onnx:
        assert args.export_onnx.endswith(".onnx"), "Export model file should end with .onnx"
        export(p_wct, [cont_img, styl_img, cont_seg, styl_seg],
               f=args.export_onnx, verbose=args.verbose)
        
    with Timer("Elapsed time in stylization: %f"):
        stylized_img = p_wct.transform(cont_img, styl_img, cont_seg, styl_seg)
    utils.save_image(stylized_img.data.cpu().float(), output_image_path, nrow=1)
    
    with Timer("Elapsed time in propagation: %f"):
        out_img = p_pro.process(output_image_path, content_image_path)
    out_img.save(output_image_path)
    
    if not cuda:
        print("NotImplemented: The CPU version of smooth filter has not been implemented currently.")
        return
    
    with Timer("Elapsed time in post processing: %f"):
        out_img = smooth_filter(output_image_path, content_image_path, f_radius=15, f_edge=1e-1)
    out_img.save(output_image_path)