Expose all netstate options (for all-in-one nets) #3863
Merged
shelhamer
merged 3 commits into
BVLC:master
from
lukeyeager:bvlc/expose-all-netstate-options
Jul 11, 2016
Commits
Jump to file or symbol
Failed to load files and symbols.
| @@ -25,6 +25,7 @@ class Net { | ||
| public: | ||
| explicit Net(const NetParameter& param, const Net* root_net = NULL); | ||
| explicit Net(const string& param_file, Phase phase, | ||
| + const int level = 0, const vector<string>* stages = NULL, | ||
lukeyeager
Contributor
|
||
| const Net* root_net = NULL); | ||
| virtual ~Net() {} | ||
| @@ -72,7 +72,11 @@ def test_save_and_read(self): | ||
| f.close() | ||
| self.net.save(f.name) | ||
| net_file = simple_net_file(self.num_output) | ||
| - net2 = caffe.Net(net_file, f.name, caffe.TRAIN) | ||
| + # Test legacy constructor | ||
| + # should print deprecation warning | ||
| + caffe.Net(net_file, f.name, caffe.TRAIN) | ||
| + # Test named constructor | ||
| + net2 = caffe.Net(net_file, caffe.TRAIN, weights=f.name) | ||
| os.remove(net_file) | ||
| os.remove(f.name) | ||
| for name in self.net.params: | ||
| @@ -93,3 +97,225 @@ def test_save_hdf5(self): | ||
| for i in range(len(self.net.params[name])): | ||
| self.assertEqual(abs(self.net.params[name][i].data | ||
| - net2.params[name][i].data).sum(), 0) | ||
| + | ||
| +class TestLevels(unittest.TestCase): | ||
shelhamer
Owner
|
||
| + | ||
| + TEST_NET = """ | ||
| +layer { | ||
| + name: "data" | ||
| + type: "DummyData" | ||
| + top: "data" | ||
| + dummy_data_param { shape { dim: 1 dim: 1 dim: 10 dim: 10 } } | ||
| +} | ||
| +layer { | ||
| + name: "NoLevel" | ||
| + type: "InnerProduct" | ||
| + bottom: "data" | ||
| + top: "NoLevel" | ||
| + inner_product_param { num_output: 1 } | ||
| +} | ||
| +layer { | ||
| + name: "Level0Only" | ||
| + type: "InnerProduct" | ||
| + bottom: "data" | ||
| + top: "Level0Only" | ||
| + include { min_level: 0 max_level: 0 } | ||
| + inner_product_param { num_output: 1 } | ||
| +} | ||
| +layer { | ||
| + name: "Level1Only" | ||
| + type: "InnerProduct" | ||
| + bottom: "data" | ||
| + top: "Level1Only" | ||
| + include { min_level: 1 max_level: 1 } | ||
| + inner_product_param { num_output: 1 } | ||
| +} | ||
| +layer { | ||
| + name: "Level>=0" | ||
| + type: "InnerProduct" | ||
| + bottom: "data" | ||
| + top: "Level>=0" | ||
| + include { min_level: 0 } | ||
| + inner_product_param { num_output: 1 } | ||
| +} | ||
| +layer { | ||
| + name: "Level>=1" | ||
| + type: "InnerProduct" | ||
| + bottom: "data" | ||
| + top: "Level>=1" | ||
| + include { min_level: 1 } | ||
| + inner_product_param { num_output: 1 } | ||
| +} | ||
| +""" | ||
| + | ||
| + def setUp(self): | ||
| + self.f = tempfile.NamedTemporaryFile(mode='w+') | ||
| + self.f.write(self.TEST_NET) | ||
| + self.f.flush() | ||
| + | ||
| + def tearDown(self): | ||
| + self.f.close() | ||
| + | ||
| + def check_net(self, net, blobs): | ||
| + net_blobs = [b for b in net.blobs.keys() if 'data' not in b] | ||
| + self.assertEqual(net_blobs, blobs) | ||
| + | ||
| + def test_0(self): | ||
| + net = caffe.Net(self.f.name, caffe.TEST) | ||
| + self.check_net(net, ['NoLevel', 'Level0Only', 'Level>=0']) | ||
| + | ||
| + def test_1(self): | ||
| + net = caffe.Net(self.f.name, caffe.TEST, level=1) | ||
| + self.check_net(net, ['NoLevel', 'Level1Only', 'Level>=0', 'Level>=1']) | ||
| + | ||
| + | ||
| +class TestStages(unittest.TestCase): | ||
| + | ||
| + TEST_NET = """ | ||
| +layer { | ||
| + name: "data" | ||
| + type: "DummyData" | ||
| + top: "data" | ||
| + dummy_data_param { shape { dim: 1 dim: 1 dim: 10 dim: 10 } } | ||
| +} | ||
| +layer { | ||
| + name: "A" | ||
| + type: "InnerProduct" | ||
| + bottom: "data" | ||
| + top: "A" | ||
| + include { stage: "A" } | ||
| + inner_product_param { num_output: 1 } | ||
| +} | ||
| +layer { | ||
| + name: "B" | ||
| + type: "InnerProduct" | ||
| + bottom: "data" | ||
| + top: "B" | ||
| + include { stage: "B" } | ||
| + inner_product_param { num_output: 1 } | ||
| +} | ||
| +layer { | ||
| + name: "AorB" | ||
| + type: "InnerProduct" | ||
| + bottom: "data" | ||
| + top: "AorB" | ||
| + include { stage: "A" } | ||
| + include { stage: "B" } | ||
| + inner_product_param { num_output: 1 } | ||
| +} | ||
| +layer { | ||
| + name: "AandB" | ||
| + type: "InnerProduct" | ||
| + bottom: "data" | ||
| + top: "AandB" | ||
| + include { stage: "A" stage: "B" } | ||
| + inner_product_param { num_output: 1 } | ||
| +} | ||
| +""" | ||
| + | ||
| + def setUp(self): | ||
| + self.f = tempfile.NamedTemporaryFile(mode='w+') | ||
| + self.f.write(self.TEST_NET) | ||
| + self.f.flush() | ||
| + | ||
| + def tearDown(self): | ||
| + self.f.close() | ||
| + | ||
| + def check_net(self, net, blobs): | ||
| + net_blobs = [b for b in net.blobs.keys() if 'data' not in b] | ||
| + self.assertEqual(net_blobs, blobs) | ||
| + | ||
| + def test_A(self): | ||
| + net = caffe.Net(self.f.name, caffe.TEST, stages=['A']) | ||
| + self.check_net(net, ['A', 'AorB']) | ||
| + | ||
| + def test_B(self): | ||
| + net = caffe.Net(self.f.name, caffe.TEST, stages=['B']) | ||
| + self.check_net(net, ['B', 'AorB']) | ||
| + | ||
| + def test_AandB(self): | ||
| + net = caffe.Net(self.f.name, caffe.TEST, stages=['A', 'B']) | ||
| + self.check_net(net, ['A', 'B', 'AorB', 'AandB']) | ||
| + | ||
| + | ||
| +class TestAllInOne(unittest.TestCase): | ||
| + | ||
| + TEST_NET = """ | ||
| +layer { | ||
| + name: "train_data" | ||
| + type: "DummyData" | ||
| + top: "data" | ||
| + top: "label" | ||
| + dummy_data_param { | ||
| + shape { dim: 1 dim: 1 dim: 10 dim: 10 } | ||
| + shape { dim: 1 dim: 1 dim: 1 dim: 1 } | ||
| + } | ||
| + include { phase: TRAIN stage: "train" } | ||
| +} | ||
| +layer { | ||
| + name: "val_data" | ||
| + type: "DummyData" | ||
| + top: "data" | ||
| + top: "label" | ||
| + dummy_data_param { | ||
| + shape { dim: 1 dim: 1 dim: 10 dim: 10 } | ||
| + shape { dim: 1 dim: 1 dim: 1 dim: 1 } | ||
| + } | ||
| + include { phase: TEST stage: "val" } | ||
| +} | ||
| +layer { | ||
| + name: "deploy_data" | ||
| + type: "Input" | ||
| + top: "data" | ||
| + input_param { shape { dim: 1 dim: 1 dim: 10 dim: 10 } } | ||
| + include { phase: TEST stage: "deploy" } | ||
| +} | ||
| +layer { | ||
| + name: "ip" | ||
| + type: "InnerProduct" | ||
| + bottom: "data" | ||
| + top: "ip" | ||
| + inner_product_param { num_output: 2 } | ||
| +} | ||
| +layer { | ||
| + name: "loss" | ||
| + type: "SoftmaxWithLoss" | ||
| + bottom: "ip" | ||
| + bottom: "label" | ||
| + top: "loss" | ||
| + include: { phase: TRAIN stage: "train" } | ||
| + include: { phase: TEST stage: "val" } | ||
| +} | ||
| +layer { | ||
| + name: "pred" | ||
| + type: "Softmax" | ||
| + bottom: "ip" | ||
| + top: "pred" | ||
| + include: { phase: TEST stage: "deploy" } | ||
| +} | ||
| +""" | ||
| + | ||
| + def setUp(self): | ||
| + self.f = tempfile.NamedTemporaryFile(mode='w+') | ||
| + self.f.write(self.TEST_NET) | ||
| + self.f.flush() | ||
| + | ||
| + def tearDown(self): | ||
| + self.f.close() | ||
| + | ||
| + def check_net(self, net, outputs): | ||
| + self.assertEqual(list(net.blobs['data'].shape), [1,1,10,10]) | ||
| + self.assertEqual(net.outputs, outputs) | ||
| + | ||
| + def test_train(self): | ||
| + net = caffe.Net(self.f.name, caffe.TRAIN, stages=['train']) | ||
| + self.check_net(net, ['loss']) | ||
| + | ||
| + def test_val(self): | ||
| + net = caffe.Net(self.f.name, caffe.TEST, stages=['val']) | ||
| + self.check_net(net, ['loss']) | ||
| + | ||
| + def test_deploy(self): | ||
| + net = caffe.Net(self.f.name, caffe.TEST, stages=['deploy']) | ||
| + self.check_net(net, ['pred']) | ||
| + | ||
Oops, something went wrong.
Could you also insert the new arguments (level, stages) after root_net? Otherwise, this breaks calling code that instantiates Net with root_net non-null:
I'm not sure how common this is, but it's pretty trivial to avoid it by just inserting the new optional arguments at the end of the declaration?