From 54b9e9c8d063fa00853e6cf80d22c922014e3e67 Mon Sep 17 00:00:00 2001 From: Sheng Zha Date: Mon, 3 Aug 2020 08:59:33 -0700 Subject: [PATCH] remove unnecessary usage of pretrained models, and prefer smaller size (#18844) --- tests/python/unittest/test_gluon.py | 76 ++++++++++--------- tests/python/unittest/test_gluon_estimator.py | 4 +- tests/python/unittest/test_gluon_model_zoo.py | 2 +- 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/tests/python/unittest/test_gluon.py b/tests/python/unittest/test_gluon.py index 69bf81e26705..a934fbe12e58 100644 --- a/tests/python/unittest/test_gluon.py +++ b/tests/python/unittest/test_gluon.py @@ -192,11 +192,11 @@ def __init__(self, **kwargs): net = Net() lines = str(net.collect_params()).splitlines() - + assert 'dense0.weight' in lines[0] assert '(10, 5)' in lines[0] assert 'float32' in lines[0] - + @with_seed() def test_collect_parameters(): @@ -312,7 +312,7 @@ def hybrid_forward(self, F, x): net_fp32.cast('float64') net_fp32.hybridize() data = mx.nd.zeros((1,3,224,224), dtype='float64', ctx=ctx) - net_fp32.forward(data) + net_fp32(data) sym_file, params_file = net_fp32.export(tmpfile, 0) # 2.a Load the saved model and verify if all the params are loaded correctly. @@ -1119,23 +1119,26 @@ def check_embedding_large_input(sparse_grad): check_embedding_large_input(False) @with_seed() -def test_export(): +def test_export(tmpdir): + tmpfile = os.path.join(str(tmpdir), 'gluon') ctx = mx.context.current_context() model = gluon.model_zoo.vision.resnet18_v1( - ctx=ctx, pretrained=True) + ctx=ctx, pretrained=False) + model.initialize() model.hybridize() data = mx.nd.random.normal(shape=(1, 3, 32, 32)) out = model(data) - symbol_filename, params_filename = model.export('gluon') - assert symbol_filename == 'gluon-symbol.json' - assert params_filename == 'gluon-0000.params' + symbol_filename, params_filename = model.export(tmpfile) + assert symbol_filename == tmpfile+'-symbol.json' + assert params_filename == tmpfile+'-0000.params' @with_seed() def test_import(): ctx = mx.context.current_context() net1 = gluon.model_zoo.vision.resnet18_v1( - ctx=ctx, pretrained=True) + ctx=ctx, pretrained=False) + net1.initialize() net1.hybridize() data = mx.nd.random.normal(shape=(1, 3, 32, 32)) out1 = net1(data) @@ -1440,7 +1443,9 @@ def test_req(): @with_seed() def test_save_load(tmpdir): - net = mx.gluon.model_zoo.vision.get_resnet(1, 18, pretrained=True, root=str(tmpdir)) + net = mx.gluon.model_zoo.vision.get_resnet(1, 18, pretrained=False, root=str(tmpdir)) + net.initialize() + net(mx.nd.ones((1,3,224,224))) net.save_parameters(os.path.join(str(tmpdir), 'test_save_load.params')) net = mx.gluon.model_zoo.vision.get_resnet(1, 18) @@ -1598,17 +1603,19 @@ def _test_multi_reset(nArrays, dtype, ctx): _test_grad_reset(ctx, dtype=type, sparse=sparse, embeddingType=embType) -def check_hybrid_static_memory(**kwargs): +@with_seed() +@pytest.mark.parametrize('static_alloc', [False, True]) +@pytest.mark.parametrize('static_shape', [False, True]) +def test_hybrid_static_memory(static_alloc, static_shape): + if static_shape and not static_alloc: + pytest.skip() x = mx.nd.random.uniform(shape=(2, 3, 32, 32)) x.attach_grad() - net1 = gluon.model_zoo.vision.get_resnet( - 1, 18, pretrained=True, ctx=mx.context.current_context()) - net2 = gluon.model_zoo.vision.get_resnet( - 1, 18, pretrained=True, ctx=mx.context.current_context()) - net2.hybridize(**kwargs) - net1(x) - net2(x) + net = gluon.model_zoo.vision.get_resnet( + 1, 18, pretrained=False, ctx=mx.context.current_context()) + net.initialize() + net(x) def test(net, x): with mx.autograd.record(): @@ -1619,23 +1626,25 @@ def test(net, x): return y, grads - y1, grads1 = test(net1, x) - y2, grads2 = test(net2, x) + y1, grads1 = test(net, x) + net.hybridize(static_alloc=static_alloc, static_shape=static_shape) + y2, grads2 = test(net, x) assert_almost_equal(y1.asnumpy(), y2.asnumpy(), rtol=1e-3, atol=1e-5) for key in grads1: assert_almost_equal(grads1[key].asnumpy(), grads2[key].asnumpy(), rtol=1e-3, atol=1e-4) -@with_seed() -def test_hybrid_static_memory(): - check_hybrid_static_memory() - check_hybrid_static_memory(static_alloc=True) - check_hybrid_static_memory(static_alloc=True, static_shape=True) -def check_hybrid_static_memory_switching(**kwargs): +@with_seed() +@pytest.mark.parametrize('static_alloc', [False, True]) +@pytest.mark.parametrize('static_shape', [False, True]) +def test_hybrid_static_memory_switching(static_alloc, static_shape): + if static_shape and not static_alloc: + pytest.skip() net = gluon.model_zoo.vision.get_resnet( - 1, 18, pretrained=True, ctx=mx.context.current_context()) - net.hybridize(**kwargs) + 1, 18, pretrained=False, ctx=mx.context.current_context()) + net.initialize() + net.hybridize(static_alloc=static_alloc, static_shape=static_shape) x = mx.nd.random.uniform(shape=(4, 3, 32, 32)) net(x) @@ -1649,12 +1658,6 @@ def check_hybrid_static_memory_switching(**kwargs): y.backward() mx.nd.waitall() -@with_seed() -def test_hybrid_static_memory_switching(): - check_hybrid_static_memory_switching() - check_hybrid_static_memory_switching(static_alloc=True) - check_hybrid_static_memory_switching(static_alloc=True, static_shape=True) - @with_seed() def test_hook(): global hook_call_count @@ -1734,7 +1737,7 @@ def mon_callback(node_name, opr_name, arr): model.add(mx.gluon.nn.AvgPool1D()) model.initialize() model.hybridize() - check_name(model, ['hybridsequential_avgpool1d0_fwd_data', 'hybridsequential_avgpool1d0_fwd_output'], + check_name(model, ['hybridsequential_avgpool1d0_fwd_data', 'hybridsequential_avgpool1d0_fwd_output'], expected_opr_names=["Pooling"], monitor_all=True) # stack two layers and test @@ -1850,7 +1853,8 @@ def hybrid_forward(self, F, x): def test_hybrid_static_memory_recording(): net = gluon.model_zoo.vision.get_resnet( - 1, 18, pretrained=True, ctx=mx.context.current_context()) + 1, 18, pretrained=False, ctx=mx.context.current_context()) + net.initialize() net.hybridize(static_alloc=True) x = mx.nd.random.uniform(shape=(1, 3, 32, 32)) diff --git a/tests/python/unittest/test_gluon_estimator.py b/tests/python/unittest/test_gluon_estimator.py index a18dce054744..8c12b5d2a13b 100644 --- a/tests/python/unittest/test_gluon_estimator.py +++ b/tests/python/unittest/test_gluon_estimator.py @@ -144,7 +144,9 @@ def test_initializer(): context=ctx) assert 'Network already fully initialized' in str(w[-1].message) # net partially initialized, fine tuning use case - net = gluon.model_zoo.vision.resnet18_v1(pretrained=True, ctx=ctx) + net = gluon.model_zoo.vision.resnet18_v1(pretrained=False, ctx=ctx) + net.features.initialize(ctx=ctx) + net.features(mx.nd.zeros((1, 3, 224, 224))) net.output = gluon.nn.Dense(10) #last layer not initialized est = Estimator(net, loss=loss, train_metrics=acc, context=ctx) dataset = gluon.data.ArrayDataset(mx.nd.zeros((10, 3, 224, 224)), mx.nd.zeros((10, 10))) diff --git a/tests/python/unittest/test_gluon_model_zoo.py b/tests/python/unittest/test_gluon_model_zoo.py index 191a070be287..0fdd3576e2b9 100644 --- a/tests/python/unittest/test_gluon_model_zoo.py +++ b/tests/python/unittest/test_gluon_model_zoo.py @@ -41,7 +41,7 @@ def eprint(*args, **kwargs): 'mobilenetv2_1.0', 'mobilenetv2_0.75', 'mobilenetv2_0.5', 'mobilenetv2_0.25' ]) def test_models(model_name): - pretrained_to_test = set(['vgg19_bn']) + pretrained_to_test = set(['mobilenetv2_0.25']) test_pretrain = model_name in pretrained_to_test model = get_model(model_name, pretrained=test_pretrain, root='model/')