chesscog.piece_classifier package

Module for piece classification.

Submodules

chesscog.piece_classifier.create_configs module

Script to generate the YAML configuration files for each of the candidate piece classification model architectures. The candidates are defined in the models module.

$ python -m chesscog.piece_classifier.create_configs --help
usage: create_configs.py [-h]

Generate the YAML configuration for the piece
classifiers.

optional arguments:
  -h, --help  show this help message and exit

chesscog.piece_classifier.create_dataset module

Script to create the piece classification dataset.

Based on the four chessboard corner points which are supplied as labels, this module is responsible for warping the image and cutting out the squares. Note that before running this module requires the rendered dataset to be downloaded and split (see the chesscog.data_synthesis module for more information).

Note that script is different to chesscog.core.piece_classifier.create_dataset because the squares are cropped differently.

$ python -m chesscog.piece_classifier.create_dataet --help p
usage: create_dataset.py [-h]

Create the dataset for piece classification.

optional arguments:
  -h, --help  show this help message and exit
chesscog.piece_classifier.create_dataset.create_dataset(input_dir: pathlib.Path = URI('data://render'), output_dir: pathlib.Path = URI('data://pieces'))

Create the piece classification dataset.

Parameters
  • input_dir (Path, optional) – the input folder of the rendered images. Defaults to data://render.

  • output_dir (Path, optional) – the output folder. Defaults to data://pieces.

chesscog.piece_classifier.create_dataset.crop_square(img: numpy.ndarray, square: int, turn: bool) numpy.ndarray

Crop a chess square from the warped input image for piece classification.

Parameters
  • img (np.ndarray) – the warped input image

  • square (chess.Square) – the square to crop

  • turn (chess.Color) – the current player

Returns

the cropped square

Return type

np.ndarray

chesscog.piece_classifier.create_dataset.warp_chessboard_image(img: numpy.ndarray, corners: numpy.ndarray) numpy.ndarray

Warp the image of the chessboard onto a regular grid.

Note: this method is different to chesscog.core.piece_classifier.create_dataset.warp_chessboard_image() because the the warped image will be bigger.

Parameters
  • img (np.ndarray) – the image of the chessboard

  • corners (np.ndarray) – pixel locations of the four corner points

Returns

the warped image

Return type

np.ndarray

chesscog.piece_classifier.download_model module

Script to download the best piece classifier (already trained).

Running this script will download the classifier that was used in the report. It will be downloaded to models://piece_classifier.

$ python -m chesscog.piece_classifier.download_model --help
usage: download_model.py [-h]

Download the piece classifier.

optional arguments:
  -h, --help  show this help message and exit

chesscog.piece_classifier.evaluate module

Script to evaluate one or more piece classifiers.

$ python -m chesscog.piece_classifier.evaluate --help
usage: evaluate.py [-h] [--model MODEL] [--dataset {train,val,test}]
                   [--out OUT] [--find-mistakes]

Evaluate trained models.

optional arguments:
  -h, --help            show this help message and exit
  --model MODEL         the model to evaluate (if unspecified, all models
                        in 'runs://piece_classifier' will be evaluated)
  --dataset {train,val,test}
                        the dataset to evaluate (if unspecified, train and
                        val will be evaluated)
  --out OUT             output folder
  --find-mistakes       whether to output all misclassification images

chesscog.piece_classifier.models module

Module containing the CNN architecture definitions of the candidate piece classifiers.

class chesscog.piece_classifier.models.AlexNet

Bases: torch.nn.modules.module.Module

AlexNet model.

forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

input_size = (100, 200)
pretrained = True
training: bool
class chesscog.piece_classifier.models.CNN100_3Conv_3Pool_2FC

Bases: torch.nn.modules.module.Module

CNN (100, 3, 3, 2) model.

forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

input_size = (100, 200)
pretrained = False
training: bool
class chesscog.piece_classifier.models.CNN100_3Conv_3Pool_3FC

Bases: torch.nn.modules.module.Module

CNN (100, 3, 3, 3) model.

forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

input_size = (100, 200)
pretrained = False
training: bool
class chesscog.piece_classifier.models.InceptionV3

Bases: torch.nn.modules.module.Module

InceptionV3 model.

forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

input_size = (299, 299)
pretrained = True
training: bool
chesscog.piece_classifier.models.MODEL_REGISTRY = <chesscog.core.registry.Registry object>

Registry of piece classifiers (registered in the global chesscog.core.models.MODELS_REGISTRY under the key PIECE_CLASSIFIER)

class chesscog.piece_classifier.models.ResNet

Bases: torch.nn.modules.module.Module

ResNet model.

forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

input_size = (100, 200)
pretrained = True
training: bool
class chesscog.piece_classifier.models.VGG

Bases: torch.nn.modules.module.Module

VGG model.

forward(x)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

input_size = (100, 200)
pretrained = True
training: bool

chesscog.piece_classifier.train module

Script to train the candidate piece classifiers.

$ python -m chesscog.piece_classifier.train --help
usage: train.py [-h]
                [--config {AlexNet,ResNet,CNN100_3Conv_3Pool_2FC,InceptionV3,VGG,CNN100_3Conv_3Pool_3FC}]

Train the network.

optional arguments:
  -h, --help            show this help message and exit
  --config {AlexNet,ResNet,CNN100_3Conv_3Pool_2FC,InceptionV3,VGG,CNN100_3Conv_3Pool_3FC}
                        the configuration to train (default: all)