2017-04-08 00:29:17 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
from __future__ import division
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
from __future__ import unicode_literals
|
2017-04-23 10:23:37 +00:00
|
|
|
from caffe2.python import workspace, brew
|
2017-04-23 10:23:37 +00:00
|
|
|
from caffe2.python.model_helper import ModelHelper
|
2017-04-08 00:29:17 +00:00
|
|
|
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
import unittest
|
2017-04-08 00:29:17 +00:00
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
2017-04-23 10:23:37 +00:00
|
|
|
class BrewTest(unittest.TestCase):
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
def setUp(self):
|
2017-04-08 00:29:17 +00:00
|
|
|
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
def myhelper(model, val=-1):
|
|
|
|
|
return val
|
|
|
|
|
|
2017-04-23 10:23:37 +00:00
|
|
|
if not brew.has_helper(myhelper):
|
|
|
|
|
brew.Register(myhelper)
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
self.myhelper = myhelper
|
|
|
|
|
|
|
|
|
|
def myhelper2(model, val=-1):
|
|
|
|
|
return val
|
|
|
|
|
|
2017-04-23 10:23:37 +00:00
|
|
|
if not brew.has_helper(myhelper2):
|
|
|
|
|
brew.Register(myhelper2)
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
self.myhelper2 = myhelper2
|
|
|
|
|
|
|
|
|
|
def test_dropout(self):
|
|
|
|
|
p = 0.2
|
|
|
|
|
X = np.ones((100, 100)).astype(np.float32) - p
|
2017-04-08 00:29:17 +00:00
|
|
|
workspace.FeedBlob("x", X)
|
2017-04-23 10:23:37 +00:00
|
|
|
model = ModelHelper(name="test_model")
|
2017-04-23 10:23:37 +00:00
|
|
|
brew.dropout(model, "x", "out")
|
2017-04-08 00:29:17 +00:00
|
|
|
workspace.RunNetOnce(model.param_init_net)
|
|
|
|
|
workspace.RunNetOnce(model.net)
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
out = workspace.FetchBlob("out")
|
|
|
|
|
self.assertLess(abs(out.mean() - (1 - p)), 0.05)
|
2017-04-08 00:29:17 +00:00
|
|
|
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
def test_fc(self):
|
|
|
|
|
m, n, k = (15, 15, 15)
|
2017-04-08 00:29:17 +00:00
|
|
|
X = np.random.rand(m, k).astype(np.float32) - 0.5
|
|
|
|
|
|
|
|
|
|
workspace.FeedBlob("x", X)
|
2017-04-23 10:23:37 +00:00
|
|
|
model = ModelHelper(name="test_model")
|
2017-04-23 10:23:37 +00:00
|
|
|
out = brew.fc(model, "x", "out_1", k, n)
|
|
|
|
|
out = brew.packed_fc(model, out, "out_2", n, n)
|
|
|
|
|
out = brew.fc_decomp(model, out, "out_3", n, n)
|
|
|
|
|
out = brew.fc_prune(model, out, "out_4", n, n)
|
2017-04-08 00:29:17 +00:00
|
|
|
|
|
|
|
|
workspace.RunNetOnce(model.param_init_net)
|
|
|
|
|
workspace.RunNetOnce(model.net)
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
|
|
|
|
|
def test_arg_scope(self):
|
|
|
|
|
myhelper = self.myhelper
|
|
|
|
|
myhelper2 = self.myhelper2
|
|
|
|
|
n = 15
|
2017-04-23 10:23:37 +00:00
|
|
|
with brew.arg_scope([myhelper], val=n):
|
|
|
|
|
res = brew.myhelper(None)
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
self.assertEqual(n, res)
|
|
|
|
|
|
2017-04-23 10:23:37 +00:00
|
|
|
with brew.arg_scope([myhelper, myhelper2], val=n):
|
|
|
|
|
res1 = brew.myhelper(None)
|
|
|
|
|
res2 = brew.myhelper2(None)
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
self.assertEqual([n, n], [res1, res2])
|
|
|
|
|
|
|
|
|
|
def test_arg_scope_single(self):
|
|
|
|
|
X = np.random.rand(64, 3, 32, 32).astype(np.float32) - 0.5
|
|
|
|
|
|
|
|
|
|
workspace.FeedBlob("x", X)
|
2017-04-23 10:23:37 +00:00
|
|
|
model = ModelHelper(name="test_model")
|
2017-04-23 10:23:37 +00:00
|
|
|
with brew.arg_scope(
|
|
|
|
|
brew.conv,
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
stride=2,
|
|
|
|
|
pad=2,
|
|
|
|
|
weight_init=('XavierFill', {}),
|
|
|
|
|
bias_init=('ConstantFill', {})
|
|
|
|
|
):
|
2017-04-23 10:23:37 +00:00
|
|
|
brew.conv(
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
model=model,
|
|
|
|
|
blob_in="x",
|
|
|
|
|
blob_out="out",
|
|
|
|
|
dim_in=3,
|
|
|
|
|
dim_out=64,
|
|
|
|
|
kernel=3,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
workspace.RunNetOnce(model.param_init_net)
|
|
|
|
|
workspace.RunNetOnce(model.net)
|
|
|
|
|
out = workspace.FetchBlob("out")
|
|
|
|
|
self.assertEqual(out.shape, (64, 64, 17, 17))
|
|
|
|
|
|
|
|
|
|
def test_arg_scope_nested(self):
|
|
|
|
|
myhelper = self.myhelper
|
|
|
|
|
n = 16
|
2017-04-23 10:23:37 +00:00
|
|
|
with brew.arg_scope([myhelper], val=-3), \
|
|
|
|
|
brew.arg_scope([myhelper], val=-2):
|
|
|
|
|
with brew.arg_scope([myhelper], val=n):
|
|
|
|
|
res = brew.myhelper(None)
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
self.assertEqual(n, res)
|
2017-04-23 10:23:37 +00:00
|
|
|
res = brew.myhelper(None)
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
self.assertEqual(res, -2)
|
|
|
|
|
|
2017-04-23 10:23:37 +00:00
|
|
|
res = brew.myhelper(None, val=15)
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
self.assertEqual(res, 15)
|
|
|
|
|
|
|
|
|
|
def test_double_register(self):
|
|
|
|
|
myhelper = self.myhelper
|
|
|
|
|
with self.assertRaises(AttributeError):
|
2017-04-23 10:23:37 +00:00
|
|
|
brew.Register(myhelper)
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
|
|
|
|
|
def test_has_helper(self):
|
2017-04-23 10:23:37 +00:00
|
|
|
self.assertTrue(brew.has_helper(brew.conv))
|
|
|
|
|
self.assertTrue(brew.has_helper("conv"))
|
arg_scope for model_helper
Summary:
arg_scope module for model_helpers.
Some coding example with it:
with model_helpers.arg_scope([model_helpers.FC], kwargs):
model_helpers.FC(model, "x", "out_1", n, n)
with model_helpers.arg_scope([myhelper], n=-3):
with model_helpers.arg_scope([myhelper], n=-2):
with model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
with model_helpers.arg_scope([myhelper], n=-3), \
model_helpers.arg_scope([myhelper], n=-2), \
model_helpers.arg_scope([myhelper], n=n):
res = model_helpers.myhelper(None)
Reviewed By: salexspb
Differential Revision: D4837180
fbshipit-source-id: 2cbd81681779d6cd1e61ee189edcc1cf3bb07d15
2017-04-21 18:16:14 +00:00
|
|
|
|
|
|
|
|
def myhelper3():
|
|
|
|
|
pass
|
|
|
|
|
|
2017-04-23 10:23:37 +00:00
|
|
|
self.assertFalse(brew.has_helper(myhelper3))
|