Loading

Environment Classification

[Getting Started Notebook] Environment Classification

A Getting Started notebook for Environment Classification Puzzle of BlitzXI.

Shubhamaicrowd

Starter Code for Environment Classification

In this challenge, you will have images of a self driving car moving through a town in different weather conditions. Your goal will be to classify the environment into 5 different classes ( using unsupervised methonds ), 1 means the weather is really good for a self driving car while 5 means the weather is very challenging for a self driving car.

What we are going to Learn

  • Unsupvised Image Classification

Note : Create a copy of the notebook and use the copy for submission. Go to File > Save a Copy in Drive to create a new copy

Downloading Dataset

So we will first need to login to AIcrowd that will allow us to download the dataset after the API key is validated and saved.

In [ ]:
!pip install aicrowd-cli
%load_ext aicrowd.magic
Collecting aicrowd-cli
  Downloading aicrowd_cli-0.1.9-py3-none-any.whl (43 kB)
     |████████████████████████████████| 43 kB 936 kB/s 
Requirement already satisfied: click<8,>=7.1.2 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (7.1.2)
Collecting requests<3,>=2.25.1
  Downloading requests-2.26.0-py2.py3-none-any.whl (62 kB)
     |████████████████████████████████| 62 kB 863 kB/s 
Collecting requests-toolbelt<1,>=0.9.1
  Downloading requests_toolbelt-0.9.1-py2.py3-none-any.whl (54 kB)
     |████████████████████████████████| 54 kB 2.5 MB/s 
Requirement already satisfied: tqdm<5,>=4.56.0 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (4.62.0)
Collecting rich<11,>=10.0.0
  Downloading rich-10.7.0-py3-none-any.whl (209 kB)
     |████████████████████████████████| 209 kB 35.3 MB/s 
Collecting GitPython==3.1.18
  Downloading GitPython-3.1.18-py3-none-any.whl (170 kB)
     |████████████████████████████████| 170 kB 66.7 MB/s 
Requirement already satisfied: toml<1,>=0.10.2 in /usr/local/lib/python3.7/dist-packages (from aicrowd-cli) (0.10.2)
Collecting gitdb<5,>=4.0.1
  Downloading gitdb-4.0.7-py3-none-any.whl (63 kB)
     |████████████████████████████████| 63 kB 1.9 MB/s 
Requirement already satisfied: typing-extensions>=3.7.4.0 in /usr/local/lib/python3.7/dist-packages (from GitPython==3.1.18->aicrowd-cli) (3.7.4.3)
Collecting smmap<5,>=3.0.1
  Downloading smmap-4.0.0-py2.py3-none-any.whl (24 kB)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (2.10)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (1.24.3)
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (2.0.4)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests<3,>=2.25.1->aicrowd-cli) (2021.5.30)
Collecting commonmark<0.10.0,>=0.9.0
  Downloading commonmark-0.9.1-py2.py3-none-any.whl (51 kB)
     |████████████████████████████████| 51 kB 6.9 MB/s 
Collecting colorama<0.5.0,>=0.4.0
  Downloading colorama-0.4.4-py2.py3-none-any.whl (16 kB)
Requirement already satisfied: pygments<3.0.0,>=2.6.0 in /usr/local/lib/python3.7/dist-packages (from rich<11,>=10.0.0->aicrowd-cli) (2.6.1)
Installing collected packages: smmap, requests, gitdb, commonmark, colorama, rich, requests-toolbelt, GitPython, aicrowd-cli
  Attempting uninstall: requests
    Found existing installation: requests 2.23.0
    Uninstalling requests-2.23.0:
      Successfully uninstalled requests-2.23.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
google-colab 1.0.0 requires requests~=2.23.0, but you have requests 2.26.0 which is incompatible.
datascience 0.10.6 requires folium==0.2.1, but you have folium 0.8.3 which is incompatible.
Successfully installed GitPython-3.1.18 aicrowd-cli-0.1.9 colorama-0.4.4 commonmark-0.9.1 gitdb-4.0.7 requests-2.26.0 requests-toolbelt-0.9.1 rich-10.7.0 smmap-4.0.0
In [ ]:
%aicrowd login
Please login here: https://api.aicrowd.com/auth/iRj_laensZRSkACTzZyNd2BCDFCbLDz9HAUrWsMetEk
API Key valid
Saved API Key successfully!
In [ ]:
# Downloading the Dataset
!rm -rf data
!mkdir data
%aicrowd ds dl -c environment-classification -o data
In [ ]:
# Unzipping and Organising the datasets
!unzip data/images.zip  -d data/images > /dev/null

Importing Libraries

In [ ]:
import pandas as pd
from sklearn.cluster import KMeans
import seaborn as sns
from glob import glob
from natsort import natsorted
import cv2
import os
import numpy as np 
import random
from PIL import Image

Visualize the data 👀

In [ ]:
images_folder = "data/images"
In [ ]:
images_list = natsorted(glob(images_folder+"/*"))
In [ ]:
img = Image.open(random.choice(images_list))
img
Out[ ]:

Loading the dataset

In [ ]:
images_data = np.array([cv2.imread(image_path, 0).flatten()  for image_path in images_list])
images_data.shape
Out[ ]:
(700, 262144)

Creating the model

Here, we will be using KMeans from sklearn model which is used in unsupervised data classification

In [ ]:
#           Number of classes
model = KMeans(n_clusters=5, n_jobs=-1) 

model
Out[ ]:
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
       n_clusters=5, n_init=10, n_jobs=None, precompute_distances='auto',
       random_state=None, tol=0.0001, verbose=0)

Training the model

In [ ]:
model.fit(images_data)
Out[ ]:
KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
       n_clusters=5, n_init=10, n_jobs=None, precompute_distances='auto',
       random_state=None, tol=0.0001, verbose=0)

Generating the predictions

In [ ]:
predictions = model.predict(images_data)
predictions.shape
Out[ ]:
(700,)
In [ ]:
df = pd.DataFrame({"ImageID":range(len(images_data)), "label":predictions})
df['ImageID'] = df["ImageID"].astype(int)
df = df.sort_values("ImageID").reset_index(drop=True)
df
Out[ ]:
ImageID label
0 0 2
1 1 3
2 2 3
3 3 4
4 4 2
... ... ...
695 695 4
696 696 4
697 697 2
698 698 3
699 699 4

700 rows × 2 columns

Submitting Results 📄

In [ ]:
!rm -rf assets
!mkdir assets

df.to_csv(os.path.join("assets", "submission.csv"), index=False)

Note : Please make sure that there should be filename submission.csv in assets folder before submitting it

Uploading the Results

In [ ]:
!aicrowd notebook submit -c environment-classification -a assets --no-verify
Mounting Google Drive 💾
Your Google Drive will be mounted to access the colab notebook
Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.activity.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fexperimentsandconfigs%20https%3a%2f%2fwww.googleapis.com%2fauth%2fphotos.native&response_type=code

Enter your authorization code:
4/1AX4XfWiM7dctdDyp3ZUKXUg0cRuhmO4BYeiJJOlJySgq2Qh8IjNk3qf62A4
Mounted at /content/drive
Using notebook: /content/drive/MyDrive/Colab Notebooks/Unsupervised Environment Classification for submission...
Scrubbing API keys from the notebook...
Collecting notebook...
submission.zip ━━━━━━━━━━━━━━━━━━ 100.0%279.6/278.0 KB815.9 kB/s0:00:00
                                                     ╭─────────────────────────╮                                                      
                                                     │ Successfully submitted! │                                                      
                                                     ╰─────────────────────────╯                                                      
                                                           Important links                                                            
┌──────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│  This submission │ https://www.aicrowd.com/challenges/blitz-xi/problems/environment-classification/submissions/153312              │
│                  │                                                                                                                 │
│  All submissions │ https://www.aicrowd.com/challenges/blitz-xi/problems/environment-classification/submissions?my_submissions=true │
│                  │                                                                                                                 │
│      Leaderboard │ https://www.aicrowd.com/challenges/blitz-xi/problems/environment-classification/leaderboards                    │
│                  │                                                                                                                 │
│ Discussion forum │ https://discourse.aicrowd.com/c/blitz-xi                                                                        │
│                  │                                                                                                                 │
│   Challenge page │ https://www.aicrowd.com/challenges/blitz-xi/problems/environment-classification                                 │
└──────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
In [ ]:


Comments

You must login before you can post a comment.

Execute