[Baseline] Chess Configuration

A getting started code for the Chess Configuration challenge.

By ashivani


Getting Started Code for Chess Configuration on AIcrowd

Author : Shubhamai

Download Necessary Packages 📚

In this baseline we are going to use FastAI as our main library to train out model and making & submitting predictions

In [ ]:
!pip install --upgrade fastai git+https://gitlab.aicrowd.com/yoogottamk/aicrowd-cli.git >/dev/null
%load_ext aicrowd.magic

Download Data

The first step is to download out train test data. We will be training a model on the train data and make predictions on test data. We submit our predictions.

In [ ]:
API_KEY = '' #Please enter your API Key [https://www.aicrowd.com/participants/me]
%aicrowd login --api-key $API_KEY
In [ ]:
%aicrowd dataset download --challenge chess-configuration -j 3
In [ ]:
!rm -rf data
!mkdir data

!unzip train.zip  -d data/ 
!unzip val.zip -d data/ 
!unzip 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 [ ]:
import pandas as pd
from fastai.vision.all import *
import os
from tqdm.notebook import tqdm

Load Data

  • We use pandas 🐼 library to load our data.
  • Pandas loads the data into dataframes and facilitates us to analyse the data.
  • Learn more about it here 🤓
In [ ]:
sample_submission = pd.read_csv("data/sample_submission.csv")

Visualize the data 👀

In [ ]:

Creating Random Submission 👀

In below cell, we are going to create a random labels to submit our predictions.

The below code was originally made by rosettacode and the original source of code is in this link

In [ ]:
piece_list = ["R", "N", "B", "Q", "P"]
def place_kings(brd):
	while True:
		rank_white, file_white, rank_black, file_black = random.randint(0,7), random.randint(0,7), random.randint(0,7), random.randint(0,7)
		diff_list = [abs(rank_white - rank_black),  abs(file_white - file_black)]
		if sum(diff_list) > 2 or set(diff_list) == set([0, 2]):
			brd[rank_white][file_white], brd[rank_black][file_black] = "K", "k"
def populate_board(brd, wp, bp):
	for x in range(2):
		if x == 0:
			piece_amount = wp
			pieces = piece_list
			piece_amount = bp
			pieces = [s.lower() for s in piece_list]
		while piece_amount != 0:
			piece_rank, piece_file = random.randint(0, 7), random.randint(0, 7)
			piece = random.choice(pieces)
			if brd[piece_rank][piece_file] == " " and pawn_on_promotion_square(piece, piece_rank) == False:
				brd[piece_rank][piece_file] = piece
				piece_amount -= 1
def fen_from_board(brd):
	fen = ""
	for x in brd:
		n = 0
		for y in x:
			if y == " ":
				n += 1
				if n != 0:
					fen += str(n)
				fen += y
				n = 0
		if n != 0:
			fen += str(n)
		fen += "/" if fen.count("/") < 7 else ""
	return fen
def pawn_on_promotion_square(pc, pr):
	if pc == "P" and pr == 0:
		return True
	elif pc == "p" and pr == 7:
		return True
	return False
def start():

  board = [[" " for x in range(8)] for y in range(8)]

  piece_amount_white, piece_amount_black = random.randint(0, 15), random.randint(0, 15)
  populate_board(board, piece_amount_white, piece_amount_black)

  fen = fen_from_board(board)

  board = [[" " for x in range(8)] for y in range(8)]

  return fen
In [ ]:
predictions = []

for n in tqdm(range(sample_submission.shape[0])):


Save the prediction to csv

In [ ]:
sample_submission['label'] = predictions

🚧 Note :

  • Do take a look at the submission format.
  • The submission file should contain a header.
  • Follow all submission guidelines strictly to avoid inconvenience.
In [ ]:
sample_submission.to_csv("submission.csv", index=False)

You can submit via AIcrowd CLI directly, (which is still in its beta phase 🙃 ). If you face any problems you can submit by downloading the submission file.

In [ ]:
%aicrowd submission create -c chess-configuration -f submission.csv

To download the generated csv in colab run the below command

In [ ]:
    from google.colab import files
    print("Option Only avilable in Google Colab")

Well Done! 👍 We are all set to make a submission and see your name on leaderborad. Let navigate to challenge page and make one.

↕️  Read More

Liked by  


You must login before you can post a comment.