Loading

Environment Classification

Resnet50 + Kmeans based image clustering model

Image clustering using Transfer learning

victorkras2008

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.

  • Unsupvised Image Classification

Image clustering using Transfer learning

Resnet50 + Kmeans based image clustering model

https://towardsdatascience.com/image-clustering-using-transfer-learning-df5862779571

In [1]:
!pip install -q aicrowd-cli
%load_ext aicrowd.magic
     |████████████████████████████████| 43 kB 1.4 MB/s 
     |████████████████████████████████| 211 kB 5.8 MB/s 
     |████████████████████████████████| 54 kB 2.4 MB/s 
     |████████████████████████████████| 170 kB 42.4 MB/s 
     |████████████████████████████████| 62 kB 782 kB/s 
     |████████████████████████████████| 63 kB 1.6 MB/s 
     |████████████████████████████████| 51 kB 6.3 MB/s 
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.
In [2]:
%aicrowd login
Please login here: https://api.aicrowd.com/auth/c6Qi0xWslXuXAsFmCs0kZaDCN5xR2f88RzqNFJV685A
API Key valid
Saved API Key successfully!
In [3]:
# Downloading the Dataset
!rm -rf data
!mkdir data
%aicrowd ds dl -c environment-classification -o data
In [4]:
# Unzipping and Organising the datasets
!unzip data/images.zip  -d data/images > /dev/null
In [5]:
import os
import csv 
from pathlib import Path
import random
import time

import pandas as pd
import numpy as np
In [6]:
DATA_DIR = "data/images/"

Model

In [7]:
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.models import Sequential

resnet = ResNet50(include_top=False, pooling='avg', weights='imagenet')
my_new_model = Sequential()
my_new_model.add(resnet)
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
94773248/94765736 [==============================] - 1s 0us/step
94781440/94765736 [==============================] - 1s 0us/step
In [8]:
# Say not to train first layer (ResNet) model. It is already trained
my_new_model.layers[0].trainable = False

Images Preprocessing

In [9]:
%%time
from tensorflow.keras.applications.resnet50 import preprocess_input
import cv2 
import numpy as np

resnet_feature_list = []
images = [f for f in os.listdir(DATA_DIR)]
for image in images:
    file = DATA_DIR+image
    #print(file)
    im = cv2.imread(file)
    #im = cv2.resize(im,(256,256))
    img = preprocess_input(np.expand_dims(im.copy(), axis=0))
    resnet_feature = my_new_model.predict(img)
    resnet_feature_np = np.array(resnet_feature)
    resnet_feature_list.append(resnet_feature_np.flatten())

array = np.array(resnet_feature_list)
CPU times: user 1min 12s, sys: 2.51 s, total: 1min 14s
Wall time: 1min 39s
In [10]:
array.shape
Out[10]:
(700, 2048)
In [42]:
from sklearn.cluster import KMeans 

kmeans = KMeans(n_clusters=5, random_state=None).fit(array)  # 

print(kmeans.labels_)
[4 1 3 3 1 3 3 4 2 0 3 1 3 2 0 3 3 1 4 4 1 1 1 0 1 4 0 2 4 4 4 2 0 4 4 4 4
 0 3 0 3 3 1 3 3 0 1 3 1 0 4 1 4 3 0 3 4 1 1 3 0 3 3 1 2 4 1 2 4 0 4 1 1 4
 1 4 3 2 0 1 0 4 0 4 3 1 4 4 4 0 0 3 0 2 3 0 2 4 2 1 1 3 1 4 1 3 2 4 4 4 0
 2 3 1 4 3 4 4 1 4 4 4 1 0 1 4 0 3 4 4 0 0 0 4 4 3 1 1 0 1 1 0 4 4 3 3 1 3
 3 1 0 2 1 3 3 2 1 2 1 1 0 2 2 4 0 3 0 3 2 1 0 1 4 0 3 1 4 4 4 3 2 1 4 3 4
 1 3 3 1 1 1 1 4 1 2 4 0 2 0 0 2 0 2 1 2 1 3 1 3 1 0 3 0 3 4 0 2 3 4 1 2 3
 1 0 4 0 4 3 3 1 1 1 2 1 4 3 3 0 3 1 0 4 2 1 2 4 3 0 1 0 1 3 4 1 1 0 3 4 3
 4 2 1 1 3 2 4 0 3 4 4 1 2 1 2 2 3 1 2 1 2 3 3 4 1 1 4 1 0 0 1 1 1 4 4 3 3
 1 1 0 3 1 0 1 0 3 1 3 2 0 3 3 4 1 1 1 3 1 1 0 4 1 4 1 4 0 2 4 3 4 3 0 0 0
 0 3 4 3 2 4 2 4 1 4 1 1 2 1 4 0 1 4 1 2 2 4 4 0 0 1 4 2 4 1 4 1 1 3 1 3 1
 4 0 1 4 1 3 1 0 1 1 1 1 3 2 3 3 1 3 1 1 1 1 1 1 4 1 4 4 4 2 4 0 1 3 4 1 0
 4 3 1 3 0 2 4 2 1 1 3 4 3 3 0 1 4 4 1 4 3 1 4 4 1 3 0 0 1 4 3 4 1 3 4 0 2
 4 4 4 4 4 4 3 2 1 3 3 4 4 3 4 1 0 1 1 2 4 2 1 3 2 0 1 1 2 3 0 0 1 0 1 2 1
 0 1 3 0 4 4 3 1 4 0 4 4 3 1 1 1 0 1 3 3 2 0 1 0 1 2 4 1 4 2 1 3 0 1 1 4 2
 2 1 1 2 0 0 4 3 2 2 1 1 1 4 0 4 2 4 1 0 1 0 3 4 4 3 0 4 3 1 3 2 1 0 3 2 4
 4 0 4 4 4 0 3 0 1 4 1 0 3 3 4 1 3 1 1 3 0 1 4 0 1 3 4 4 1 4 1 3 2 4 2 1 1
 4 4 4 1 1 2 1 1 2 0 4 4 4 1 4 1 1 1 4 2 1 4 4 3 4 1 0 3 0 4 1 1 3 4 4 4 4
 1 1 1 4 0 4 1 4 3 3 4 1 4 3 2 3 3 4 3 3 4 1 1 1 4 3 1 3 4 1 1 4 1 0 3 3 0
 2 3 3 1 1 1 4 3 3 1 1 2 4 2 0 3 3 4 3 1 0 4 4 4 1 3 3 1 3 4 0 1 4 4]

Submission

In [43]:
img_ids_list = [f[:-4] for f in images]
In [44]:
images[0]
Out[44]:
'95.jpg'
In [45]:
submission = {'ImageID':img_ids_list, "label":kmeans.labels_}
submission = pd.DataFrame(submission)

submission = submission.astype(int)
submission = submission.sort_values(by=['ImageID'])  

submission
Out[45]:
ImageID label
493 0 3
657 1 4
282 2 4
205 3 1
327 4 3
... ... ...
622 695 1
104 696 1
681 697 3
86 698 4
380 699 1

700 rows × 2 columns

In [46]:
submission.label.value_counts()
Out[46]:
1    202
4    177
3    140
0    106
2     75
Name: label, dtype: int64
In [47]:
!rm -rf assets
!mkdir assets

submission.to_csv(os.path.join("assets", "submission.csv"), index=False)
In [47]:

Making Direct Submission thought Aicrowd CLI

In [48]:
!aicrowd notebook submit -c environment-classification -a assets --no-verify
Using notebook: /content/drive/MyDrive/Colab Notebooks/BlitzXI_Resnet50_Kmeans_Cluster.ipynb for submission...
Removing existing files from submission directory...
Scrubbing API keys from the notebook...
Collecting notebook...
submission.zip ━━━━━━━━━━━━━━━━━━━━━━ 100.0%23.2/21.5 KB2.3 MB/s0:00:00
                                                       ╭─────────────────────────╮                                                       
                                                       │ Successfully submitted! │                                                       
                                                       ╰─────────────────────────╯                                                       
                                                             Important links                                                             
┌──────────────────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│  This submission │ https://www.aicrowd.com/challenges/ai-blitz-xi/problems/environment-classification/submissions/154484              │
│                  │                                                                                                                    │
│  All submissions │ https://www.aicrowd.com/challenges/ai-blitz-xi/problems/environment-classification/submissions?my_submissions=true │
│                  │                                                                                                                    │
│      Leaderboard │ https://www.aicrowd.com/challenges/ai-blitz-xi/problems/environment-classification/leaderboards                    │
│                  │                                                                                                                    │
│ Discussion forum │ https://discourse.aicrowd.com/c/ai-blitz-xi                                                                        │
│                  │                                                                                                                    │
│   Challenge page │ https://www.aicrowd.com/challenges/ai-blitz-xi/problems/environment-classification                                 │
└──────────────────┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
In [48]:


Comments

You must login before you can post a comment.

Execute