SNAKE
[Getting Started Notebook] SNAKE Challange
This is a Baseline Code to get you started with the challenge.
You can use this notebook to start your implementation and improve for a better score.
Baseline for SNAKE Challenge on AIcrowd¶
Author : Gauransh Kumar¶
Download Necessary Packages¶
In [1]:
import sys
!{sys.executable} -m pip install aicrowd-cli
%load_ext aicrowd.magic
Dataset Download¶
In [2]:
%aicrowd login
In [4]:
%aicrowd ds dl -c snake
In [5]:
# creating
!mkdir data
!mkdir data/test
!mkdir data/train
!mkdir data/val
In [6]:
# Unzipping the images
!unzip -q train.zip
!unzip -q test.zip
!unzip -q val.zip
In [7]:
!mv content/data/train/* data/train
!mv content/data/val/* data/val
!mv content/data/test/* data/test
In [8]:
!rm -rf content/
In [9]:
!mkdir models
Imports¶
In [80]:
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from tqdm import tqdm
import os
In [14]:
# checking tensorflow version and CUDA device
print(tf.__version__)
print(tf.test.gpu_device_name())
Model and Data Preprocessing¶
In [21]:
# constanst
train_dir = os.path.join("data", 'train')
validation_dir = os.path.join("data", 'val')
test_dir = os.path.join("data", 'test')
BATCH_SIZE = 32
IMG_SIZE = (224, 224)
In [18]:
# training dataset
train_dataset = tf.keras.utils.image_dataset_from_directory(train_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE)
In [22]:
# validation dataset
validation_dataset = tf.keras.utils.image_dataset_from_directory(validation_dir,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE)
In [53]:
# training dataset
test_dataset = tf.keras.utils.image_dataset_from_directory(test_dir,
labels=None,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE)
In [23]:
# plotting some images from the Dataset
class_names = train_dataset.class_names
plt.figure(figsize=(10, 10))
for images, labels in train_dataset.take(1):
for i in range(9):
ax = plt.subplot(3, 3, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])
plt.axis("off")
In [25]:
# fine tuning for perforamnce
AUTOTUNE = tf.data.AUTOTUNE
train_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)
validation_dataset = validation_dataset.prefetch(buffer_size=AUTOTUNE)
In [26]:
preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input
In [27]:
# Create the base model from the pre-trained model MobileNet V2
IMG_SHAPE = IMG_SIZE + (3,)
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet')
In [28]:
image_batch, label_batch = next(iter(train_dataset))
feature_batch = base_model(image_batch)
print(feature_batch.shape)
In [29]:
# Freezing (by setting layer.trainable = False) prevents the weights in a given layer from being updated during training.
base_model.trainable = False
# Let's take a look at the base model architecture
base_model.summary()
In [30]:
# Applying a pooling layer
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
feature_batch_average = global_average_layer(feature_batch)
print(feature_batch_average.shape)
# Applying a dense layer
prediction_layer = tf.keras.layers.Dense(1)
prediction_batch = prediction_layer(feature_batch_average)
print(prediction_batch.shape)
In [33]:
# Building the final model
inputs = tf.keras.Input(shape=(224, 224, 3))
x = preprocess_input(inputs)
x = base_model(x, training=False)
x = global_average_layer(x)
x = tf.keras.layers.Dropout(0.2)(x)
outputs = prediction_layer(x)
model = tf.keras.Model(inputs, outputs)
# learning rate can be tweeked for better accuracy
base_learning_rate = 0.01
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=base_learning_rate),
loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
metrics=['accuracy'])
In [34]:
model.summary()
Training¶
In [38]:
initial_epochs = 2
loss0, accuracy0 = model.evaluate(validation_dataset)
In [39]:
print("initial loss: {:.2f}".format(loss0))
print("initial accuracy: {:.2f}".format(accuracy0))
In [40]:
history = model.fit(train_dataset,
epochs=initial_epochs,
validation_data=validation_dataset)
In [ ]:
# initial accuracy
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
# initial loss
loss = history.history['loss']
val_loss = history.history['val_loss']
Fine Tuning¶
In [59]:
# fine tuning
base_model.trainable = True
# Let's take a look to see how many layers are in the base model
print("Number of layers in the base model: ", len(base_model.layers))
# Fine-tune from this layer onwards
fine_tune_at = 100
# Freeze all the layers before the `fine_tune_at` layer
for layer in base_model.layers[:fine_tune_at]:
layer.trainable = False
model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
optimizer = tf.keras.optimizers.RMSprop(learning_rate=base_learning_rate/10),
metrics=['accuracy'])
In [60]:
model.summary()
In [61]:
fine_tune_epochs = 2
total_epochs = initial_epochs + fine_tune_epochs
history_fine = model.fit(train_dataset,
epochs=total_epochs,
initial_epoch=history.epoch[-1],
validation_data=validation_dataset)
In [62]:
# storing the model for future use
model.save("models/mobilenet_baseline")
In [63]:
# plotting the learning curve
acc += history_fine.history['accuracy']
val_acc += history_fine.history['val_accuracy']
loss += history_fine.history['loss']
val_loss += history_fine.history['val_loss']
plt.figure(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Training Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.ylim([0.8, 1])
plt.plot([initial_epochs-1,initial_epochs-1],
plt.ylim(), label='Start Fine Tuning')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(2, 1, 2)
plt.plot(loss, label='Training Loss')
plt.plot(val_loss, label='Validation Loss')
plt.ylim([0, 1.0])
plt.plot([initial_epochs-1,initial_epochs-1],
plt.ylim(), label='Start Fine Tuning')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.xlabel('epoch')
plt.show()
Prediction¶
In [101]:
def make_submission_csv(model, name):
images = []
prediction = []
probability = []
test_path = "data/test/"
test = pd.read_csv("sample_submission.csv")
test_files = os.listdir(test_path)
test_files = [x.split(".jpg")[0] for x in test_files]
test = test.loc[test["id"].isin(test_files)]
files = test["id"]
files = [x + ".jpg" for x in files]
for i in tqdm(files):
images.append(i.split(".")[0])
# reading images and converting to np array
img = tf.keras.utils.load_img(os.path.join(test_path, i), target_size = IMG_SIZE)
img = tf.keras.utils.img_to_array(img)
# predicting the class
pred = model.predict(img[None,...])
# Apply a sigmoid since our model returns logits
pred = tf.nn.sigmoid(pred)
pred = tf.where(pred < 0.5, 0, 1)
# some flattening and slicing to get the predicted class and finally getting the label
pred_class = class_names[pred.numpy().flatten()[0]]
prediction.append(pred_class)
answer = pd.DataFrame({'id': images, 'class': prediction})
display(answer.head())
answer.to_csv(name, index=False)
In [102]:
make_submission_csv(model, "submission.csv")
Make a submission using the aicrwd-cli¶
In [103]:
!aicrowd submission create -c snake -f submission.csv
Content
Comments
You must login before you can post a comment.