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

[ONNX] Export ROIAlign with aligned=True #2613

Merged
merged 3 commits into from Aug 27, 2020

Conversation

neginraoof
Copy link
Contributor

[ONNX] Export ROIAlign with aligned=True

Copy link
Member

@fmassa fmassa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

I think there are still a few issues that need to be addressed, let me know what you think.

Also, the ONNX CI is currently broken, and would be great to have it fixed before we can merge this.

if(aligned):
raise RuntimeError('Unsupported: ONNX export of roi_align with aligned')
if aligned:
rois = g.op("Sub", rois, torch.tensor(0.5))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unfortunately not all that needs to be changed so that the implementation supports the aligned flag.

Indeed, this only works if the spatial_scale is 1, and if the boxes are not malformed.

See

T offset = aligned ? (T)0.5 : (T)0.0;
T roi_start_w = offset_rois[1] * spatial_scale - offset;
T roi_start_h = offset_rois[2] * spatial_scale - offset;
T roi_end_w = offset_rois[3] * spatial_scale - offset;
T roi_end_h = offset_rois[4] * spatial_scale - offset;
T roi_width = roi_end_w - roi_start_w;
T roi_height = roi_end_h - roi_start_h;
if (!aligned) {
// Force malformed ROIs to be 1x1
roi_width = std::max(roi_width, (T)1.);
roi_height = std::max(roi_height, (T)1.);
}
for the locations where the aligned flag plays a role. I believe we would need at least to do something like

if aligned:
    rois = (rois * spatial_scale - 0.5) / spatial_scale
    # or equivalently
    rois = rois - 0.5 / spatial_scale

but the difference wrt the clamping in

if (!aligned) {
// Force malformed ROIs to be 1x1
roi_width = std::max(roi_width, (T)1.);
roi_height = std::max(roi_height, (T)1.);
}
persists

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for reviewing this. It was very helpful.
Since ONNX forces all ROIs to be 1x1 or larger, I think fixing the export for malformed ROIs needs a change in spec.

Comment on lines 125 to 126
single_roi = torch.tensor([[0, 0, 0, 4, 4]], dtype=torch.float32)
model = ops.RoIAlign((5, 5), 1, 2, aligned=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test for more configurations of the roi (including malformed boxes), and different scale_factor?

Copy link
Member

@fmassa fmassa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@fmassa fmassa merged commit 279fca5 into pytorch:master Aug 27, 2020
bryant1410 pushed a commit to bryant1410/vision-1 that referenced this pull request Nov 22, 2020
* Add support for export ROIAlign

* Fix for feedback

* flake8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants