Skip to content

Hocrox

Hocrox is an image preprocessing and augmentation library.

It provides a Keras like simple interface to make preprocessing and augmentation pipelines. Hocrox internally uses OpenCV to perform the operations on images. OpenCV is one of the most popular Computer Vision library.

Here are some of the highlights of Hocrox:

  • Provides an easy interface that is suitable for radio pipeline development
  • It internally uses OpenCV
  • Highly configurable with support for custom layers

layer special

Layers are the fundamental blocks of Hocrox image preprocessing and augmentation library.

In Hocrox, each layer basically means a function that will be performed on the images to preprocess or/and augment.

Currently, in Hocrox, there are several layers. The complete list is below.

  • Preprocessing
    • Crop
    • Grayscale
    • Horizontal Flip
    • Padding
    • Resize
    • Rotate
    • Save
    • Vertical Flip
  • Augmentation
    • RandomFlip
    • RandomRotate

augmentation special

Image Augmentation is the process of increasing the image data by slightly tweaking and changing the original images.

In other words, Augmentation means artificially creating more data to train ML or DL models.

In image Augmentation, we slightly rotate, flip, change the color of the images to generate multiple versions of the image. Since all these images are from the original image with slight modifications, they still represent the original patterns from the original images (most of the time).

In Hocrox, currently, there are only 2 augmentation layers, RandomFlip and RandomRotate.

random_flip

RandomFlip layer for Hocrox.

RandomFlip (Layer)

RandomFlip layer randomly flips the image vertically or horizontally.

Here is an example code to use the RandomFlip layer in a model.

from hocrox.model import Model
from hocrox.layer.augmentation import RandomFlip
from hocrox.layer import Read

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))
model.add(RandomFlip(number_of_outputs=1))

# Printing the summary of the model
print(model.summary())
__init__(self, number_of_outputs=1, name=None) special

Init method for the RandomFlip layer.

Parameters:

Name Type Description Default
number_of_outputs int

Number of images to output. Defaults to 1.

1
name str

Name of the layer, if not provided then automatically generates a unique name for the layer. Defaults to None.

None

Exceptions:

Type Description
ValueError

If the number_of_images parameter is not valid

Source code in hocrox/layer/augmentation/random_flip.py
def __init__(self, number_of_outputs=1, name=None):
    """Init method for the RandomFlip layer.

    Args:
        number_of_outputs (int, optional): Number of images to output. Defaults to 1.
        name (str, optional): Name of the layer, if not provided then automatically generates a unique name for
            the layer. Defaults to None.

    Raises:
        ValueError: If the number_of_images parameter is not valid
    """
    if isinstance(number_of_outputs, int) and number_of_outputs < 1:
        raise ValueError(f"The value {number_of_outputs} for the argument number_of_outputs is not valid")

    super().__init__(
        name,
        "random_flip",
        [
            "resize",
            "greyscale",
            "rotate",
            "crop",
            "padding",
            "save",
            "horizontal_flip",
            "vertical_flip",
            "random_rotate",
            "random_flip",
            "read",
            "rescale",
        ],
        f"Number of Outputs: {number_of_outputs}",
    )

    self.__number_of_outputs = number_of_outputs

random_rotate

RandomRotate layer for Hocrox.

RandomRotate (Layer)

RandomRotate layer randomly rotates an image to a certain angle.

Here is an example code to use RandomRotate layer in a model.

from hocrox.model import Model
from hocrox.layer.augmentation import RandomRotate
from hocrox.layer import Read

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))
model.add(RandomRotate(start_angle=-10.0, end_angle=10.0, number_of_outputs=5))

# Printing the summary of the model
print(model.summary())
__init__(self, start_angle, end_angle, number_of_outputs=1, name=None) special

Init method for the RandomRotate layer.

Parameters:

Name Type Description Default
start_angle float

Start of the range of angle

required
end_angle float

End of the range of angle

required
number_of_outputs int

Number of images to output. Defaults to 1.

1
name str

Name of the layer, if not provided then automatically generates a unique name for the layer. Defaults to None.

None

Exceptions:

Type Description
ValueError

If the start_angle parameter is not valid

ValueError

If the end_angle parameter is not valid

ValueError

If the number_of_outputs parameter is not valid

ValueError

If the name parameter is not valid

Source code in hocrox/layer/augmentation/random_rotate.py
def __init__(self, start_angle, end_angle, number_of_outputs=1, name=None):
    """Init method for the RandomRotate layer.

    Args:
        start_angle (float): Start of the range of angle
        end_angle (float): End of the range of angle
        number_of_outputs (int, optional): Number of images to output. Defaults to 1.
        name (str, optional): Name of the layer, if not provided then automatically generates a unique name for
            the layer. Defaults to None.

    Raises:
        ValueError: If the start_angle parameter is not valid
        ValueError: If the end_angle parameter is not valid
        ValueError: If the number_of_outputs parameter is not valid
        ValueError: If the name parameter is not valid
    """
    if not (isinstance(start_angle, int) or isinstance(start_angle, float)):
        raise ValueError(f"The value {start_angle} for the argument start_angle is not valid")

    if not (isinstance(end_angle, int) or isinstance(end_angle, float)):
        raise ValueError(f"The value {end_angle} for the argument end_angle is not valid")

    if start_angle > end_angle:
        raise ValueError(f"The value {start_angle} for the argument start_angle is not valid")

    if isinstance(number_of_outputs, int) and number_of_outputs < 1:
        raise ValueError(f"The value {number_of_outputs} for the argument number_of_outputs is not valid")

    self.__start_angle = start_angle
    self.__end_angle = end_angle
    self.__number_of_outputs = number_of_outputs

    super().__init__(
        name,
        "random_rotate",
        [
            "resize",
            "greyscale",
            "rotate",
            "crop",
            "padding",
            "save",
            "horizontal_flip",
            "vertical_flip",
            "random_rotate",
            "random_flip",
            "read",
            "rescale",
        ],
        f"Number of Outputs: {number_of_outputs}",
    )

preprocessing special

Image preprocessing is the process of formatting and tweaking images before they are used by some models.

crop

Crop layer for Hocrox.

Crop (Layer)

Crop layer crops an image.

Here is an example code to use the Crop layer in a model.

from hocrox.model import Model
from hocrox.layer.preprocessing import Crop
from hocrox.layer import Read

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))
model.add(Crop(x=10, y=10, w=100, h=100))

# Printing the summary of the model
print(model.summary())
__init__(self, x, y, w, h, name=None) special

Init method for the crop layer.

Parameters:

Name Type Description Default
x int

X coordinate for the crop.

required
y int

Y coordinate for the crop.

required
w int

Width of the cropped image.

required
h int

Height of the cropped image.

required
name str

Name of the layer, if not provided then automatically generates a unique name for the layer. Defaults to None.

None

Exceptions:

Type Description
ValueError

If the parameter x is not valid

ValueError

If the parameter y is not valid

ValueError

If the parameter w is not valid

ValueError

If the parameter h is not valid

Source code in hocrox/layer/preprocessing/crop.py
def __init__(self, x, y, w, h, name=None):
    """Init method for the crop layer.

    Args:
        x (int): X coordinate for the crop.
        y (int): Y coordinate for the crop.
        w (int): Width of the cropped image.
        h (int): Height of the cropped image.
        name (str, optional): Name of the layer, if not provided then automatically generates a unique name for
            the layer. Defaults to None.

    Raises:
        ValueError: If the parameter x is not valid
        ValueError: If the parameter y is not valid
        ValueError: If the parameter w is not valid
        ValueError: If the parameter h is not valid
    """
    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",
        [
            "resize",
            "greyscale",
            "rotate",
            "crop",
            "padding",
            "save",
            "horizontal_flip",
            "vertical_flip",
            "random_rotate",
            "random_flip",
            "read",
            "rescale",
        ],
        f"X: {self.__x}, Y: {self.__y}, W: {self.__w}, H: {self.__h}",
    )

grayscale

Grayscale layer for Hocrox.

Grayscale (Layer)

Grayscale layer grayscaled an image.

Here is an example code to use the Crop layer in a model.

from hocrox.model import Model
from hocrox.layer.preprocessing import Grayscale
from hocrox.layer import Read

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))
model.add(Grayscale())

# Printing the summary of the model
print(model.summary())
__init__(self, name=None) special

Init method for grayscale layer.

Parameters:

Name Type Description Default
name str

Name of the layer, if not provided then automatically generates a unique name for the layer. Defaults to None.

None
Source code in hocrox/layer/preprocessing/grayscale.py
def __init__(self, name=None):
    """Init method for grayscale layer.

    Args:
        name (str, optional): Name of the layer, if not provided then automatically generates a unique name for
            the layer. Defaults to None.

    """
    super().__init__(
        name,
        "greyscale",
        [
            "resize",
            "greyscale",
            "rotate",
            "crop",
            "padding",
            "save",
            "horizontal_flip",
            "vertical_flip",
            "random_rotate",
            "random_flip",
            "read",
            "rescale",
        ],
        "-",
    )

horizontal_flip

HorizontalFlip layer for Hocrox.

HorizontalFlip (Layer)

HorizontalFlip layer horizontally flips an image.

Here is an example code to use the Crop layer in a model.

from hocrox.model import Model
from hocrox.layer.preprocessing import HorizontalFlip
from hocrox.layer import Read

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))
model.add(HorizontalFlip())

# Printing the summary of the model
print(model.summary())
__init__(self, name=None) special

Init method for horizontal flip layer.

Parameters:

Name Type Description Default
name str

Name of the layer, if not provided then automatically generates a unique name for the layer. Defaults to None.

None
Source code in hocrox/layer/preprocessing/horizontal_flip.py
def __init__(self, name=None):
    """Init method for horizontal flip layer.

    Args:
        name (str, optional): Name of the layer, if not provided then automatically generates a unique name for
            the layer. Defaults to None.

    """
    super().__init__(
        name,
        "horizontal_flip",
        [
            "resize",
            "greyscale",
            "rotate",
            "crop",
            "padding",
            "save",
            "horizontal_flip",
            "vertical_flip",
            "random_rotate",
            "random_flip",
            "read",
            "rescale",
        ],
        "-",
    )

pading

Padding layer for Hocrox.

Padding (Layer)

Padding layer add a padding to an image.

Here is an example code to use the Padding layer in a model.

from hocrox.model import Model
from hocrox.layer.preprocessing import Padding
from hocrox.layer import Read

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))
model.add(Padding(top=20, bottom=20, left=20, right=20, color=[255, 255, 255]))

# Printing the summary of the model
print(model.summary())
__init__(self, top, bottom, left, right, color=[255, 255, 255], name=None) special

Init method for Padding layer.

Parameters:

Name Type Description Default
top int

Top padding size.

required
bottom int

Bottom padding size.

required
left int

Left padding size.

required
right int

Right padding size.

required
color list

Color of the padding. Defaults to [255, 255, 255].

[255, 255, 255]
name str

Name of the layer, if not provided then automatically generates a unique name for the layer. Defaults to None.

None

Exceptions:

Type Description
ValueError

If the top parameter is not valid.

ValueError

If the bottom parameter is not valid.

ValueError

If the left parameter is not valid.

ValueError

If the right parameter is not valid.

ValueError

If the color parameter is not valid.

Source code in hocrox/layer/preprocessing/pading.py
def __init__(self, top, bottom, left, right, color=[255, 255, 255], name=None):
    """Init method for Padding layer.

    Args:
        top (int): Top padding size.
        bottom (int): Bottom padding size.
        left (int): Left padding size.
        right (int): Right padding size.
        color (list, optional): Color of the padding. Defaults to [255, 255, 255].
        name (str, optional): Name of the layer, if not provided then automatically generates a unique name for
            the layer. Defaults to None.

    Raises:
        ValueError: If the top parameter is not valid.
        ValueError: If the bottom parameter is not valid.
        ValueError: If the left parameter is not valid.
        ValueError: If the right parameter is not valid.
        ValueError: If the color parameter is not valid.
    """
    if top and not isinstance(top, int):
        raise ValueError(f"The value {top} for the argument top is not valid")

    if bottom and not isinstance(bottom, int):
        raise ValueError(f"The value {bottom} for the argument bottom is not valid")

    if left and not isinstance(left, int):
        raise ValueError(f"The value {left} for the argument left is not valid")

    if right and not isinstance(right, int):
        raise ValueError(f"The value {right} for the argument right is not valid")

    if not isinstance(color, list) or len(color) != 3:
        raise ValueError(f"The value {color} for the argument color is not valid")

    self.__top = top
    self.__bottom = bottom
    self.__right = right
    self.__left = left
    self.__color = color

    super().__init__(
        name,
        "padding",
        [
            "resize",
            "greyscale",
            "rotate",
            "crop",
            "padding",
            "save",
            "horizontal_flip",
            "vertical_flip",
            "random_rotate",
            "random_flip",
            "read",
            "rescale",
        ],
        f"Top: {self.__top}, Bottom: {self.__bottom}, Left: {self.__left}, Right: {self.__right}",
    )

rescale

Resscale layer for Hocrox.

Resscale (Layer)

Resscale layer rescales an image.

Here is an example code to use the Resscale layer in a model.

from hocrox.model import Model
from hocrox.layer.preprocessing import Resscale
from hocrox.layer import Read

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))
model.add(Resscale(rescale=1.0 / 255.0))

# Printing the summary of the model
print(model.summary())
__init__(self, rescale=0.00392156862745098, name=None) special

Init method for Resize layer.

Parameters:

Name Type Description Default
rescale float

Rescale factor for rescaling image, Defaults to 1.0 / 255.0.

0.00392156862745098
name str

Name of the layer, if not provided then automatically generates a unique name for the layer. Defaults to None.

None

Exceptions:

Type Description
ValueError

If the rescale parameter is not valid

Source code in hocrox/layer/preprocessing/rescale.py
def __init__(self, rescale=1.0 / 255.0, name=None):
    """Init method for Resize layer.

    Args:
        rescale (float, optional): Rescale factor for rescaling image, Defaults to 1.0 / 255.0.
        name (str, optional): Name of the layer, if not provided then automatically generates a unique name for
            the layer. Defaults to None.

    Raises:
        ValueError: If the rescale parameter is not valid
    """
    if not isinstance(rescale, float):
        raise ValueError(f"The value {rescale} for the argument rescale is not valid")

    self.__rescale = rescale

    super().__init__(
        name,
        "rescale",
        [
            "resize",
            "greyscale",
            "rotate",
            "crop",
            "padding",
            "save",
            "horizontal_flip",
            "vertical_flip",
            "random_rotate",
            "random_flip",
            "read",
            "rescale",
        ],
        f"Rescale: {self.__rescale}",
    )

resize

Resize layer for Hocrox.

Resize (Layer)

Resize layer resize an image to specific dimension.

Here is an example code to use the Resize layer in a model.

from hocrox.model import Model
from hocrox.layer.preprocessing import Resize
from hocrox.layer import Read

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))
model.add(Resize(dim=(200,200), interpolation="INTER_LINEAR"))

# Printing the summary of the model
print(model.summary())
__init__(self, dim, interpolation='INTER_LINEAR', name=None) special

Init method for Resize layer.

Parameters:

Name Type Description Default
dim tuple

New dimension for the image

required
interpolation str

Interpolation method for the image. Defaults to "INTER_LINEAR".

'INTER_LINEAR'
name str

Name of the layer, if not provided then automatically generates a unique name for the layer. Defaults to None.

None

Exceptions:

Type Description
ValueError

If the dim parameter is not valid

ValueError

If the dim parameter values are less than 0

ValueError

If the interpolation parameter is not valid

Source code in hocrox/layer/preprocessing/resize.py
def __init__(self, dim, interpolation="INTER_LINEAR", name=None):
    """Init method for Resize layer.

    Args:
        dim (tuple): New dimension for the image
        interpolation (str, optional): Interpolation method for the image. Defaults to "INTER_LINEAR".
        name (str, optional): Name of the layer, if not provided then automatically generates a unique name for
            the layer. Defaults to None.

    Raises:
        ValueError: If the dim parameter is not valid
        ValueError: If the dim parameter values are less than 0
        ValueError: If the interpolation parameter is not valid
    """
    if not isinstance(dim, tuple):
        raise ValueError(f"The value {dim} for the argument dim is not valid")

    if dim[0] <= 0 or dim[1] <= 0:
        raise ValueError(f"The value {dim} for the argument dim is not valid")

    if interpolation not in ("INTER_LINEAR", "INTER_AREA", "INTER_CUBIC"):
        raise ValueError(f"The value {interpolation} for the argument interpolation is not valid")

    self.__dim = dim

    if interpolation == "INTER_LINEAR":
        self.__interpolation = cv2.INTER_LINEAR
    elif interpolation == "INTER_AREA":
        self.__interpolation = cv2.INTER_AREA
    else:
        self.__interpolation = cv2.INTER_CUBIC

    super().__init__(
        name,
        "resize",
        [
            "resize",
            "greyscale",
            "rotate",
            "crop",
            "padding",
            "save",
            "horizontal_flip",
            "vertical_flip",
            "random_rotate",
            "random_flip",
            "read",
            "rescale",
        ],
        f"Dim: {self.__dim}, Interpolation: {self.__interpolation}",
    )

rotate

Rotate layer for Hocrox.

Rotate (Layer)

Grayscale layer grayscaled an image.

Here is an example code to use the Crop layer in a model.

from hocrox.model import Model
from hocrox.layer.preprocessing import Grayscale
from hocrox.layer import Hocrox

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))
model.add(Grayscale())

# Printing the summary of the model
print(model.summary())
__init__(self, angle, name=None) special

Init method for the Rotate layer.

Parameters:

Name Type Description Default
angle float

Angle to ratate the image.

required
name str

Name of the layer, if not provided then automatically generates a unique name for the layer. Defaults to None.

None

Exceptions:

Type Description
ValueError

If the angle is not valid.

Source code in hocrox/layer/preprocessing/rotate.py
def __init__(self, angle, name=None):
    """Init method for the Rotate layer.

    Args:
        angle (float): Angle to ratate the image.
        name (str, optional): Name of the layer, if not provided then automatically generates a unique name for
            the layer. Defaults to None.

    Raises:
        ValueError: If the angle is not valid.
    """
    if not (isinstance(angle, int) or isinstance(angle, float)):
        raise ValueError(f"The value {angle} for the argument angle is not valid")

    self.__angle = angle

    super().__init__(
        name,
        "rotate",
        [
            "resize",
            "greyscale",
            "rotate",
            "crop",
            "padding",
            "save",
            "horizontal_flip",
            "vertical_flip",
            "random_rotate",
            "random_flip",
            "read",
            "rescale",
        ],
        f"Angle: {self.__angle}",
    )

vertical_flip

VerticalFlip layer for Hocrox.

VerticalFlip (Layer)

VerticalFlip layer vertically flips an image.

Here is an example code to use the Crop layer in a model.

from hocrox.model import Model
from hocrox.layer.preprocessing import VerticalFlip
from hocrox.layer import Read

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))
model.add(VerticalFlip())

# Printing the summary of the model
print(model.summary())
__init__(self, name=None) special

Init method for horizontal flip layer.

Parameters:

Name Type Description Default
name str

Name of the layer, if not provided then automatically generates a unique name for the layer. Defaults to None.

None
Source code in hocrox/layer/preprocessing/vertical_flip.py
def __init__(self, name=None):
    """Init method for horizontal flip layer.

    Args:
        name (str, optional): Name of the layer, if not provided then automatically generates a unique name for
            the layer. Defaults to None.

    """
    super().__init__(
        name,
        "vertical_flip",
        [
            "resize",
            "greyscale",
            "rotate",
            "crop",
            "padding",
            "save",
            "horizontal_flip",
            "vertical_flip",
            "random_rotate",
            "random_flip",
            "read",
            "rescale",
        ],
        "-",
    )

read

Read layer for Hocrox.

Read (Layer)

Read layer reads images from the local filesystem.

Here is an example code to use the Read layer in a model.

from hocrox.model import Model
from hocrox.layer import Read

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))

# Printing the summary of the model
print(model.summary())
__init__(self, path, name=None) special

Init method for the Read layer.

Parameters:

Name Type Description Default
path str

Path to store the image

required
name str

Name of the layer, if not provided then automatically generates a unique name for the layer. Defaults to None.

None

Exceptions:

Type Description
ValueError

If the name parameter is invalid

Source code in hocrox/layer/read.py
def __init__(self, path, name=None):
    """Init method for the Read layer.

    Args:
        path (str): Path to store the image
        name (str, optional): Name of the layer, if not provided then automatically generates a unique name for
            the layer. Defaults to None.

    Raises:
        ValueError: If the name parameter is invalid
    """
    if path and not isinstance(path, str):
        raise ValueError(f"The value {path} for the argument path is not valid")

    self.__path = path

    super().__init__(
        name,
        "read",
        [],
        f"Path: {self.__path}",
    )

save

Save layer for Hocrox.

Save (Layer)

Save layer saves images on the local filesystem.

Here is an example code to use the Save layer in a model.

from hocrox.model import Model
from hocrox.layer import Read, Save

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))
model.add(Save(path="./img_to_store", format="npy"))

# Printing the summary of the model
print(model.summary())
__init__(self, path, format='npy', name=None) special

Init method for the Save layer.

Parameters:

Name Type Description Default
path str

Path to store the image

required
format str

Format to save the image. Supported formats are npy and img. Defaults to "npy".

'npy'
name str

Name of the layer, if not provided then automatically generates a unique name for the layer. Defaults to None.

None

Exceptions:

Type Description
ValueError

If the name parameter is invalid

ValueError

If the format parameter is invalid

Source code in hocrox/layer/save.py
def __init__(self, path, format="npy", name=None):
    """Init method for the Save layer.

    Args:
        path (str): Path to store the image
        format (str, optional): Format to save the image. Supported formats are npy and img. Defaults to "npy".
        name (str, optional): Name of the layer, if not provided then automatically generates a unique name for
            the layer. Defaults to None.

    Raises:
        ValueError: If the name parameter is invalid
        ValueError: If the format parameter is invalid
    """
    if path and not isinstance(path, str):
        raise ValueError(f"The value {path} for the argument path is not valid")

    if format not in ("npy", "img"):
        raise ValueError(f"The value {format} for the argument format is not valid")

    self.__path = path
    self.__format = format

    super().__init__(
        name,
        "save",
        [
            "resize",
            "greyscale",
            "rotate",
            "crop",
            "padding",
            "save",
            "horizontal_flip",
            "vertical_flip",
            "random_rotate",
            "random_flip",
            "read",
            "rescale",
        ],
        f"Path: {self.__path}, Format: {self.__format}",
    )

model special

Model module for Hocrox library.

Models are the heart of Hocrox. In Hocrox, the model is used to make the entire pipeline for preprocessing and/or augmenting the image.

model

Model class for Hocrox.

Model

Model class is used for making Hocrox models.

Here is an example code to use the Model class for making a Hocrox model.

from hocrox.model import Model
from hocrox.layer.augmentation import RandomFlip, RandomRotate
from hocrox.layer import Read

# Initializing the model
model = Model()

# Adding model layers
model.add(Read(path="./img"))
model.add(RandomFlip(number_of_outputs=2))
model.add(RandomRotate(start_angle=-10.0, end_angle=10.0, number_of_outputs=5))

# Printing the summary of the model
print(model.summary())
__init__(self) special

Init method for the Model class.

Source code in hocrox/model/model.py
def __init__(self):
    """Init method for the Model class."""
    self.__frozen = False
    self.__layers = []
add(self, layer)

Add a new layer to the model.

Parameters:

Name Type Description Default
layer layer

Layer class to add into the model.

required

Exceptions:

Type Description
ValueError

If the model is frozen.

ValueError

If the layer is not valid.

ValueError

If the layer does support the parent layer.

ValueError

If the first layer is not a read layer.

Source code in hocrox/model/model.py
def add(self, layer):
    """Add a new layer to the model.

    Args:
        layer (layer): Layer class to add into the model.

    Raises:
        ValueError: If the model is frozen.
        ValueError: If the layer is not valid.
        ValueError: If the layer does support the parent layer.
        ValueError: If the first layer is not a read layer.
    """
    if self.__frozen:
        raise ValueError("Model is frozen")

    if not is_valid_layer(layer):
        raise ValueError("The layer is not a valid layer")

    if len(self.__layers) == 0 and layer._get_type() != "read":
        raise ValueError("The first layer needed to be a read layer")

    if len(self.__layers) > 0:
        previous_layer_type = self.__layers[-1]._get_type()

        if not layer._is_valid_child(previous_layer_type):
            tp = layer._get_type()
            raise ValueError(
                f"The layer of type '{tp}' does not support layer of type '{previous_layer_type}' as parent layer"
            )

    self.__layers.append(layer)
freeze(self)

Freeze the model. Frozen models cannot be modified.

Here is an example code to use .freeze() function in a model.

from hocrox.model import Model

# Initializing the model
model = Model()

...
...

# Freeze the model so it can not be modified
model.freeze()
Source code in hocrox/model/model.py
def freeze(self):
    """Freeze the model. Frozen models cannot be modified.

    Here is an example code to use .freeze() function in a model.

    ```python
    from hocrox.model import Model

    # Initializing the model
    model = Model()

    ...
    ...

    # Freeze the model so it can not be modified
    model.freeze()
    ```
    """
    self.__frozen = True
load(self, path)

Load a model from the filesystem.

Here is an example code to use .load() function in a model.

from hocrox.model import Model

# Initializing the model
model = Model()

...
...

# Load the model to specific path
model.load(path="./model.hocrox")

Parameters:

Name Type Description Default
path str

Path to read the model from.

required

Exceptions:

Type Description
ValueError

If the path is not valid.

Source code in hocrox/model/model.py
def load(self, path):
    """Load a model from the filesystem.

    Here is an example code to use .load() function in a model.

    ```python
    from hocrox.model import Model

    # Initializing the model
    model = Model()

    ...
    ...

    # Load the model to specific path
    model.load(path="./model.hocrox")
    ```

    Args:
        path (str): Path to read the model from.

    Raises:
        ValueError: If the path is not valid.
    """
    if not isinstance(path, str):
        raise ValueError("Path is not valid")

    with open(path, "rb") as f:
        model_config = pickle.load(f)

        self.__layers = model_config["layers"]
        self.__frozen = model_config["frozen"]
save(self, path)

Save the model into the filesystem.

It internally uses the pickle module to save the model.

Here is an example code to use .save() function in a model.

from hocrox.model import Model

# Initializing the model
model = Model()

...
...

# Save the model to specific path
model.save(path="./model.hocrox")

Parameters:

Name Type Description Default
path str

Path to store the model.

required

Exceptions:

Type Description
ValueError

If the path is not valid.

Source code in hocrox/model/model.py
def save(self, path):
    """Save the model into the filesystem.

    It internally uses the pickle module to save the model.

    Here is an example code to use .save() function in a model.

    ```python
    from hocrox.model import Model

    # Initializing the model
    model = Model()

    ...
    ...

    # Save the model to specific path
    model.save(path="./model.hocrox")
    ```

    Args:
        path (str): Path to store the model.

    Raises:
        ValueError: If the path is not valid.
    """
    if not isinstance(path, str):
        raise ValueError("Path is not valid")

    model_config = {"frozen": self.__frozen, "layers": self.__layers}

    with open(path, "wb") as f:
        pickle.dump(model_config, f)
summary(self)

Generate a summary of the model.

Here is an example code to use .summary() function in a model.

from hocrox.model import Model

# Initializing the model
model = Model()

...
...

# Printing the summary of the model
print(model.summary())

Returns:

Type Description
str

Summary of the model.

Source code in hocrox/model/model.py
def summary(self):
    """Generate a summary of the model.

    Here is an example code to use .summary() function in a model.

    ```python
    from hocrox.model import Model

    # Initializing the model
    model = Model()

    ...
    ...

    # Printing the summary of the model
    print(model.summary())
    ```

    Returns:
        str: Summary of the model.
    """
    t = PrettyTable(["Index", "Name", "Parameters"])

    for index, layer in enumerate(self.__layers):
        (name, parameters) = layer._get_description()

        t.add_row([f"#{index+1}", name, parameters])

    return str(t)
transform(self)

Perform the transformation of the images using the defined model pipeline.

Here is an example code to use .transform() function in a model.

from hocrox.model import Model

# Initializing the model
model = Model()

...
...

# Apply transformation to the images based on the defined model pipeline.
model.transform()
Source code in hocrox/model/model.py
def transform(self):
    """Perform the transformation of the images using the defined model pipeline.

    Here is an example code to use .transform() function in a model.

    ```python
    from hocrox.model import Model

    # Initializing the model
    model = Model()

    ...
    ...

    # Apply transformation to the images based on the defined model pipeline.
    model.transform()
    ```
    """
    read_image_layer = self.__layers[0]
    images, gen = read_image_layer._apply_layer()

    for path, image in tqdm(gen, total=len(images)):
        for layer in self.__layers[1:]:
            image = layer._apply_layer(image, path)

utils special

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",
            [
                "resize",
                "greyscale",
                "rotate",
                "crop",
                "padding",
                "save",
                "horizontal_flip",
                "vertical_flip",
                "random_rotate",
                "random_flip",
            ],
            f"X: {self.__x}, Y: {self.__y}, W: {self.__w}, H: {self.__h}",
        )

    # This method below receives a list of images and 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.

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.
        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