Skip to content

Commit

Permalink
Merge pull request #12 from Idein/bilinear_savannah
Browse files Browse the repository at this point in the history
add bilinear mode
  • Loading branch information
notogawa authored May 17, 2019
2 parents b10a77c + ca70fd4 commit 98ab283
Show file tree
Hide file tree
Showing 44 changed files with 970 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
name: Test nnoir-chainer library
command: |
cd nnoir-chainer
nosetests3 -v nnoir_chainer
nosetests3 -v test
- run:
name: Test nnoir-chainer example
command: |
Expand Down
5 changes: 4 additions & 1 deletion nnoir-chainer/nnoir_chainer/converter/bilinear_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
class ConvertBilinear2D():

def __init__(self, function, inputs, outputs):
self.f = lambda x: F.resize_images(x, tuple(function.params['size']))
if 'mode' not in function.params or function.params['mode'] == b'align_corners':
self.f = lambda x: F.resize_images(x, tuple(function.params['size']))
else:
raise Exception('chainer has no correspond function Bilinear({})'.format(function.params['mode']))

def __call__(self, x):
return self.f(x)
3 changes: 2 additions & 1 deletion nnoir-chainer/nnoir_chainer/functions/array/broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@


def to_nnoir_node(self, inputs, outputs):
return NNOIR.BroadcastTo([x.name for x in inputs], [x.name for x in outputs])
output = outputs[0]
return NNOIR.BroadcastTo([x.name for x in inputs], [x.name for x in outputs], shape=outputs.shape)


BroadcastTo.to_nnoir_node = to_nnoir_node
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def to_nnoir_node(self, inputs, outputs):
return NNOIR.Bilinear2D([x.name for x in inputs], [x.name for x in outputs], size=(self.out_H, self.out_W))
return NNOIR.Bilinear2D([x.name for x in inputs], [x.name for x in outputs], size=(self.out_H, self.out_W), mode='align_corners')


ResizeImages.to_nnoir_node = to_nnoir_node
27 changes: 24 additions & 3 deletions nnoir-chainer/nnoir_chainer/tests/test_bilinear_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import util


def test_bilinear_2d():
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'))]
Expand All @@ -18,11 +18,32 @@ def test_bilinear_2d():
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.nnoir')
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.nnoir')
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))
4 changes: 2 additions & 2 deletions nnoir-chainer/nnoir_chainer/tests/test_broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ def test_broadcast():
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)
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, out_shape)
ref = function.run(x)
with chainer.using_config('train', False):
m = NNOIRFunction('broadcast.nnoir')
y = m(x)
Expand Down
25 changes: 25 additions & 0 deletions nnoir-chainer/test/test_add.py
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))
43 changes: 43 additions & 0 deletions nnoir-chainer/test/test_add_constant.py
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))
33 changes: 33 additions & 0 deletions nnoir-chainer/test/test_average_pooling_2d.py
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))
41 changes: 41 additions & 0 deletions nnoir-chainer/test/test_batch_normalization.py
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))
26 changes: 26 additions & 0 deletions nnoir-chainer/test/test_bias.py
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))
49 changes: 49 additions & 0 deletions nnoir-chainer/test/test_bilinear_2d.py
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))
28 changes: 28 additions & 0 deletions nnoir-chainer/test/test_broadcast.py
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))
23 changes: 23 additions & 0 deletions nnoir-chainer/test/test_clipped_relu.py
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))
Loading

0 comments on commit 98ab283

Please sign in to comment.