<jupyter_start><jupyter_text>Data Augumentation on Animals dataset using MiniVGG netIn this example, We explore the Data Augmentation. This method of regularization can aid us to improve our results during the training process. In addition, we consider the Aspect Ratio when resizing the images. Maintaining the aspect ratio help the CNN to extract considerable features from each image, unlike the simple resizing that distort the images, causing the loss of information contained on the images.We perform two experiments. The fist, we just consider the resizing process maintaining the aspect ratio. For the second experiment, we consider the Data Augmentation and the aspect ration for the resizing. For the both models, We consider the Learning Rate Scheduler, defining a piecewise function.  Importing libraries<jupyter_code>from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
import tensorflow as tf
#compvis modeule
from compvis.preprocessing import ImageToArrayPreprocessor
from compvis.preprocessing import ResizeAR
from compvis.datasets import SimpleDatasetLoader
from compvis.ann.cnns import MiniVGG
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import LearningRateScheduler
from imutils import paths
import matplotlib.pyplot as plt
import numpy as np<jupyter_output><empty_output><jupyter_text>Loading and preprocessing the dataset<jupyter_code>dataset = "/home/igor/Documents/Artificial_Inteligence/Datasets/Animals" #path
imagePaths = list(paths.list_images(dataset)) # list of all images
ar = ResizeAR(32, 32) # simple image preprocessor
iap = ImageToArrayPreprocessor() # to convert image into arraey
sdl = SimpleDatasetLoader(preprocessors=[ar, iap]) # to load images from disk and process them
(data, labels) = sdl.load(imagePaths, verbose = 500) # return images and labels
data = data.astype("float") / 255. # Normalize into 0 and 1<jupyter_output>[INFO] processed 500/3000
[INFO] processed 1000/3000
[INFO] processed 1500/3000
[INFO] processed 2000/3000
[INFO] processed 2500/3000
[INFO] processed 3000/3000<jupyter_text>Splitting the dataset into train and test set<jupyter_code>(X_train, X_test, y_train, y_test) = train_test_split(data, labels, test_size = 0.25,
                                                      random_state = 42)<jupyter_output><empty_output><jupyter_text>Transforming the labels<jupyter_code>y_train = LabelBinarizer().fit_transform(y_train)
y_test = LabelBinarizer().fit_transform(y_test)<jupyter_output><empty_output><jupyter_text>Defining the Image Generator function from KerasThe Data Augmentations consists in transform an image, rotating, shearing, zooming and flipping horizontally it. In this way, a simple image generates others six.<jupyter_code>aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1,
                         height_shift_range=0.1, shear_range=0.2, zoom_range=0.2,
                         horizontal_flip=True, fill_mode="nearest")<jupyter_output><empty_output><jupyter_text>Building the model Model **Defining the step decay function**<jupyter_code>def step_decay(epoch, lr):
    
    init_alpha = 0.01 # initial learning rate
    factor = 0.25
    dropEvery = 5 # step size
    
    if epoch == 1:
        lr = init_alpha
    elif   epoch%5 == 0:
        lr = init_alpha * (factor ** np.floor((1 + epoch) / dropEvery))
    else:
        lr = lr
    return lr<jupyter_output><empty_output><jupyter_text>**Defining the Callbacks**<jupyter_code>callbacks = [LearningRateScheduler(step_decay)]<jupyter_output><empty_output><jupyter_text>Defining the first model<jupyter_code>opt = SGD(momentum=0.9, nesterov=True) # regularization
model = MiniVGG.build(32, 32, 3, 3) #model
model.compile(loss = "categorical_crossentropy", optimizer = opt, metrics = ["accuracy"]) # compiling the model<jupyter_output><empty_output><jupyter_text>Training the first modelWe choose the batch size of $32$ and $100$ epochs.<jupyter_code>ne = 100
bs = 32
H = model.fit(X_train, y_train, validation_data = (X_test, y_test),
              batch_size = bs, epochs = ne, callbacks=callbacks, verbose = 1)<jupyter_output>Train on 2250 samples, validate on 750 samples
Epoch 1/100
2250/2250 [==============================] - 3s 1ms/sample - loss: 1.3921 - accuracy: 0.5516 - val_loss: 3.5832 - val_accuracy: 0.4067
Epoch 2/100
2250/2250 [==============================] - 1s 241us/sample - loss: 1.1532 - accuracy: 0.5960 - val_loss: 1.4698 - val_accuracy: 0.5293
Epoch 3/100
2250/2250 [==============================] - 1s 238us/sample - loss: 1.0479 - accuracy: 0.6307 - val_loss: 1.2169 - val_accuracy: 0.4947
Epoch 4/100
2250/2250 [==============================] - 1s 236us/sample - loss: 1.0022 - accuracy: 0.6511 - val_loss: 1.0516 - val_accuracy: 0.5920
Epoch 5/100
2250/2250 [==============================] - 1s 241us/sample - loss: 0.9758 - accuracy: 0.6640 - val_loss: 0.8298 - val_accuracy: 0.6507
Epoch 6/100
2250/2250 [==============================] - 1s 238us/sample - loss: 0.7358 - accuracy: 0.7049 - val_loss: 0.7089 - val_accuracy: 0.6840
Epoch 7/100
2250/2250 [==============================] - 1s 2[...]<jupyter_text>Predicting on the first trained model<jupyter_code>predictions = model.predict(X_test, batch_size = bs)<jupyter_output><empty_output><jupyter_text>Evaluating the first model<jupyter_code>cr = classification_report(y_test.argmax(axis = 1),
                           predictions.argmax(axis = 1),
                           target_names = ["cat", "dog", "panda"])
print(cr)<jupyter_output>precision    recall  f1-score   support

         cat       0.69      0.67      0.68       249
         dog       0.61      0.71      0.66       239
       panda       0.91      0.82      0.87       262

    accuracy                           0.73       750
   macro avg       0.74      0.73      0.73       750
weighted avg       0.75      0.73      0.74       750<jupyter_text>Visualizing the metrics results<jupyter_code>plt.style.use("ggplot")
plt.figure(figsize=(20,20))
plt.plot(np.arange(0, ne), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, ne), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, ne), H.history["accuracy"], label="train_acc")
plt.plot(np.arange(0, ne), H.history["val_accuracy"], label="val_acc")
plt.axis((0, ne, 0.25, 1))
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Defining the second model with Data Augmentation<jupyter_code>model1 = MiniVGG.build(32, 32, 3, 3)
model1.compile(loss = "categorical_crossentropy", optimizer = opt, metrics = ["accuracy"])<jupyter_output><empty_output><jupyter_text>Training the second modelIn the fit, We have add the aug.flow() that accepts as argument, the training and the batch size.<jupyter_code>H = model1.fit(aug.flow(X_train, y_train, batch_size=bs), validation_data=(X_test, y_test),
              steps_per_epoch=len(X_train) // 32,
              epochs=ne, callbacks=callbacks, verbose=1)<jupyter_output>WARNING:tensorflow:sample_weight modes were coerced from
  ...
    to  
  ['...']
Train for 70 steps, validate on 750 samples
Epoch 1/100
70/70 [==============================] - 2s 26ms/step - loss: 1.4887 - accuracy: 0.5298 - val_loss: 2.1248 - val_accuracy: 0.3853
Epoch 2/100
70/70 [==============================] - 1s 15ms/step - loss: 1.2853 - accuracy: 0.5550 - val_loss: 2.2235 - val_accuracy: 0.3547
Epoch 3/100
70/70 [==============================] - 1s 15ms/step - loss: 1.2097 - accuracy: 0.5654 - val_loss: 1.6565 - val_accuracy: 0.4093
Epoch 4/100
70/70 [==============================] - 1s 16ms/step - loss: 1.1782 - accuracy: 0.5717 - val_loss: 1.6933 - val_accuracy: 0.4213
Epoch 5/100
70/70 [==============================] - 1s 15ms/step - loss: 1.1248 - accuracy: 0.5920 - val_loss: 1.0718 - val_accuracy: 0.6160
Epoch 6/100
70/70 [==============================] - 1s 15ms/step - loss: 1.0167 - accuracy: 0.6168 - val_loss: 0.7027 - val_accuracy: 0.6733
Epoch 7/100
70/70 [===[...]<jupyter_text>Predicting on the second model<jupyter_code>predictions = model.predict(X_test, batch_size = bs)<jupyter_output><empty_output><jupyter_text>Evaluating the second model<jupyter_code>cr = classification_report(y_test.argmax(axis = 1),
                           predictions.argmax(axis = 1),
                           target_names = ["cat", "dog", "panda"])
print(cr)<jupyter_output>precision    recall  f1-score   support

         cat       0.69      0.67      0.68       249
         dog       0.61      0.71      0.66       239
       panda       0.91      0.82      0.87       262

    accuracy                           0.73       750
   macro avg       0.74      0.73      0.73       750
weighted avg       0.75      0.73      0.74       750<jupyter_text>Visualizing the metrics results<jupyter_code>plt.style.use("ggplot")
plt.figure(figsize=(20,20))
plt.plot(np.arange(0, ne), H.history["loss"], label="train_loss")
plt.plot(np.arange(0, ne), H.history["val_loss"], label="val_loss")
plt.plot(np.arange(0, ne), H.history["accuracy"], label="train_acc")
plt.plot(np.arange(0, ne), H.history["val_accuracy"], label="val_acc")
#plt.axis('on')
plt.axis((0, ne, 0.25, 1))
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend()
plt.savefig("1off")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1484154377157201, 'fraction_numerical': 0.07478297249241711, 'mean_word_length': 3.8985655737704916, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '5519', 'license': 'Apache-2.0', 'n_tokens_mistral': 3665, 'n_tokens_neox': 3119, 'n_words': 797, 'path': 'Practical Examples/7 - DataAugumentation  and aspect ratio/animals_data_augmentation.ipynb', 'repo_name': 'IgorMeloS/Computer-Vision-Training', 'size': '9561'}