For my knowledge storytelling capstone undertaking, I explored Harsh Walia’s Chook vs. Drone dataset on Kaggle. The dataset comprises a whole lot of photographs initially meant to help Indian Intelligence in detecting birds or drones current within the sky. It has a complete of 828 photographs divided into 3 folders meant for coaching, testing, and validation. The dataset is particularly designed for binary picture classification and holds a lot worth in purposes like safety and surveillance.
I made a decision to make use of convolutional neural networks as my machine studying algorithm of alternative as a result of it excels in its capacity to acknowledge patterns in photographs, making it good for picture recognition. I’ll use VGG16, a pre-trained mannequin, to categorise the pictures within the dataset as both drones or birds. I’ll modify the mannequin to work higher with my dataset and use methods like picture resizing to scrub and put together the info for modeling. Lastly, I’ll consider my mannequin with coaching and validation accuracy and loss metrics.
Earlier than beginning, be sure to import all obligatory features and libraries.
!pip set up --quiet gdown==4.5.4 --no-cache-dir
import os
import random
from PIL import Picture as PILImage
from IPython.show import Pictureimport numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
from math import flooring
import tensorflow as tf
from tensorflow import keras
from keras import layers
from sklearn.model_selection import train_test_split
from keras.preprocessing.picture import ImageDataGenerator
from keras.fashions import Sequential
from keras.layers import Enter, Dense, Flatten
from keras.layers import Conv2D, MaxPooling2D, Lambda
from keras import backend as Ok
from keras.optimizers import *
from keras.purposes.vgg16 import VGG16
from keras.metrics import *
from keras.purposes.vgg16 import preprocess_input
from tensorflow.keras.preprocessing import image_dataset_from_directory
from tensorflow.keras.layers.experimental.preprocessing import RandomFlip, RandomRotation
Step #1: Import and break up the info right into a practice/validation set
!gdown 1S8IezIq2waOeSjSjYoOvSk16XzOH3oJB && unzip -qq drones_or_birds.zipBATCH_SIZE = 64
# Resize the pictures to standardize dimensions.
IMG_SIZE = (160, 160)
listing = 'drones_or_birds/'
# Use keras's image_dataset_from_directory perform
# to load the pictures from the listing.
train_dataset = image_dataset_from_directory(listing,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE,
validation_split=0.2,
subset='coaching',
seed=42)
validation_dataset = image_dataset_from_directory(listing,
shuffle=True,
batch_size=BATCH_SIZE,
image_size=IMG_SIZE,
validation_split=0.2,
subset='validation',
seed=42)
# Get the names of every class from the dataset.
# On this case, the lessons are both "drone" or "fowl"
class_names = train_dataset.class_names
# Plot 9 photographs from the coaching dataset.
plt.determine(figsize=(10, 10))
for photographs, labels in train_dataset.take(1):
for i in vary(9):
ax = plt.subplot(3, 3, i + 1)
plt.imshow(photographs[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])
plt.axis("off")
Step #2: Outline the enter form of the info
input_shape = (160, 160, 3)
Steps #3–6: Construct a CNN utilizing VGG16. VGG16 is a strong mannequin pre-trained on the ImageNet database, making it good for this binary picture classification activity. I additionally froze the parameters of some layers, as was achieved in a earlier lab, as a result of I solely wished to coach the ultimate layers.
# Outline the enter form for the mannequin
input_shape = (160, 160, 3)# Initialize the mannequin
new_model = Sequential()
# Add enter layer
new_model.add(Enter(form=input_shape))
# Add knowledge augmentation layers
new_model.add(RandomFlip('horizontal'))
new_model.add(RandomRotation(0.2))
# You don't want to fret about this step. However in case you're curious:
# The "Lambda" layer is one other particular layer that permit's you apply a customized
# perform to the info. As a result of we all know we will use VGG16, we'll use
# the identical pre-processing perform that was used when it was first skilled.
new_model.add(
Lambda(
preprocess_input,
identify='preprocessing',
input_shape=input_shape
)
)
# Load a pre-trained mannequin
VGG = VGG16(
# we need to use our personal last layers custom-made for our activity
include_top=False,
input_shape=input_shape,
weights='imagenet'
)
# Freeze parameters every layer as a result of we solely need to practice the ultimate layers
for layer in VGG.layers:
layer.trainable = False
# Add the pre-trained mannequin to our mannequin
new_model.add(VGG)
# Add a flattening layer
new_model.add(Flatten())
# Add a totally linked layer
new_model.add(Dense(256, activation='relu'))
# Add the output layer
new_model.add(Dense(1, activation='sigmoid'))
new_model.abstract()
new_model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])historical past = new_model.match(
train_dataset,
validation_data=validation_dataset,
epochs=10)
Consider the mannequin’s efficiency by monitoring accuracy and loss.
acc = [0.] + historical past.historical past['accuracy']
val_acc = [0.] + historical past.historical past['val_accuracy']loss = historical past.historical past['loss']
val_loss = historical past.historical past['val_loss']
plt.determine(figsize=(8, 8))
plt.subplot(2, 1, 1)
plt.plot(acc, label='Coaching Accuracy')
plt.plot(val_acc, label='Validation Accuracy')
plt.legend(loc='decrease proper')
plt.ylabel('Accuracy')
plt.ylim([min(plt.ylim()),1])
plt.title('Coaching and Validation Accuracy')
plt.subplot(2, 1, 2)
plt.plot(loss, label='Coaching Loss')
plt.plot(val_loss, label='Validation Loss')
plt.legend(loc='higher proper')
plt.ylabel('Cross Entropy')
plt.ylim([0,1.0])
plt.title('Coaching and Validation Loss')
plt.xlabel('epoch')
plt.present()
Plot photographs and assess the mannequin’s efficiency.
# We'll take 9 photographs from the validation dataset to plot
plt.determine(figsize=(15, 15))
for x, y in validation_dataset.take(1):
for i in vary(9):
prediction = new_model.predict(x)[i]
ax = plt.subplot(3, 3, i + 1)
plt.imshow(x[i].numpy().astype("uint8"))
tmp2 = "predicted {:.0f}% fowl, {:.0f}% drone, really ".format(100 * (1 - prediction[0]), 100 * prediction[0])
plt.title(tmp2 + class_names[y[i].numpy()])
plt.axis("off")