question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

exporting to onnx

See original GitHub issue

I am trying to get this model (trained on custom dataset) to run under opencv so it will perform better on jetson nano. However, conversion to ONNX does not go well. this is the code i am using:

from effdet import get_efficientdet_config, EfficientDet                                                                                                                 
from effdet.bench import DetBenchPredict                                                                                                                                 
from effdet.efficientdet import HeadNet     

def load_net(checkpoint_path):                                                                                                                                           
    config = get_efficientdet_config('tf_efficientdet_d1')                                                                                                               
    net = EfficientDet(config, pretrained_backbone=False)                                                                                                                
                                                                                                                                                                         
    config.num_classes = 1                                                                                                                                               
    config.image_size=512                                                                                                                                                
    net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))                                                            
                                                                                                                                                                         
    checkpoint = torch.load(checkpoint_path)                                                                                                                             
    net.load_state_dict(checkpoint['model_state_dict'])                                                                                                                  
    net = DetBenchPredict(net, config)                                                                                                                                   
    net.eval();                                                                                                                                                          
    return net.cuda()  

import torch
from functools import partial

def load_model_weight(model, model_path):
    model=torch.load(model_path)
    model = model.eval()
    return model

def export_onnx_model(model, input_shape, onnx_path, input_names=None, output_names=None, dynamic_axes=None):
    inputs = torch.ones(*input_shape)
    origin_forward = model.forward
    model.forward = partial(
        model.forward, img_scales=torch.tensor([1.0], dtype=torch.float).cuda(), img_size=torch.tensor([input_shape[-2:]], dtype=torch.float).cuda())
    #model(inputs)
    torch.onnx.export(model, (inputs), onnx_path, input_names=input_names, output_names=output_names, dynamic_axes=dynamic_axes)

model_path = "best-checkpoint-013epoch.bin"
model = load_net(model_path)
input_shape = (1, 3, 512, 512)
onnx_path = "test.onnx"
export_onnx_model(model, input_shape, onnx_path)

but it gives:

     17 # Calculate asymmetric TensorFlow-like 'SAME' padding for a convolution
     18 def get_same_padding(x: int, k: int, s: int, d: int):
---> 19     return max((math.ceil(x / s) - 1) * s + (k - 1) * d + 1 - x, 0)
     20 
     21 

RuntimeError: Integer division of tensors using div or / is no longer supported, and in a future release div will perform true division as in Python 3. Use true_divide or floor_divide (// in Python) instead.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
msknorrcommented, Dec 15, 2020

@mosheliv did you find a way to use the tf_efficientdet weights? I could export “efficientdet_dx” (without the padding) in onnx.

1reaction
moshelivcommented, Dec 23, 2020

I didn’t need efficientdet specifically at this stage so switched to yolov4/yolov5. They have comparable performance and were ported to tensorrt (whichbwas why i started this unfortunate path). Good luck with trying, its not trivial…

On Wed, Dec 23, 2020, 18:43 Ekta P Bhojwani notifications@github.com wrote:

@mosheliv https://github.com/mosheliv did you find a way to use the tf_efficientdet weights? I could export “efficientdet_dx” (without the padding) in onnx.

Hey @mosheliv https://github.com/mosheliv. I have been looking to get the model trained on a custom dataset using the PyTorch framework and export it to ONNX for inference. I have tried to use both “efficientdet_d0” and “tf_efficientdet_d0” as my pretrained weights, but still couldn’t convert to ONNX successfully.

https://github.com/rwightman/efficientdet-pytorch/issues/155 http://url. I raised an issue for this couple of days ago. Have you encountered this issue when you exported to ONNX. I am wondering to know how you managed to export your model.

Same, I need help too

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/rwightman/efficientdet-pytorch/issues/89#issuecomment-749947660, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC7IWCYLNER6GIJJJ7BCUU3SWF7RZANCNFSM4R6SZIUA .

Read more comments on GitHub >

github_iconTop Results From Across the Web

(optional) Exporting a Model from PyTorch to ONNX and ...
To export a model, we call the torch.onnx.export() function. This will execute the model, recording a trace of what operators are used to...
Read more >
Convert your PyTorch training model to ONNX
To export a model, you will use the torch.onnx.export() function. This function executes the model, and records a trace of what operators ...
Read more >
Export to ONNX - Transformers
When a model is exported to the ONNX format, these operators are used to construct a computational graph (often called an intermediate representation)...
Read more >
Best Practices for Neural Network Exports to ONNX
Exporting your model to ONNX helps you to decouple the (trained) model from the rest of your project. Moreover, exporting also avoids environment ......
Read more >
Exporting your model to ONNX format | Barracuda
To use your trained neural network in Unity, you need to export it to the ONNX format. ONNX (Open Neural Network Exchange) is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found