ROVER CLASSIFICATION
How to create a State of the Art Image Classifier
Having fun joining the race to M.A.R.S in the AI Blitz Perseverance
How to create a State of the Art Image Classifier in less than 10 minutes
In [1]:
!pip install aicrowd-cli==0.1
API_KEY = "4c49d255257272f8caf90b1e74b8cccd"
!aicrowd login --api-key $API_KEY
In [2]:
!aicrowd dataset download --challenge rover-classification -j 3
In [3]:
!rm -rf data
!mkdir data
!unzip train.zip -d data/train >/dev/null
!unzip val.zip -d data/val >/dev/null
!unzip test.zip -d data/test >/dev/null
In [4]:
import pandas as pd
import os
import re
import tensorflow as tf
In [5]:
df_train = pd.read_csv("train.csv")
In [6]:
df_val = pd.read_csv("val.csv")
In [7]:
df_train['ImageID'] = df_train['ImageID'].astype(str)+".jpg"
df_val['ImageID'] = df_val['ImageID'].astype(str)+".jpg"
In [8]:
INPUT_SIZE = 256
In [9]:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
In [10]:
datagen=ImageDataGenerator(rescale=1./255.)
train_generator=datagen.flow_from_dataframe(
dataframe=df_train,
directory="data/train/",
x_col="ImageID",
y_col="label",
batch_size=32,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(INPUT_SIZE,INPUT_SIZE))
In [11]:
val_generator=datagen.flow_from_dataframe(
dataframe=df_val,
directory="data/val/",
x_col="ImageID",
y_col="label",
batch_size=64,
seed=42,
shuffle=True,
class_mode="categorical",
target_size=(INPUT_SIZE,INPUT_SIZE))
In [12]:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, Conv2D, Flatten, Dropout, MaxPooling2D, BatchNormalization
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import regularizers, optimizers
import os
import numpy as np
import pandas as pd
In [13]:
class CustomAugment(object):
def __call__(self, image):
# Random flips and grayscale with some stochasticity
img = self._random_apply(tf.image.flip_left_right, image, p=0.5)
img = self._random_apply(self._color_drop, img, p=0.8)
return img
def _color_drop(self, x):
image = tf.image.rgb_to_grayscale(x)
image = tf.tile(x, [1, 1, 1, 3])
return x
def _random_apply(self, func, x, p):
return tf.cond(
tf.less(tf.random.uniform([], minval=0, maxval=1, dtype=tf.float32),
tf.cast(p, tf.float32)),
lambda: func(x),
lambda: x)
In [14]:
data_augmentation = tf.keras.Sequential(
[
tf.keras.layers.Lambda(CustomAugment()),
tf.keras.layers.experimental.preprocessing.RandomFlip("horizontal",
input_shape=(INPUT_SIZE,
INPUT_SIZE,
3)),
tf.keras.layers.experimental.preprocessing.RandomRotation(0.1),
tf.keras.layers.experimental.preprocessing.RandomZoom(0.1),
]
)
In [15]:
model = Sequential()
model.add(data_augmentation)
model.add(tf.keras.applications.ResNet152V2(
include_top=False,
weights="imagenet",
input_shape=(INPUT_SIZE, INPUT_SIZE, 3),
))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(2, activation='softmax'))
model.compile(optimizers.RMSprop(lr=0.0001/10), loss="categorical_crossentropy", metrics=["Recall", "Precision"])
In [16]:
model.layers[1].trainable = False
In [17]:
STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size
STEP_SIZE_VAL=val_generator.n//train_generator.batch_size
In [18]:
model.fit(train_generator, validation_data=val_generator, epochs=5)
Out[18]:
In [19]:
model.layers[1].trainable = True
In [20]:
len(model.layers[1].layers)
Out[20]:
In [21]:
model.summary()
In [22]:
model.layers[1].trainable = True
for layer in model.layers[1].layers[:100]:
layer.trainable = False
In [23]:
model.summary()
In [24]:
tf.keras.backend.set_value(model.optimizer.learning_rate, 0.0001/100)
In [25]:
model.fit(train_generator, validation_data=val_generator, epochs=2)
Out[25]:
In [26]:
df_test = pd.read_csv("sample_submission.csv",dtype=str)
df_test["ImageID"] = df_test["ImageID"].astype(str)+".jpg"
In [27]:
test_generator=datagen.flow_from_dataframe(
dataframe=df_test,
directory="data/test/",
x_col="ImageID",
y_col="label",
batch_size=1,
seed=42,
shuffle=False,
class_mode="categorical",
target_size=(INPUT_SIZE,INPUT_SIZE))
In [28]:
STEP_SIZE_TEST = test_generator.n//test_generator.batch_size
In [29]:
STEP_SIZE_TEST
Out[29]:
In [30]:
test_generator.reset()
pred = model.predict(test_generator,
steps=STEP_SIZE_TEST, verbose=1)
In [31]:
predicted_class_indices = np.argmax(pred,axis=1)
In [32]:
labels = (train_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
predictions = [labels[k] for k in predicted_class_indices]
In [33]:
df_test["pred"] = predictions
In [34]:
df_test.head()
Out[34]:
In [35]:
df_test.drop("label", axis=1, inplace=True)
df_test.rename(columns={"pred": "label"}, inplace=True)
In [36]:
df_test.head()
Out[36]:
In [37]:
df_test["ImageID"] = df_test["ImageID"].map(lambda x: re.sub(r"\D", "", str(x)))
In [38]:
df_test.head()
Out[38]:
In [39]:
df_test.to_csv("data/03_sub.csv", index=False)
In [40]:
!aicrowd submission create -c rover-classification -f data/03_sub.csv
Content
Comments
You must login before you can post a comment.