Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pyronn-torch
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Stephan Seitz
pyronn-torch
Commits
770c3ea2
Commit
770c3ea2
authored
5 years ago
by
Stephan Seitz
Browse files
Options
Downloads
Patches
Plain Diff
Apply crazy state hack
parent
e3458864
No related branches found
No related tags found
Loading
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/pyronn_torch/conebeam.py
+57
-30
57 additions, 30 deletions
src/pyronn_torch/conebeam.py
with
57 additions
and
30 deletions
src/pyronn_torch/conebeam.py
+
57
−
30
View file @
770c3ea2
...
@@ -12,7 +12,7 @@ import pyronn_torch
...
@@ -12,7 +12,7 @@ import pyronn_torch
import
sympy
as
sp
import
sympy
as
sp
class
_ForwardProjection
(
torch
.
autograd
.
Function
)
:
class
State
:
def
__init__
(
self
,
def
__init__
(
self
,
projection_shape
,
projection_shape
,
volume_shape
,
volume_shape
,
...
@@ -35,40 +35,63 @@ class _ForwardProjection(torch.autograd.Function):
...
@@ -35,40 +35,63 @@ class _ForwardProjection(torch.autograd.Function):
self
.
with_texture
=
with_texture
self
.
with_texture
=
with_texture
self
.
step_size
=
step_size
self
.
step_size
=
step_size
def
forward
(
self
,
volume
):
class
_ForwardProjection
(
torch
.
autograd
.
Function
):
@staticmethod
def
forward
(
self
,
volume
,
state
=
None
):
if
state
is
None
:
state
=
self
.
state
return_none
=
True
else
:
return_none
=
False
volume
=
volume
.
float
().
cuda
().
contiguous
()
volume
=
volume
.
float
().
cuda
().
contiguous
()
projection
=
torch
.
zeros
(
s
elf
.
projection_shape
,
projection
=
torch
.
zeros
(
s
tate
.
projection_shape
,
device
=
'
cuda
'
,
device
=
'
cuda
'
,
requires_grad
=
volume
.
requires_grad
)
requires_grad
=
volume
.
requires_grad
)
assert
pyronn_torch
.
cpp_extension
assert
pyronn_torch
.
cpp_extension
if
s
elf
.
with_texture
:
if
s
tate
.
with_texture
:
pyronn_torch
.
cpp_extension
.
call_Cone_Projection_Kernel_Tex_Interp_Launcher
(
pyronn_torch
.
cpp_extension
.
call_Cone_Projection_Kernel_Tex_Interp_Launcher
(
s
elf
.
inverse_matrices
,
projection
,
s
elf
.
source_points
,
s
tate
.
inverse_matrices
,
projection
,
s
tate
.
source_points
,
s
elf
.
step_size
,
volume
,
*
s
elf
.
volume_spacing
)
s
tate
.
step_size
,
volume
,
*
s
tate
.
volume_spacing
)
else
:
else
:
pyronn_torch
.
cpp_extension
.
call_Cone_Projection_Kernel_Launcher
(
pyronn_torch
.
cpp_extension
.
call_Cone_Projection_Kernel_Launcher
(
s
elf
.
inverse_matrices
,
projection
,
s
elf
.
source_points
,
s
tate
.
inverse_matrices
,
projection
,
s
tate
.
source_points
,
s
elf
.
step_size
,
volume
,
*
s
elf
.
volume_spacing
)
s
tate
.
step_size
,
volume
,
*
s
tate
.
volume_spacing
)
return
projection
,
self
.
state
=
state
if
return_none
:
return
projection
,
None
else
:
return
projection
,
@staticmethod
def
backward
(
self
,
projection_grad
,
state
=
None
,
*
args
):
if
state
is
None
:
state
=
self
.
state
return_none
=
True
else
:
return_none
=
False
def
backward
(
self
,
*
projection_grad
):
projection_grad
=
projection_grad
[
0
]
projection_grad
=
projection_grad
.
float
().
cuda
().
contiguous
()
projection_grad
=
projection_grad
.
float
().
cuda
().
contiguous
()
volume_grad
=
torch
.
zeros
(
self
.
volume_shape
,
device
=
'
cuda
'
,
requires_grad
=
projection_grad
.
requires_grad
)
volume_grad
=
torch
.
zeros
(
state
.
volume_shape
,
device
=
'
cuda
'
,
requires_grad
=
projection_grad
.
requires_grad
)
assert
pyronn_torch
.
cpp_extension
assert
pyronn_torch
.
cpp_extension
pyronn_torch
.
cpp_extension
.
call_Cone_Backprojection3D_Kernel_Launcher
(
pyronn_torch
.
cpp_extension
.
call_Cone_Backprojection3D_Kernel_Launcher
(
s
elf
.
projection_matrices
,
projection_grad
,
s
tate
.
projection_matrices
,
projection_grad
,
s
elf
.
projection_multiplier
,
volume_grad
,
*
s
elf
.
volume_origin
,
s
tate
.
projection_multiplier
,
volume_grad
,
*
s
tate
.
volume_origin
,
*
s
elf
.
volume_spacing
)
*
s
tate
.
volume_spacing
)
return
volume_grad
,
if
return_none
:
return
volume_grad
,
None
else
:
return
volume_grad
,
class
_BackwardProjection
(
torch
.
autograd
.
Function
):
class
_BackwardProjection
(
torch
.
autograd
.
Function
):
__init__
=
_ForwardProjection
.
__init__
backward
=
_ForwardProjection
.
forward
backward
=
_ForwardProjection
.
forward
forward
=
_ForwardProjection
.
backward
forward
=
_ForwardProjection
.
backward
...
@@ -118,29 +141,33 @@ class ConeBeamProjector:
...
@@ -118,29 +141,33 @@ class ConeBeamProjector:
requires_grad
=
requires_grad
).
cuda
()
requires_grad
=
requires_grad
).
cuda
()
def
project_forward
(
self
,
volume
,
step_size
=
1.
,
use_texture
=
True
):
def
project_forward
(
self
,
volume
,
step_size
=
1.
,
use_texture
=
True
):
return
_ForwardProjection
(
self
.
_projection_shape
,
self
.
_volume_shape
,
return
_ForwardProjection
().
apply
(
self
.
_source_points
,
self
.
_inverse_matrices
,
volume
,
self
.
_projection_matrices
,
State
(
self
.
_projection_shape
,
self
.
_volume_shape
,
self
.
_volume_origin
,
self
.
_volume_spacing
,
self
.
_source_points
,
self
.
_inverse_matrices
,
self
.
_projection_multiplier
,
step_size
,
self
.
_projection_matrices
,
self
.
_volume_origin
,
use_texture
).
forward
(
volume
)[
0
]
self
.
_volume_spacing
,
self
.
_projection_multiplier
,
step_size
,
use_texture
))[
0
]
def
project_backward
(
self
,
def
project_backward
(
self
,
projection_stack
,
projection_stack
,
step_size
=
1.
,
step_size
=
1.
,
use_texture
=
True
):
use_texture
=
True
):
return
_BackwardProjection
(
self
.
_projection_shape
,
self
.
_volume_shape
,
return
_BackwardProjection
().
apply
(
self
.
_source_points
,
self
.
_inverse_matrices
,
projection_stack
,
self
.
_projection_matrices
,
State
(
self
.
_projection_shape
,
self
.
_volume_shape
,
self
.
_volume_origin
,
self
.
_volume_spacing
,
self
.
_source_points
,
self
.
_inverse_matrices
,
self
.
_projection_multiplier
,
step_size
,
self
.
_projection_matrices
,
self
.
_volume_origin
,
use_texture
).
forward
(
projection_stack
)[
0
]
self
.
_volume_spacing
,
self
.
_projection_multiplier
,
step_size
,
use_texture
))[
0
]
def
_calc_inverse_matrices
(
self
):
def
_calc_inverse_matrices
(
self
):
if
self
.
_projection_matrices_numpy
is
None
:
if
self
.
_projection_matrices_numpy
is
None
:
return
return
self
.
_projection_matrices
=
torch
.
stack
(
self
.
_projection_matrices
=
torch
.
stack
(
tuple
(
torch
.
from_numpy
(
p
.
astype
(
np
.
float32
))
for
p
in
self
.
_projection_matrices_numpy
)).
cuda
().
contiguous
()
tuple
(
torch
.
from_numpy
(
p
.
astype
(
np
.
float32
))
for
p
in
self
.
_projection_matrices_numpy
)).
cuda
().
contiguous
()
inv_spacing
=
np
.
array
([
1
/
s
for
s
in
reversed
(
self
.
_volume_spacing
)],
inv_spacing
=
np
.
array
([
1
/
s
for
s
in
reversed
(
self
.
_volume_spacing
)],
np
.
float32
)
np
.
float32
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment