Binary Image Classification Problem
Getting Started Code for F1 Team Classification Challenge on AIcrowd¶
Download Necessary Packages 📚¶
!pip install --upgrade fastai
!pip install aicrowd-cli
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.
API_KEY = '' #Please enter your API Key from [https://www.aicrowd.com/participants/me]
!aicrowd login --api-key $API_KEY
!aicrowd dataset download --challenge f1-team-classification -j 3
Below, we create a new directory to put our downloaded data! 🏎
We unzip the ZIP files and move the CSVs.
!rm -rf data
!mkdir data
!unzip train.zip -d data/train
!unzip val.zip -d data/val
!unzip test.zip -d data/test
!mv train.csv data/train.csv
!mv val.csv data/val.csv
!mv sample_submission.csv data/sample_submission.csv
!rm -rf ./train.zip ./val.zip ./test.zip
Import packages¶
import pandas as pd
from fastai.vision.all import *
from fastai.data.core import *
import os
data_folder = "data"
train_df = pd.read_csv(os.path.join(data_folder, "train.csv"))
Visualize the data 👀¶
Using Pandas and the Matplot Library in Python, we will be viewing the images in our datasets.
train_df
Adding .jpg to all the ImageIDs in "ImageID" column. This will help us with adding the path behind the names of these images.
train_df['ImageID'] = train_df['ImageID'].astype(str)+".jpg"
train_df
train_df['label'].value_counts()
dls = ImageDataLoaders.from_df(train_df, path=os.path.join(data_folder, "train"), bs=16)
# Defining a function to take a look at the images
dls.show_batch()
TRAINING PHASE 🏋️¶
Now that we have the dataset is ready, it's time to create a model that we will train on our data!
# learn = cnn_learner(dls, alexnet, metrics=F1Score())
learn = cnn_learner(dls, models.resnet18, pretrained=True, metrics=F1Score()) #, lr=0.0001
Train the Model 🏃🏽♂️¶
learn.fine_tune(1) #1, base_lr=0.0001
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.
Load Test Set¶
Load the test data on which final submission is to be made.
test_imgs_name = get_image_files(os.path.join(data_folder, "test"))
test_dls = dls.test_dl(test_imgs_name)
# Convert categorical values into label names
class_to_label_mapping = {v: k for v, k in enumerate(dls.vocab)}
print(class_to_label_mapping)
test_img_ids = [re.sub(r"\D", "", str(img_name)) for img_name in test_imgs_name]
test_dls.show_batch()
Predict Test Set¶
Predict on the test set and you are all set to make the submission!
_,_,results = learn.get_preds(dl = test_dls, with_decoded = True)
results = [class_to_label_mapping[i] for i in results.numpy()]
Save the prediction to csv¶
🚧 Note :¶
- Do take a look at the submission format.
- The submission file should contain a header.
- Follow all submission guidelines strictly to avoid inconvenience.
submission = pd.DataFrame({"ImageID":test_img_ids, "label":results})
submission
submission.to_csv("submission.csv", index=False)
Making Direct Submission thought Aicrowd CLI¶
#!aicrowd submission create -c f1-team-classification -f submission.csv
Content
Comments
You must login before you can post a comment.