Loading

CHESS CONFIGURATION

FEN Generator for Chess Configuration Chalenge

A solution code for Chess Configuration Chalenge

victorkras2008

This trained model may be used for solution others puzzles of the chelenge.😉

Download Data

In [1]:
!pip install --upgrade fastai git+https://gitlab.aicrowd.com/yoogottamk/aicrowd-cli.git >/dev/null
%load_ext aicrowd.magic
  Running command git clone -q https://gitlab.aicrowd.com/yoogottamk/aicrowd-cli.git /tmp/pip-req-build-a3selx8e
In [2]:
API_KEY = '' #Please enter your API Key [https://www.aicrowd.com/participants/me]
%aicrowd login --api-key $API_KEY
Verifying API Key...
API Key valid
Saved API Key successfully!
In [3]:
%aicrowd dataset download --challenge chess-configuration -j 3




In [4]:
!rm -rf data
!mkdir data

!unzip -q train.zip  -d data/ 
!unzip -q val.zip -d data/ 
!unzip -q test.zip  -d data/ 

!mv train.csv data/train.csv
!mv val.csv data/val.csv
!mv sample_submission.csv data/sample_submission.csv

Import packages

In [5]:
import numpy as np
import pandas as pd
import os
import glob
import re
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from random import shuffle
from skimage.util.shape import view_as_blocks
from skimage import io, transform
from sklearn.model_selection import train_test_split
from keras.models import Sequential, load_model
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D
import warnings
warnings.filterwarnings('ignore')

Data Import

In [6]:
#DATADIR = 'D:/Datasets/CHESS/CONFIGURATION/'
DATADIR = './data/'
In [7]:
train_df = pd.read_csv(DATADIR+"train.csv")
val_df = pd.read_csv(DATADIR+"val.csv")

test_df = pd.read_csv(DATADIR+"sample_submission.csv")
In [8]:
train_size = 10000
val_size = 4000
test_size = 10000

train = [f'{DATADIR}train/{i}.jpg' for i in train_df.ImageID]
val = [f'{DATADIR}val/{i}.jpg' for i in val_df.ImageID]
test = [f'{DATADIR}test/{i}.jpg' for i in test_df.ImageID]

train = train[:train_size]
val =  val[:val_size]
In [9]:
mpimg.imread(test[0]).shape
Out[9]:
(254, 254, 3)

Function to extract FEN

In [10]:
def fen_from_filename(filename):
    base = os.path.basename(filename)
    image_id = int(os.path.splitext(base)[0])
    #print(image_id)
    fen = train_df.iloc[image_id].label
    return fen

Examples:

In [11]:
print(fen_from_filename(train[0]))
print(fen_from_filename(train[1]))
print(fen_from_filename(train[2]))
1rbqkb1r/p1p1n1pp/1pn1p3/1P1p1p2/3P4/N3B2P/P1PNPPP1/R2QKB1R
2bk4/2q1p3/3p3P/5r2/r5nP/P2K2N1/8/2q1NB1R
3rnq2/3k1p2/5rP1/pppp1P2/P1BP2P1/RPP1K2N/3B3P/1N5R

Plotting samples

In [12]:
f, axarr = plt.subplots(1,3, figsize=(120, 120))

for i in range(0,3):
    axarr[i].set_title(fen_from_filename(train[i]), fontsize=70, pad=30)
    axarr[i].imshow(mpimg.imread(train[i]))
    axarr[i].axis('off')