Getting Started Code for Chess Pieces 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-pieces -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 *
from fastai.data.core import *
import os
In [ ]:
train_df = pd.read_csv("data/train.csv")
Visualize the data πΒΆ
In [ ]:
train_df
In [ ]:
train_df['ImageID'] = train_df['ImageID'].astype(str)+".jpg"
train_df
In [ ]:
dls = ImageDataLoaders.from_df(train_df, path="data/train", bs=8)
dls.show_batch()
TRAINING PHASE ποΈΒΆ
In [ ]:
learn = cnn_learner(dls, alexnet, metrics=F1Score())
Train the ModelΒΆ
In [ ]:
learn.fine_tune(1)
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)}
print(label_to_class_mapping)
test_img_ids = [re.sub(r"\D", "", str(img_name)) for img_name in test_imgs_name]
In [ ]:
test_dls.show_batch()
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})
submission
π§ 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 [ ]:
submission.to_csv("submission.csv", index=False)
To download the generated csv in colab run the below command.ΒΆ
In [ ]:
try:
from google.colab import files
files.download('submission.csv')
except:
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.ΒΆ
In [ ]:
Content
Comments
You must login before you can post a comment.