Class Model
Defined in File model.h
Class Documentation
-
class Model
The Model class is used to define a MindSpore model, facilitating computational graph management.
Public Functions
-
Status Build(const void *model_data, size_t data_size, ModelType model_type, const std::shared_ptr<Context> &model_context = nullptr)
Build a model from model buffer so that it can run on a device. Only valid for Lite.
- Parameters
model_data – [in] Define the buffer read from a model file.
data_size – [in] Define bytes number of model buffer.
model_type – [in] Define The type of model file. Options: ModelType::kMindIR, ModelType::kOM. Only ModelType::kMindIR is valid for Lite.
model_context – [in] Define the context used to store options during execution.
- Returns
Status.
-
inline Status Build(const std::string &model_path, ModelType model_type, const std::shared_ptr<Context> &model_context = nullptr)
Load and build a model from model buffer so that it can run on a device. Only valid for Lite.
- Parameters
model_path – [in] Define the model path.
model_type – [in] Define The type of model file. Options: ModelType::kMindIR, ModelType::kOM. Only ModelType::kMindIR is valid for Lite.
model_context – [in] Define the context used to store options during execution.
- Returns
Status.
-
Status Build(const void *model_data, size_t data_size, ModelType model_type, const std::shared_ptr<Context> &model_context, const Key &dec_key, const std::string &dec_mode, const std::string &cropto_lib_path)
Build a model from model buffer so that it can run on a device. Only valid for Lite.
- Parameters
model_data – [in] Define the buffer read from a model file.
data_size – [in] Define bytes number of model buffer.
model_type – [in] Define The type of model file. Options: ModelType::kMindIR, ModelType::kOM. Only ModelType::kMindIR is valid for Lite.
model_context – [in] Define the context used to store options during execution.
dec_key – [in] Define the key used to decrypt the ciphertext model. The key length is 16.
dec_mode – [in] Define the decryption mode. Options: AES-GCM.
cropto_lib_path – [in] Define the openssl library path.
- Returns
Status.
-
inline Status Build(const std::string &model_path, ModelType model_type, const std::shared_ptr<Context> &model_context, const Key &dec_key, const std::string &dec_mode, const std::string &cropto_lib_path)
Load and build a model from model buffer so that it can run on a device. Only valid for Lite.
- Parameters
model_path – [in] Define the model path.
model_type – [in] Define The type of model file. Options: ModelType::kMindIR, ModelType::kOM. Only ModelType::kMindIR is valid for Lite.
model_context – [in] Define the context used to store options during execution.
dec_key – [in] Define the key used to decrypt the ciphertext model. The key length is 16.
dec_mode – [in] Define the decryption mode. Options: AES-GCM.
cropto_lib_path – [in] Define the openssl library path.
- Returns
Status.
-
Status Build(GraphCell graph, const std::shared_ptr<Context> &model_context = nullptr, const std::shared_ptr<TrainCfg> &train_cfg = nullptr)
Builds a model.
- Parameters
graph – [in] GraphCell is a derivative of Cell. Cell is not available currently. GraphCell can be constructed from Graph, for example, model.Build(GraphCell(graph), context).
model_context – [in] A context used to store options during execution.
train_cfg – [in] A config used by training.
- Returns
Status.
-
Status Build(GraphCell graph, Node *optimizer, std::vector<Expr*> inputs, const std::shared_ptr<Context> &model_context, const std::shared_ptr<TrainCfg> &train_cfg)
Build train model.
- Parameters
graph – [in] A forward network
optimizer – [in] An optimizer node
inputs – [in] Inputs expression for the trained network (ex: input, label )
model_contex – [in] A context used to store options during execution.
train_cfg – [in] A config used by training
- Returns
Status
-
Status BuildTransferLearning(GraphCell backbone, GraphCell head, const std::shared_ptr<Context> &context, const std::shared_ptr<TrainCfg> &train_cfg = nullptr)
Builds a Transfer Learning model where the backbone weights are fixed and the head weights are trainable.
- Parameters
backbone – [in] The static, non-learnable part of the graph
head – [in] The trainable part of the graph
context – [in] A context used to store options during execution
train_cfg – [in] A config used by training
- Returns
Status
-
Status Resize(const std::vector<MSTensor> &inputs, const std::vector<std::vector<int64_t>> &dims)
Resizes the shapes of inputs.
- Parameters
inputs – [in] A vector that includes all input tensors in order.
dims – [in] Defines the new shapes of inputs, should be consistent with inputs.
- Returns
Status.
-
Status UpdateWeights(const std::vector<MSTensor> &new_weights)
Change the size and or content of weight tensors.
- Parameters
new_weights – [in] a vector of tensors with new shapes and data to use in the model If data pointer is null, the data of the original tensors will be copied to the new ones
- Returns
Status.
-
Status Predict(const std::vector<MSTensor> &inputs, std::vector<MSTensor> *outputs, const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr)
Inference model.
- Parameters
inputs – [in] A vector where model inputs are arranged in sequence.
outputs – [out] Which is a pointer to a vector. The model outputs are filled in the container in sequence.
before – [in] CallBack before predict.
after – [in] CallBack after predict.
- Returns
Status.
-
Status Predict(const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr)
Inference model.
- Parameters
before – [in] CallBack before predict.
after – [in] CallBack after predict.
- Returns
Status.
-
Status RunStep(const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr)
Train model by step.
- Parameters
before – [in] CallBack before predict.
after – [in] CallBack after predict.
- Returns
Status.
-
Status PredictWithPreprocess(const std::vector<std::vector<MSTensor>> &inputs, std::vector<MSTensor> *outputs, const MSKernelCallBack &before = nullptr, const MSKernelCallBack &after = nullptr)
Inference model with preprocess in model.
- Parameters
inputs – [in] A vector where model inputs are arranged in sequence.
outputs – [out] Which is a pointer to a vector. The model outputs are filled in the container in sequence.
before – [in] CallBack before predict.
after – [in] CallBack after predict.
- Returns
Status.
-
Status Preprocess(const std::vector<std::vector<MSTensor>> &inputs, std::vector<MSTensor> *outputs)
Apply data preprocess if it exits in model.
- Parameters
inputs – [in] A vector where model inputs are arranged in sequence.
outputs – [out] Which is a pointer to a vector. The model outputs are filled in the container in sequence.
- Returns
Status.
-
bool HasPreprocess()
Check if data preprocess exists in model.
- Returns
true if data preprocess exists.
-
inline Status LoadConfig(const std::string &config_path)
Load config file.
- Parameters
config_path – [in] config file path.
- Returns
Status.
-
inline Status UpdateConfig(const std::string §ion, const std::pair<std::string, std::string> &config)
Update config.
- Parameters
section – [in] define the config section.
config – [in] define the config will be updated.
- Returns
Status.
-
std::vector<MSTensor> GetInputs()
Obtains all input tensors of the model.
- Returns
The vector that includes all input tensors.
-
inline MSTensor GetInputByTensorName(const std::string &tensor_name)
Obtains the input tensor of the model by name.
- Returns
The input tensor with the given name, if the name is not found, an invalid tensor is returned.
-
std::vector<MSTensor> GetGradients() const
Obtains all gradient tensors of the model.
- Returns
The vector that includes all gradient tensors.
-
Status ApplyGradients(const std::vector<MSTensor> &gradients)
update gradient tensors of the model.
- Parameters
gradients – [in] A vector new gradients.
- Returns
Status of operation
-
std::vector<MSTensor> GetFeatureMaps() const
Obtains all weights tensors of the model.
- Returns
The vector that includes all gradient tensors.
-
Status UpdateFeatureMaps(const std::vector<MSTensor> &new_weights)
update weights tensors of the model.
- Parameters
new_weights – [in] A vector new weights.
- Returns
Status of operation
-
std::vector<MSTensor> GetOptimizerParams() const
Obtains optimizer params tensors of the model.
- Returns
The vector that includes all params tensors.
-
Status SetOptimizerParams(const std::vector<MSTensor> ¶ms)
update the optimizer parameters.
- Parameters
params – [in] A vector new optimizer params.
- Returns
Status of operation.
-
Status SetupVirtualBatch(int virtual_batch_multiplier, float lr = -1.0f, float momentum = -1.0f)
Setup training with virtual batches.
- Parameters
virtual_batch_multiplier – [in] - virtual batch multiplier, use any number < 1 to disable.
lr – [in] - learning rate to use for virtual batch, -1 for internal configuration.
momentum – [in] - batch norm momentum to use for virtual batch, -1 for internal configuration.
- Returns
Status of operation.
-
Status SetLearningRate(float learning_rate)
Sets the Learning Rate of the training.
- Parameters
learning_rate – [in] to set.
- Returns
Status of operation.
-
float GetLearningRate()
Gets the Learning Rate of the optimizer.
- Returns
Learning rate. 0.0 if no optimizer was found.
-
Status InitMetrics(std::vector<Metrics*> metrics)
Initialize object with metrics.
- Parameters
metrics – [in] A verctor of metrics objects.
- Returns
0 on success or -1 in case of error
-
std::vector<Metrics*> GetMetrics()
Accessor to TrainLoop metric objects.
- Returns
A vector of metrics
-
std::vector<MSTensor> GetOutputs()
Obtains all output tensors of the model.
- Returns
The vector that includes all output tensors.
-
inline std::vector<std::string> GetOutputTensorNames()
Obtains names of all output tensors of the model.
- Returns
A vector that includes names of all output tensors.
-
inline MSTensor GetOutputByTensorName(const std::string &tensor_name)
Obtains the output tensor of the model by name.
- Returns
The output tensor with the given name, if the name is not found, an invalid tensor is returned.
-
inline std::vector<MSTensor> GetOutputsByNodeName(const std::string &node_name)
Get output MSTensors of model by node name.
Note
Deprecated, replace with GetOutputByTensorName
- Parameters
node_name – [in] Define node name.
- Returns
The vector of output MSTensor.
-
Status BindGLTexture2DMemory(const std::map<std::string, unsigned int> &inputGLTexture, std::map<std::string, unsigned int> *outputGLTexture)
Bind GLTexture2D object to cl Memory.
- Parameters
inputGLTexture – [in] The input GLTexture id for Model.
outputGLTexture – [in] The output GLTexture id for Model.
- Returns
Status of operation.
-
Status SetTrainMode(bool train)
Set the model running mode.
- Parameters
train – [in] True means model runs in Train Mode, otherwise Eval Mode.
- Returns
Status of operation.
-
bool GetTrainMode() const
Get the model running mode.
- Returns
Is Train Mode or not.
-
Status Train(int epochs, std::shared_ptr<dataset::Dataset> ds, std::vector<TrainCallBack*> cbs)
Performs the training Loop in Train Mode.
- Parameters
epochs – [in] The number of epoch to run.
dataset – [in] A smart pointer to MindData Dataset object.
cbs – [in] A vector of TrainLoopCallBack objects.
- Returns
Status of operation.
-
Status Evaluate(std::shared_ptr<dataset::Dataset> ds, std::vector<TrainCallBack*> cbs)
Performs the training loop over all data in Eval Mode.
- Parameters
dataset – [in] A smart pointer to MindData Dataset object.
cbs – [in] A vector of TrainLoopCallBack objects.
- Returns
Status of operation.
Public Static Functions
-
static bool CheckModelSupport(enum DeviceType device_type, ModelType model_type)
Inference model.
- Parameters
device_type – [in] Device type,options are kGPU, kAscend etc.
model_type – [in] The type of model file, options are ModelType::kMindIR, ModelType::kOM.
- Returns
Is supported or not.
-
Status Build(const void *model_data, size_t data_size, ModelType model_type, const std::shared_ptr<Context> &model_context = nullptr)