Turi Create
4.0
|
#include <ml/neural_net/model_spec.hpp>
Public Types | |
enum | padding_type |
enum | padding_policy |
enum | pooling_type |
Public Member Functions | |
model_spec () | |
model_spec (const CoreML::Specification::NeuralNetwork &nn_model) | |
model_spec (const std::string &mlmodel_path) | |
const CoreML::Specification::NeuralNetwork & | get_coreml_spec () const |
std::unique_ptr< CoreML::Specification::NeuralNetwork > | move_coreml_spec () && |
float_array_map | export_params_view () const |
void | update_params (const float_array_map &weights, bool use_quantization=false) |
bool | has_layer_output (const std::string &layer_name) const |
void | add_relu (const std::string &name, const std::string &input) |
void | add_leakyrelu (const std::string &name, const std::string &input, float alpha) |
void | add_sigmoid (const std::string &name, const std::string &input) |
void | add_pooling (const std::string &name, const std::string &input, size_t kernel_height, size_t kernel_width, size_t stride_h, size_t stride_w, padding_type padding, bool use_poolexcludepadding=false, pooling_type pooling=pooling_type::MAX) |
void | add_convolution (const std::string &name, const std::string &input, size_t num_output_channels, size_t num_kernel_channels, size_t kernel_height, size_t kernel_width, size_t stride_h, size_t stride_w, padding_type padding, weight_initializer weight_initializer_fn, weight_initializer bias_initializer_fn=nullptr) |
void | add_padding (const std::string &name, const std::string &input, size_t padding_top, size_t padding_bottom, size_t padding_left, size_t padding_right, padding_policy policy=padding_policy::REFLECTIVE) |
void | add_upsampling (const std::string &name, const std::string &input, size_t scaling_x, size_t scaling_y) |
void | add_inner_product (const std::string &name, const std::string &input, size_t num_output_channels, size_t num_input_channels, weight_initializer weight_initializer_fn, weight_initializer bias_initializer_fn=nullptr) |
void | add_batchnorm (const std::string &name, const std::string &input, size_t num_channels, float epsilon) |
void | add_instancenorm (const std::string &name, const std::string &input, size_t num_channels, float epsilon) |
void | add_channel_concat (const std::string &name, const std::vector< std::string > &inputs) |
void | add_softmax (const std::string &name, const std::string &input) |
void | add_flatten (const std::string &name, const std::string &input) |
void | add_addition (const std::string &name, const std::vector< std::string > &inputs) |
void | add_multiplication (const std::string &name, const std::vector< std::string > &inputs) |
void | add_exp (const std::string &name, const std::string &input) |
void | add_scale (const std::string &name, const std::string &input, const std::vector< size_t > &shape_c_h_w, weight_initializer scale_initializer_fn) |
void | add_constant (const std::string &name, const std::array< size_t, 3 > &shape_c_h_w, weight_initializer weight_initializer_fn) |
void | add_reshape (const std::string &name, const std::string &input, const std::array< size_t, 4 > &seq_c_h_w) |
void | add_permute (const std::string &name, const std::string &input, const std::array< size_t, 4 > &axis_permutation) |
void | add_channel_slice (const std::string &name, const std::string &input, int start_index, int end_index, size_t stride) |
void | add_lstm (const std::string &name, const std::string &input, const std::string &hidden_input, const std::string &cell_input, const std::string &hidden_output, const std::string &cell_output, size_t input_vector_size, size_t output_vector_size, float cell_clip_threshold, const lstm_weight_initializers &initializers) |
void | add_preprocessing (const std::string &feature_name, const float image_scale) |
void | add_transpose (const std::string &name, const std::string &input, std::vector< size_t > axes) |
void | add_split_nd (const std::string &name, const std::string &input, size_t axis, size_t num_splits, const std::vector< size_t > &split_sizes) |
void | add_concat_nd (const std::string &name, const std::vector< std::string > &inputs, size_t axis) |
void | add_reshape_static (const std::string &name, const std::string &input, const std::vector< size_t > &targetShape) |
void | add_reshape_dynamic (const std::string &name, const std::vector< std::string > &inputs) |
void | add_expand_dims (const std::string &name, const std::string &input, const std::vector< size_t > &axes, const std::vector< size_t > &inputVector, const std::vector< size_t > &outputVector) |
void | add_squeeze (const std::string &name, const std::string &input, const std::vector< size_t > &axes, const std::vector< size_t > &inputVector, const std::vector< size_t > &outputVector) |
void | add_add_broadcastable (const std::string &name, const std::vector< std::string > &inputs) |
void | add_gather (const std::string &name, const std::vector< std::string > &inputs) |
void | add_constant_nd (const std::string &name, const std::vector< size_t > &shape, const weight_initializer &data) |
void | add_get_shape (const std::string &name, const std::string &input) |
void | add_slice_dynamic (const std::string &name, const std::vector< std::string > &inputs) |
void | add_nms_layer (const std::string &name, const std::vector< std::string > &inputs, const std::vector< std::string > &outputs, float iou_threshold, float confidence_threshold, size_t max_boxes, bool per_class_supression) |
Representation for a neural-network model (structure and parameters), optimized for convenient export to CoreML.
This class just wraps CoreML::Specification::NeuralNetwork, helping to insulate toolkits from protobuf code.
Definition at line 40 of file model_spec.hpp.
|
strong |
Parameter for the padding layer
Definition at line 50 of file model_spec.hpp.
|
strong |
Parameter for convolution and pooling layers.
Definition at line 44 of file model_spec.hpp.
|
strong |
Parameter for pooling types.
Definition at line 57 of file model_spec.hpp.
turi::neural_net::model_spec::model_spec | ( | ) |
Creates an empty model_spec (with no layers).
turi::neural_net::model_spec::model_spec | ( | const CoreML::Specification::NeuralNetwork & | nn_model | ) |
Initializes a model_spec from a NeuralNetwork proto.
turi::neural_net::model_spec::model_spec | ( | const std::string & | mlmodel_path | ) |
Initializes a model_spec from the top-level NeuralNetwork found inside a CoreML model specification on disk.
mlmodel_path | Path to a CoreM::Specification::Model proto on disk. |
If | the indicated path could not be read or parsed. |
void turi::neural_net::model_spec::add_add_broadcastable | ( | const std::string & | name, |
const std::vector< std::string > & | inputs | ||
) |
Appends an Add Broadcastable layer.
name | The name of the layer and its output |
inputs | The vector of names of the layer's inputs |
void turi::neural_net::model_spec::add_addition | ( | const std::string & | name, |
const std::vector< std::string > & | inputs | ||
) |
Appends a layer that performs elementwise addition.
name | The name of the layer and its output |
inputs | The names of the layer's inputs |
void turi::neural_net::model_spec::add_batchnorm | ( | const std::string & | name, |
const std::string & | input, | ||
size_t | num_channels, | ||
float | epsilon | ||
) |
Appends a batch norm layer.
The beta and mean parameters are initialized to 0.f; the gamma and variance parameters are initialized to 1.f
name | The name of the layer and its output |
input | The name of the layer's input |
num_channels | The C dimension of the input and output |
epsilon | Added to the variance for each input before normalizing |
void turi::neural_net::model_spec::add_channel_concat | ( | const std::string & | name, |
const std::vector< std::string > & | inputs | ||
) |
Appends a layer that concatenates its inputs along the channel axis.
name | The name of the layer and its output |
inputs | The names of the layer's inputs |
void turi::neural_net::model_spec::add_channel_slice | ( | const std::string & | name, |
const std::string & | input, | ||
int | start_index, | ||
int | end_index, | ||
size_t | stride | ||
) |
Appends a layer that slices the input along the channel axis.
name | The name of the layer and its output |
input | The name of the layer's input |
start_index | The first channel to include |
end_index | The first channel to stop including. If negative, then the number of channels is added first (so -1 becomes n - 1). |
stride | The interval between channels to include |
void turi::neural_net::model_spec::add_concat_nd | ( | const std::string & | name, |
const std::vector< std::string > & | inputs, | ||
size_t | axis | ||
) |
Appends an Concat layer.
name | The name of the layer and its output |
inputs | The vector of names of the layer's inputs |
axis | The axis to concat the layer on |
void turi::neural_net::model_spec::add_constant | ( | const std::string & | name, |
const std::array< size_t, 3 > & | shape_c_h_w, | ||
weight_initializer | weight_initializer_fn | ||
) |
Appends a layer with fixed values.
name | The name of the layer and its output |
shape_c_h_w | The shape of the output |
weight_initializer_fn | Callback used to initialize the weights |
void turi::neural_net::model_spec::add_constant_nd | ( | const std::string & | name, |
const std::vector< size_t > & | shape, | ||
const weight_initializer & | data | ||
) |
Appends a Constant ND layer.
name | The name of the layer and its output |
shape | The shape of the constant layer |
data | The data being loaded in the constant layer |
void turi::neural_net::model_spec::add_convolution | ( | const std::string & | name, |
const std::string & | input, | ||
size_t | num_output_channels, | ||
size_t | num_kernel_channels, | ||
size_t | kernel_height, | ||
size_t | kernel_width, | ||
size_t | stride_h, | ||
size_t | stride_w, | ||
padding_type | padding, | ||
weight_initializer | weight_initializer_fn, | ||
weight_initializer | bias_initializer_fn = nullptr |
||
) |
Appends a convolution layer.
name | The name of the layer and its output |
input | The name of the layer's input |
num_output_channels | The number of distinct filters in this layer |
num_kernel_channels | The number of input features per "pixel" |
kernel_size | The height and width of the kernel |
weight_initializer_fn | Callback used to initialize the conv weights |
bias_initializer_fn | Callback used to initialize the conv bias. If nullptr, then no bias vector is set. |
void turi::neural_net::model_spec::add_exp | ( | const std::string & | name, |
const std::string & | input | ||
) |
Appends a layer that applies the unary function f(x) = e^x to its input.
name | The name of the layer and its output |
input | The name of the layer's input |
void turi::neural_net::model_spec::add_expand_dims | ( | const std::string & | name, |
const std::string & | input, | ||
const std::vector< size_t > & | axes, | ||
const std::vector< size_t > & | inputVector, | ||
const std::vector< size_t > & | outputVector | ||
) |
Appends an Expand Dims layer.
name | The name of the layer and its output |
input | The vector of names of the layer's input |
axes | The axes to expand the layer on |
void turi::neural_net::model_spec::add_flatten | ( | const std::string & | name, |
const std::string & | input | ||
) |
Appends a layer that performs flatten normalization (along channel axis).
currently only supports channel first flattening, which means if the input order is [C, H, W]
, then output array will be [C * H * W, 1, 1]
, still C-major
orderring. No underlying array storage will be changed.
name | The name of the layer and its output |
input | The name of the layer's input |
void turi::neural_net::model_spec::add_gather | ( | const std::string & | name, |
const std::vector< std::string > & | inputs | ||
) |
Appends a Gather layer.
name | The name of the layer and its output |
inputs | The vector of names of the layer's inputs |
void turi::neural_net::model_spec::add_get_shape | ( | const std::string & | name, |
const std::string & | input | ||
) |
Appends a Get Shape layer.
name | The name of the layer and its output |
input | The vector of names of the layer's input |
void turi::neural_net::model_spec::add_inner_product | ( | const std::string & | name, |
const std::string & | input, | ||
size_t | num_output_channels, | ||
size_t | num_input_channels, | ||
weight_initializer | weight_initializer_fn, | ||
weight_initializer | bias_initializer_fn = nullptr |
||
) |
Appends an inner-product (dense, fully connected) layer.
name | The name of the layer and its output |
input | The name of the layer's input |
num_output_channels | Size of the output vector |
num_input_channels | Size of the input vector |
weight_initializer_fn | Callback used to initialize the weights |
bias_initializer_fn | Callback used to initialize the bias. If nullptr, then no bias vector is set. |
void turi::neural_net::model_spec::add_instancenorm | ( | const std::string & | name, |
const std::string & | input, | ||
size_t | num_channels, | ||
float | epsilon | ||
) |
Appends an instance norm layer.
The beta is initialized to 0.f; the gamma is initialized to 1.f
name | The name of the layer and its output |
input | The name of the layer's input |
num_channels | The C dimension of the input and output |
epsilon | Added to the variance for each input before normalizing |
void turi::neural_net::model_spec::add_leakyrelu | ( | const std::string & | name, |
const std::string & | input, | ||
float | alpha | ||
) |
Appends a leaky ReLU activation layer.
name | The name of the layer and its output |
input | The name of the layer's input |
alpha | Multiplied to negative inputs |
void turi::neural_net::model_spec::add_lstm | ( | const std::string & | name, |
const std::string & | input, | ||
const std::string & | hidden_input, | ||
const std::string & | cell_input, | ||
const std::string & | hidden_output, | ||
const std::string & | cell_output, | ||
size_t | input_vector_size, | ||
size_t | output_vector_size, | ||
float | cell_clip_threshold, | ||
const lstm_weight_initializers & | initializers | ||
) |
Appends an LSTM layer.
name | The name of the layer and its output |
input | The name of the layer's input |
hidden_input | The name of the initial hidden state |
cell_input | The name of the initial cell state |
hidden_output | The name of the resulting hidden state |
cell_output | The name of the resulting cell state |
input_vector_size | The size of the input vector |
output_vector_size | The size of the output vector (hidden state and cell state) |
cell_clip_threshold | Maximum magnitude of cell state values |
initializers | LSTM weights |
void turi::neural_net::model_spec::add_multiplication | ( | const std::string & | name, |
const std::vector< std::string > & | inputs | ||
) |
Appends a layer that performs elementwise multiplication.
name | The name of the layer and its output |
inputs | The names of the layer's inputs |
void turi::neural_net::model_spec::add_nms_layer | ( | const std::string & | name, |
const std::vector< std::string > & | inputs, | ||
const std::vector< std::string > & | outputs, | ||
float | iou_threshold, | ||
float | confidence_threshold, | ||
size_t | max_boxes, | ||
bool | per_class_supression | ||
) |
Appends a non maximum suppression layer.
name | The name of the layer and its output |
inputs | The name of the layer's inputs |
outputs | The outputs of the layer |
iou_thrsshold | The default value for the iou threshold |
confidence_threshold | The default value for the confidence threshold |
max_boxes | The maximum number of boxes you want NMS to run |
per_class_suppression | When false, suppression happens for all classes. |
void turi::neural_net::model_spec::add_padding | ( | const std::string & | name, |
const std::string & | input, | ||
size_t | padding_top, | ||
size_t | padding_bottom, | ||
size_t | padding_left, | ||
size_t | padding_right, | ||
padding_policy | policy = padding_policy::REFLECTIVE |
||
) |
Appends a padding layer.
name | The name of the layer and its output |
input | The name of the layer's input |
padding_top | The padding on the top |
padding_bottom | The padding on the bottom |
padding_left | The padding to the left |
padding_right | The padding to the right |
policy | The padding policy of zero, reflective, or replication |
void turi::neural_net::model_spec::add_permute | ( | const std::string & | name, |
const std::string & | input, | ||
const std::array< size_t, 4 > & | axis_permutation | ||
) |
Appends a layer that transposes the dimensions of its input
name | The name of the layer and its output |
input | The name of the layer's input |
axis_permutation | A permutation of [0, 1, 2, 3], describing how to rearrange the [Seq, C, H, W] input. |
void turi::neural_net::model_spec::add_pooling | ( | const std::string & | name, |
const std::string & | input, | ||
size_t | kernel_height, | ||
size_t | kernel_width, | ||
size_t | stride_h, | ||
size_t | stride_w, | ||
padding_type | padding, | ||
bool | use_poolexcludepadding = false , |
||
pooling_type | pooling = pooling_type::MAX |
||
) |
Appends a pooling layer. By default, it's a max pooling layer.
It can be of type:
pooling | this sets the type of pooling this layer performs. |
use_poolexcludepadding | padded values are excluded from the count (denominator) when computing average pooling. |
void turi::neural_net::model_spec::add_preprocessing | ( | const std::string & | feature_name, |
const float | image_scale | ||
) |
Appends a preprocessing layer Now only support image scaling preprocessing though.
void turi::neural_net::model_spec::add_relu | ( | const std::string & | name, |
const std::string & | input | ||
) |
Appends a ReLU activation layer.
name | The name of the layer and its output |
input | The name of the layer's input |
void turi::neural_net::model_spec::add_reshape | ( | const std::string & | name, |
const std::string & | input, | ||
const std::array< size_t, 4 > & | seq_c_h_w | ||
) |
Appends a layer that reshapes its input.
name | The name of the layer and its output |
input | The name of the layer's input |
shape_c_h_w | The shape of the output |
void turi::neural_net::model_spec::add_reshape_dynamic | ( | const std::string & | name, |
const std::vector< std::string > & | inputs | ||
) |
Appends a Reshape Dynamic layer.
name | The name of the layer and its output |
inputs | The vector of names of the layer's inputs |
void turi::neural_net::model_spec::add_reshape_static | ( | const std::string & | name, |
const std::string & | input, | ||
const std::vector< size_t > & | targetShape | ||
) |
Appends a Reshape Static layer.
name | The name of the layer and its output |
input | The vector of names of the layer's input |
targetShape | The target shape |
void turi::neural_net::model_spec::add_scale | ( | const std::string & | name, |
const std::string & | input, | ||
const std::vector< size_t > & | shape_c_h_w, | ||
weight_initializer | scale_initializer_fn | ||
) |
Appends a layer that performs elementwise multiplication between its input and some fixed weights.
name | The name of the layer and its output |
input | The name of the layer's input |
shape_c_h_w | The shape of the input and output |
weight_initializer_fn | Callback used to initialize the weights |
void turi::neural_net::model_spec::add_sigmoid | ( | const std::string & | name, |
const std::string & | input | ||
) |
Appends a sigmoid activation layer.
name | The name of the layer and its output |
input | The name of the layer's input |
void turi::neural_net::model_spec::add_slice_dynamic | ( | const std::string & | name, |
const std::vector< std::string > & | inputs | ||
) |
Appends dynamic slicing.
name | The name of the layer and its output |
inputs | The name of the layer's inputs |
void turi::neural_net::model_spec::add_softmax | ( | const std::string & | name, |
const std::string & | input | ||
) |
Appends a layer that performs softmax normalization (along channel axis).
name | The name of the layer and its output |
input | The name of the layer's input |
void turi::neural_net::model_spec::add_split_nd | ( | const std::string & | name, |
const std::string & | input, | ||
size_t | axis, | ||
size_t | num_splits, | ||
const std::vector< size_t > & | split_sizes | ||
) |
Appends an Split layer.
name | The name of the layer and its output |
input | The name of the layer's input |
axis | The axis to split the layer on |
num_splits | The number of splits to perform |
split_sizes | The size of each split |
void turi::neural_net::model_spec::add_squeeze | ( | const std::string & | name, |
const std::string & | input, | ||
const std::vector< size_t > & | axes, | ||
const std::vector< size_t > & | inputVector, | ||
const std::vector< size_t > & | outputVector | ||
) |
Appends a Squeeze layer.
name | The name of the layer and its output |
input | The vector of names of the layer's input |
axes | The axes to squeeze the layer on |
void turi::neural_net::model_spec::add_transpose | ( | const std::string & | name, |
const std::string & | input, | ||
std::vector< size_t > | axes | ||
) |
Appends an Transpose layer.
name | The name of the layer and its output |
input | The name of the layer's input |
axes | The ordering of the axes to transpose for instance {0, 2, 1, 3} would flip the channel and height axes |
void turi::neural_net::model_spec::add_upsampling | ( | const std::string & | name, |
const std::string & | input, | ||
size_t | scaling_x, | ||
size_t | scaling_y | ||
) |
Appends an upsampling layer.
name | The name of the layer and its output |
input | The name of the layer's input |
scaling_x | The upsample scale on the x axis |
scaling_y | The upsample scale on the y axis |
float_array_map turi::neural_net::model_spec::export_params_view | ( | ) | const |
Creates a shared_float_array view (weak reference) into the parameters of the model, indexed by layer name.
If | a NeuralNetworkLayer in the specification seems malformed (e.g. WeightParams with size inconsistent with declared layer shape). |
To avoid copying data, the data backing the shared_float_array instances in the return value will only remain valid for the lifetime of this instance!
|
inline |
Exposes the underlying CoreML proto.
Definition at line 88 of file model_spec.hpp.
bool turi::neural_net::model_spec::has_layer_output | ( | const std::string & | layer_name | ) | const |
Determines whether the neural network contains a layer with the given output name.
In general, it is only safe to add a new layer that takes a named input if this method returns true for that name.
std::unique_ptr<CoreML::Specification::NeuralNetwork> turi::neural_net::model_spec::move_coreml_spec | ( | ) | && |
Transfer ownership of the underlying CoreML proto, invalidating the current instance (leaving it in a "moved-from" state).
(Note that this method may only be invoked from a model_spec&&)
void turi::neural_net::model_spec::update_params | ( | const float_array_map & | weights, |
bool | use_quantization = false |
||
) |
Overwrites existing WeightParams values using the provided float_array values.
weights | A dictionary whose keys follow the same naming scheme used by export_params_view . |
use_quantization | If true, weights are stored in half precision. |
If | a float_array's shape does not match the corresponding NeuralNetworkLayer. |