text_plain_output_1.png image_output_2.png image_output_1.png
from keras.layers import Activation, Flatten, Dense, Dropout ,Input

from tensorflow.keras import backend

from tensorflow.keras import backend, models, layers, regularizers , optimizers

from tensorflow.keras.callbacks import EarlyStopping

from tensorflow.keras.layers import BatchNormalization , Concatenate

from tensorflow.keras.layers import Conv2D, MaxPooling2D, AveragePooling2D

from tensorflow.keras.models import Model, Sequential

from tensorflow.keras.preprocessing.image import ImageDataGenerator

import matplotlib.image as mpimg

import matplotlib.pyplot as plt

import os

import os

import random

import numpy as np
import pandas as pd
import os

train_directory = '../input/100-bird-species/train'
val_directory = '../input/100-bird-species/valid'
test_directory = '../input/100-bird-species/test'

train_datagen = ImageDataGenerator(rescale=1 / 255)
val_datagen = ImageDataGenerator(rescale=1 / 255)
test_datagen = ImageDataGenerator(rescale=1 / 255)

train_generator = train_datagen.flow_from_directory(train_directory, target_size=(224, 224), batch_size=32, color_mode='rgb', class_mode='sparse')
validation_generator = train_datagen.flow_from_directory(val_directory, target_size=(224, 224), batch_size=32, color_mode='rgb', class_mode='sparse')
test_generator = test_datagen.flow_from_directory(test_directory, target_size=(224, 224), batch_size=32, color_mode='rgb', class_mode='sparse')

train_generator.class_indices

num_classes = len(train_generator.class_indices)

def display_random_grid(ncols=5, ds_path=train_directory):
    fig, ax = plt.subplots(ncols=ncols, nrows=ncols, figsize=(15, 15))
    
    for i in range(ncols):
        for j in range(ncols):
            bird_species = random.choice(os.listdir(ds_path))
            random_bird_path = random.choice(os.listdir(ds_path + '/'+ bird_species))
            random_bird = mpimg.imread(ds_path + '/' + bird_species + '/' + random_bird_path)
            ax[i, j].imshow(random_bird)
            ax[i, j].set_title(bird_species)
            ax[i, j].axis('off')
            
display_random_grid()

backend.clear_session()
model_base = models.Sequential()
model_base.add(layers.Conv2D(512, (3, 3), activation='relu', input_shape=(224, 224, 3)))
model_base.add(layers.MaxPool2D((2, 2)))
model_base.add(BatchNormalization())
model_base.add(layers.Conv2D(512, (3, 3), activation='relu'))
model_base.add(layers.MaxPool2D((2, 2)))
model_base.add(BatchNormalization())
model_base.add(layers.Conv2D(256, (3, 3), activation='relu'))
model_base.add(layers.MaxPool2D((2, 2)))
model_base.add(BatchNormalization())
model_base.add(layers.Flatten())
model_base.add(layers.Dense(128, activation='relu'))
model_base.add(layers.Dropout(0.5))
model_base.add(layers.Dense(num_classes, activation='softmax'))
model_base.compile(optimizer=optimizers.Adam(lr=0.001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
history = model_base.fit_generator(train_generator, epochs=50, validation_data=validation_generator, validation_steps=20, verbose=1, callbacks=[EarlyStopping(monitor='val_accuracy', patience=5, restore_best_weights=True)])
test_loss, test_acc = model_base.evaluate_generator(test_generator, steps=20)

backend.clear_session()
visible = Input(shape=(224, 224, 3))
conv_1_1 = Conv2D(64, (1, 1), padding='same', activation='relu', strides=2)(visible)
conv_1_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(visible)
conv_2_2 = Conv2D(64, (3, 3), padding='same', activation='relu', strides=2)(conv_1_2)
conv_1_3 = AveragePooling2D((3, 3), padding='same', strides=2)(visible)
conv_2_3 = Conv2D(64, (3, 3), padding='same', activation='relu')(conv_1_3)
conv_1_4 = Conv2D(64, (1, 1), padding='same', activation='relu')(visible)
conv_2_4 = Conv2D(64, (3, 3), padding='same', activation='relu')(conv_1_4)
conv_3_4 = Conv2D(64, (3, 3), padding='same', activation='relu', strides=2)(conv_2_4)
merge = Concatenate(axis=-1)([conv_1_1, conv_2_2, conv_2_3, conv_3_4])
flat = Flatten()(merge)
hidden = Dense(32, activation='relu')(flat)
drop = Dropout(0.5)(hidden)
output = Dense(num_classes, activation='softmax')(drop)
model_birdcl = Model(inputs=visible, outputs=output)
model_birdcl.compile(optimizer=optimizers.Adam(lr=0.001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
history = model_birdcl.fit_generator(train_generator, steps_per_epoch=1001, epochs=50, validation_data=validation_generator, validation_steps=29, verbose=1, callbacks=[EarlyStopping(monitor='val_accuracy', patience=5, restore_best_weights=True)])
test_loss, test_acc = model_birdcl.evaluate_generator(test_generator, steps=29)
print('Using Functional API and Adam Optimizer the accuracy is ---', test_acc)
history_dict = history.history
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
acc_values = history_dict['accuracy']
val_acc_values = history_dict['val_accuracy']
epochs = range(1, len(history_dict['accuracy']) + 1)
plt.plot(epochs, loss_values, 'bo', label='Training loss')
plt.plot(epochs, val_loss_values, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.plot(epochs, acc_values, 'bo', label='Training accuracy')
plt.plot(epochs, val_acc_values, 'b', label='Validation accuracy')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()