ultralytics / yolov3

YOLOv3 in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
10.19k stars 3.44k forks source link

Exporting the Model to ONNX #561

Closed SmallHedgehog closed 4 years ago

SmallHedgehog commented 5 years ago

I convert this model to ONNX and running it using ONNX Runtime, but the output of ONNX Rumtime and PyToch are different with same input.

image

nscotto commented 5 years ago

Sorry to interrupt, but could you share your code for converting to ONNX? I can't manage to pass that step, even after having read the doc many times and several trials.

SmallHedgehog commented 5 years ago

Sorry to interrupt, but could you share your code for converting to ONNX? I can't manage to pass that step, even after having read the doc many times and several trials.

`

# Initialize model
model = Darknet(opt.cfg, img_size)

# Load weights
if opt.weights.endswith('.pt'):  # pytorch format
    model.load_state_dict(torch.load(opt.weights, map_location=device)['model'])
else:  # darknet format
    _ = load_darknet_weights(model, opt.weights)

# Fuse Conv2d + BatchNorm2d layers
# model.fuse()

# Eval mode
model.to(device).eval()

from PIL import Image
img0 = Image.open('test.jpg')
img = np.array(img0)

# Padded resize
img = pad_to_square(img, value=0)
img = np.array(resize(img, img_size))
# Normalize RGB
img = img.transpose(2, 0, 1)
img = img.astype(np.float32)
img /= 255.0  # 0 - 255 to 0.0 - 1.0

# img = torch.from_numpy(img).unsqueeze(0)
img = img.reshape((1, ) + img.shape)
img = torch.from_numpy(img)
xout = model(img)[0]
print('xout: ', xout.size())

# img = torch.zeros((1, 3) + img_size)  # (1, 3, 320, 192)
# torch.onnx.export(model, img, 'weights/export.onnx', verbose=True)
# Export the model
torch.onnx.export(model,                 # model being run
              img,                       # model input (or a tuple for multiple inputs)
              "weights/export.onnx",     # where to save the model (can be a file or file-like object)
              export_params=True,        # store the trained parameter weights inside the model file
              opset_version=10,          # the ONNX version to export the model to
              do_constant_folding=True,  # wether to execute constant folding for optimization
              input_names = ['input'],   # the model's input names
              output_names = ['output'], # the model's output names
              )

model = onnxruntime.InferenceSession('weights/export.onnx')

ort_inputs = {model.get_inputs()[0].name: img.numpy()}
ort_outs = model.run(None, ort_inputs)
out = ort_outs[0]

# out = model(img)[0]
print(out.shape)

# compare ONNX Runtime and PyTorch results
np.testing.assert_allclose(xout.numpy(), ort_outs[0], rtol=1e-03, atol=1e-05)

`

Gaondong commented 4 years ago

I convert this model to ONNX and running it using ONNX Runtime, but the output of ONNX Rumtime and PyToch are different with same input.

image

Sorry to bother you,did you fixed it?

glenn-jocher commented 4 years ago

See https://github.com/ultralytics/yolov3/issues/653 for a more direct conversion to onnx.

CConory commented 4 years ago

I convert this model to ONNX and running it using ONNX Runtime, but the output of ONNX Rumtime and PyToch are different with same input. image

Sorry to bother you,did you fixed it?

I also meet this problem, I can convert successfully, but the output is now same between the onnx and pytorch

CConory commented 4 years ago

I convert this model to ONNX and running it using ONNX Runtime, but the output of ONNX Rumtime and PyToch are different with same input. image

Sorry to bother you,did you fixed it?

I also meet this problem, I can convert successfully, but the output is now same between the onnx and pytorch

I had fixed the problem. It's not the Upsampling problem. using pytorch1.2 and the opset9 version is also ok

Gaondong commented 4 years ago

I convert this model to ONNX and running it using ONNX Runtime, but the output of ONNX Rumtime and PyToch are different with same input. image

Sorry to bother you,did you fixed it?

I also meet this problem, I can convert successfully, but the output is now same between the onnx and pytorch

I had fixed the problem. It's not the Upsampling problem. using pytorch1.2 and the opset9 version is also ok

Thank you!

github-actions[bot] commented 4 years ago

This issue is stale because it has been open 30 days with no activity. Remove Stale label or comment or this will be closed in 5 days.

flyingmrwang commented 4 years ago

I convert this model to ONNX and running it using ONNX Runtime, but the output of ONNX Rumtime and PyToch are different with same input. image

Sorry to bother you,did you fixed it?

I also meet this problem, I can convert successfully, but the output is now same between the onnx and pytorch

I had fixed the problem. It's not the Upsampling problem. using pytorch1.2 and the opset9 version is also ok

Hi, the same problem. So I tried to replace pytorch1.4 with 1.2 as you suggested. But it says "AttributeError: module 'torch.jit' has no attribute 'unused'". Seems to be another problem arguing pytorch version is too old. Is there any solution without changeing pytorch version?

glenn-jocher commented 4 years ago

@flyingmrwang onnx model fuses obj and cls confidences. This is suitable for export to coreml and the iDetection iOS app.

Nioolek commented 4 years ago

My environment: Pytorch v1.2 onnx V1.6.0 and opset V9 is ok. But Pytorch V1.4 failed.

glenn-jocher commented 4 years ago

@Nioolek you might want to try a verified environment:

Reproduce Our Environment

To access an up-to-date working environment (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled), consider a: