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
5a42d7a9
Commit
5a42d7a9
authored
Mar 07, 2018
by
Benjamin Thomas Graham
Browse files
tidy
parent
c54569a8
Changes
27
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
82 additions
and
72 deletions
+82
-72
examples/Assamese_handwriting/data.py
examples/Assamese_handwriting/data.py
+4
-4
examples/Chinese_handwriting/DenseNet-A-VSC.py
examples/Chinese_handwriting/DenseNet-A-VSC.py
+2
-2
examples/Chinese_handwriting/ResNet-A-VSC.py
examples/Chinese_handwriting/ResNet-A-VSC.py
+2
-2
examples/Chinese_handwriting/VGG-C.py
examples/Chinese_handwriting/VGG-C.py
+32
-27
examples/Chinese_handwriting/VGG-Cplus.py
examples/Chinese_handwriting/VGG-Cplus.py
+32
-27
examples/Chinese_handwriting/data.py
examples/Chinese_handwriting/data.py
+4
-4
examples/hello-world.py
examples/hello-world.py
+6
-6
No files found.
examples/Assamese_handwriting/data.py
View file @
5a42d7a9
...
...
@@ -40,7 +40,7 @@ def train(spatial_size, Scale, precomputeSize):
v
=
torch
.
FloatTensor
([
1
,
0
,
0
])
np_random
=
np
.
random
.
RandomState
(
tbl
[
'idx'
])
for
char
in
tbl
[
'input'
]:
inp
.
add
S
ample
()
inp
.
add
_s
ample
()
m
=
torch
.
eye
(
2
)
r
=
np_random
.
randint
(
1
,
3
)
alpha
=
random
.
uniform
(
-
0.2
,
0.2
)
...
...
@@ -80,7 +80,7 @@ def train(spatial_size, Scale, precomputeSize):
# for j in np.arange(0,1,1/l):
# p[0]=math.floor(x1*j+x2*(1-j))
# p[1]=math.floor(y1*j+y2*(1-j))
# inp.set
L
ocation(p,v,False)
# inp.set
_l
ocation(p,v,False)
###############################################################
inp
.
precomputeMetadata
(
precomputeSize
)
return
{
'input'
:
inp
,
'target'
:
torch
.
LongTensor
(
tbl
[
'target'
])
-
1
}
...
...
@@ -107,7 +107,7 @@ def val(spatial_size, Scale, precomputeSize):
p
=
torch
.
LongTensor
(
2
)
v
=
torch
.
FloatTensor
([
1
,
0
,
0
])
for
char
in
tbl
[
'input'
]:
inp
.
add
S
ample
()
inp
.
add
_s
ample
()
for
stroke
in
char
:
stroke
=
stroke
.
float
()
*
(
Scale
-
0.01
)
/
255
-
0.5
*
(
Scale
-
0.01
)
stroke
+=
center
.
expand_as
(
stroke
)
...
...
@@ -128,5 +128,5 @@ def val(spatial_size, Scale, precomputeSize):
return
iter
def
get
I
terators
(
*
args
):
def
get
_i
terators
(
*
args
):
return
{
'train'
:
train
(
*
args
),
'val'
:
val
(
*
args
)}
examples/Chinese_handwriting/DenseNet-A-VSC.py
View file @
5a42d7a9
...
...
@@ -7,7 +7,7 @@
import
torch
import
torch.legacy.nn
as
nn
import
sparseconvnet.legacy
as
scn
from
data
import
get
I
terators
from
data
import
get
_i
terators
# Use the GPU if there is one, otherwise CPU
dtype
=
'torch.cuda.FloatTensor'
if
torch
.
cuda
.
is_available
()
else
'torch.FloatTensor'
...
...
@@ -38,7 +38,7 @@ print(model)
spatial_size
=
sparseModel
.
suggestInputSize
(
torch
.
LongTensor
([
1
,
1
]))
print
(
'input spatial size'
,
spatial_size
)
dataset
=
get
I
terators
(
spatial_size
,
64
,
2
)
dataset
=
get
_i
terators
(
spatial_size
,
64
,
2
)
scn
.
ClassificationTrainValidate
(
model
,
dataset
,
{
'nEpochs'
:
100
,
'initial_LR'
:
0.1
,
'LR_decay'
:
0.05
,
'weightDecay'
:
1e-4
})
examples/Chinese_handwriting/ResNet-A-VSC.py
View file @
5a42d7a9
...
...
@@ -7,7 +7,7 @@
import
torch
import
torch.legacy.nn
as
nn
import
sparseconvnet.legacy
as
scn
from
data
import
get
I
terators
from
data
import
get
_i
terators
# Use the GPU if there is one, otherwise CPU
dtype
=
'torch.cuda.FloatTensor'
if
torch
.
cuda
.
is_available
()
else
'torch.FloatTensor'
...
...
@@ -34,7 +34,7 @@ print(model)
spatial_size
=
sparseModel
.
suggestInputSize
(
torch
.
LongTensor
([
1
,
1
]))
print
(
'input spatial size'
,
spatial_size
)
dataset
=
get
I
terators
(
spatial_size
,
63
,
3
)
dataset
=
get
_i
terators
(
spatial_size
,
63
,
3
)
scn
.
ClassificationTrainValidate
(
model
,
dataset
,
{
'nEpochs'
:
100
,
'initial_LR'
:
0.1
,
'LR_decay'
:
0.05
,
'weightDecay'
:
1e-4
})
examples/Chinese_handwriting/VGG-C.py
View file @
5a42d7a9
...
...
@@ -5,35 +5,40 @@
# LICENSE file in the root directory of this source tree.
import
torch
import
torch.legacy.nn
as
nn
import
sparseconvnet.legacy
as
scn
from
data
import
getIterators
# Use the GPU if there is one, otherwise CPU
dtype
=
'torch.cuda.FloatTensor'
if
torch
.
cuda
.
is_available
()
else
'torch.FloatTensor'
import
torch.nn
as
nn
import
sparseconvnet
as
scn
from
data
import
get_iterators
# two-dimensional SparseConvNet
model
=
nn
.
Sequential
()
sparseModel
=
scn
.
Sequential
()
denseModel
=
nn
.
Sequential
()
model
.
add
(
sparseModel
).
add
(
denseModel
)
sparseModel
.
add
(
scn
.
SparseVggNet
(
2
,
3
,
[
class
Model
(
nn
.
Module
):
def
__init__
(
self
):
nn
.
Module
.
__init__
(
self
)
self
.
sparseModel
=
scn
.
SparseVggNet
(
2
,
3
,
[
[
'C'
,
16
],
[
'C'
,
16
],
'MP'
,
[
'C'
,
32
],
[
'C'
,
32
],
'MP'
,
[
'C'
,
48
],
[
'C'
,
48
],
'MP'
,
[
'C'
,
64
],
[
'C'
,
64
],
'MP'
,
[
'C'
,
96
],
[
'C'
,
96
]]))
sparseModel
.
add
(
scn
.
Convolution
(
2
,
96
,
128
,
3
,
2
,
False
))
sparseModel
.
add
(
scn
.
BatchNormReLU
(
128
))
sparseModel
.
add
(
scn
.
SparseToDense
(
2
))
denseModel
.
add
(
nn
.
View
(
-
1
,
128
))
denseModel
.
add
(
nn
.
Linear
(
128
,
3755
))
model
.
type
(
dtype
)
print
(
model
)
[
'C'
,
96
],
[
'C'
,
96
]]
).
add
(
scn
.
Convolution
(
2
,
96
,
128
,
3
,
2
,
False
)
).
add
(
scn
.
BatchNormReLU
(
128
)
).
add
(
scn
.
SparseToDense
(
2
,
128
))
self
.
linear
=
nn
.
Linear
(
128
,
3755
)
def
forward
(
self
,
x
):
x
=
self
.
sparseModel
(
x
)
x
=
x
.
view
(
-
1
,
128
)
x
=
self
.
linear
(
x
)
return
x
spatial_size
=
sparseModel
.
suggestInputSize
(
torch
.
LongTensor
([
1
,
1
]))
print
(
'input spatial size'
,
spatial_size
)
dataset
=
getIterators
(
spatial_size
,
63
,
3
)
model
=
Model
()
spatial_size
=
model
.
sparseModel
.
input_spatial_size
(
torch
.
LongTensor
([
1
,
1
]))
print
(
'Input spatial size:'
,
spatial_size
)
dataset
=
get_iterators
(
spatial_size
,
63
,
3
)
scn
.
ClassificationTrainValidate
(
model
,
dataset
,
{
'nEpochs'
:
100
,
'initial_LR'
:
0.1
,
'LR_decay'
:
0.05
,
'weightDecay'
:
1e-4
})
{
'n_epochs'
:
100
,
'initial_lr'
:
0.1
,
'lr_decay'
:
0.05
,
'weight_decay'
:
1e-4
,
'use_gpu'
:
torch
.
cuda
.
is_available
(),
'check_point'
:
True
,
})
examples/Chinese_handwriting/VGG-Cplus.py
View file @
5a42d7a9
...
...
@@ -5,35 +5,40 @@
# LICENSE file in the root directory of this source tree.
import
torch
import
torch.legacy.nn
as
nn
import
sparseconvnet.legacy
as
scn
from
data
import
getIterators
# Use the GPU if there is one, otherwise CPU
dtype
=
'torch.cuda.FloatTensor'
if
torch
.
cuda
.
is_available
()
else
'torch.FloatTensor'
import
torch.nn
as
nn
import
sparseconvnet
as
scn
from
data
import
get_iterators
# two-dimensional SparseConvNet
model
=
nn
.
Sequential
()
sparseModel
=
scn
.
Sequential
()
denseModel
=
nn
.
Sequential
()
model
.
add
(
sparseModel
).
add
(
denseModel
)
sparseModel
.
add
(
scn
.
SparseVggNet
(
2
,
3
,
[
class
Model
(
nn
.
Module
):
def
__init__
(
self
):
nn
.
Module
.
__init__
(
self
)
self
.
sparseModel
=
scn
.
SparseVggNet
(
2
,
3
,
[
[
'C'
,
16
,
8
],
[
'C'
,
16
,
8
],
'MP'
,
[
'C'
,
32
,
8
],
[
'C'
,
32
,
8
],
'MP'
,
[
'C'
,
48
,
16
],
[
'C'
,
48
,
16
],
'MP'
,
[
'C'
,
64
,
16
],
[
'C'
,
64
,
16
],
'MP'
,
[
'C'
,
96
,
16
],
[
'C'
,
96
,
16
]]))
sparseModel
.
add
(
scn
.
Convolution
(
2
,
96
+
16
,
128
,
3
,
2
,
False
))
sparseModel
.
add
(
scn
.
BatchNormReLU
(
128
))
sparseModel
.
add
(
scn
.
SparseToDense
(
2
))
denseModel
.
add
(
nn
.
View
(
-
1
,
128
))
denseModel
.
add
(
nn
.
Linear
(
128
,
3755
))
model
.
type
(
dtype
)
print
(
model
)
[
'C'
,
96
,
16
],
[
'C'
,
96
,
16
]]
).
add
(
scn
.
Convolution
(
2
,
112
,
128
,
3
,
2
,
False
)
).
add
(
scn
.
BatchNormReLU
(
128
)
).
add
(
scn
.
SparseToDense
(
2
,
128
))
self
.
linear
=
nn
.
Linear
(
128
,
3755
)
def
forward
(
self
,
x
):
x
=
self
.
sparseModel
(
x
)
x
=
x
.
view
(
-
1
,
128
)
x
=
self
.
linear
(
x
)
return
x
spatial_size
=
sparseModel
.
suggestInputSize
(
torch
.
LongTensor
([
1
,
1
]))
print
(
'input spatial size'
,
spatial_size
)
dataset
=
getIterators
(
spatial_size
,
63
,
3
)
model
=
Model
()
spatial_size
=
model
.
sparseModel
.
input_spatial_size
(
torch
.
LongTensor
([
1
,
1
]))
print
(
'Input spatial size:'
,
spatial_size
)
dataset
=
get_iterators
(
spatial_size
,
63
,
3
)
scn
.
ClassificationTrainValidate
(
model
,
dataset
,
{
'nEpochs'
:
100
,
'initial_LR'
:
0.1
,
'LR_decay'
:
0.05
,
'weightDecay'
:
1e-4
})
{
'n_epochs'
:
100
,
'initial_lr'
:
0.1
,
'lr_decay'
:
0.05
,
'weight_decay'
:
1e-4
,
'use_gpu'
:
torch
.
cuda
.
is_available
(),
'check_point'
:
True
,
})
examples/Chinese_handwriting/data.py
View file @
5a42d7a9
...
...
@@ -39,7 +39,7 @@ def train(spatial_size, Scale, precomputeSize):
p
=
torch
.
LongTensor
(
2
)
v
=
torch
.
FloatTensor
([
1
,
0
,
0
])
for
char
in
tbl
[
'input'
]:
inp
.
add
S
ample
()
inp
.
add
_s
ample
()
for
stroke
in
char
:
stroke
=
stroke
.
float
()
*
(
Scale
-
0.01
)
/
255
-
0.5
*
(
Scale
-
0.01
)
stroke
+=
center
.
expand_as
(
stroke
)
...
...
@@ -66,7 +66,7 @@ def train(spatial_size, Scale, precomputeSize):
# for j in numpy.arange(0,1,1/l):
# p[0]=math.floor(x1*j+x2*(1-j))
# p[1]=math.floor(y1*j+y2*(1-j))
# inp.set
L
ocation(p,v,False)
# inp.set
_l
ocation(p,v,False)
###############################################################
inp
.
precomputeMetadata
(
precomputeSize
)
return
{
'input'
:
inp
,
'target'
:
torch
.
LongTensor
(
tbl
[
'target'
])}
...
...
@@ -93,7 +93,7 @@ def val(spatial_size, Scale, precomputeSize):
p
=
torch
.
LongTensor
(
2
)
v
=
torch
.
FloatTensor
([
1
,
0
,
0
])
for
char
in
tbl
[
'input'
]:
inp
.
add
S
ample
()
inp
.
add
_s
ample
()
for
stroke
in
char
:
stroke
=
stroke
.
float
()
*
(
Scale
-
0.01
)
/
255
-
0.5
*
(
Scale
-
0.01
)
stroke
+=
center
.
expand_as
(
stroke
)
...
...
@@ -114,5 +114,5 @@ def val(spatial_size, Scale, precomputeSize):
return
iter
def
get
I
terators
(
*
args
):
def
get
_i
terators
(
*
args
):
return
{
'train'
:
train
(
*
args
),
'val'
:
val
(
*
args
)}
examples/hello-world.py
View file @
5a42d7a9
...
...
@@ -36,17 +36,17 @@ msg = [
" X X X X X X X X X X X X X X X X X X "
,
" X X XXX XXX XXX XX X X XX X X XXX XXX "
]
# Add a sample using set
L
ocation
input
.
add
S
ample
()
# Add a sample using set
_l
ocation
input
.
add
_s
ample
()
for
y
,
line
in
enumerate
(
msg
):
for
x
,
c
in
enumerate
(
line
):
if
c
==
'X'
:
location
=
torch
.
LongTensor
([
y
,
x
])
featureVector
=
torch
.
FloatTensor
([
1
])
input
.
set
L
ocation
(
location
,
featureVector
,
0
)
input
.
set
_l
ocation
(
location
,
featureVector
,
0
)
# Add a sample using set
L
ocations
input
.
add
S
ample
()
# Add a sample using set
_l
ocations
input
.
add
_s
ample
()
locations
=
[]
features
=
[]
for
y
,
line
in
enumerate
(
msg
):
...
...
@@ -56,7 +56,7 @@ for y, line in enumerate(msg):
features
.
append
([
1
])
locations
=
torch
.
LongTensor
(
locations
)
features
=
torch
.
FloatTensor
(
features
)
input
.
set
L
ocations
(
locations
,
features
,
0
)
input
.
set
_l
ocations
(
locations
,
features
,
0
)
# Optional: allow metadata preprocessing to be done in batch preparation threads
# to improve GPU utilization.
...
...
Prev
1
2
Next
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