tinyms.model

class tinyms.model.Model(network)[源代码]

High-Level API for Training or Evaluation.

Model groups layers into an object with training and inference features.

参数

network (layers.Layer) – A training or testing network.

实际案例

>>> from tinyms.model import Model, lenet5
>>> form tinyms.losses import SoftmaxCrossEntropyWithLogits
>>> from tinyms.optimizers import Momentum
>>>
>>> net = lenet5(class_num=10)
>>> model = Model(net)
>>> net_loss = SoftmaxCrossEntropyWithLogits()
>>> net_opt = Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> model.compile(loss_fn=net_loss, optimizer=net_opt, metrics=None)
>>> # For details about how to build the dataset, please refer to the API document on the official website.
>>> ds_train = create_custom_dataset()
>>> model.train(2, ds_train)
compile(loss_fn=None, optimizer=None, metrics=None, eval_network=None, amp_level='O0', **kwargs)[源代码]

High-Level API for configure the train or eval network.

参数
  • loss_fn (layers.Layer) – Objective function, if loss_fn is None, the network should contain the logic of loss and grads calculation, and the logic of parallel if needed. Default: None.

  • optimizer (layers.Layer) – Optimizer for updating the weights. Default: None.

  • metrics (Union[dict, set]) – A Dictionary or a set of metrics to be evaluated by the model during training and testing. eg: {‘accuracy’, ‘recall’}. Default: None.

  • eval_network (layers.Layer) – Network for evaluation. If not defined, network and loss_fn would be wrapped as eval_network. Default: None.

  • amp_level (str) –

    Option for argument level in mindspore.amp.build_train_network, level for mixed precision training. Supports [“O0”, “O2”, “O3”, “auto”]. Default: “O0”.

    • O0: Do not change.

    • O2: Cast network to float16, keep batchnorm run in float32, using dynamic loss scale.

    • O3: Cast network to float16, with additional property ‘keep_batchnorm_fp32=False’.

    • auto: Set to level to recommended level in different devices. Set level to O2 on GPU, Set

    level to O3 Ascend. The recommended level is choose by the export experience, cannot always generalize. User should specify the level for special network.

    O2 is recommended on GPU, O3 is recommended on Ascend.

eval(valid_dataset, callbacks=None, dataset_sink_mode=True)[源代码]

Evaluation API where the iteration is controlled by python front-end.

Configure to pynative mode or CPU, the evaluating process will be performed with dataset non-sink mode.

注解

If dataset_sink_mode is True, data will be sent to device. If device is Ascend, features of data will be transferred one by one. The limitation of data transmission per time is 256M.

参数
  • valid_dataset (Dataset) – Dataset to evaluate the model.

  • callbacks (Optional[list(Callback)]) – List of callback objects which should be executed while training. Default: None.

  • dataset_sink_mode (bool) – Determines whether to pass the data through dataset channel. Default: True.

返回

Dict, which returns the loss value and metrics values for the model in the test mode.

实际案例

>>> from mindspore import Model, nn
>>>
>>> # For details about how to build the dataset, please refer to the tutorial
>>> # document on the official website.
>>> dataset = create_custom_dataset()
>>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> model = Model(net, loss_fn=loss, optimizer=None, metrics={'acc'})
>>> acc = model.eval(dataset, dataset_sink_mode=False)
property eval_network

Get the model’s eval_network.

export(inputs, file_name, file_format='MINDIR', **kwargs)[源代码]

Export the TinyMS prediction model to a file in the specified format.

参数
  • inputs (Tensor) – Inputs of the net.

  • file_name (str) – File name of the model to be exported.

  • file_format (str) –

    MindSpore currently supports AIR, ONNX and MINDIR format for exported model. Default: MINDIR.

    • AIR: Ascend Intermediate Representation. An intermediate representation format of Ascend model.

    Recommended suffix for output file is ‘.air’.

    • ONNX: Open Neural Network eXchange. An open format built to represent machine learning models.

    Recommended suffix for output file is ‘.onnx’.

    • MINDIR: MindSpore Native Intermediate Representation for Anf. An intermediate representation format

    for MindSpore models. Recommended suffix for output file is ‘.mindir’.

  • kwargs (dict) –

    Configuration options dictionary.

    • quant_mode: The mode of quant.

    • mean: Input data mean. Default: 127.5.

    • std_dev: Input data variance. Default: 127.5.

infer_predict_layout(*predict_data)[源代码]

Generate parameter layout for the predict network in auto or semi auto parallel mode.

Data could be a single tensor or multiple tensors.

注解

Batch data should be put together in one tensor.

参数

predict_data (Tensor) – One tensor or multiple tensors of predict data.

返回

Dict, Parameter layout dictionary used for load distributed checkpoint

实际案例

>>> import numpy as np
>>> import mindspore as ms
>>> from mindspore import Model, context, Tensor
>>> from mindspore.context import ParallelMode
>>>
>>> context.set_context(mode=context.GRAPH_MODE)
>>> context.set_auto_parallel_context(full_batch=True, parallel_mode=ParallelMode.SEMI_AUTO_PARALLEL)
>>> input_data = Tensor(np.random.randint(0, 255, [1, 3, 224, 224]), ms.float32)
>>> model = Model(Net())
>>> model.infer_predict_layout(input_data)
load_checkpoint(ckpt_file_name, strict_load=False)[源代码]

Loads checkpoint info from a specified file.

参数
  • ckpt_file_name (str) – Checkpoint file name.

  • strict_load (bool) – Whether to strict load the parameter into net. If False, it will load parameter in the param_dict into net with the same suffix. Default: False.

返回

Dict, key is parameter name, value is a Parameter.

引发

ValueError – Checkpoint file is incorrect.

实际案例

>>> ckpt_file_name = "./checkpoint/LeNet5-1_32.ckpt"
>>> param_dict = model.load_checkpoint(ckpt_file_name)
predict(*predict_data)[源代码]

Generate output predictions for the input samples.

Data could be a single tensor, a list of tensor, or a tuple of tensor.

注解

Batch data should be put together in one tensor.

参数

predict_data (Tensor) – The predict data, can be bool, int, float, str, None, tensor, or tuple, list and dict that store these types.

返回

Tensor, array(s) of predictions.

实际案例

>>> import mindspore as ms
>>> from mindspore import Model, Tensor
>>>
>>> input_data = Tensor(np.random.randint(0, 255, [1, 1, 32, 32]), ms.float32)
>>> model = Model(Net())
>>> result = model.predict(input_data)
property predict_network

Get the model’s predict_network.

save_checkpoint(ckpt_file_name)[源代码]

Saves checkpoint info to a specified file.

参数

ckpt_file_name (str) – Checkpoint file name. If the file name already exists, it will be overwritten.

引发

TypeError – If the parameter save_obj is not layers.Layer or list type.

train(epoch, train_dataset, callbacks=None, dataset_sink_mode=True, sink_size=-1)[源代码]

Training API where the iteration is controlled by python front-end.

When setting pynative mode or CPU, the training process will be performed with dataset not sink.

注解

If dataset_sink_mode is True, data will be sent to device. If device is Ascend, features of data will be transferred one by one. The limitation of data transmission per time is 256M. If sink_size > 0, each epoch the dataset can be traversed unlimited times until you get sink_size elements of the dataset. Next epoch continues to traverse from the end position of the previous traversal.

参数
  • epoch (int) – Generally, total number of iterations on the data per epoch. When dataset_sink_mode is set to true and sink_size>0, each epoch sink sink_size steps on the data instead of total number of iterations.

  • train_dataset (Dataset) – A training dataset iterator. If there is no loss_fn, a tuple with multiple data (data1, data2, data3, …) should be returned and passed to the network. Otherwise, a tuple (data, label) should be returned. The data and label would be passed to the network and loss function respectively.

  • callbacks (Optional[list[Callback], Callback]) – List of callback objects or callback object, which should be executed while training. Default: None.

  • dataset_sink_mode (bool) – Determines whether to pass the data through dataset channel. Default: True. Configure pynative mode or CPU, the training process will be performed with dataset not sink. Default: True.

  • sink_size (int) – Control the amount of data in each sink. If sink_size = -1, sink the complete dataset for each epoch. If sink_size > 0, sink sink_size data for each epoch. If dataset_sink_mode is False, set sink_size as invalid. Default: -1.

实际案例

>>> from mindspore import Model, nn
>>> from mindspore.train.loss_scale_manager import FixedLossScaleManager
>>>
>>> # For details about how to build the dataset, please refer to the tutorial
>>> # document on the official website.
>>> dataset = create_custom_dataset()
>>> net = Net()
>>> loss = nn.SoftmaxCrossEntropyWithLogits()
>>> loss_scale_manager = FixedLossScaleManager()
>>> optim = nn.Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
>>> model = Model(net, loss_fn=loss, optimizer=optim, metrics=None, loss_scale_manager=loss_scale_manager)
>>> model.train(2, dataset)
property train_network

Get the model’s train_network.

tinyms.model.load(file_name)[源代码]

Load MindIR graph and return the network with parameters.

The returned object is wrapperred by a GraphLayer. However, there are some limitations to the current use of GraphLayer, see class tinyms.layers.GraphLayer for more details.

参数

file_name (str) – MindIR file name.

返回

GraphLayer instance, a compiled graph with parameters.

引发

ValueError – MindIR file is incorrect.

实际案例

>>> import tinyms as ts
>>> import tinyms.layers as layers
>>> from tinyms.model import Model, load
>>>
>>> net = layers.Conv2d(1, 1, kernel_size=3)
>>> model = Model(net)
>>> input = ts.ones([1, 1, 3, 3])
>>> model.export(input, "net", file_format="MINDIR")
...
>>> net = load("net.mindir")
>>> print(net(input))
[[[[ 0.02548009  0.04010789  0.03120251]
   [ 0.00268656  0.02353744  0.03807815]
   [-0.00896441 -0.00303641  0.01502199]]]]
tinyms.model.lenet5(**kwargs)[源代码]

Get LeNet5 neural network.

参数

class_num (int) – Class number. Default: 10.

返回

layers.Layer, layer instance of LeNet5 neural network.

实际案例

>>> net = lenet5(class_num=10)
class tinyms.model.LeNet(class_num=10, channel_num=1)[源代码]

LeNet architecture.

参数
  • class_num (int) – The number of classes that the training images are belonging to.

  • channel_num (int) – The channel number.

返回

Tensor, output tensor.

实际案例

>>> LeNet(class_num=10)
tinyms.model.resnet50(**kwargs)[源代码]

Get ResNet50 neural network.

参数

class_num (int) – Class number. Default: 10.

返回

layers.Layer, layer instance of ResNet50 neural network.

实际案例

>>> net = resnet50(10)
class tinyms.model.ResNet(block, layer_nums, in_channels, out_channels, strides, num_classes)[源代码]

ResNet architecture.

参数
  • block (layers.Layer) – Block for network.

  • layer_nums (list) – Numbers of block in different layers.

  • in_channels (list) – Input channel in each layer.

  • out_channels (list) – Output channel in each layer.

  • strides (list) – Stride size in each layer.

  • num_classes (int) – The number of classes that the training images are belonging to.

返回

Tensor, output tensor.

实际案例

>>> ResNet(ResidualBlock,
>>>        [3, 4, 6, 3],
>>>        [64, 256, 512, 1024],
>>>        [256, 512, 1024, 2048],
>>>        [1, 2, 2, 2],
>>>        10)
tinyms.model.mobilenetv2(**kwargs)[源代码]

Get MobileNetV2 instance for model training, evaluation and prediction.

参数
  • class_num (int) – The number of classes. Default: 1000.

  • is_training (bool) – Whether to do training job, default: True.

返回

model.MobileNetV2, MobileNetV2 instance.

class tinyms.model.MobileNetV2(class_num=1000, width_mult=1.0, round_nearest=8, input_channel=32, last_channel=1280, is_training=True)[源代码]

MobileNetV2 architecture.

参数
  • class_num (int) – The number of classes.

  • width_mult (float) – Channels multiplier for round to 8/16 and others. Default is 1.0.

  • round_nearest (int) – Channel round to. Default is 8.

  • input_channel (int) – Input channel. Default is 32.

  • last_channel (int) – The channel of last layer. Default is 1280.

返回

Tensor, output tensor.

tinyms.model.ssd300_mobilenetv2(**kwargs)[源代码]

Get SSD300 model instance for training, evaluation and prediction.

参数
  • class_num (int) – The number of classes. Default: 21.

  • is_training (bool) – Whether to do training job, default: True.

返回

model.SSD300, SSD300 instance.

class tinyms.model.SSD300(backbone, class_num=21, is_training=True)[源代码]

SSD300 Network. Default backbone is MobileNetV2.

参数
  • backbone (layers.Layer) – backbone of ssd300 model.

  • class_num (int) – number of classes. Default: 21.

  • is_training (bool) – Specify if in training step. Default: True.

返回

Tensor, localization predictions. Tensor, class conf scores.

tinyms.model.cycle_gan(G_A, G_B)[源代码]

Get Cycle GAN network.

参数
  • G_A (layers.Layer) – The generator net, currently it should be in [resnet, unet].

  • G_B (layers.Layer) – The generator net, currently it should be in [resnet, unet].

返回

Cycle GAN instance.

实际案例

>>> gan_net = cycle_gan(G_A, G_B)
tinyms.model.cycle_gan_infer(g_model='resnet')[源代码]

Get Cycle GAN network for predict.

参数

g_model (str) – The generator network type, currently it should be in [‘resnet’, ‘unet’]. Default: resnet.

返回

Cycle GAN instance.

实际案例

>>> gan_net = cycle_gan(G_A, G_B)
tinyms.model.densenet100(**kwargs)[源代码]

Get DenseNet instance for model training, evaluation and prediction.

参数

class_num (int) – The number of classes. Default: 10.

返回

model.DenseNet, DenseNet instance.

tinyms.model.alexnet(**kwargs)[源代码]

Get AlexNet neural network.

参数

class_num (int) – Class number. Default: 10.

返回

layers.Layer, layer instance of AlexNet neural network.

实际案例

>>> from tinyms.model import alexnet
>>>
>>> net = alexnet(class_num=10)
class tinyms.model.AlexNet(class_num=1000)[源代码]

Get AlexNet neural network.

参数

class_num (int) – Class number. Default: 1000.

返回

layers.Layer, layer instance of AlexNet neural network.

实际案例

>>> from tinyms.model import AlexNet
>>>
>>> net = AlexNet(class_num=1000)
tinyms.model.sentimentnet(vocab_size, embed_size, num_hiddens, num_layers, bidirectional, num_classes, weight, batch_size)[源代码]

Sentiment network structure.

class tinyms.model.SentimentNet(vocab_size, embed_size, num_hiddens, num_layers, bidirectional, num_classes, weight, batch_size)[源代码]

Sentiment network structure.

tinyms.model.bert(config, is_training, use_one_hot_embeddings=False)[源代码]

Get bert neural network.

class tinyms.model.Bert(config, is_training, use_one_hot_embeddings=False)[源代码]

Bidirectional Encoder Representations from Transformers.

参数
  • config (Class) – Configuration for BertModel.

  • is_training (bool) – True for training mode. False for eval mode.

  • use_one_hot_embeddings (bool) – Specifies whether to use one hot encoding form. Default: False.

construct(input_ids, token_type_ids, input_mask)[源代码]

Bidirectional Encoder Representations from Transformers.

tinyms.model.vgg11(**kwargs)[源代码]

Get vgg11 neural network.

参数
  • class_num (int) – Class number. Default: 10.

  • batch_norm (bool) – Whether to use BatchNormalization. Default: True

返回

layers.Layer, layer instance of vgg11 neural network.

实际案例

>>> from tinyms.model import vgg11
>>>
>>> net = vgg11(class_num=10)
tinyms.model.vgg13(**kwargs)[源代码]

Get vgg13 neural network.

参数
  • class_num (int) – Class number. Default: 10.

  • batch_norm (bool) – Whether to use BatchNormalization. Default: True

返回

layers.Layer, layer instance of vgg13 neural network.

实际案例

>>> from tinyms.model import vgg13
>>>
>>> net = vgg13(class_num=10)
tinyms.model.vgg16(**kwargs)[源代码]

Get vgg16 neural network.

参数
  • class_num (int) – Class number. Default: 10.

  • batch_norm (bool) – Whether to use BatchNormalization. Default: True

返回

layers.Layer, layer instance of vgg16 neural network.

实际案例

>>> from tinyms.model import vgg16
>>>
>>> net = vgg16(class_num=10)
tinyms.model.vgg19(**kwargs)[源代码]

Get vgg19 neural network.

参数
  • class_num (int) – Class number. Default: 10.

  • batch_norm (bool) – Whether to use BatchNormalization. Default: True

返回

layers.Layer, layer instance of vgg19 neural network.

实际案例

>>> from tinyms.model import vgg19
>>>
>>> net = vgg19(class_num=10)
class tinyms.model.VGG(features, class_num=1000)[源代码]

Get VGG neural network.

参数
  • features (layers.Layer) – Feature extractor.

  • class_num (int) – Class number. Default: 1000.

返回

layers.Layer, layer instance of AlexNet neural network.

实际案例

>>> from tinyms.model import VGG
>>>
>>> net = VGG(features=make_layers(cfg=cfgs['A']),class_num=1000)