Python mxnet.gluon.SymbolBlock() Examples
The following are 7
code examples of mxnet.gluon.SymbolBlock().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
mxnet.gluon
, or try the search function
.
Example #1
Source File: script.py From sagemaker-python-sdk with Apache License 2.0 | 6 votes |
def model_fn(model_dir): """Load the gluon model. Called once when hosting service starts. Args: model_dir: The directory where model files are stored. Returns: a model (in this case a Gluon network) """ symbol = mx.sym.load("%s/model.json" % model_dir) outputs = mx.symbol.softmax(data=symbol, name="softmax_label") inputs = mx.sym.var("data") param_dict = gluon.ParameterDict("model_") net = gluon.SymbolBlock(outputs, inputs, param_dict) net.load_params("%s/model.params" % model_dir, ctx=mx.cpu()) return net
Example #2
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def test_sparse_symbol_block(): data = mx.sym.var('data') weight = mx.sym.var('weight', stype='row_sparse') bias = mx.sym.var('bias') out = mx.sym.broadcast_add(mx.sym.dot(data, weight), bias) # an exception is expected when creating a SparseBlock w/ sparse param net = gluon.SymbolBlock(out, data)
Example #3
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def test_import(): ctx = mx.context.current_context() net1 = gluon.model_zoo.vision.resnet18_v1( prefix='resnet', ctx=ctx, pretrained=True) net1.hybridize() data = mx.nd.random.normal(shape=(1, 3, 32, 32)) out1 = net1(data) net1.export('net1', epoch=1) net2 = gluon.SymbolBlock.imports( 'net1-symbol.json', ['data'], 'net1-0001.params', ctx) out2 = net2(data) assert_almost_equal(out1.asnumpy(), out2.asnumpy())
Example #4
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def test_symbol_block_save_load(): class Net(gluon.HybridBlock): def __init__(self): super(Net, self).__init__() with self.name_scope(): backbone = gluon.model_zoo.vision.resnet18_v1() data = mx.sym.var('data') featnames = ['stage1_activation0', 'stage2_activation0', 'stage3_activation0'] out_names = ['_'.join([backbone.name, featname, 'output']) for featname in featnames] internals = backbone(data).get_internals() outs = [internals[out_name] for out_name in out_names] self.backbone = gluon.SymbolBlock(outs, data, params=backbone.collect_params()) self.body = nn.Conv2D(3, 1) def hybrid_forward(self, F, x): x = self.body(x) return self.backbone(x) net1 = Net() net1.initialize(mx.init.Normal()) net1.hybridize() net1(mx.nd.random.normal(shape=(1, 3, 32, 32))) net1.save_parameters('./test_symbol_block_save_load.params') net2 = Net() net2.load_parameters('./test_symbol_block_save_load.params', ctx=mx.cpu())
Example #5
Source File: test_gluon.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def test_legacy_save_params(): net = gluon.nn.HybridSequential(prefix='') with net.name_scope(): net.add(gluon.nn.Conv2D(10, (3, 3))) net.add(gluon.nn.Dense(50)) net.initialize() net(mx.nd.ones((1,1,50,50))) a = net(mx.sym.var('data')) a.save('test.json') net.save_params('test.params') model = gluon.nn.SymbolBlock(outputs=mx.sym.load_json(open('test.json', 'r').read()), inputs=mx.sym.var('data')) model.load_params('test.params', ctx=mx.cpu())
Example #6
Source File: gluon.py From mlflow with Apache License 2.0 | 5 votes |
def load_model(model_uri, ctx): """ Load a Gluon model from a local file or a run. :param model_uri: The location, in URI format, of the MLflow model. For example: - ``/Users/me/path/to/local/model`` - ``relative/path/to/local/model`` - ``s3://my_bucket/path/to/model`` - ``runs:/<mlflow_run_id>/run-relative/path/to/model`` - ``models:/<model_name>/<model_version>`` - ``models:/<model_name>/<stage>`` For more information about supported URI schemes, see `Referencing Artifacts <https://www.mlflow.org/docs/latest/concepts.html# artifact-locations>`_. :param ctx: Either CPU or GPU. :return: A Gluon model instance. .. code-block:: python :caption: Example # Load persisted model as a Gluon model, make inferences against an NDArray model = mlflow.gluon.load_model("runs:/" + gluon_random_data_run.info.run_id + "/model") model(nd.array(np.random.rand(1000, 1, 32))) """ local_model_path = _download_artifact_from_uri(artifact_uri=model_uri) model_arch_path = os.path.join(local_model_path, "data", _MODEL_SAVE_PATH) + "-symbol.json" model_params_path = os.path.join(local_model_path, "data", _MODEL_SAVE_PATH) + "-0000.params" symbol = sym.load(model_arch_path) inputs = sym.var('data', dtype='float32') net = gluon.SymbolBlock(symbol, inputs) net.collect_params().load(model_params_path, ctx) return net
Example #7
Source File: test_gluon.py From SNIPER-mxnet with Apache License 2.0 | 5 votes |
def test_symbol_block(): model = nn.HybridSequential() model.add(nn.Dense(128, activation='tanh')) model.add(nn.Dropout(0.5)) model.add(nn.Dense(64, activation='tanh'), nn.Dense(32, in_units=64)) model.add(nn.Activation('relu')) model.initialize() inputs = mx.sym.var('data') outputs = model(inputs).get_internals() smodel = gluon.SymbolBlock(outputs, inputs, params=model.collect_params()) assert len(smodel(mx.nd.zeros((16, 10)))) == 14 out = smodel(mx.sym.var('in')) assert len(out) == len(outputs.list_outputs()) class Net(nn.HybridBlock): def __init__(self, model): super(Net, self).__init__() self.model = model def hybrid_forward(self, F, x): out = self.model(x) return F.add_n(*[i.sum() for i in out]) net = Net(smodel) net.hybridize() assert isinstance(net(mx.nd.zeros((16, 10))), mx.nd.NDArray) inputs = mx.sym.var('data') outputs = model(inputs) smodel = gluon.SymbolBlock(outputs, inputs, params=model.collect_params()) net = Net(smodel) net.hybridize() assert isinstance(net(mx.nd.zeros((16, 10))), mx.nd.NDArray)