chesscog.occupancy_classifier package

Module for occupancy classification.

Submodules

chesscog.occupancy_classifier.create_configs module

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

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

Generate the YAML configuration for the occupancy
classifiers.

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

chesscog.occupancy_classifier.create_dataset module

Script to create the occupancy 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).

$ python -m chesscog.occupancy_classifier.create_dataset --help
usage: create_dataset.py [-h]

Create the dataset for occupancy classification.

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

Create the occupancy 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://occupancy.

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

Crop a chess square from the warped input image for occupancy 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.occupancy_classifier.create_dataset.warp_chessboard_image(img: numpy.ndarray, corners: numpy.ndarray) numpy.ndarray

Warp the image of the chessboard onto a regular grid.

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.occupancy_classifier.download_model module

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

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

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

Download the occupancy classifier.

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

chesscog.occupancy_classifier.evaluate module

Script to evaluate one or more occupancy classifiers.

$ python -m chesscog.occupancy_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://occupancy_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.occupancy_classifier.models module

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

class chesscog.occupancy_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, 100)
pretrained = True
training: bool
class chesscog.occupancy_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, 100)
pretrained = False
training: bool
class chesscog.occupancy_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, 100)
pretrained = False
training: bool
class chesscog.occupancy_classifier.models.CNN50_2Conv_2Pool_2FC

Bases: torch.nn.modules.module.Module

CNN (50, 2, 2, 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 = (50, 50)
pretrained = False
training: bool
class chesscog.occupancy_classifier.models.CNN50_2Conv_2Pool_3FC

Bases: torch.nn.modules.module.Module

CNN (50, 2, 2, 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 = (50, 50)
pretrained = False
training: bool
class chesscog.occupancy_classifier.models.CNN50_3Conv_1Pool_2FC

Bases: torch.nn.modules.module.Module

CNN (50, 3, 1, 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 = (50, 50)
pretrained = False
training: bool
class chesscog.occupancy_classifier.models.CNN50_3Conv_1Pool_3FC

Bases: torch.nn.modules.module.Module

CNN (50, 3, 1, 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 = (50, 50)
pretrained = False
training: bool
chesscog.occupancy_classifier.models.MODEL_REGISTRY = <chesscog.core.registry.Registry object>

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

class chesscog.occupancy_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, 100)
pretrained = True
training: bool
class chesscog.occupancy_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, 100)
pretrained = True
training: bool

chesscog.occupancy_classifier.train module

Script to train the candidate occupancy classifiers.

$ python -m chesscog.occupancy_classifier.train --help
usage: train.py [-h]
                [--config {AlexNet,ResNet,VGG_centercrop,AlexNet_centercrop,CNN50_3Conv_1Pool_2FC,CNN100_3Conv_3Pool_2FC,CNN50_2Conv_2Pool_2FC,VGG,CNN100_3Conv_3Pool_3FC_centercrop,CNN50_3Conv_1Pool_3FC_centercrop,CNN50_2Conv_2Pool_2FC_centercrop,CNN100_3Conv_3Pool_2FC_centercrop,CNN50_3Conv_1Pool_2FC_centercrop,CNN50_2Conv_2Pool_3FC_centercrop,ResNet_centercrop,CNN50_3Conv_1Pool_3FC,CNN50_2Conv_2Pool_3FC,CNN100_3Conv_3Pool_3FC}]

Train the network.

optional arguments:
  -h, --help            show this help message and exit
  --config {AlexNet,ResNet,VGG_centercrop,AlexNet_centercrop,CNN50_3Conv_1Pool_2FC,CNN100_3Conv_3Pool_2FC,CNN50_2Conv_2Pool_2FC,VGG,CNN100_3Conv_3Pool_3FC_centercrop,CNN50_3Conv_1Pool_3FC_centercrop,CNN50_2Conv_2Pool_2FC_centercrop,CNN100_3Conv_3Pool_2FC_centercrop,CNN50_3Conv_1Pool_2FC_centercrop,CNN50_2Conv_2Pool_3FC_centercrop,ResNet_centercrop,CNN50_3Conv_1Pool_3FC,CNN50_2Conv_2Pool_3FC,CNN100_3Conv_3Pool_3FC}
                        the configuration to train (default: all)