From 706c6ce888fd99000e2d409434517678df92cd0b Mon Sep 17 00:00:00 2001 From: Eric Junyuan Xie Date: Wed, 13 Jun 2018 22:01:40 -0700 Subject: [PATCH] add import_ for SymbolBlock (#11127) --- docs/tutorials/gluon/hybrid.md | 2 +- docs/tutorials/gluon/naming.md | 6 +- docs/tutorials/gluon/save_load_params.md | 261 ++++++++++++++++++ example/gluon/dcgan.py | 8 +- example/gluon/embedding_learning/train.py | 2 +- example/gluon/image_classification.py | 8 +- example/gluon/mnist.py | 2 +- example/gluon/style_transfer/main.py | 8 +- example/gluon/super_resolution.py | 4 +- example/gluon/tree_lstm/main.py | 2 +- example/gluon/word_language_model/train.py | 4 +- python/mxnet/gluon/block.py | 90 +++++- .../mxnet/gluon/model_zoo/vision/alexnet.py | 2 +- .../mxnet/gluon/model_zoo/vision/densenet.py | 2 +- .../mxnet/gluon/model_zoo/vision/inception.py | 2 +- .../mxnet/gluon/model_zoo/vision/mobilenet.py | 4 +- python/mxnet/gluon/model_zoo/vision/resnet.py | 4 +- .../gluon/model_zoo/vision/squeezenet.py | 2 +- python/mxnet/gluon/model_zoo/vision/vgg.py | 4 +- tests/python/unittest/test_gluon.py | 101 ++++++- 20 files changed, 470 insertions(+), 48 deletions(-) create mode 100644 docs/tutorials/gluon/save_load_params.md diff --git a/docs/tutorials/gluon/hybrid.md b/docs/tutorials/gluon/hybrid.md index 3554a15fa3b7..5c8372a51f4e 100644 --- a/docs/tutorials/gluon/hybrid.md +++ b/docs/tutorials/gluon/hybrid.md @@ -117,7 +117,7 @@ x = mx.sym.var('data') y = net(x) print(y) y.save('model.json') -net.save_params('model.params') +net.save_parameters('model.params') ``` If your network outputs more than one value, you can use `mx.sym.Group` to diff --git a/docs/tutorials/gluon/naming.md b/docs/tutorials/gluon/naming.md index 37b63fa08a9e..3606a03dcbd2 100644 --- a/docs/tutorials/gluon/naming.md +++ b/docs/tutorials/gluon/naming.md @@ -203,12 +203,12 @@ except Exception as e: Parameter 'model1_dense0_weight' is missing in file 'model.params', which contains parameters: 'model0_mydense_weight', 'model0_dense1_bias', 'model0_dense1_weight', 'model0_dense0_weight', 'model0_dense0_bias', 'model0_mydense_bias'. Please make sure source and target networks have the same prefix. -To solve this problem, we use `save_params`/`load_params` instead of `collect_params` and `save`/`load`. `save_params` uses model structure, instead of parameter name, to match parameters. +To solve this problem, we use `save_parameters`/`load_parameters` instead of `collect_params` and `save`/`load`. `save_parameters` uses model structure, instead of parameter name, to match parameters. ```python -model0.save_params('model.params') -model1.load_params('model.params') +model0.save_parameters('model.params') +model1.load_parameters('model.params') print(mx.nd.load('model.params').keys()) ``` diff --git a/docs/tutorials/gluon/save_load_params.md b/docs/tutorials/gluon/save_load_params.md new file mode 100644 index 000000000000..d8eac88d8f59 --- /dev/null +++ b/docs/tutorials/gluon/save_load_params.md @@ -0,0 +1,261 @@ +# Saving and Loading Gluon Models + +Training large models take a lot of time and it is a good idea to save the trained models to files to avoid training them again and again. There are a number of reasons to do this. For example, you might want to do inference on a machine that is different from the one where the model was trained. Sometimes model's performance on validation set decreases towards the end of the training because of overfitting. If you saved your model parameters after every epoch, at the end you can decide to use the model that performs best on the validation set. Another reason would be to train your model using one language (like Python that has a lot of tools for training) and run inference using a different language (like Scala probably because your application is built on Scala). + +In this tutorial, we will learn ways to save and load Gluon models. There are two ways to save/load Gluon models: + +**1. Save/load model parameters only** + +Parameters of any Gluon model can be saved using the `save_parameters` and `load_parameters` method. This does not save model architecture. This method is used to save parameters of dynamic (non-hybrid) models. Model architecture cannot be saved for dynamic models because model architecture changes during execution. + +**2. Save/load model parameters AND architecture** + +The Model architecture of `Hybrid` models stays static and don't change during execution. Therefore both model parameters AND architecture can be saved and loaded using `export`, `imports` methods. + +Let's look at the above methods in more detail. Let's start by importing the modules we'll need. + +```python +from __future__ import print_function + +import mxnet as mx +import mxnet.ndarray as nd +from mxnet import nd, autograd, gluon +from mxnet.gluon.data.vision import transforms + +import numpy as np +``` + +## Setup: build and train a simple model + +We need a trained model before we can save it to a file. So let's go ahead and build a very simple convolutional network and train it on MNIST data. + +Let's define a helper function to build a LeNet model and another helper to train LeNet with MNIST. + +```python +# Use GPU if one exists, else use CPU +ctx = mx.gpu() if mx.test_utils.list_gpus() else mx.cpu() + +# MNIST images are 28x28. Total pixels in input layer is 28x28 = 784 +num_inputs = 784 +# Clasify the images into one of the 10 digits +num_outputs = 10 +# 64 images in a batch +batch_size = 64 + +# Load the training data +train_data = gluon.data.DataLoader(gluon.data.vision.MNIST(train=True).transform_first(transforms.ToTensor()), + batch_size, shuffle=True) + +# Build a simple convolutional network +def build_lenet(net): + with net.name_scope(): + # First convolution + net.add(gluon.nn.Conv2D(channels=20, kernel_size=5, activation='relu')) + net.add(gluon.nn.MaxPool2D(pool_size=2, strides=2)) + # Second convolution + net.add(gluon.nn.Conv2D(channels=50, kernel_size=5, activation='relu')) + net.add(gluon.nn.MaxPool2D(pool_size=2, strides=2)) + # Flatten the output before the fully connected layers + net.add(gluon.nn.Flatten()) + # First fully connected layers with 512 neurons + net.add(gluon.nn.Dense(512, activation="relu")) + # Second fully connected layer with as many neurons as the number of classes + net.add(gluon.nn.Dense(num_outputs)) + + return net + +# Train a given model using MNIST data +def train_model(model): + # Initialize the parameters with Xavier initializer + model.collect_params().initialize(mx.init.Xavier(), ctx=ctx) + # Use cross entropy loss + softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss() + # Use Adam optimizer + trainer = gluon.Trainer(model.collect_params(), 'adam', {'learning_rate': .001}) + + # Train for one epoch + for epoch in range(1): + # Iterate through the images and labels in the training data + for batch_num, (data, label) in enumerate(train_data): + # get the images and labels + data = data.as_in_context(ctx) + label = label.as_in_context(ctx) + # Ask autograd to record the forward pass + with autograd.record(): + # Run the forward pass + output = model(data) + # Compute the loss + loss = softmax_cross_entropy(output, label) + # Compute gradients + loss.backward() + # Update parameters + trainer.step(data.shape[0]) + + # Print loss once in a while + if batch_num % 50 == 0: + curr_loss = nd.mean(loss).asscalar() + print("Epoch: %d; Batch %d; Loss %f" % (epoch, batch_num, curr_loss)) +``` + +Let's build a model and train it. After training, we will save and restore this model from a file. + +```python +net = build_lenet(gluon.nn.Sequential()) +train_model(net) +``` +
Epoch: 0; Batch 0; Loss 2.288904 
+Epoch: 0; Batch 50; Loss 0.269372 
+Epoch: 0; Batch 100; Loss 0.238990 
+Epoch: 0; Batch 150; Loss 0.320592 
+Epoch: 0; Batch 200; Loss 0.048619 
+Epoch: 0; Batch 250; Loss 0.121555 
+Epoch: 0; Batch 300; Loss 0.083645 
+Epoch: 0; Batch 350; Loss 0.040627 
+Epoch: 0; Batch 400; Loss 0.195946 
+Epoch: 0; Batch 450; Loss 0.155514 
+Epoch: 0; Batch 500; Loss 0.031762 
+Epoch: 0; Batch 550; Loss 0.056516 
+Epoch: 0; Batch 600; Loss 0.095174 
+Epoch: 0; Batch 650; Loss 0.054901 
+Epoch: 0; Batch 700; Loss 0.030067 
+Epoch: 0; Batch 750; Loss 0.102611 
+Epoch: 0; Batch 800; Loss 0.010036 
+Epoch: 0; Batch 850; Loss 0.051853 
+Epoch: 0; Batch 900; Loss 0.008402 
+
+ +## Saving model parameters to file + +Okay, we now have a model (`net`) that we can save to a file. Let's save the parameters of this model to a file using the `save_parameters` function. + +```python +file_name = "net.params" +net.save_parameters(file_name) +``` + +We have successfully saved the parameters of the model into a file. + +Note: `Block.collect_params().save()` is not a recommended way to save parameters of a Gluon network if you plan to load the parameters back into a Gluon network using `Block.load_parameters()`. + +## Loading model parameters from file + +Let's now create a network with the parameters we saved into the file. We build the network again using the helper first and then load the weights from the file we saved using the `load_parameters` function. + +```python +new_net = build_lenet(gluon.nn.Sequential()) +new_net.load_parameters(file_name, ctx=ctx) +``` + +Note that to do this, we need the definition of the network as Python code. If we want to recreate this network on a different machine using the saved weights, we need the same Python code (`build_lenet`) that created the network to create the `new_net` object shown above. This means Python code needs to be copied over to any machine where we want to run this network. + +If our network is [Hybrid](https://mxnet.incubator.apache.org/tutorials/gluon/hybrid.html), we can even save the network architecture into files and we won't need the network definition in a Python file to load the network. We'll see how to do it in the next section. + +Let's test the model we just loaded from file. + +```python +import matplotlib.pyplot as plt + +def verify_loaded_model(net): + """Run inference using ten random images. + Print both input and output of the model""" + + def transform(data, label): + return data.astype(np.float32)/255, label.astype(np.float32) + + # Load ten random images from the test dataset + sample_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=False, transform=transform), + 10, shuffle=True) + + for data, label in sample_data: + + # Display the images + img = nd.transpose(data, (1,0,2,3)) + img = nd.reshape(img, (28,10*28,1)) + imtiles = nd.tile(img, (1,1,3)) + plt.imshow(imtiles.asnumpy()) + plt.show() + + # Display the predictions + data = nd.transpose(data, (0, 3, 1, 2)) + out = net(data.as_in_context(ctx)) + predictions = nd.argmax(out, axis=1) + print('Model predictions: ', predictions.asnumpy()) + + break + +verify_loaded_model(new_net) +``` +![Model inputs](https://raw.githubusercontent.com/indhub/web-data/4a9c100aa996df3dff0e7f493029d411c2b526c3/mxnet/tutorials/gluon/save_load_params/mnist_in_1.png) + +Model predictions: [1. 1. 4. 5. 0. 5. 7. 0. 3. 6.] + +## Saving model parameters AND architecture to file + +[Hybrid](https://mxnet.incubator.apache.org/tutorials/gluon/hybrid.html) models can be serialized as JSON files using the `export` function. Once serialized, these models can be loaded from other language bindings like C++ or Scala for faster inference or inference in different environments. + +Note that the network we created above is not a Hybrid network and therefore cannot be serialized into a JSON file. So, let's create a Hybrid version of the same network and train it. + +```python +net = build_lenet(gluon.nn.HybridSequential()) +net.hybridize() +train_model(net) +``` + +
Epoch: 0; Batch 0; Loss 2.323284 
+Epoch: 0; Batch 50; Loss 0.444733 
+Epoch: 0; Batch 100; Loss 0.103407 
+Epoch: 0; Batch 150; Loss 0.166772 
+Epoch: 0; Batch 200; Loss 0.227569 
+Epoch: 0; Batch 250; Loss 0.069515 
+Epoch: 0; Batch 300; Loss 0.074086 
+Epoch: 0; Batch 350; Loss 0.074382 
+Epoch: 0; Batch 400; Loss 0.026569 
+Epoch: 0; Batch 450; Loss 0.097248 
+Epoch: 0; Batch 500; Loss 0.059895 
+Epoch: 0; Batch 550; Loss 0.053194 
+Epoch: 0; Batch 600; Loss 0.076294 
+Epoch: 0; Batch 650; Loss 0.047274 
+Epoch: 0; Batch 700; Loss 0.007898 
+Epoch: 0; Batch 750; Loss 0.039478 
+Epoch: 0; Batch 800; Loss 0.031342 
+Epoch: 0; Batch 850; Loss 0.059289 
+Epoch: 0; Batch 900; Loss 0.037809 
+
+ +We now have a trained hybrid network. This can be exported into files using the `export` function. The `export` function will export the model architecture into a `.json` file and model parameters into a `.params` file. + +```python +net.export("lenet", epoch=1) +``` + +`export` in this case creates `lenet-symbol.json` and `lenet-0001.params` in the current directory. + +## Loading model parameters AND architecture from file + +### From a different frontend + +One of the main reasons to serialize model architecture into a JSON file is to load it from a different frontend like C, C++ or Scala. Here is a couple of examples: +1. [Loading serialized Hybrid networks from C](https://github.com/apache/incubator-mxnet/blob/master/example/image-classification/predict-cpp/image-classification-predict.cc) +2. [Loading serialized Hybrid networks from Scala](https://github.com/apache/incubator-mxnet/blob/master/scala-package/infer/src/main/scala/org/apache/mxnet/infer/ImageClassifier.scala) + +### From Python + +Serialized Hybrid networks (saved as .JSON and .params file) can be loaded and used inside Python frontend using `gluon.nn.SymbolBlock`. To demonstrate that, let's load the network we serialized above. + +```python +deserialized_net = gluon.nn.SymbolBlock.imports("lenet-symbol.json", ['data'], "lenet-0001.params") +``` + +`deserialized_net` now contains the network we deserialized from files. Let's test the deserialized network to make sure it works. + +```python +verify_loaded_model(deserialized_net) +``` + +![Model inputs](https://raw.githubusercontent.com/indhub/web-data/4a9c100aa996df3dff0e7f493029d411c2b526c3/mxnet/tutorials/gluon/save_load_params/mnist_in_2.png) + +Model predictions: [4. 8. 0. 1. 5. 5. 8. 8. 1. 9.] + +That's all! We learned how to save and load Gluon networks from files. Parameters of any Gluon network can be persisted into files. For hybrid networks, both the architecture of the network and the parameters can be saved to and loaded from files. + + diff --git a/example/gluon/dcgan.py b/example/gluon/dcgan.py index 3233f430eeac..8ac9c522cf59 100644 --- a/example/gluon/dcgan.py +++ b/example/gluon/dcgan.py @@ -229,8 +229,8 @@ def transformer(data, label): logging.info('time: %f' % (time.time() - tic)) if check_point: - netG.save_params(os.path.join(outf,'generator_epoch_%d.params' %epoch)) - netD.save_params(os.path.join(outf,'discriminator_epoch_%d.params' % epoch)) + netG.save_parameters(os.path.join(outf,'generator_epoch_%d.params' %epoch)) + netD.save_parameters(os.path.join(outf,'discriminator_epoch_%d.params' % epoch)) -netG.save_params(os.path.join(outf, 'generator.params')) -netD.save_params(os.path.join(outf, 'discriminator.params')) +netG.save_parameters(os.path.join(outf, 'generator.params')) +netD.save_parameters(os.path.join(outf, 'discriminator.params')) diff --git a/example/gluon/embedding_learning/train.py b/example/gluon/embedding_learning/train.py index 46f76b55614a..b8a5bf2716c1 100644 --- a/example/gluon/embedding_learning/train.py +++ b/example/gluon/embedding_learning/train.py @@ -246,7 +246,7 @@ def train(epochs, ctx): if val_accs[0] > best_val: best_val = val_accs[0] logging.info('Saving %s.' % opt.save_model_prefix) - net.save_params('%s.params' % opt.save_model_prefix) + net.save_parameters('%s.params' % opt.save_model_prefix) return best_val diff --git a/example/gluon/image_classification.py b/example/gluon/image_classification.py index a67a31790a06..2cf12f091eb5 100644 --- a/example/gluon/image_classification.py +++ b/example/gluon/image_classification.py @@ -122,7 +122,7 @@ def get_model(model, ctx, opt): net = models.get_model(model, **kwargs) if opt.resume: - net.load_params(opt.resume) + net.load_parameters(opt.resume) elif not opt.use_pretrained: if model in ['alexnet']: net.initialize(mx.init.Normal()) @@ -176,12 +176,12 @@ def update_learning_rate(lr, trainer, epoch, ratio, steps): def save_checkpoint(epoch, top1, best_acc): if opt.save_frequency and (epoch + 1) % opt.save_frequency == 0: fname = os.path.join(opt.prefix, '%s_%d_acc_%.4f.params' % (opt.model, epoch, top1)) - net.save_params(fname) + net.save_parameters(fname) logger.info('[Epoch %d] Saving checkpoint to %s with Accuracy: %.4f', epoch, fname, top1) if top1 > best_acc[0]: best_acc[0] = top1 fname = os.path.join(opt.prefix, '%s_best.params' % (opt.model)) - net.save_params(fname) + net.save_parameters(fname) logger.info('[Epoch %d] Saving checkpoint to %s with Accuracy: %.4f', epoch, fname, top1) def train(opt, ctx): @@ -267,7 +267,7 @@ def main(): optimizer = 'sgd', optimizer_params = {'learning_rate': opt.lr, 'wd': opt.wd, 'momentum': opt.momentum, 'multi_precision': True}, initializer = mx.init.Xavier(magnitude=2)) - mod.save_params('image-classifier-%s-%d-final.params'%(opt.model, opt.epochs)) + mod.save_parameters('image-classifier-%s-%d-final.params'%(opt.model, opt.epochs)) else: if opt.mode == 'hybrid': net.hybridize() diff --git a/example/gluon/mnist.py b/example/gluon/mnist.py index 198d7ca5ab2a..6aea3abc5041 100644 --- a/example/gluon/mnist.py +++ b/example/gluon/mnist.py @@ -117,7 +117,7 @@ def train(epochs, ctx): name, val_acc = test(ctx) print('[Epoch %d] Validation: %s=%f'%(epoch, name, val_acc)) - net.save_params('mnist.params') + net.save_parameters('mnist.params') if __name__ == '__main__': diff --git a/example/gluon/style_transfer/main.py b/example/gluon/style_transfer/main.py index 7fcc927f9cb4..c67b830fe729 100644 --- a/example/gluon/style_transfer/main.py +++ b/example/gluon/style_transfer/main.py @@ -54,7 +54,7 @@ def train(args): style_model.initialize(init=mx.initializer.MSRAPrelu(), ctx=ctx) if args.resume is not None: print('Resuming, initializing using weight from {}.'.format(args.resume)) - style_model.load_params(args.resume, ctx=ctx) + style_model.load_parameters(args.resume, ctx=ctx) print('style_model:',style_model) # optimizer and loss trainer = gluon.Trainer(style_model.collect_params(), 'adam', @@ -118,14 +118,14 @@ def train(args): save_model_filename = "Epoch_" + str(e) + "iters_" + str(count) + "_" + str(time.ctime()).replace(' ', '_') + "_" + str( args.content_weight) + "_" + str(args.style_weight) + ".params" save_model_path = os.path.join(args.save_model_dir, save_model_filename) - style_model.save_params(save_model_path) + style_model.save_parameters(save_model_path) print("\nCheckpoint, trained model saved at", save_model_path) # save model save_model_filename = "Final_epoch_" + str(args.epochs) + "_" + str(time.ctime()).replace(' ', '_') + "_" + str( args.content_weight) + "_" + str(args.style_weight) + ".params" save_model_path = os.path.join(args.save_model_dir, save_model_filename) - style_model.save_params(save_model_path) + style_model.save_parameters(save_model_path) print("\nDone, trained model saved at", save_model_path) @@ -140,7 +140,7 @@ def evaluate(args): style_image = utils.preprocess_batch(style_image) # model style_model = net.Net(ngf=args.ngf) - style_model.load_params(args.model, ctx=ctx) + style_model.load_parameters(args.model, ctx=ctx) # forward style_model.setTarget(style_image) output = style_model(content_image) diff --git a/example/gluon/super_resolution.py b/example/gluon/super_resolution.py index 38c3bec8949a..0f2f21f3c0a7 100644 --- a/example/gluon/super_resolution.py +++ b/example/gluon/super_resolution.py @@ -168,13 +168,13 @@ def train(epoch, ctx): print('training mse at epoch %d: %s=%f'%(i, name, acc)) test(ctx) - net.save_params('superres.params') + net.save_parameters('superres.params') def resolve(ctx): from PIL import Image if isinstance(ctx, list): ctx = [ctx[0]] - net.load_params('superres.params', ctx=ctx) + net.load_parameters('superres.params', ctx=ctx) img = Image.open(opt.resolve_img).convert('YCbCr') y, cb, cr = img.split() data = mx.nd.expand_dims(mx.nd.expand_dims(mx.nd.array(y), axis=0), axis=0) diff --git a/example/gluon/tree_lstm/main.py b/example/gluon/tree_lstm/main.py index d2fe464638a4..ad5d59f7a47d 100644 --- a/example/gluon/tree_lstm/main.py +++ b/example/gluon/tree_lstm/main.py @@ -138,7 +138,7 @@ def test(ctx, data_iter, best, mode='validation', num_iter=-1): if test_r >= best: best = test_r logging.info('New optimum found: {}. Checkpointing.'.format(best)) - net.save_params('childsum_tree_lstm_{}.params'.format(num_iter)) + net.save_parameters('childsum_tree_lstm_{}.params'.format(num_iter)) test(ctx, test_iter, -1, 'test') return best diff --git a/example/gluon/word_language_model/train.py b/example/gluon/word_language_model/train.py index 9e152636bb08..7f0a916b79bd 100644 --- a/example/gluon/word_language_model/train.py +++ b/example/gluon/word_language_model/train.py @@ -185,7 +185,7 @@ def train(): if val_L < best_val: best_val = val_L test_L = eval(test_data) - model.save_params(args.save) + model.save_parameters(args.save) print('test loss %.2f, test ppl %.2f'%(test_L, math.exp(test_L))) else: args.lr = args.lr*0.25 @@ -193,6 +193,6 @@ def train(): if __name__ == '__main__': train() - model.load_params(args.save, context) + model.load_parameters(args.save, context) test_L = eval(test_data) print('Best test loss %.2f, test ppl %.2f'%(test_L, math.exp(test_L))) diff --git a/python/mxnet/gluon/block.py b/python/mxnet/gluon/block.py index 0f415436116a..17205267d94d 100644 --- a/python/mxnet/gluon/block.py +++ b/python/mxnet/gluon/block.py @@ -16,7 +16,7 @@ # under the License. # coding: utf-8 -# pylint: disable= arguments-differ +# pylint: disable= arguments-differ, too-many-lines """Base container class for all neural network models.""" __all__ = ['Block', 'HybridBlock', 'SymbolBlock'] @@ -304,7 +304,7 @@ def _collect_params_with_prefix(self, prefix=''): ret.update(child._collect_params_with_prefix(prefix + name)) return ret - def save_params(self, filename): + def save_parameters(self, filename): """Save parameters to file. filename : str @@ -314,8 +314,23 @@ def save_params(self, filename): arg_dict = {key : val._reduce() for key, val in params.items()} ndarray.save(filename, arg_dict) - def load_params(self, filename, ctx=None, allow_missing=False, - ignore_extra=False): + def save_params(self, filename): + """[Deprecated] Please use save_parameters. + + Save parameters to file. + + filename : str + Path to file. + """ + warnings.warn("save_params is deprecated. Please use save_parameters.") + try: + self.collect_params().save(filename, strip_prefix=self.prefix) + except ValueError as e: + raise ValueError('%s\nsave_params is deprecated. Using ' \ + 'save_parameters may resolve this error.'%e.message) + + def load_parameters(self, filename, ctx=None, allow_missing=False, + ignore_extra=False): """Load parameters from file. filename : str @@ -355,6 +370,25 @@ def load_params(self, filename, ctx=None, allow_missing=False, params[name]._load_init(loaded[name], ctx) + def load_params(self, filename, ctx=None, allow_missing=False, + ignore_extra=False): + """[Deprecated] Please use load_parameters. + + Load parameters from file. + + filename : str + Path to parameter file. + ctx : Context or list of Context, default cpu() + Context(s) initialize loaded parameters on. + allow_missing : bool, default False + Whether to silently skip loading parameters not represents in the file. + ignore_extra : bool, default False + Whether to silently ignore parameters from the file that are not + present in this Block. + """ + warnings.warn("load_params is deprecated. Please use load_parameters.") + self.load_parameters(filename, ctx, allow_missing, ignore_extra) + def register_child(self, block, name=None): """Registers block as a child of self. :py:class:`Block` s assigned to self as attributes will be registered automatically.""" @@ -579,8 +613,8 @@ def infer_type(self, *args): self._infer_attrs('infer_type', 'dtype', *args) def export(self, path, epoch=0): - """Export HybridBlock to json format that can be loaded by `mxnet.mod.Module` - or the C++ interface. + """Export HybridBlock to json format that can be loaded by + `SymbolBlock.imports`, `mxnet.mod.Module` or the C++ interface. .. note:: When there are only one input, it will have name `data`. When there Are more than one inputs, they will be named as `data0`, `data1`, etc. @@ -681,6 +715,50 @@ class SymbolBlock(HybridBlock): >>> x = mx.nd.random.normal(shape=(16, 3, 224, 224)) >>> print(feat_model(x)) """ + @staticmethod + def imports(symbol_file, input_names, param_file=None, ctx=None): + """Import model previously saved by `HybridBlock.export` or + `Module.save_checkpoint` as a SymbolBlock for use in Gluon. + + Parameters + ---------- + symbol_file : str + Path to symbol file. + input_names : list of str + List of input variable names + param_file : str, optional + Path to parameter file. + ctx : Context, default None + The context to initialize SymbolBlock on. + + Returns + ------- + SymbolBlock + SymbolBlock loaded from symbol and parameter files. + + Examples + -------- + >>> net1 = gluon.model_zoo.vision.resnet18_v1( + ... prefix='resnet', pretrained=True) + >>> net1.hybridize() + >>> x = mx.nd.random.normal(shape=(1, 3, 32, 32)) + >>> out1 = net1(x) + >>> net1.export('net1', epoch=1) + >>> + >>> net2 = gluon.SymbolBlock.imports( + ... 'net1-symbol.json', ['data'], 'net1-0001.params') + >>> out2 = net2(x) + """ + sym = symbol.load(symbol_file) + if isinstance(input_names, str): + input_names = [input_names] + inputs = [symbol.var(i) for i in input_names] + ret = SymbolBlock(sym, inputs) + if param_file is not None: + ret.collect_params().load(param_file, ctx=ctx) + return ret + + def __init__(self, outputs, inputs, params=None): super(SymbolBlock, self).__init__(prefix=None, params=None) self._prefix = '' diff --git a/python/mxnet/gluon/model_zoo/vision/alexnet.py b/python/mxnet/gluon/model_zoo/vision/alexnet.py index 554994704604..fdb006258c2a 100644 --- a/python/mxnet/gluon/model_zoo/vision/alexnet.py +++ b/python/mxnet/gluon/model_zoo/vision/alexnet.py @@ -83,5 +83,5 @@ def alexnet(pretrained=False, ctx=cpu(), net = AlexNet(**kwargs) if pretrained: from ..model_store import get_model_file - net.load_params(get_model_file('alexnet', root=root), ctx=ctx) + net.load_parameters(get_model_file('alexnet', root=root), ctx=ctx) return net diff --git a/python/mxnet/gluon/model_zoo/vision/densenet.py b/python/mxnet/gluon/model_zoo/vision/densenet.py index 835336739a63..b03f5ce8d52a 100644 --- a/python/mxnet/gluon/model_zoo/vision/densenet.py +++ b/python/mxnet/gluon/model_zoo/vision/densenet.py @@ -141,7 +141,7 @@ def get_densenet(num_layers, pretrained=False, ctx=cpu(), net = DenseNet(num_init_features, growth_rate, block_config, **kwargs) if pretrained: from ..model_store import get_model_file - net.load_params(get_model_file('densenet%d'%(num_layers), root=root), ctx=ctx) + net.load_parameters(get_model_file('densenet%d'%(num_layers), root=root), ctx=ctx) return net def densenet121(**kwargs): diff --git a/python/mxnet/gluon/model_zoo/vision/inception.py b/python/mxnet/gluon/model_zoo/vision/inception.py index 6d75050b83f2..7c54691f1b59 100644 --- a/python/mxnet/gluon/model_zoo/vision/inception.py +++ b/python/mxnet/gluon/model_zoo/vision/inception.py @@ -216,5 +216,5 @@ def inception_v3(pretrained=False, ctx=cpu(), net = Inception3(**kwargs) if pretrained: from ..model_store import get_model_file - net.load_params(get_model_file('inceptionv3', root=root), ctx=ctx) + net.load_parameters(get_model_file('inceptionv3', root=root), ctx=ctx) return net diff --git a/python/mxnet/gluon/model_zoo/vision/mobilenet.py b/python/mxnet/gluon/model_zoo/vision/mobilenet.py index 7c3b7d643ebe..f75de2197f79 100644 --- a/python/mxnet/gluon/model_zoo/vision/mobilenet.py +++ b/python/mxnet/gluon/model_zoo/vision/mobilenet.py @@ -201,7 +201,7 @@ def get_mobilenet(multiplier, pretrained=False, ctx=cpu(), version_suffix = '{0:.2f}'.format(multiplier) if version_suffix in ('1.00', '0.50'): version_suffix = version_suffix[:-1] - net.load_params( + net.load_parameters( get_model_file('mobilenet%s' % version_suffix, root=root), ctx=ctx) return net @@ -233,7 +233,7 @@ def get_mobilenet_v2(multiplier, pretrained=False, ctx=cpu(), version_suffix = '{0:.2f}'.format(multiplier) if version_suffix in ('1.00', '0.50'): version_suffix = version_suffix[:-1] - net.load_params( + net.load_parameters( get_model_file('mobilenetv2_%s' % version_suffix, root=root), ctx=ctx) return net diff --git a/python/mxnet/gluon/model_zoo/vision/resnet.py b/python/mxnet/gluon/model_zoo/vision/resnet.py index 5ee67b510a88..da279b89583e 100644 --- a/python/mxnet/gluon/model_zoo/vision/resnet.py +++ b/python/mxnet/gluon/model_zoo/vision/resnet.py @@ -386,8 +386,8 @@ def get_resnet(version, num_layers, pretrained=False, ctx=cpu(), net = resnet_class(block_class, layers, channels, **kwargs) if pretrained: from ..model_store import get_model_file - net.load_params(get_model_file('resnet%d_v%d'%(num_layers, version), - root=root), ctx=ctx) + net.load_parameters(get_model_file('resnet%d_v%d'%(num_layers, version), + root=root), ctx=ctx) return net def resnet18_v1(**kwargs): diff --git a/python/mxnet/gluon/model_zoo/vision/squeezenet.py b/python/mxnet/gluon/model_zoo/vision/squeezenet.py index 09f62a520740..aaff4c36dfa0 100644 --- a/python/mxnet/gluon/model_zoo/vision/squeezenet.py +++ b/python/mxnet/gluon/model_zoo/vision/squeezenet.py @@ -132,7 +132,7 @@ def get_squeezenet(version, pretrained=False, ctx=cpu(), net = SqueezeNet(version, **kwargs) if pretrained: from ..model_store import get_model_file - net.load_params(get_model_file('squeezenet%s'%version, root=root), ctx=ctx) + net.load_parameters(get_model_file('squeezenet%s'%version, root=root), ctx=ctx) return net def squeezenet1_0(**kwargs): diff --git a/python/mxnet/gluon/model_zoo/vision/vgg.py b/python/mxnet/gluon/model_zoo/vision/vgg.py index dbae53858983..a3b1685b4130 100644 --- a/python/mxnet/gluon/model_zoo/vision/vgg.py +++ b/python/mxnet/gluon/model_zoo/vision/vgg.py @@ -114,8 +114,8 @@ def get_vgg(num_layers, pretrained=False, ctx=cpu(), if pretrained: from ..model_store import get_model_file batch_norm_suffix = '_bn' if kwargs.get('batch_norm') else '' - net.load_params(get_model_file('vgg%d%s'%(num_layers, batch_norm_suffix), - root=root), ctx=ctx) + net.load_parameters(get_model_file('vgg%d%s'%(num_layers, batch_norm_suffix), + root=root), ctx=ctx) return net def vgg11(**kwargs): diff --git a/tests/python/unittest/test_gluon.py b/tests/python/unittest/test_gluon.py index 0a5bda831d9c..79b4fa347d63 100644 --- a/tests/python/unittest/test_gluon.py +++ b/tests/python/unittest/test_gluon.py @@ -96,20 +96,20 @@ def forward(self, x): net1.collect_params().initialize() net2(mx.nd.zeros((3, 5))) - net1.save_params('net1.params') + net1.save_parameters('net1.params') net3 = Net(prefix='net3_') - net3.load_params('net1.params', mx.cpu()) + net3.load_parameters('net1.params', mx.cpu()) net4 = Net(prefix='net4_') net5 = Net(prefix='net5_', in_units=5, params=net4.collect_params()) net4.collect_params().initialize() net5(mx.nd.zeros((3, 5))) - net4.save_params('net4.params') + net4.save_parameters('net4.params') net6 = Net(prefix='net6_') - net6.load_params('net4.params', mx.cpu()) + net6.load_parameters('net4.params', mx.cpu()) @with_seed() @@ -672,7 +672,7 @@ def test_export(): model = gluon.model_zoo.vision.resnet18_v1( prefix='resnet', ctx=ctx, pretrained=True) model.hybridize() - data = mx.nd.random.normal(shape=(1, 3, 224, 224)) + data = mx.nd.random.normal(shape=(1, 3, 32, 32)) out = model(data) model.export('gluon') @@ -690,6 +690,22 @@ def test_export(): assert_almost_equal(out.asnumpy(), out2.asnumpy()) +@with_seed() +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()) @with_seed() def test_hybrid_stale_cache(): @@ -806,7 +822,7 @@ def test_fill_shape_load(): net1.hybridize() net1.initialize(ctx=ctx) net1(mx.nd.ones((2,3,5,7), ctx)) - net1.save_params('net_fill.params') + net1.save_parameters('net_fill.params') net2 = nn.HybridSequential() with net2.name_scope(): @@ -815,7 +831,7 @@ def test_fill_shape_load(): nn.Dense(10)) net2.hybridize() net2.initialize() - net2.load_params('net_fill.params', ctx) + net2.load_parameters('net_fill.params', ctx) assert net2[0].weight.shape[1] == 3, net2[0].weight.shape[1] assert net2[1].gamma.shape[0] == 64, net2[1].gamma.shape[0] assert net2[2].weight.shape[1] == 3072, net2[2].weight.shape[1] @@ -959,13 +975,80 @@ def test_req(): def test_save_load(): net = mx.gluon.model_zoo.vision.get_resnet(1, 18, pretrained=True) - net.save_params('test.params') + net.save_parameters('test_save_load.params') net = mx.gluon.model_zoo.vision.get_resnet(1, 18) net.output = mx.gluon.nn.Dense(1000) - net.load_params('test.params') + net.load_parameters('test_save_load.params') + +@with_seed() +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()) + +@with_seed() +def test_hybrid_multi_context(): + net = mx.gluon.model_zoo.vision.get_resnet(1, 18) + net.initialize(ctx=[mx.cpu(0), mx.cpu(1)]) + net.hybridize() + net(mx.nd.zeros((1, 3, 32, 32), ctx=mx.cpu(0))).asnumpy() + +@with_seed() +def test_zero_grad(): + data = mx.nd.random.uniform(shape=(3,3)) + net = nn.Embedding(3, 4, prefix='test_zero_grad_') + net.initialize() + with mx.autograd.record(): + l = net(data) + l.backward() + net.collect_params().zero_grad() + grad = net.collect_params()['test_zero_grad_weight'].grad() + assert_almost_equal(grad.asnumpy(), grad.asnumpy() * 0) + +def check_hybrid_static_memory(**kwargs): + x = mx.nd.random.uniform(shape=(2, 3, 32, 32)) + x.attach_grad() + + +@with_seed() +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()) if __name__ == '__main__':