Tensorflow models¶
It is in charge of generating the layers and blocks that make up the architectures of the different neural networks using the tools offered by Tensorflow. Within models there are two submodules blocks and layers, the first one stores groups of layers, which can be Tensorflow’s own or customized layers which are found in the layers submodule.
Blocks¶
- class
py2vision.models.blocks.backbone_block.BackboneStrategy[source]¶The Strategy interface declares operations common to all supported versions of some algorithm.
The Context uses this interface to call the algorithm defined by Concrete Strategies.
Layers¶
- class
py2vision.models.layers.batch_normalization_layer.BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001, center=True, scale=True, beta_initializer='zeros', gamma_initializer='ones', moving_mean_initializer='zeros', moving_variance_initializer='ones', beta_regularizer=None, gamma_regularizer=None, beta_constraint=None, gamma_constraint=None, **kwargs)[source]¶A modified batch normalization layer.
Parameters:
- x – input of the layer.
- training – it’s necessary to know when freeze the layer weights.
Returns: A batchNormalization layer.
call(x, training=False)[source]¶This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state in __init__(), or the build() method that is called automatically before call() executes the first time.
Parameters:
- inputs –
Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
- NumPy array or Python scalar values in inputs get cast as tensors.
- Keras mask metadata is only collected from inputs.
- Layers are built (build(input_shape) method) using shape info from inputs only.
- input_spec compatibility is only checked against inputs.
- Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
- The SavedModel input specification is generated using inputs only.
- Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
- *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
- **kwargs –
Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
- mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
Returns: A tensor or list/tuple of tensors.
py2vision.models.layers.conv2d_bn_leaky_relu_layer.conv2d_bn_leaky_relu_layer(input_layer, filters_shape, downsample=False, activate=True, bn=True)[source]¶A resnet block with depthwise separable convolutions to reduce the computational demand.
Parameters:
- input_layer – A tensor that works like input.
- filters_shape – An array or list or tuple that contains the shape of the filters which filters_shape[0] is kernel size for conv2d layer.
- downsample – a boolean to know when apply zero padding layer.
- activate – a boolean to know when apply leaky ReLu layer.
- bn – a boolean to know when apply a batch normalization layer.
Returns: A resnet block
- class
py2vision.models.layers.upsample_layer.UpsampleLayer(trainable=True, name=None, dtype=None, dynamic=False, **kwargs)[source]¶A layer for upsampling an image.
call(inputs)[source]¶This is where the layer’s logic lives.
The call() method may not create state (except in its first invocation, wrapping the creation of variables or other resources in tf.init_scope()). It is recommended to create state in __init__(), or the build() method that is called automatically before call() executes the first time.
Parameters:
- inputs –
Input tensor, or dict/list/tuple of input tensors. The first positional inputs argument is subject to special rules: - inputs must be explicitly passed. A layer cannot have zero
arguments, and inputs cannot be provided via the default value of a keyword argument.
- NumPy array or Python scalar values in inputs get cast as tensors.
- Keras mask metadata is only collected from inputs.
- Layers are built (build(input_shape) method) using shape info from inputs only.
- input_spec compatibility is only checked against inputs.
- Mixed precision input casting is only applied to inputs. If a layer has tensor arguments in *args or **kwargs, their casting behavior in mixed precision should be handled manually.
- The SavedModel input specification is generated using inputs only.
- Integration with various ecosystem packages like TFMOT, TFLite, TF.js, etc is only supported for inputs and not for tensors in positional and keyword arguments.
- *args – Additional positional arguments. May contain tensors, although this is not recommended, for the reasons above.
- **kwargs –
Additional keyword arguments. May contain tensors, although this is not recommended, for the reasons above. The following optional keyword arguments are reserved: - training: Boolean scalar tensor of Python boolean indicating
whether the call is meant for training or inference.
- mask: Boolean input mask. If the layer’s call() method takes a mask argument, its default value will be set to the mask generated for inputs by the previous layer (if input did come from a layer that generated a corresponding mask, i.e. if it came from a Keras layer with masking support).
Returns: A tensor or list/tuple of tensors.
py2vision.models.layers.residual_layer.residual_layer(input_layer, input_channel, filter_num1, filter_num2)[source]¶A residual block with two Convolutional-batch normalization-leakyReLu layers stacked one above other.
Parameters:
- input_layer – A tensor that works like input.
- input_channel – input layer dimensions.
- filter_num1 – filter depth for the first convolutional-batch normalization-leakyRelu layer.
- filter_num2 – filter depth for the second convolutional-batch normalization-leakyRelu layer.
Returns: A residual block.
Models manager¶
Since the number of existing network architectures for recognition is enormous, the role of the model manager is to manage the use of network architectures by means of a mediator class.
- class
py2vision.models.models_manager.ModelManager[source]¶A selector of models that depends of which method is used.
model1¶an instance of Yolov3Model
model2¶an instance of Yolov3TinyModel
build_yolov3(backbone, num_class)[source]¶Create the model to do YoloV3 based in darknet53.
Parameters:
- backbone – an object with a backbone network.
- num_class – an integer with the quantity of classes.
Returns: A list where the first one is used to predict large-sized objects, the second one is used to predict medium-sized objects, the third one is used to small objects and the last one is the input shape returned.
build_yolov3_tiny(backbone, num_class)[source]¶Create the model to do YoloV3 based in darknet19 tiny.
Parameters:
- backbone – an object with a backbone network.
- num_class – an integer with the quantity of classes.
Returns: A list where the first one is used to predict large-sized objects, the second one is used to predict medium-sized objects and the last one is the input shape returned.
- class
py2vision.models.models_manager.ModelManagerInterface[source]¶An interface, which each method correspond with a network architecture.
- class
py2vision.models.yolov3_model.BuildYoloV3(backbone, num_class)[source]¶Build YoloV3 model given a backbone.
Parameters:
- backbone – an object with a backbone network.
- num_class – an integer with the quantity of classes.
Returns: A list where the first one is used to predict large-sized objects, the second one is used to predict medium-sized objects, the third one is used to small objects and the last one is the input shape returned.
call(input_shape)[source]¶Calls the model on new inputs and returns the outputs as tensors.
In this case call() just reapplies all ops in the graph to the new inputs (e.g. build a new computational graph from the provided inputs).
Note: This method should not be called directly. It is only meant to be overridden when subclassing tf.keras.Model. To call a model on an input, always use the __call__() method, i.e. model(inputs), which relies on the underlying call() method.
Parameters:
- inputs – Input tensor, or dict/list/tuple of input tensors.
- training – Boolean or boolean scalar tensor, indicating whether to run the Network in training mode or inference mode.
- mask –
A mask or list of masks. A mask can be either a boolean tensor or None (no mask). For more details, check the guide
Returns: A tensor if there is a single output, or a list of tensors if there are more than one outputs.
- class
py2vision.models.yolov3_tiny_model.BuildYoloV3Tiny(backbone, num_class)[source]¶Build YoloV3 tiny model given a backbone
Parameters:
- name – a string with the name of the model
- backbone – an object with a backbone network
- num_class – an integer with the quantity of classes
Returns: A list where the first one is used to predict large-sized objects, the second one is used to predict medium-sized objects and the last one is the input shape returned.
call(input_shape)[source]¶Calls the model on new inputs and returns the outputs as tensors.
In this case call() just reapplies all ops in the graph to the new inputs (e.g. build a new computational graph from the provided inputs).
Note: This method should not be called directly. It is only meant to be overridden when subclassing tf.keras.Model. To call a model on an input, always use the __call__() method, i.e. model(inputs), which relies on the underlying call() method.
Parameters:
- inputs – Input tensor, or dict/list/tuple of input tensors.
- training – Boolean or boolean scalar tensor, indicating whether to run the Network in training mode or inference mode.
- mask –
A mask or list of masks. A mask can be either a boolean tensor or None (no mask). For more details, check the guide
Returns: A tensor if there is a single output, or a list of tensors if there are more than one outputs.
How to use?¶
1 2 3 4 5 6 7 8 9 10 | from py2vision.models.models_manager import ModelManager from py2vision.models.blocks.backbone_block import BackboneBlock from py2vision.models.blocks.backbone_block import darknet53 np.random.seed(2000) input_data = np.random.randint(0, 255, size=(416, 416, 3)) backbone_net = BackboneBlock(darknet53()) model_manager = ModelManager() conv_sbbox, conv_mbbox, conv_lbbox, _ = model_manager.build_yolov3(backbone_net, 80)(self.input_data.shape) |