Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
SparseConvNet
Commits
159e5f9a
Commit
159e5f9a
authored
Jul 22, 2020
by
Benjamin Thomas Graham
Browse files
example
parent
aa071d62
Changes
3
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
51 deletions
+80
-51
examples/hello-world.py
examples/hello-world.py
+29
-1
sparseconvnet/SCN/sparseconvnet_cuda.cpp
sparseconvnet/SCN/sparseconvnet_cuda.cpp
+43
-43
sparseconvnet/utils.py
sparseconvnet/utils.py
+8
-7
No files found.
examples/hello-world.py
View file @
159e5f9a
...
...
@@ -32,6 +32,7 @@ model = scn.Sequential().add(
# output will be 10x10
inputSpatialSize
=
model
.
input_spatial_size
(
torch
.
LongTensor
([
10
,
10
]))
input_layer
=
scn
.
InputLayer
(
2
,
inputSpatialSize
)
bl_input_layer
=
scn
.
BLInputLayer
(
2
,
inputSpatialSize
)
msgs
=
[[
" X X XXX X X XX X X XX XXX X XXX "
,
" X X X X X X X X X X X X X X X X "
,
...
...
@@ -46,7 +47,7 @@ msgs = [[" X X XXX X X XX X X XX XXX X XXX ",
" X X XXXX x x x x xxxx x "
,]]
# Create Nx3 and Nx1 vectors to encode the messages above:
# Create Nx3 and Nx1 vectors to encode the messages above
using InputLayer
:
locations
=
[]
features
=
[]
for
batchIdx
,
msg
in
enumerate
(
msgs
):
...
...
@@ -65,3 +66,30 @@ output = model(input)
# Output is 2x32x10x10: our minibatch has 2 samples, the network has 32 output
# feature planes, and 10x10 is the spatial size of the output.
print
(
'Output SparseConvNetTensor:'
,
output
)
# Alternatively:
# Create Nx3 and Nx1 vectors to encode the messages above using BLInputLayer:
batch
=
[]
for
batchIdx
,
msg
in
enumerate
(
msgs
):
l
,
f
=
[],[]
for
y
,
line
in
enumerate
(
msg
):
for
x
,
c
in
enumerate
(
line
):
if
c
==
'X'
:
l
.
append
([
y
,
x
])
#Locations
f
.
append
([
1
])
#Features
batch
.
append
([
torch
.
LongTensor
(
l
),
torch
.
FloatTensor
(
f
)])
batch
=
scn
.
prepare_BLInput
(
batch
)
batch
[
1
]
=
batch
[
1
].
to
(
device
)
input
=
bl_input_layer
(
batch
)
print
(
'Input SparseConvNetTensor:'
,
input
)
output
=
model
(
input
)
# Output is 2x32x10x10: our minibatch has 2 samples, the network has 32 output
# feature planes, and 10x10 is the spatial size of the output.
print
(
'Output SparseConvNetTensor:'
,
output
)
sparseconvnet/SCN/sparseconvnet_cuda.cpp
View file @
159e5f9a
This diff is collapsed.
Click to expand it.
sparseconvnet/utils.py
View file @
159e5f9a
...
...
@@ -124,14 +124,15 @@ def batch_location_tensors(location_tensors):
a
.
append
(
pad_with_batch_idx
(
lt
,
batch_idx
))
return
torch
.
cat
(
a
,
0
)
def
prepare_BLInput
(
l
,
f
):
def
prepare_BLInput
(
batch
):
with
torch
.
no_grad
():
n
=
max
([
x
.
size
(
0
)
for
x
in
l
])
L
=
torch
.
empty
(
len
(
l
),
n
,
l
[
0
].
size
(
1
),
dtype
=
torch
.
int64
).
fill_
(
-
1
)
F
=
torch
.
zeros
(
len
(
l
),
n
,
f
[
0
].
size
(
1
))
for
i
,
(
ll
,
ff
)
in
enumerate
(
zip
(
l
,
f
)):
L
[
i
,:
ll
.
size
(
0
),:].
copy_
(
ll
)
F
[
i
,:
ff
.
size
(
0
),:].
copy_
(
ff
)
n
=
max
([
l
.
size
(
0
)
for
l
,
f
in
batch
])
l
,
f
=
batch
[
0
]
L
=
torch
.
empty
(
len
(
batch
),
n
,
l
.
size
(
1
),
dtype
=
torch
.
int64
).
fill_
(
-
1
)
F
=
torch
.
zeros
(
len
(
batch
),
n
,
f
.
size
(
1
))
for
i
,
(
l
,
f
)
in
enumerate
(
batch
):
L
[
i
,:
l
.
size
(
0
),:].
copy_
(
l
)
F
[
i
,:
f
.
size
(
0
),:].
copy_
(
f
)
return
[
L
,
F
]
def
checkpoint_restore
(
model
,
exp_name
,
name2
,
use_cuda
=
True
,
epoch
=
0
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment