<jupyter_start><jupyter_text>**Import Dataset from Kaggle**<jupyter_code>! pip install --upgrade --force-reinstall --no-deps kaggle
from google.colab import files
files.upload()
! mkdir ~/.kaggle
! cp kaggle.json ~/.kaggle/
! chmod 600 ~/.kaggle/kaggle.json
! kaggle datasets list
! kaggle datasets download -d gpiosenka/100-bird-species
! unzip 100-bird-species.zip -d dataset<jupyter_output><empty_output><jupyter_text>**Import required libraries and modules**<jupyter_code>import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator
from keras import applications
from keras.applications import ResNet152V2
from keras.layers import Dropout, BatchNormalization, GlobalAveragePooling2D, Dense
from keras.optimizers import Adam<jupyter_output><empty_output><jupyter_text>**Paths and Data Augmentation**<jupyter_code>train_path = "/content/dataset/train"
test_path = "/content/dataset/test"<jupyter_output><empty_output><jupyter_text>> ImageDataGenerator is used for data augmentation.* Performing horizontal and vertical flip to increase size of dataset<jupyter_code>datagen = ImageDataGenerator(
    rescale = 1./255,
    horizontal_flip = True,
    vertical_flip = True,
)
train_data = datagen.flow_from_directory(
    train_path,
    target_size = (224, 224),
    color_mode = 'rgb',
    batch_size = 32,
    shuffle = True,
    seed = 123,
    class_mode= 'categorical'
)
test_data = datagen.flow_from_directory(
    test_path,
    target_size = (224,224),
    color_mode = 'rgb',
    batch_size = 32,
    shuffle = True,
    seed = 123,
    class_mode = 'categorical'
)<jupyter_output>Found 1250 images belonging to 250 classes.<jupyter_text>**Importing and using Pretrained Model** > ResNet152V2* Using pretrained model ResNet 152* Model is pretrained on weights of imagenet* Image input resolution is 224 x 224, colored.<jupyter_code>resnet = ResNet152V2( 
    include_top = False,
    weights = 'imagenet',
    input_shape = (224, 224, 3)
)
resnet.trainable = False<jupyter_output><empty_output><jupyter_text>**Creating Model for Classification** > Sequential Model* Model consists of ResNet for feature extraction.* Dropout layers and BatchNormalization is used to increase accuracy and prevent overfitting* Output layer classifies into 250 distinct classes, as a probability distribution using activation softmax<jupyter_code>model = tf.keras.Sequential()
model.add(resnet)
model.add(GlobalAveragePooling2D())
model.add(Dropout(0.4))
model.add(Dense(2048, activation='relu'))
model.add(Dense(256, activation = 'relu'))
model.add(Dropout(0.33))
model.add(Dense(1024, activation='relu'))
model.add(BatchNormalization())
model.add(Dense(512, activation='relu'))
model.add(Dense(250, activation='softmax'))
model.summary()<jupyter_output><empty_output><jupyter_text>**Compiling and Training the Model** * Model is compiled with optimizer as Adam, with a modified learning rate of 1e-04, as accuracy wasn't great with default learning rate.* Loss function used is categorical crossentropy* Model is trained for 50 epochs, with validation data being the test dataset.<jupyter_code>model.compile(optimizer=Adam(learning_rate=0.0001), loss = 'categorical_crossentropy', metrics = ['accuracy'])
history = model.fit(train_data, validation_data=test_data, epochs = 50)<jupyter_output>Epoch 1/50
1101/1101 [==============================] - 271s 246ms/step - loss: 1.9296 - accuracy: 0.5029 - val_loss: 0.8227 - val_accuracy: 0.7712
Epoch 2/50
1101/1101 [==============================] - 271s 246ms/step - loss: 1.6188 - accuracy: 0.5696 - val_loss: 0.6519 - val_accuracy: 0.8136
Epoch 3/50
1101/1101 [==============================] - 270s 245ms/step - loss: 1.4350 - accuracy: 0.6115 - val_loss: 0.5648 - val_accuracy: 0.8352
Epoch 4/50
1101/1101 [==============================] - 269s 245ms/step - loss: 1.3124 - accuracy: 0.6398 - val_loss: 0.5370 - val_accuracy: 0.8304
Epoch 5/50
1101/1101 [==============================] - 270s 246ms/step - loss: 1.2208 - accuracy: 0.6657 - val_loss: 0.4343 - val_accuracy: 0.8720
Epoch 6/50
1101/1101 [==============================] - 271s 246ms/step - loss: 1.1492 - accuracy: 0.6842 - val_loss: 0.4250 - val_accuracy: 0.8816
Epoch 7/50
1101/1101 [==============================] - 271s 246ms/step - loss: 1.0983 - accuracy: 0.6956 - val_[...]<jupyter_text>**Plotting Graphs** > **Using Matplotlib Library*** Graph is plotted for ``` Epoch vs Accuracy ```, for training accuracy and validation accuracy.* Graph is plotted for ``` Epoch vs Losses ```, for training loss and validation loss.<jupyter_code>plt.plot(history.history['accuracy'], label = 'Accuracy')
plt.plot(history.history['val_accuracy'], label = 'Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend(loc = "lower right")
plt.plot(history.history['loss'], label = 'Loss')
plt.plot(history.history['val_loss'], label = 'Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend(loc = "upper right")<jupyter_output><empty_output><jupyter_text>**Saving the model as HDF5 File**<jupyter_code>model.save('birds.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1569879984514131, 'fraction_numerical': 0.06968641114982578, 'mean_word_length': 5.641388174807198, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '484771', 'license': 'CC0-1.0', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1671, 'n_words': 431, 'path': 'resources/PESU-IO Slot 11 Deep Learning Course material/Bird_Classification.ipynb', 'repo_name': 'SR42-dev/AI-ML-Course', 'size': '5166'}