dualing.models

This is the heart. All models are declared and implemented here. We will offer you the most fantastic implementation of everything we are working with. Please take a closer look at this package.

A package for already-implemented machine learning Siamese architectures.

class dualing.models.ContrastiveSiamese(base: dualing.core.model.Base, margin: Optional[float] = 1.0, distance_metric: Optional[str] = 'L2', name: Optional[str] = '')

Bases: dualing.core.Siamese

A ContrastiveSiamese class is responsible for implementing the contrastive version of Siamese Neural Networks.

References

I. Melekhov, J. Kannala and E. Rahtu. Siamese network features for image matching. 23rd International Conference on Pattern Recognition (2016).

__init__(self, base: dualing.core.model.Base, margin: Optional[float] = 1.0, distance_metric: Optional[str] = 'L2', name: Optional[str] = '')

Initialization method.

Parameters
  • base – Twin architecture.

  • margin – Radius around the embedding space.

  • distance_metric – Distance metric.

  • name – Naming identifier.

compile(self, optimizer: tensorflow.keras.optimizers)

Method that builds the network by attaching optimizer, loss and metrics.

Parameters

optimizer – Optimization algorithm.

property distance(self)

Distance metric.

evaluate(self, batches: Union[dualing.datasets.pair.BalancedPairDataset, dualing.datasets.pair.RandomPairDataset])

Method that evaluates the model over validation or testing batches.

Parameters

batches – Batches of tuples holding validation / testing samples and labels.

fit(self, batches: Union[dualing.datasets.pair.BalancedPairDataset, dualing.datasets.pair.RandomPairDataset], epochs: Optional[int] = 100)

Method that trains the model over training batches.

Parameters
  • batches – Batches of tuples holding training samples and labels.

  • epochs – Maximum number of epochs.

property margin(self)

Radius around the embedding space.

predict(self, x1: tensorflow.Tensor, x2: tensorflow.Tensor)

Method that performs a forward pass over samples and returns the network’s output.

Parameters
  • x1 – Tensor containing first samples from input pairs.

  • x2 – Tensor containing second samples from input pairs.

Returns

The distance between samples x1 and x2.

Return type

(tf.Tensor)

step(self, x1: tensorflow.Tensor, x2: tensorflow.Tensor, y: tensorflow.Tensor)

Method that performs a single batch optimization step.

Parameters
  • x1 – Tensor containing first samples from input pairs.

  • x2 – Tensor containing second samples from input pairs.

  • y – Tensor containing labels (1 for similar, 0 for dissimilar).

class dualing.models.CrossEntropySiamese(base: dualing.core.model.Base, distance_metric: Optional[str] = 'concat', name: Optional[str] = '')

Bases: dualing.core.Siamese

A CrossEntropySiamese class is responsible for implementing the cross-entropy version of Siamese Neural Networks.

References

G. Koch, R. Zemel and R. Salakhutdinov. Siamese neural networks for one-shot image recognition. ICML Deep Learning Workshop (2015).

__init__(self, base: dualing.core.model.Base, distance_metric: Optional[str] = 'concat', name: Optional[str] = '')

Initialization method.

Parameters
  • base – Twin architecture.

  • distance_metric – Distance metric.

  • name – Naming identifier.

compile(self, optimizer: tensorflow.keras.optimizers)

Method that builds the network by attaching optimizer, loss and metrics.

Parameters

optimizer – Optimization algorithm.

property distance(self)

Distance metric.

evaluate(self, batches: Union[dualing.datasets.pair.BalancedPairDataset, dualing.datasets.pair.RandomPairDataset])

Method that evaluates the model over validation or testing batches.

Parameters

batches – Batches of tuples holding validation / testing samples and labels.

fit(self, batches: Union[dualing.datasets.pair.BalancedPairDataset, dualing.datasets.pair.RandomPairDataset], epochs: Optional[int] = 100)

Method that trains the model over training batches.

Parameters
  • batches – Batches of tuples holding training samples and labels.

  • epochs – Maximum number of epochs.

predict(self, x1: tensorflow.Tensor, x2: tensorflow.Tensor)

Method that performs a forward pass over samples and returns the network’s output.

Parameters
  • x1 – Tensor containing first samples from input pairs.

  • x2 – Tensor containing second samples from input pairs.

Returns

The similarity score between samples x1 and x2.

Return type

(tf.Tensor)

step(self, x1: tensorflow.Tensor, x2: tensorflow.Tensor, y: tensorflow.Tensor)

Method that performs a single batch optimization step.

Parameters
  • x1 – Tensor containing first samples from input pairs.

  • x2 – Tensor containing second samples from input pairs.

  • y – Tensor containing labels (1 for similar, 0 for dissimilar).

class dualing.models.TripletSiamese(base: dualing.core.model.Base, loss: Optional[str] = 'hard', margin: Optional[int] = 1.0, soft: Optional[bool] = False, distance_metric: Optional[str] = 'L2', name: Optional[str] = '')

Bases: dualing.core.Siamese

A TripletSiamese class is responsible for implementing the triplet-loss version of Siamese Neural Networks.

References

X. Dong and J. Shen. Triplet loss in siamese network for object tracking. Proceedings of the European Conference on Computer Vision (2018).

__init__(self, base: dualing.core.model.Base, loss: Optional[str] = 'hard', margin: Optional[int] = 1.0, soft: Optional[bool] = False, distance_metric: Optional[str] = 'L2', name: Optional[str] = '')

Initialization method.

Parameters
  • base – Twin architecture.

  • loss – Whether network should use hard or semi-hard negative mining.

  • margin – Radius around the embedding space.

  • soft – Whether network should use soft margin or not.

  • distance_metric – Distance metric.

  • name – Naming identifier.

compile(self, optimizer: tensorflow.keras.optimizers)

Method that builds the network by attaching optimizer, loss and metrics.

Parameters

optimizer – Optimization algorithm.

property distance(self)

Distance metric.

evaluate(self, batches: dualing.datasets.batch.BatchDataset)

Method that evaluates the model over validation or testing batches.

Parameters

batches – Batches of tuples holding validation / test samples and labels.

fit(self, batches: dualing.datasets.batch.BatchDataset, epochs: Optional[int] = 100)

Method that trains the model over training batches.

Parameters
  • batches – Batches of tuples holding training samples and labels.

  • epochs – Maximum number of epochs.

property loss_type(self)

Type of loss (hard or semi-hard).

property margin(self)

Radius around the embedding space.

predict(self, x1: tensorflow.Tensor, x2: tensorflow.Tensor)

Method that performs a forward pass over samples and returns the network’s output.

Parameters
  • x1 – Tensor containing first samples from input pairs.

  • x2 – Tensor containing second samples from input pairs.

Returns

The distance between samples x1 and x2.

Return type

(tf.Tensor)

property soft(self)

Whether soft margin should be used or not.

step(self, x: tensorflow.Tensor, y: tensorflow.Tensor)

Method that performs a single batch optimization step.

Parameters
  • x – Tensor containing samples.

  • y – Tensor containing labels.