This repository explores the ability of a convolutional neural network (TensorFlow) to classify chess board images into a vector ([0,1]^64) that encodes whether or not a piece is on a given square. Input images are 180x180px gray scale pngs and the output is a vector of 1's and 0's of length 64. Training/validation/test data is gathered artificially using a python library that produces chessboard images. Images are rotated by a random amount (both positive and negative) to increase generalizability of the model. The neural network was trained using ~1 million examples and tested on new unseen boards (again generated by a random artificial method). The overall accuracy of the model was 91% and 72%, at the grid and board level respectively.
For this particular project we used the python-chess package to generate random chessboards for train, test and validation sets. The generation algorithm with randomly select (or not) a chess piece of any type from both colors and place it on a given grid. We enforce that both color kings are present on the board and that no pawns exists on the 1st and 8th rank. We save the board on disk with a specific filename such that we can encode the type and location of each piece. The names of each image is 64 characters long with each character of the name corresponding to a given square on the chessboard. The specific character encodes the actual piece while the case encodes the color of the piece. For example, a capital Q corresponds to a black queen where a lower-case k corresponds to a white king. Knights are assigned the character N or n for black or white respectively. We also append the angle of rotation to the end of the filename. From this naming convention we can extract the output vector that we will use as the target of this neural network. Each output vector element encodes the boolean occupancy of an individual square within the 8x8 chessboard grid. For example, the first element in the 64 element vector may correspond to the square A1, such that, if a piece exists on that square that element would be 1 and 0 otherwise. Using the method above we generate approx 1 million images for training and generate approx 10,000 images for both validation and test.
We report two test set accuracies here. The first accuracy is on the square level, which is defined as sum(correct square prediction)/total_number_of_squares. The second accuracy is on the board level. The board level accuracy is much more demanding than the square level and its defined as total_number_of_entire_boards_correct/total_number_of_boards. For example assume the algorithm correctly identifies pieces on 60/64 squares, the square level accuracy would be 60/64 but the board level accuracy would be 0 for that particular board since it didn’t guess the entire board correctly.
Type | Train Set Accuracy | Test Set Accuracy |
---|---|---|
Square | 97% | 96% |
Board | 76% | 72% |