chesscog.core package

chesscog.core.DEVICE = 'cpu'

Device to be used for computation (GPU if available, else CPU).

chesscog.core.device(x: Union[torch.Tensor, torch.nn.modules.module.Module, List[torch.Tensor], tuple, dict, Generator], dev: str = 'cpu') Union[torch.Tensor, torch.nn.modules.module.Module, List[torch.Tensor], tuple, dict, Generator]

Convenience method to move a tensor/module/other structure containing tensors to the device.

Parameters
  • x (T) – the tensor (or strucure containing tensors)

  • dev (str, optional) – the device to move the tensor to. Defaults to DEVICE.

Raises

TypeError – if the type was not a compatible tensor

Returns

the input tensor moved to the device

Return type

T

chesscog.core.listify(func: Callable[[...], Iterable]) Callable[[...], List]

Decorator to convert the output of a generator function to a list.

Parameters

func (typing.Callable[..., typing.Iterable]) – the function to be decorated

Returns

the decorated function

Return type

typing.Callable[…, typing.List]

chesscog.core.sort_corner_points(points: numpy.ndarray) numpy.ndarray

Permute the board corner coordinates to the order [top left, top right, bottom right, bottom left].

Parameters

points (np.ndarray) – the four corner coordinates

Returns

the permuted array

Return type

np.ndarray

Submodules

chesscog.core.coordinates module

Utility functions to convert between Cartesian and homogenous coordinates.

chesscog.core.coordinates.from_homogenous_coordinates(coordinates: numpy.ndarray) numpy.ndarray

Convert homogenous to Cartesian coordinates.

Parameters

coordinates (np.ndarray) – the homogenous coordinates (shape: […, 3])

Returns

the Cartesian coordinates (shape: […, 2])

Return type

np.ndarray

chesscog.core.coordinates.to_homogenous_coordinates(coordinates: numpy.ndarray) numpy.ndarray

Convert Cartesian to homogenous coordinates.

Parameters

coordinates (np.ndarray) – the Cartesian coordinates (shape: […, 2])

Returns

the homogenous coordinates (shape: […, 3])

Return type

np.ndarray

chesscog.core.evaluation module

Common functions for evaluation CNNs.

chesscog.core.evaluation.evaluate(model_path: pathlib.Path, datasets: List[chesscog.core.dataset.datasets.Datasets], output_folder: pathlib.Path, find_mistakes: bool = False, include_heading: bool = False) str

Evaluate a model, returning the results as CSV.

Parameters
  • model_path (Path) – path to the model folder containing the YAML file and the saved weights

  • datasets (typing.List[Datasets]) – the datasets to evaluate on

  • output_folder (Path) – output folder for the mistake images (if applicable)

  • find_mistakes (bool, optional) – whether to output all mistakes as images to the output folder. Defaults to False.

  • include_heading (bool, optional) – whether to include a heading in the CSV output. Defaults to False.

Raises

ValueError – if the YAML config file is missing

Returns

the CSV string

Return type

str

chesscog.core.evaluation.perform_evaluation(classifier: str)

Function to set up the CLI for the evaluation script.

Parameters

classifier (str) – the classifier

chesscog.core.exceptions module

Core exceptions.

exception chesscog.core.exceptions.ChessboardNotLocatedException(reason: Optional[str] = None)

Bases: chesscog.core.exceptions.RecognitionException

Exception if the chessboard could not be located.

exception chesscog.core.exceptions.RecognitionException(message: str = 'unknown error')

Bases: Exception

Exception representing an error in the chess recognition pipeline.

chesscog.core.models module

Common tasks related to models.

chesscog.core.models.MODELS_REGISTRY = <chesscog.core.registry.Registry object>

The global models registry

chesscog.core.models.build_model(cfg: recap.config.CfgNode) torch.nn.modules.module.Module

Build a CNN from a configuration.

Parameters

cfg (CN) – the configuration

Returns

the built CNN model

Return type

nn.Module

chesscog.core.registry module

Common interface for registries.

class chesscog.core.registry.Registry

Bases: object

A registry class.

A registry is a collection of functions or classes that are each associated with a name. These can be loaded dynamically based on a configuration.

>>> my_registry = Registry()
>>> @my_registry.register
... def abc():
...     print("abc was called")
>>> my_registry.items()
dict_items([('abc', <function abc at 0x103197430>)])
>>> my_registry["abc"]()
abc was called
items() Iterable

Obtain a view of the registered items.

Returns

the registered items

Return type

typing.Iterable

register(item: Any, name: Optional[str] = None) Any

Register an item.

This function is typically used as a decorator.

Parameters
  • item (typing.Any) – the item to register

  • name (str, optional) – the name under which to register it. If not supplied, use the item.__name__ attribute.

Returns

the registered item

Return type

typing.Any

register_as(name: str) callable

Decorator for registering a function or class.

Parameters

name (str) – the name to register it under

Returns

the decorator

Return type

callable

chesscog.core.statistics module

Compute batch statistics

class chesscog.core.statistics.StatsAggregator(classes: list)

Bases: object

Simple class for aggregating statistics between batches.

accuracy() float

Obtain the overall accuracy.

Returns

the accuracy

Return type

float

add_batch(one_hot_outputs: torch.Tensor, labels: torch.Tensor, inputs: Optional[torch.Tensor] = None)

Add a batch to compute statistics over.

Parameters
  • one_hot_outputs (torch.Tensor) – the one hot outputs of the model

  • labels (torch.Tensor) – the groundtruth labels

  • inputs (torch.Tensor, optional) – the inputs (if supplied, will be used to keep track of mistakes)

f1_score(cls: str) float

Obtain the F1-score for a specific class label.

Parameters

cls (str) – the class

Returns

the F1-score

Return type

float

precision(cls: str) float

Obtain the precision for a specific class label.

Parameters

cls (str) – the class

Returns

the precision

Return type

float

recall(cls: str) float

Obtain the recall for a specific class label.

Parameters

cls (str) – the class

Returns

the recall

Return type

float

reset()

Reset the aggregator.