Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TorchScript exporting for custom checkpoints #267

Merged
merged 6 commits into from
Jan 9, 2022
Merged

Conversation

zhiqwang
Copy link
Owner

@zhiqwang zhiqwang commented Jan 9, 2022

An example to export the torchscript for the checkpoint trained from ultralytics/yolov5:

import cv2
import torch
from yolort.models import YOLO
from yolort.utils import read_image_to_tensor
from yolort.v5 import letterbox, attempt_download

# Loading and pre-processing the image
img_path = "test/assets/zidane.jpg"
img_raw = cv2.imread(img_path)
img = letterbox(img_raw, new_shape=(640, 640))[0]
img = read_image_to_tensor(img)

# will download yolov5n6.pt from
# <https://github.com/ultralytics/yolov5/releases/download/v6.0/yolov5n6.pt>
checkpoint_path = attempt_download("yolov5n6.pt")

model = YOLO.load_from_yolov5(checkpoint_path, score_thresh=0.25)
model.eval()
# Script the model
scripted_model = torch.jit.script(model)
scripted_model.eval()

out = model(img[None])
out_script = scripted_model(img[None])

torch.testing.assert_close(out[0]["scores"], out_script[1][0]["scores"], rtol=0, atol=0)
torch.testing.assert_close(out[0]["labels"], out_script[1][0]["labels"], rtol=0, atol=0)
torch.testing.assert_close(out[0]["boxes"], out_script[1][0]["boxes"], rtol=0, atol=0)

cc @mattpopovich , this will fix #265 .

@zhiqwang zhiqwang added the bug / fix Something isn't working label Jan 9, 2022
@CLAassistant
Copy link

CLAassistant commented Jan 9, 2022

CLA assistant check
All committers have signed the CLA.

@codecov
Copy link

codecov bot commented Jan 9, 2022

Codecov Report

Merging #267 (87c1403) into main (9ebcea9) will increase coverage by 0.19%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #267      +/-   ##
==========================================
+ Coverage   93.62%   93.82%   +0.19%     
==========================================
  Files          11       11              
  Lines         675      696      +21     
==========================================
+ Hits          632      653      +21     
  Misses         43       43              
Flag Coverage Δ
unittests 93.82% <100.00%> (+0.19%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
test/test_models.py 95.86% <100.00%> (+0.39%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9ebcea9...87c1403. Read the comment docs.

@zhiqwang zhiqwang merged commit c9b62db into main Jan 9, 2022
@zhiqwang zhiqwang deleted the fix-torchscript branch January 9, 2022 19:08
@zhiqwang zhiqwang added the deployment Inference acceleration for production label Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working deployment Inference acceleration for production
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to convert custom Ultralytics YOLOv5 weights to yolo-rt weights and export as torchscript?
2 participants