AI Blitz #6

[Baseline] Chess Win Prediction

A getting started code for the Chess Win Prediction challenge.



Getting Started Code for Chess Win Prediction 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-win-prediction -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

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 [ ]:
train_df = pd.read_csv("data/train.csv")

Visualize the data 👀

In [ ]:
In [ ]:
train_df['ImageID'] = train_df['ImageID'].astype(str)+".jpg"
In [ ]:
dls = ImageDataLoaders.from_df(train_df, path="data/train", label_col=2, bs=8)


In [ ]:
learn = cnn_learner(dls, alexnet, metrics=F1Score())

Train the Model

In [ ]:

Testing Phase 😅

We are almost done. We trained and validated on the training data. Now its the time to predict on test set and make a submission.# Prediction on Evaluation Set

Predict Test Set

Predict on the test set and you are all set to make the submission!

In [ ]:
test_imgs_name = get_image_files("data/test")
test_dls = dls.test_dl(test_imgs_name)

label_to_class_mapping = {v: k for v, k in enumerate(dls.vocab)}

test_img_ids = [re.sub(r"\D", "", str(img_name)) for img_name in test_imgs_name]
In [ ]:
In [ ]:
_,_,results = learn.get_preds(dl = test_dls, with_decoded = True)

results = [label_to_class_mapping[i] for i in results.numpy()]

Save the prediction to csv

In [ ]:
submission = pd.DataFrame({"ImageID":test_img_ids, "label":results})
In [ ]:
submission.to_csv("submission.csv", index=False)

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.


You must login before you can post a comment.