mxnet.gluon.data.vision.transforms.image¶
Image transforms.
Classes
|
Crops the image src to the given size by trimming on all four sides and preserving the center of the image. |
|
Crop the input image with and optionally resize it. |
|
Normalize an tensor of shape (C x H x W) or (N x C x H x W) with mean and standard deviation. |
|
Randomly jitters image brightness with a factor chosen from [max(0, 1 - brightness), 1 + brightness]. |
|
Randomly jitters the brightness, contrast, saturation, and hue of an image. |
|
Randomly jitters image contrast with a factor chosen from [max(0, 1 - contrast), 1 + contrast]. |
|
Randomly crop src with size (width, height). |
|
Randomly flip the input image left to right with a probability of p(0.5 by default). |
|
Randomly flip the input image top to bottom with a probability of p(0.5 by default). |
|
Randomly convert to gray image. |
|
Randomly jitters image hue with a factor chosen from [max(0, 1 - hue), 1 + hue]. |
|
Add AlexNet-style PCA-based noise to an image. |
|
Crop the input image with random scale and aspect ratio. |
|
Random rotate the input image by a random angle. |
|
Randomly jitters image saturation with a factor chosen from [max(0, 1 - saturation), 1 + saturation]. |
|
Resize an image or a batch of image NDArray to the given size. |
|
Rotate the input image by a given angle. |
|
Converts an image NDArray or batch of image NDArray to a tensor NDArray. |
- class mxnet.gluon.data.vision.transforms.image.CenterCrop(size, interpolation=1)[source]¶
Bases:
HybridBlockCrops the image src to the given size by trimming on all four sides and preserving the center of the image. Upsamples if src is smaller than size.
- Parameters:
- Inputs:
data: input tensor with (Hi x Wi x C) shape.
- Outputs:
out: output tensor with (H x W x C) shape.
Examples
>>> transformer = vision.transforms.CenterCrop(size=(1000, 500)) >>> image = mx.nd.random.uniform(0, 255, (2321, 3482, 3)).astype(dtype=np.uint8) >>> transformer(image) <NDArray 500x1000x3 @cpu(0)>
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.CropResize(x, y, width, height, size=None, interpolation=None)[source]¶
Bases:
HybridBlockCrop the input image with and optionally resize it.
Makes a crop of the original image then optionally resize it to the specified size.
- Parameters:
x (int) – Left boundary of the cropping area
y (int) – Top boundary of the cropping area
w (int) – Width of the cropping area
h (int) – Height of the cropping area
size (int or tuple of (w, h)) – Optional, resize to new size after cropping
interpolation (int, optional) – Interpolation method for resizing. By default uses bilinear interpolation. See OpenCV’s resize function for available choices. https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html?highlight=resize#resize Note that the Resize on gpu use contrib.bilinearResize2D operator which only support bilinear interpolation(1).
- Inputs:
data: input tensor with (H x W x C) or (N x H x W x C) shape.
- Outputs:
out: input tensor with (H x W x C) or (N x H x W x C) shape.
Examples
>>> transformer = vision.transforms.CropResize(x=0, y=0, width=100, height=100) >>> image = mx.nd.random.uniform(0, 255, (224, 224, 3)).astype(dtype=np.uint8) >>> transformer(image) <NDArray 100x100x3 @cpu(0)> >>> image = mx.nd.random.uniform(0, 255, (3, 224, 224, 3)).astype(dtype=np.uint8) >>> transformer(image) <NDArray 3x100x100x3 @cpu(0)> >>> transformer = vision.transforms.CropResize(x=0, y=0, width=100, height=100, size=(50, 50), interpolation=1) >>> transformer(image) <NDArray 3x50x50 @cpu(0)>
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.Normalize(mean=0.0, std=1.0)[source]¶
Bases:
HybridBlockNormalize an tensor of shape (C x H x W) or (N x C x H x W) with mean and standard deviation.
Given mean (m1, …, mn) and std (s1, …, sn) for n channels, this transform normalizes each channel of the input tensor with:
output[i] = (input[i] - mi) / si
If mean or std is scalar, the same value will be applied to all channels.
- Parameters:
- Inputs:
data: input tensor with (C x H x W) or (N x C x H x W) shape.
- Outputs:
out: output tensor with the shape as data.
Examples
>>> transformer = transforms.Normalize(mean=(0, 1, 2), std=(3, 2, 1)) >>> image = mx.nd.random.uniform(0, 1, (3, 4, 2)) >>> transformer(image) [[[ 0.18293785 0.19761486] [ 0.23839645 0.28142193] [ 0.20092112 0.28598186] [ 0.18162774 0.28241724]] [[-0.2881726 -0.18821815] [-0.17705294 -0.30780914] [-0.2812064 -0.3512327 ] [-0.05411351 -0.4716435 ]] [[-1.0363373 -1.7273437 ] [-1.6165586 -1.5223348 ] [-1.208275 -1.1878313 ] [-1.4711051 -1.5200229 ]]] <NDArray 3x4x2 @cpu(0)>
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.RandomBrightness(brightness)[source]¶
Bases:
HybridBlockRandomly jitters image brightness with a factor chosen from [max(0, 1 - brightness), 1 + brightness].
- Parameters:
brightness (float) – How much to jitter brightness. brightness factor is randomly chosen from [max(0, 1 - brightness), 1 + brightness].
- Inputs:
data: input tensor with (H x W x C) shape.
- Outputs:
out: output tensor with same shape as data.
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.RandomColorJitter(brightness=0, contrast=0, saturation=0, hue=0)[source]¶
Bases:
HybridBlockRandomly jitters the brightness, contrast, saturation, and hue of an image.
- Parameters:
brightness (float) – How much to jitter brightness. brightness factor is randomly chosen from [max(0, 1 - brightness), 1 + brightness].
contrast (float) – How much to jitter contrast. contrast factor is randomly chosen from [max(0, 1 - contrast), 1 + contrast].
saturation (float) – How much to jitter saturation. saturation factor is randomly chosen from [max(0, 1 - saturation), 1 + saturation].
hue (float) – How much to jitter hue. hue factor is randomly chosen from [max(0, 1 - hue), 1 + hue].
- Inputs:
data: input tensor with (H x W x C) shape.
- Outputs:
out: output tensor with same shape as data.
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.RandomContrast(contrast)[source]¶
Bases:
HybridBlockRandomly jitters image contrast with a factor chosen from [max(0, 1 - contrast), 1 + contrast].
- Parameters:
contrast (float) – How much to jitter contrast. contrast factor is randomly chosen from [max(0, 1 - contrast), 1 + contrast].
- Inputs:
data: input tensor with (H x W x C) shape.
- Outputs:
out: output tensor with same shape as data.
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.RandomCrop(size, pad=None, pad_value=0, interpolation=1)[source]¶
Bases:
HybridBlockRandomly crop src with size (width, height). Padding is optional. Upsample result if src is smaller than size .
- Parameters:
if int, size of the zero-padding if tuple, number of values padded to the edges of each axis.
((before_1, after_1), … (before_N, after_N)) unique pad widths for each axis. ((before, after),) yields same before and after pad for each axis. (pad,) or int is a shortcut for before = after = pad width for all axes.
pad_value (int) – The value to use for padded pixels
interpolation (int) – Interpolation method for resizing. By default uses bilinear interpolation. See OpenCV’s resize function for available choices.
Inputs –
data: input tensor with (Hi x Wi x C) shape.
Outputs –
out: output tensor with ((H+2*pad) x (W+2*pad) x C) shape.
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.RandomFlipLeftRight(p=0.5)[source]¶
Bases:
HybridBlockRandomly flip the input image left to right with a probability of p(0.5 by default).
- Inputs:
data: input tensor with (H x W x C) or (N x H x W x C) shape.
- Outputs:
out: output tensor with same shape as data.
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.RandomFlipTopBottom(p=0.5)[source]¶
Bases:
HybridBlockRandomly flip the input image top to bottom with a probability of p(0.5 by default).
- Inputs:
data: input tensor with (H x W x C) or (N x H x W x C) shape.
- Outputs:
out: output tensor with same shape as data.
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.RandomGray(p=0.5)[source]¶
Bases:
HybridBlockRandomly convert to gray image.
- Parameters:
p (float) – Probability to convert to grayscale
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.RandomHue(hue)[source]¶
Bases:
HybridBlockRandomly jitters image hue with a factor chosen from [max(0, 1 - hue), 1 + hue].
- Parameters:
hue (float) – How much to jitter hue. hue factor is randomly chosen from [max(0, 1 - hue), 1 + hue].
- Inputs:
data: input tensor with (H x W x C) shape.
- Outputs:
out: output tensor with same shape as data.
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.RandomLighting(alpha)[source]¶
Bases:
HybridBlockAdd AlexNet-style PCA-based noise to an image.
- Parameters:
alpha (float) – Intensity of the image.
- Inputs:
data: input tensor with (H x W x C) shape.
- Outputs:
out: output tensor with same shape as data.
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.RandomResizedCrop(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=1)[source]¶
Bases:
HybridBlockCrop the input image with random scale and aspect ratio.
Makes a crop of the original image with random size (default: 0.08 to 1.0 of the original image size) and random aspect ratio (default: 3/4 to 4/3), then resize it to the specified size.
- Parameters:
scale (tuple of two floats) – If scale is (min_area, max_area), the cropped image’s area will range from min_area to max_area of the original image’s area
ratio (tuple of two floats) – Range of aspect ratio of the cropped image before resizing.
interpolation (int) – Interpolation method for resizing. By default uses bilinear interpolation. See OpenCV’s resize function for available choices.
- Inputs:
data: input tensor with (Hi x Wi x C) shape.
- Outputs:
out: output tensor with (H x W x C) shape.
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.RandomRotation(angle_limits, zoom_in=False, zoom_out=False, rotate_with_proba=1.0)[source]¶
Bases:
Block- Random rotate the input image by a random angle.
Keeps the original image shape and aspect ratio.
- Parameters:
angle_limits (tuple) – Tuple of 2 elements containing the upper and lower limit for rotation angles in degree.
zoom_in (bool) – Zoom in image so that no padding is present in final output.
zoom_out (bool) – Zoom out image so that the entire original image is present in final output.
rotate_with_proba (float32)
- Inputs:
data: input tensor with (C x H x W) or (N x C x H x W) shape.
- Outputs:
out: output tensor with (C x H x W) or (N x C x H x W) shape.
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- forward(x, *args)[source]¶
Overrides to implement forward computation using
NDArray. Only accepts positional arguments.
- hybridize(active=True, **kwargs)¶
Please refer description of HybridBlock hybridize().
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install callback monitor.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
Block.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.RandomSaturation(saturation)[source]¶
Bases:
HybridBlockRandomly jitters image saturation with a factor chosen from [max(0, 1 - saturation), 1 + saturation].
- Parameters:
saturation (float) – How much to jitter saturation. saturation factor is randomly chosen from [max(0, 1 - saturation), 1 + saturation].
- Inputs:
data: input tensor with (H x W x C) shape.
- Outputs:
out: output tensor with same shape as data.
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.Resize(size, keep_ratio=False, interpolation=1)[source]¶
Bases:
HybridBlockResize an image or a batch of image NDArray to the given size. Should be applied before mxnet.gluon.data.vision.transforms.ToTensor.
- Parameters:
keep_ratio (bool) – Whether to resize the short edge or both edges to size, if size is give as an integer.
interpolation (int) – Interpolation method for resizing. By default uses bilinear interpolation. See OpenCV’s resize function for available choices. Note that the Resize on gpu use contrib.bilinearResize2D operator which only support bilinear interpolation(1).
- Inputs:
data: input tensor with (H x W x C) or (N x H x W x C) shape.
- Outputs:
out: output tensor with (H x W x C) or (N x H x W x C) shape.
Examples
>>> transformer = vision.transforms.Resize(size=(1000, 500)) >>> image = mx.nd.random.uniform(0, 255, (224, 224, 3)).astype(dtype=np.uint8) >>> transformer(image) <NDArray 500x1000x3 @cpu(0)> >>> image = mx.nd.random.uniform(0, 255, (3, 224, 224, 3)).astype(dtype=np.uint8) >>> transformer(image) <NDArray 3x500x1000x3 @cpu(0)>
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.Rotate(rotation_degrees, zoom_in=False, zoom_out=False)[source]¶
Bases:
BlockRotate the input image by a given angle. Keeps the original image shape.
- Parameters:
- Inputs:
data: input tensor with (C x H x W) or (N x C x H x W) shape.
- Outputs:
out: output tensor with (C x H x W) or (N x C x H x W) shape.
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- forward(x, *args)[source]¶
Overrides to implement forward computation using
NDArray. Only accepts positional arguments.
- hybridize(active=True, **kwargs)¶
Please refer description of HybridBlock hybridize().
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install callback monitor.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
Block.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.
- class mxnet.gluon.data.vision.transforms.image.ToTensor[source]¶
Bases:
HybridBlockConverts an image NDArray or batch of image NDArray to a tensor NDArray.
Converts an image NDArray of shape (H x W x C) in the range [0, 255] to a float32 tensor NDArray of shape (C x H x W) in the range [0, 1].
If batch input, converts a batch image NDArray of shape (N x H x W x C) in the range [0, 255] to a float32 tensor NDArray of shape (N x C x H x W).
- Inputs:
data: input tensor with (H x W x C) or (N x H x W x C) shape and uint8 type.
- Outputs:
out: output tensor with (C x H x W) or (N x C x H x W) shape and float32 type.
Examples
>>> transformer = vision.transforms.ToTensor() >>> image = mx.nd.random.uniform(0, 255, (4, 2, 3)).astype(dtype=np.uint8) >>> transformer(image) [[[ 0.85490197 0.72156864] [ 0.09019608 0.74117649] [ 0.61960787 0.92941177] [ 0.96470588 0.1882353 ]] [[ 0.6156863 0.73725492] [ 0.46666667 0.98039216] [ 0.44705883 0.45490196] [ 0.01960784 0.8509804 ]] [[ 0.39607844 0.03137255] [ 0.72156864 0.52941179] [ 0.16470589 0.7647059 ] [ 0.05490196 0.70588237]]] <NDArray 3x4x2 @cpu(0)>
- apply(fn)¶
Applies
fnrecursively to every child block as well as self.- Parameters:
fn (callable) – Function to be applied to each submodule, of form fn(block).
- Return type:
this block
- cast(dtype)¶
Cast this Block to use another data type.
- Parameters:
dtype (str or numpy.dtype) – The new data type.
- collect_params(select=None)¶
Returns a
Dictcontaining thisBlockand all of its children’s Parameters(default), also can returns the selectDictwhich match some given regular expressions.For example, collect the specified parameters in [‘conv1.weight’, ‘conv1.bias’, ‘fc.weight’, ‘fc.bias’]:
model.collect_params('conv1.weight|conv1.bias|fc.weight|fc.bias')
or collect all parameters whose names end with ‘weight’ or ‘bias’, this can be done using regular expressions:
model.collect_params('.*weight|.*bias')
- Parameters:
select (str) – regular expressions
- Return type:
The selected
Dict
- export(path, epoch=0, remove_amp_cast=True)¶
Export HybridBlock to json format that can be loaded by gluon.SymbolBlock.imports 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.
- Parameters:
path (str or None) – Path to save model. Two files path-symbol.json and path-xxxx.params will be created, where xxxx is the 4 digits epoch number. If None, do not export to file but return Python Symbol object and corresponding dictionary of parameters.
epoch (int) – Epoch number of saved model.
remove_amp_cast (bool, optional) – Whether to remove the amp_cast and amp_multicast operators, before saving the model.
- Returns:
symbol_filename (str) – Filename to which model symbols were saved, including path prefix.
params_filename (str) – Filename to which model parameters were saved, including path prefix.
- forward(x, *args)[source]¶
Overrides the forward computation. Arguments must be
mxnet.numpy.ndarray.
- hybridize(active=True, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None)¶
Activates or deactivates
HybridBlocks recursively. Has no effect on non-hybrid children.- Parameters:
active (bool, default True) – Whether to turn hybrid on or off.
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
- infer_shape(*args)¶
Infers shape of Parameters from inputs.
- infer_type(*args)¶
Infers data type of Parameters from inputs.
- initialize(init=<mxnet.initializer.Uniform object>, device=None, verbose=False, force_reinit=False)¶
Initializes
Parameters of thisBlockand its children.- Parameters:
init (Initializer) – Global default Initializer to be used when
Parameter.init()isNone. Otherwise,Parameter.init()takes precedence.device (Device or list of Device) – Keeps a copy of Parameters on one or many device(s).
verbose (bool, default False) – Whether to verbosely print out details on initialization.
force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
- load(prefix)¶
Load a model saved using the save API
Reconfigures a model using the saved configuration. This function does not regenerate the model architecture. It resets each Block’s parameter UUIDs as they were when saved in order to match the names of the saved parameters.
This function assumes the Blocks in the model were created in the same order they were when the model was saved. This is because each Block is uniquely identified by Block class name and a unique ID in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph (Symbol & inputs) and settings are restored if it had been hybridized before saving.
- Parameters:
prefix (str) – The prefix to use in filenames for loading this model: <prefix>-model.json and <prefix>-model.params
- load_dict(param_dict, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from dict
- Parameters:
param_dict (dict) – Dictionary containing model parameters
device (Device, optional) – Device context on which the memory is allocated. Default is mxnet.device.current_device().
allow_missing (bool, default False) – Whether to silently skip loading parameters not represented in the file.
ignore_extra (bool, default False) – Whether to silently ignore parameters from the file that are not present in this dict.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
- load_parameters(filename, device=None, allow_missing=False, ignore_extra=False, cast_dtype=False, dtype_source='current')¶
Load parameters from file previously saved by save_parameters.
- Parameters:
filename (str) – Path to parameter file.
device (Device or list of Device, default cpu()) – Device(s) to 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.
cast_dtype (bool, default False) – Cast the data type of the NDArray loaded from the checkpoint to the dtype provided by the Parameter if any.
dtype_source (str, default 'current') – must be in {‘current’, ‘saved’} Only valid if cast_dtype=True, specify the source of the dtype for casting the parameters
References
- optimize_for(x, *args, backend=None, clear=False, partition_if_dynamic=True, static_alloc=False, static_shape=False, inline_limit=2, forward_bulk_size=None, backward_bulk_size=None, **kwargs)¶
Partitions the current HybridBlock and optimizes it for a given backend without executing a forward pass. Modifies the HybridBlock in-place.
Immediately partitions a HybridBlock using the specified backend. Combines the work done in the hybridize API with part of the work done in the forward pass without calling the CachedOp. Can be used in place of hybridize, afterwards export can be called or inference can be run. See README.md in example/extensions/lib_subgraph/README.md for more details.
Examples
# partition and then export to file block.optimize_for(x, backend=’myPart’) block.export(‘partitioned’)
# partition and then run inference block.optimize_for(x, backend=’myPart’) block(x)
- Parameters:
x (NDArray) – first input to model
*args (NDArray) – other inputs to model
backend (str) – The name of backend, as registered in SubgraphBackendRegistry, default None
backend_opts (dict of user-specified options to pass to the backend for partitioning, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
clear (bool, default False) – clears any previous optimizations
partition_if_dynamic (bool, default False) – whether to partition the graph when dynamic shape op exists
static_alloc (bool, default False) – Statically allocate memory to improve speed. Memory usage may increase.
static_shape (bool, default False) – Optimize for invariant input shapes between iterations. Must also set static_alloc to True. Change of input shapes is still allowed but slower.
inline_limit (optional int, default 2) – Maximum number of operators that can be inlined.
forward_bulk_size (optional int, default None) – Segment size of bulk execution during forward pass.
backward_bulk_size (optional int, default None) – Segment size of bulk execution during backward pass.
**kwargs (The backend options, optional) – Passed on to PrePartition and PostPartition functions of SubgraphProperty
- property params¶
Returns this
Block’s parameter dictionary (does not include its children’s parameters).
- register_child(block, name=None)¶
Registers block as a child of self.
Blocks assigned to self as attributes will be registered automatically.
- register_forward_hook(hook)¶
Registers a forward hook on the block.
The hook function is called immediately after
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input, output) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_forward_pre_hook(hook)¶
Registers a forward pre-hook on the block.
The hook function is called immediately before
forward(). It should not modify the input or output.- Parameters:
hook (callable) – The forward hook function of form hook(block, input) -> None.
- Return type:
mxnet.gluon.utils.HookHandle
- register_op_hook(callback, monitor_all=False)¶
Install op hook for block recursively.
- Parameters:
callback (function) – Function called to inspect the values of the intermediate outputs of blocks after hybridization. It takes 3 parameters: name of the tensor being inspected (str) name of the operator producing or consuming that tensor (str) tensor being inspected (NDArray).
monitor_all (bool, default False) – If True, monitor both input and output, otherwise monitor output only.
- reset_ctx(ctx)¶
This function has been deprecated. Please refer to
HybridBlock.reset_device.
- reset_device(device)¶
Re-assign all Parameters to other devices. If the Block is hybridized, it will reset the _cached_op_args.
- Parameters:
device (Device or list of Device, default
device.current_device().) – Assign Parameter to given device. If device is a list of Device, a copy will be made for each device.
- save(prefix)¶
Save the model architecture and parameters to load again later
Saves the model architecture as a nested dictionary where each Block in the model is a dictionary and its children are sub-dictionaries.
Each Block is uniquely identified by Block class name and a unique ID. We save each Block’s parameter UUID to restore later in order to match the saved parameters.
Recursively traverses a Block’s children in order (since its an OrderedDict) and uses the unique ID to denote that specific Block.
Assumes that the model is created in an identical order every time. If the model is not able to be recreated deterministically do not use this set of APIs to save/load your model.
For HybridBlocks, the cached_graph is saved (Symbol & inputs) if it has already been hybridized.
- Parameters:
prefix (str) – The prefix to use in filenames for saving this model: <prefix>-model.json and <prefix>-model.params
- save_parameters(filename, deduplicate=False)¶
Save parameters to file.
Saved parameters can only be loaded with load_parameters. Note that this method only saves parameters, not model structure. If you want to save model structures, please use
HybridBlock.export().- Parameters:
References
- setattr(name, value)¶
Set an attribute to a new value for all Parameters.
For example, set grad_req to null if you don’t need gradient w.r.t a model’s Parameters:
model.setattr('grad_req', 'null')
or change the learning rate multiplier:
model.setattr('lr_mult', 0.5)
- Parameters:
name (str) – Name of the attribute.
value (valid type for attribute name) – The new value for the attribute.
Share parameters recursively inside the model.
For example, if you want
dense1to sharedense0’s weights, you can do:dense0 = nn.Dense(20) dense1 = nn.Dense(20) dense1.share_parameters(dense0.collect_params())
- which equals to
dense1.weight = dense0.weight dense1.bias = dense0.bias
Note that unlike the load_parameters or load_dict functions, share_parameters results in the Parameter object being shared (or tied) between the models, whereas load_parameters or load_dict only set the value of the data dictionary of a model. If you call load_parameters or load_dict after share_parameters, the loaded value will be reflected in all networks that use the shared (or tied) Parameter object.
- Parameters:
shared (Dict) – Dict of the shared parameters.
- Return type:
this block
- summary(*inputs)¶
Print the summary of the model’s output and parameters.
The network must have been initialized, and must not have been hybridized.
- Parameters:
inputs (object) – Any input that the model supports. For any tensor in the input, only
mxnet.ndarray.NDArrayis supported.
- zero_grad()¶
Sets all Parameters’ gradient buffer to 0.