sandeep-krishnamurthy commented on a change in pull request #12412: Infer dtype
in SymbolBlock import from input symbol
URL: https://github.com/apache/incubator-mxnet/pull/12412#discussion_r216884637
##########
File path: tests/python/unittest/test_gluon.py
##########
@@ -336,6 +339,41 @@ def hybrid_forward(self, F, x):
net.hybridize()
assert isinstance(net(mx.nd.zeros((16, 10))), mx.nd.NDArray)
+ # Test case to verify if initializing the SymbolBlock from a model with
params
+ # other than fp32 param dtype.
+
+ # 1. Load a resnet model, cast it to fp64 and export
+ tmp = tempfile.mkdtemp()
+ tmpfile = os.path.join(tmp, 'resnet34_fp64')
+ ctx = mx.cpu(0)
+
+ net_fp32 = mx.gluon.model_zoo.vision.resnet34_v2(pretrained=True, ctx=ctx,
root=tmp)
+ 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.export(tmpfile, 0)
+
+ # 2. Load the saved model and verify if all the params are loaded
correctly.
+ # and choose one of the param to verify the type if fp64.
+ sm = mx.sym.load(tmpfile + '-symbol.json')
+ inputs = mx.sym.var('data', dtype='float64')
+ net_fp64 = mx.gluon.SymbolBlock(sm, inputs)
+ net_fp64.collect_params().load(tmpfile + '-0000.params', ctx=ctx)
+ # 3. Get a conv layer's weight parameter name. Conv layer's weight param is
+ # expected to be of dtype casted, fp64.
+ for param_name in net_fp64.params.keys():
+ if 'conv' in param_name and 'weight' in param_name:
+ break
+ assert np.dtype(net_fp64.params[param_name].dtype) == np.dtype(np.float64)
+
+ # Cast the symbol block to FP32 and try to forward a FP32 data.
Review comment:
@zhreshold - Added a test here to verify the SymbolBlock.cast functionality.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services