Environment Classification
Resnet50 + Kmeans based image clustering model
Image clustering using Transfer learning
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
In [2]:
%aicrowd login
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)
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)
In [10]:
array.shape
Out[10]:
In [42]:
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=5, random_state=None).fit(array) #
print(kmeans.labels_)
Submission¶
In [43]:
img_ids_list = [f[:-4] for f in images]
In [44]:
images[0]
Out[44]:
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]:
In [46]:
submission.label.value_counts()
Out[46]:
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
In [48]:
Content
Comments
You must login before you can post a comment.