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

sliding-window-inferer #765

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

liqunfu
Copy link
Contributor

@liqunfu liqunfu commented Jun 4, 2023

No description provided.

Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

CodeQL found more than 10 potential problems in the proposed changes. Check the Files changed tab for more details.

@liqunfu liqunfu changed the title onnx_script_test_case to support multuple outputs models sliding-window-inferer Jun 5, 2023

import numpy as np

from onnxscript import script

Check notice

Code scanning / CodeQL

Unused import

Import of 'script' is not used.

from onnxscript import script
from onnxscript.onnx_opset import opset15 as op
from onnxscript.onnx_types import FLOAT, INT64

Check notice

Code scanning / CodeQL

Unused import

Import of 'FLOAT' is not used. Import of 'INT64' is not used.
from onnxscript import script
from onnxscript.onnx_opset import opset15 as op
from onnxscript.onnx_types import FLOAT, INT64
from onnxscript.tests.common import onnx_script_test_case, testutils

Check notice

Code scanning / CodeQL

Unused import

Import of 'testutils' is not used.
onnxscript/tests/models/pnp.py Fixed Show fixed Hide fixed
onnxscript/tests/models/pnp.py Fixed Show fixed Hide fixed
onnxscript/tests/models/pnp.py Fixed Show fixed Hide fixed
Comment on lines +166 to +168
# if sw_batch_size > 1:
# for idx in op.Range(slice_g + 1, op.Min(slice_g + sw_batch_size, num_win)):
# win_data = op.Concat(win_data, op.Slice(inputs, slices[idx, :, 0], slices[idx, :, 1], spatial_axes))

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.
from onnxscript.tests.models.pnp import roi_indices_3d, aggrregate_predictor_output, sliding_window_inference, predict_mock, predict_mock_2

class PnpOpTest(onnx_script_test_case.OnnxScriptTestCase):
def test_roi_indices_3d(delf):

Check notice

Code scanning / CodeQL

First parameter of a method is not named 'self'

Normal methods should have 'self', rather than 'delf', as their first parameter.
onnxscript/tests/models/pnp.py Fixed Show fixed Hide fixed

save_model = False
if save_model:
model = sliding_window_inference.function_ir.to_model_proto(producer_name="monai")

Check warning

Code scanning / CodeQL

Unreachable code

This statement is unreachable.
from onnx import TensorProto
from onnx.helper import make_tensor

from onnxscript import script, graph

Check notice

Code scanning / CodeQL

Unused import

Import of 'graph' is not used.
Comment on lines +11 to +15
from typing import (
Any,
Callable,
Mapping,
)

Check notice

Code scanning / CodeQL

Unused import

Import of 'Any' is not used. Import of 'Callable' is not used. Import of 'Mapping' is not used.
Comment on lines +19 to +36
from onnxscript.onnx_types import (
BFLOAT16,
BOOL,
COMPLEX64,
COMPLEX128,
DOUBLE,
FLOAT,
FLOAT16,
INT8,
INT16,
INT32,
INT64,
STRING,
UINT8,
UINT16,
UINT32,
UINT64,
)

Check notice

Code scanning / CodeQL

Unused import

Import of 'BFLOAT16' is not used. Import of 'BOOL' is not used. Import of 'COMPLEX64' is not used. Import of 'COMPLEX128' is not used. Import of 'DOUBLE' is not used. Import of 'FLOAT16' is not used. Import of 'INT8' is not used. Import of 'INT16' is not used. Import of 'INT32' is not used. Import of 'UINT8' is not used. Import of 'UINT16' is not used. Import of 'UINT32' is not used. Import of 'UINT64' is not used.
UINT32,
UINT64,
)
from onnxscript.values import Op, Opset

Check notice

Code scanning / CodeQL

Unused import

Import of 'Op' is not used.
UINT64,
)
from onnxscript.values import Op, Opset
from typing import Callable, Optional, Sequence, Tuple, Union

Check notice

Code scanning / CodeQL

Unused import

Import of 'Optional' is not used. Import of 'Callable' is not used. Import of 'Sequence' is not used. Import of 'Tuple' is not used. Import of 'Union' is not used.
onnxscript/tests/pnp_test.py Fixed Show fixed Hide fixed
from onnxscript.values import Op, Opset
from typing import Callable, Optional, Sequence, Tuple, Union

import numpy as np

Check notice

Code scanning / CodeQL

Module is imported more than once

This import of module numpy is redundant, as it was previously imported [on line 1](1).

import numpy as np
import onnxruntime as rt
import onnx

Check notice

Code scanning / CodeQL

Module is imported more than once

This import of module onnx is redundant, as it was previously imported [on line 3](1).
import onnxruntime as rt
import onnx

class Opset18Ext(Opset18):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do you need an opset for this? Can we just define OpaqueOp as a normal python function and use it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I need a custom operator so that it can be programmatically edited to replace the op with the core model graph. I should not has used onnx domain though.

"""

inputs_shape = op.Shape(inputs)
inputs_spatial_shape = op.Slice(inputs_shape, op.Constant(value_ints=[2]), op.Constant(value_ints=[5]), op.Constant(value_ints=[0]))
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: may be this will be more readable if we move it after the line below, and rewrite it to:

   inputs_spatial_shape = op.Concat(D, H, W, axis=0)

Alternatively, we can also say:

   inputs_spatial_shape = op.Shape(inputs, from=2)

return indices, roi_shape

@script()
def aggrregate_predictor_output(
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: spelling "aggregate"

one = op.Constant(value_int=1)
one_ = op.Constant(value_ints=[1])
roi_D, roi_H, roi_W, _ = op.Split(op.Shape(roi_indices), num_outputs=4)
shape_1_seg_C_roi_D_H_W_1 = op.Concat(one_, seg_C, roi_D, roi_H, roi_W, one_, axis=0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: you can directly use op.Shape(roi_indices) instead of roi_D, roi_H, roi_W ... no need to split it and concat it back.

one_ = op.Constant(value_ints=[1])
roi_D, roi_H, roi_W, _ = op.Split(op.Shape(roi_indices), num_outputs=4)
shape_1_seg_C_roi_D_H_W_1 = op.Concat(one_, seg_C, roi_D, roi_H, roi_W, one_, axis=0)
zeros_1_seg_C_D_H_W_1 = op.Cast(op.ConstantOfShape(shape_1_seg_C_roi_D_H_W_1), to=INT64.dtype) # seg_C, roi_D, roi_H, roi_W, 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: you can directly specify the value/type as attribute of ConstantOfShape, instead of casting by adding value=onnx.helper.make_tensor(...)

grid_w = grid_w_0 + zeros_D_H_W

indices_seq = op.SequenceConstruct(grid_d, grid_h, grid_w)
indices = op.ConcatFromSequence(indices_seq, axis=-1, new_axis=1) # [D, H, W, 3]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess you need this because Concat doesn't support the new_axis attributes? I wonder if we should extend Concat to support it.

We could explicitly unsqueeze grid_d etc. to add the extra dimension at end and then use Concat. It is a bit more cumbersome, but onnxruntime doesn't handle sequence op that well (it may create extra copies of the tensor). Anyway, may not be worth bothering about for now.

for i in range(start[0], stop[0]):
for j in range(start[1], stop[1]):
for k in range(start[2], stop[2]):
aggrregated_pred_expected[i, j, k] += pred[i - start[0], j - start[1], k - start[2]]

Check failure

Code scanning / CodeQL

Modification of parameter with default

This expression mutates a [default value](1).
for i in range(start[0], stop[0]):
for j in range(start[1], stop[1]):
for k in range(start[2], stop[2]):
aggrregated_pred_expected[i, j, k] += pred[i - start[0], j - start[1], k - start[2]]

Check failure

Code scanning / CodeQL

Modification of parameter with default

This expression mutates a [default value](1). This expression mutates a [default value](1).
for j in range(start[1], stop[1]):
for k in range(start[2], stop[2]):
aggrregated_pred_expected[i, j, k] += pred[i - start[0], j - start[1], k - start[2]]
aggrregated_count_expected[i, j, k] += 1

Check failure

Code scanning / CodeQL

Modification of parameter with default

This expression mutates a [default value](1).
for j in range(start[1], stop[1]):
for k in range(start[2], stop[2]):
aggrregated_pred_expected[i, j, k] += pred[i - start[0], j - start[1], k - start[2]]
aggrregated_count_expected[i, j, k] += 1

Check failure

Code scanning / CodeQL

Modification of parameter with default

This expression mutates a [default value](1). This expression mutates a [default value](1).
onnxscript/tests/pnp_test.py Fixed Show fixed Hide fixed
@codecov
Copy link

codecov bot commented Jul 28, 2023

Codecov Report

Merging #765 (9d418ba) into main (176141e) will decrease coverage by 0.43%.
The diff coverage is 55.95%.

@@            Coverage Diff             @@
##             main     #765      +/-   ##
==========================================
- Coverage   76.87%   76.45%   -0.43%     
==========================================
  Files         112      114       +2     
  Lines       13736    14011     +275     
  Branches     1407     1430      +23     
==========================================
+ Hits        10560    10712     +152     
- Misses       2824     2935     +111     
- Partials      352      364      +12     
Files Changed Coverage Δ
onnxscript/tests/common/onnx_script_test_case.py 71.81% <37.50%> (-3.19%) ⬇️
onnxscript/tests/models/pnp.py 49.46% <49.46%> (ø)
onnxscript/tests/pnp_test.py 72.83% <72.83%> (ø)

... and 4 files with indirect coverage changes

@github-actions
Copy link

github-actions bot commented Jul 28, 2023

Test Results

         18 files  ±           0         18 suites  ±0   2h 19m 27s ⏱️ + 1h 12m 27s
    8 977 tests +           3    6 965 ✔️ ±         0      2 009 💤 ±         0    3 +  3 
231 360 runs  +115 491  50 867 ✔️ +25 234  180 445 💤 +90 209  48 +48 

For more details on these failures, see this check.

Results for commit 9d418ba. ± Comparison against base commit 176141e.

This pull request removes 1 and adds 4 tests. Note that renamed tests count towards both.
docs.test.test_documentation_examples.TestDocumentationExample ‑ test_documentation_examples
onnxscript.tests.pnp_test.PnpOpTest ‑ test_aggrregate_pred
onnxscript.tests.pnp_test.PnpOpTest ‑ test_get_scan_interval_script
onnxscript.tests.pnp_test.PnpOpTest ‑ test_roi_indices_3d
onnxscript.tests.pnp_test.PnpOpTest ‑ test_sliding_window_inference

♻️ This comment has been updated with latest results.

Comment on lines +12 to +20
from onnxscript.tests.models.pnp import (
roi_indices_3d,
aggregate_predictor_output,
sliding_window_inference,
predict_mock,
predict_mock_2,
Opset18Ext,
get_scan_interval_script,
)

Check notice

Code scanning / CodeQL

Unused import

Import of 'predict_mock' is not used. Import of 'predict_mock_2' is not used.
inputs_shape = op.Shape(inputs)
inputs_spatial_shape = op.Shape(inputs, start=2)
N, _, D, H, W = op.Split(inputs_shape, num_outputs=5)
roi_D, roi_H, roi_W = op.Split(roi_size, num_outputs=3)

Check notice

Code scanning / CodeQL

Unused local variable

Variable roi_D is not used.
inputs_shape = op.Shape(inputs)
inputs_spatial_shape = op.Shape(inputs, start=2)
N, _, D, H, W = op.Split(inputs_shape, num_outputs=5)
roi_D, roi_H, roi_W = op.Split(roi_size, num_outputs=3)

Check notice

Code scanning / CodeQL

Unused local variable

Variable roi_H is not used.
inputs_shape = op.Shape(inputs)
inputs_spatial_shape = op.Shape(inputs, start=2)
N, _, D, H, W = op.Split(inputs_shape, num_outputs=5)
roi_D, roi_H, roi_W = op.Split(roi_size, num_outputs=3)

Check notice

Code scanning / CodeQL

Unused local variable

Variable roi_W is not used.
use 1 instead to make sure sliding window works.
"""
zero = op.Constant(value_int=0)
scan_interval = zero

Check warning

Code scanning / CodeQL

Variable defined multiple times

This assignment to 'scan_interval' is unnecessary as it is [redefined](1) before this value is used.
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.

3 participants