Utils
Utility module for Hocrox library.
This module contains some utility classes and functions.
is_valid_layer
is_valid_layer method is used to check if a layer is valid or not.
is_valid_layer(layer)
Check if the layer is valid or not.
This function is used to check if the layer is valid or not. It should be used when building custom layers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
layer |
class |
Custom layer class. |
required |
Returns:
Type | Description |
---|---|
bool |
True if the layer is valid, else False. |
Source code in hocrox/utils/is_valid_layer.py
def is_valid_layer(layer):
"""Check if the layer is valid or not.
This function is used to check if the layer is valid or not. It should be used when building custom layers.
Args:
layer (class): Custom layer class.
Returns:
bool: True if the layer is valid, else False.
"""
if not hasattr(layer, "_get_description"):
return False
if not hasattr(layer, "_get_name"):
return False
if not hasattr(layer, "_get_type"):
return False
if not hasattr(layer, "_is_valid_child"):
return False
if not hasattr(layer, "_apply_layer"):
return False
return True
layer
Layer class is used to make layers for Hocrox.
Layer
Layer class is used to make layers for Hocrox.
Here is an example code for making a new custom CropCustom Hocrox layer using this Layer class as a base class.
from hocrox.utils import Layer
class CropCustom(Layer):
def __init__(self, x, y, w, h, name=None):
if x and not isinstance(x, int):
raise ValueError(f"The value {x} for the argument x is not valid")
if y and not isinstance(y, int):
raise ValueError(f"The value {y} for the argument y is not valid")
if w and not isinstance(w, int):
raise ValueError(f"The value {w} for the argument w is not valid")
if h and not isinstance(h, int):
raise ValueError(f"The value {h} for the argument h is not valid")
self.__x = x
self.__y = y
self.__w = w
self.__h = h
super().__init__(
name,
"crop",
self.STANDARD_SUPPORTED_LAYERS,
f"X: {self.__x}, Y: {self.__y}, W: {self.__w}, H: {self.__h}",
)
# This method below receives a list of images and the name of the image, transforms the images, and finally
# returns the transformed image
def _apply_layer(self, images, name=None):
transformed_images = []
for image in images:
transformed_images.append(image[self.__x : self.__x + self.__w, self.__y : self.__y + self.__h])
return transformed_images
__init__(self, name, type, supported_parent_layer, parameter_str, bypass_validation=False)
special
Init method for Layer class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str |
Name of the layer. |
required |
type |
str |
Type of the layer |
required |
supported_parent_layer |
list |
List of layers that the current layers support as a parent. Layer class has a standard list of layers supported by most of layers. It can be used here. Check the example code for more information. |
required |
parameter_str |
str |
Parameter string used for model summary generation. |
required |
bypass_validation |
bool |
Flag to bypass validation flags in model.add() function. Used heavy when making new custom layers. Defaults to False. |
False |
Exceptions:
Type | Description |
---|---|
ValueError |
If the name parameter is not valid. |
ValueError |
If the type parameter is not valid. |
ValueError |
If the supported_parent_layer parameter is not valid. |
ValueError |
If the parameter_str parameter is not valid. |
ValueError |
If the bypass_validation parameter is not valid. |
Source code in hocrox/utils/layer.py
def __init__(self, name, type, supported_parent_layer, parameter_str, bypass_validation=False):
"""Init method for Layer class.
Args:
name (str): Name of the layer.
type (str): Type of the layer
supported_parent_layer (list): List of layers that the current layers support as a parent. Layer class has
a standard list of layers supported by most of layers. It can be used here. Check the example code
for more information.
parameter_str (str): Parameter string used for model summary generation.
bypass_validation (bool, optional): Flag to bypass validation flags in model.add() function. Used heavy
when making new custom layers. Defaults to False.
Raises:
ValueError: If the name parameter is not valid.
ValueError: If the type parameter is not valid.
ValueError: If the supported_parent_layer parameter is not valid.
ValueError: If the parameter_str parameter is not valid.
ValueError: If the bypass_validation parameter is not valid.
"""
if name and not isinstance(name, str):
raise ValueError(f"The value {name} for the argument name is not valid")
if not isinstance(type, str):
raise ValueError(f"The value {type} for the argument type is not valid")
if not isinstance(supported_parent_layer, list):
raise ValueError(f"The value {supported_parent_layer} for the argument supported_parent_layer is not valid")
if not isinstance(parameter_str, str):
raise ValueError(f"The value {parameter_str} for the argument parameter_str is not valid")
if not isinstance(bypass_validation, bool):
raise ValueError(f"The value {bypass_validation} for the argument bypass_validation is not valid")
self.__name = name or f"{type.capitalize().replace('_', ' ')} Layer"
self.__type = type
self.__supported_parent_layer = supported_parent_layer
self.__parameter_str = parameter_str
self.__bypass_validation = bypass_validation