-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Idein/bilinear_savannah
add bilinear mode
- Loading branch information
Showing
44 changed files
with
970 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import nnoir | ||
import chainer | ||
from nnoir_chainer import NNOIRFunction | ||
import numpy as np | ||
import util | ||
|
||
|
||
def test_add(): | ||
inputs = [nnoir.Value(b'v0', np.zeros((10, 10)).astype('float32')), | ||
nnoir.Value(b'v1', np.zeros((10, 10)).astype('float32'))] | ||
outputs = [nnoir.Value(b'v2', np.zeros((10, 10)).astype('float32'))] | ||
nodes = inputs + outputs | ||
input_names = [x.name for x in inputs] | ||
output_names = [x.name for x in outputs] | ||
function = nnoir.functions.Add(input_names, output_names) | ||
result = nnoir.NNOIR(b'Add', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function]) | ||
result.dump('add.nnoir') | ||
|
||
x1 = np.random.randn(10, 10).astype('float32') | ||
x2 = np.random.randn(10, 10).astype('float32') | ||
ref = function.run(x1, x2) | ||
with chainer.using_config('train', False): | ||
m = NNOIRFunction('add.nnoir') | ||
y = m(x1, x2) | ||
assert(np.all(abs(y-ref).data < util.epsilon)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import nnoir | ||
import chainer | ||
from nnoir_chainer import NNOIRFunction, Graph | ||
import numpy as np | ||
import util | ||
|
||
|
||
def test_import_add_constant(): | ||
inputs = [nnoir.Value(b'v0', np.zeros((10, 10)).astype('float32'))] | ||
outputs = [nnoir.Value(b'v2', np.zeros((10, 10)).astype('float32'))] | ||
nodes = inputs + outputs | ||
input_names = [x.name for x in inputs] | ||
output_names = [x.name for x in outputs] | ||
function = nnoir.functions.AddConstant(input_names, output_names, value=2.0) | ||
result = nnoir.NNOIR(b'AddConstant', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function]) | ||
result.dump('import_add_constant.nnoir') | ||
|
||
x = np.random.randn(10, 10).astype('float32') | ||
ref = function.run(x) | ||
with chainer.using_config('train', False): | ||
m = NNOIRFunction('import_add_constant.nnoir') | ||
y = m(x) | ||
assert(np.all(abs(y-ref) < util.epsilon)) | ||
|
||
|
||
def test_export_add_constant(): | ||
|
||
class Model(chainer.Link): | ||
def __call__(self, x): | ||
return x + 1.0 | ||
|
||
with chainer.using_config('train', False): | ||
model = Model() | ||
x = chainer.Variable(np.array([0, 1, 2]).astype(np.float32)) | ||
y = model(x) | ||
g = Graph(model, (x,), (y,)) | ||
result = g.to_nnoir() | ||
with open('export_add_constant.nnoir', 'w') as f: | ||
f.buffer.write(result) | ||
|
||
m = NNOIRFunction('export_add_constant.nnoir') | ||
z = m(x) | ||
assert(np.all(abs(z.data-y.data) < util.epsilon)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import nnoir | ||
import chainer | ||
from nnoir_chainer import NNOIRFunction | ||
import numpy as np | ||
import util | ||
import chainer.functions as F | ||
|
||
|
||
def test_average_pooling_2d(): | ||
ksize = (2, 3) | ||
stride = (1, 2) | ||
pad = (0, 1) | ||
|
||
inputs = [nnoir.Value(b'v0', np.zeros((2, 3, 4, 5)).astype('float32'))] | ||
outputs = [nnoir.Value(b'v2', np.zeros((2, 3, 3, 3)).astype('float32'))] | ||
nodes = inputs + outputs | ||
input_names = [x.name for x in inputs] | ||
output_names = [x.name for x in outputs] | ||
function = nnoir.functions.AveragePooling2D(input_names, output_names, | ||
kernel=list(ksize), | ||
stride=list(stride), | ||
pad_h=[pad[0], pad[0]+stride[0]-1], | ||
pad_w=[pad[1], pad[1]+stride[1]-1], | ||
count_exclude_pad=False) | ||
result = nnoir.NNOIR(b'AveragePooling2D', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function]) | ||
result.dump('average_pooling_2d.nnoir') | ||
|
||
x = np.random.randn(2, 3, 4, 5).astype('float32') | ||
ref = function.run(x) | ||
with chainer.using_config('train', False): | ||
m = NNOIRFunction('average_pooling_2d.nnoir') | ||
y = m(x) | ||
assert(np.all(abs(y-ref).data < util.epsilon)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import nnoir | ||
import chainer | ||
from nnoir_chainer import NNOIRFunction | ||
import numpy as np | ||
import util | ||
import chainer.links as L | ||
|
||
|
||
def test_batch_normalization(): | ||
shape = (2, 3, 4, 5) | ||
channel = 3 | ||
gamma = np.zeros(channel, dtype=np.float32) | ||
beta = np.zeros(channel, dtype=np.float32) | ||
avg_mean = np.zeros(channel, dtype=np.float32) | ||
avg_var = np.zeros(channel, dtype=np.float32) | ||
eps = 2e-05 | ||
gamma[:] = 0.9 | ||
beta[:] = 0.1 | ||
avg_mean[:] = 0.2 | ||
avg_var[:] = 0.8 | ||
|
||
inputs = [nnoir.Value(b'v0', np.zeros(shape).astype('float32'))] | ||
outputs = [nnoir.Value(b'v2', np.zeros(shape).astype('float32'))] | ||
nodes = inputs + outputs | ||
input_names = [x.name for x in inputs] | ||
output_names = [x.name for x in outputs] | ||
function = nnoir.functions.BatchNormalization(input_names, output_names, | ||
eps=eps, | ||
avg_mean=avg_mean, | ||
avg_var=avg_var, | ||
gamma=gamma, | ||
beta=beta) | ||
result = nnoir.NNOIR(b'BatchNormalization', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function]) | ||
result.dump('batch_normalization.nnoir') | ||
|
||
x = np.random.randn(*shape).astype('float32') | ||
ref = function.run(x) | ||
with chainer.using_config('train', False): | ||
m = NNOIRFunction('batch_normalization.nnoir') | ||
y = m(x) | ||
assert(np.all(abs(y-ref).data < util.epsilon)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import nnoir | ||
import chainer | ||
from nnoir_chainer import NNOIRFunction | ||
import numpy as np | ||
import util | ||
|
||
|
||
def test_bias(): | ||
inputs = [nnoir.Value(b'v0', np.zeros((2, 3, 4, 5)).astype('float32'))] | ||
outputs = [nnoir.Value(b'v2', np.zeros((2, 3, 4, 5)).astype('float32'))] | ||
nodes = inputs + outputs | ||
input_names = [x.name for x in inputs] | ||
output_names = [x.name for x in outputs] | ||
b = np.random.randn(3).astype('float32') | ||
params = {'axis': 1, | ||
'b': b} | ||
function = nnoir.functions.Bias(input_names, output_names, **params) | ||
result = nnoir.NNOIR(b'Bias', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function]) | ||
result.dump('bias.nnoir') | ||
|
||
x = np.random.randn(2, 3, 4, 5).astype('float32') | ||
ref = function.run(x) | ||
with chainer.using_config('train', False): | ||
m = NNOIRFunction('bias.nnoir') | ||
y = m(x) | ||
assert(np.all(abs(y-ref).data < util.epsilon)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import nnoir | ||
import chainer | ||
from nnoir_chainer import NNOIRFunction | ||
import numpy as np | ||
import sys | ||
import chainer.functions as F | ||
import util | ||
|
||
|
||
def test_bilinear_2d_without_mode(): | ||
in_shape = (2, 3, 9, 10) | ||
out_shape = (2, 3, 4, 5) | ||
inputs = [nnoir.Value(b'v0', np.zeros(in_shape).astype('float32'))] | ||
outputs = [nnoir.Value(b'v2', np.zeros(out_shape).astype('float32'))] | ||
nodes = inputs + outputs | ||
input_names = [x.name for x in inputs] | ||
output_names = [x.name for x in outputs] | ||
function = nnoir.functions.Bilinear2D(input_names, output_names, size=(out_shape[2], out_shape[3])) | ||
|
||
result = nnoir.NNOIR(b'Bilinear2D', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function]) | ||
result.dump('bilinear_2d_without_mode.nnoir') | ||
|
||
x = np.random.randn(*in_shape).astype('float32') | ||
ref = function.run(x) | ||
with chainer.using_config('train', False): | ||
m = NNOIRFunction('bilinear_2d_without_mode.nnoir') | ||
y = m(x) | ||
assert(np.all(abs(y-ref).data < util.epsilon)) | ||
|
||
|
||
def test_bilinear_2d_with_mode(): | ||
in_shape = (2, 3, 9, 10) | ||
out_shape = (2, 3, 4, 5) | ||
inputs = [nnoir.Value(b'v0', np.zeros(in_shape).astype('float32'))] | ||
outputs = [nnoir.Value(b'v2', np.zeros(out_shape).astype('float32'))] | ||
nodes = inputs + outputs | ||
input_names = [x.name for x in inputs] | ||
output_names = [x.name for x in outputs] | ||
function = nnoir.functions.Bilinear2D(input_names, output_names, size=(out_shape[2], out_shape[3]), mode=b'align_corners') | ||
|
||
result = nnoir.NNOIR(b'Bilinear2D', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function]) | ||
result.dump('bilinear_2d_with_mode.nnoir') | ||
|
||
x = np.random.randn(*in_shape).astype('float32') | ||
ref = function.run(x) | ||
with chainer.using_config('train', False): | ||
m = NNOIRFunction('bilinear_2d_with_mode.nnoir') | ||
y = m(x) | ||
assert(np.all(abs(y-ref).data < util.epsilon)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import nnoir | ||
import chainer | ||
from nnoir_chainer import NNOIRFunction | ||
import numpy as np | ||
import sys | ||
import chainer.functions as F | ||
import util | ||
|
||
|
||
def test_broadcast(): | ||
in_shape = (1, 1, 4, 5) | ||
out_shape = (2, 3, 4, 5) | ||
inputs = [nnoir.Value(b'v0', np.zeros(in_shape).astype('float32'))] | ||
outputs = [nnoir.Value(b'v2', np.zeros(out_shape).astype('float32'))] | ||
nodes = inputs + outputs | ||
input_names = [x.name for x in inputs] | ||
output_names = [x.name for x in outputs] | ||
function = nnoir.functions.BroadcastTo(input_names, output_names, shape=out_shape) | ||
|
||
result = nnoir.NNOIR(b'BroadcastTo', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function]) | ||
result.dump('broadcast.nnoir') | ||
|
||
x = np.random.randn(*in_shape).astype('float32') | ||
ref = function.run(x) | ||
with chainer.using_config('train', False): | ||
m = NNOIRFunction('broadcast.nnoir') | ||
y = m(x) | ||
assert(np.all(abs(y-ref).data < util.epsilon)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import nnoir | ||
import chainer | ||
from nnoir_chainer import NNOIRFunction | ||
import numpy as np | ||
import util | ||
|
||
|
||
def test_clipped_relu(): | ||
inputs = [nnoir.Value(b'v0', np.zeros((10, 10)).astype('float32'))] | ||
outputs = [nnoir.Value(b'v2', np.zeros((10, 10)).astype('float32'))] | ||
nodes = inputs + outputs | ||
input_names = [x.name for x in inputs] | ||
output_names = [x.name for x in outputs] | ||
function = nnoir.functions.ClippedReLU(input_names, output_names, upper=40.0) | ||
result = nnoir.NNOIR(b'ClippedReLU', b'nnoir2chainer_test', '0.1', input_names, output_names, nodes, [function]) | ||
result.dump('clipped_relu.nnoir') | ||
|
||
x = np.random.randn(10, 10).astype('float32') | ||
ref = function.run(x) | ||
with chainer.using_config('train', False): | ||
m = NNOIRFunction('clipped_relu.nnoir') | ||
y = m(x) | ||
assert(np.all(abs(y-ref).data < util.epsilon)) |
Oops, something went wrong.