# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="5XWjPOj4LGhc"
# Import libraries

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras

# + id="T6kLEGTLLwuQ"
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Flatten, BatchNormalization, Conv2D, MaxPool2D, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix

# + id="Ts7DxuvVMIHw"
import matplotlib.image as mpimg
# %matplotlib inline

from tensorflow.keras.preprocessing import image_dataset_from_directory

# for reproducibility
SEED = 42
np.random.seed(SEED)
tf.random.set_seed(SEED)

# + colab={"base_uri": "https://localhost:8080/"} id="-CbS8ecdMKrJ" outputId="c5f2199a-231a-4611-8645-f54d80fafc31"
# Define file paths

# ! git clone https://github.com/chibykelaw/Hamoye_capstone_project_smote.git
train_path = 'Hamoye_capstone_project_smote/Data/train/'
val_path = 'Hamoye_capstone_project_smote/Data/val/'
test_path = 'Hamoye_capstone_project_smote/Data/test/'

# + colab={"base_uri": "https://localhost:8080/"} id="8bo6ZnHRMM6a" outputId="974d2a1a-63d1-46b4-92a2-1df1795c07a4"
# generate train, test and validation sets from directory

train_ds = image_dataset_from_directory(train_path, label_mode = 'categorical',
                                        image_size = (528, 528))
val_ds = image_dataset_from_directory(val_path, label_mode = 'categorical',
                                      image_size = (528, 528))
test_ds = image_dataset_from_directory(test_path, label_mode = 'categorical',
                                       shuffle= False, image_size = (528, 528))                                    

# + colab={"base_uri": "https://localhost:8080/", "height": 324} id="N5-iTFzgQGMx" outputId="4bc0088b-bbfb-4cdc-c507-9bc0906de60d"
# View dataset

class_names = train_ds.class_names

plt.figure(figsize=(10, 10))
for images, labels in train_ds.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[np.argmax(labels[i], axis=None, out=None)])
    plt.axis("off")

# + colab={"base_uri": "https://localhost:8080/", "height": 281} id="1wizO4teVNkV" outputId="2dd40590-7234-4e22-a638-85619aabf99b"
for images, label in train_ds.take(1):
  first_image = images[0]

plt.imshow(first_image.numpy().astype("uint8"))
plt.title(class_names[np.argmax(label[i], axis=None, out=None)])
plt.axis('off')

# + colab={"base_uri": "https://localhost:8080/"} id="yd1zQEocZsel" outputId="8f369907-ed8a-4b51-8ada-e5072e280ed7"
# make predictions using a pretrained model

from keras.applications.efficientnet import EfficientNetB6
from tensorflow.keras.applications.efficientnet import decode_predictions
from tensorflow.keras.preprocessing import image

x = image.img_to_array(first_image)
x = np.expand_dims(x, axis=0)

base_model = EfficientNetB6(weights='imagenet')
pred = base_model.predict(x)
print('Predicted:', decode_predictions(pred))

# + id="k_K7L7UAfxx_"
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D

# + colab={"base_uri": "https://localhost:8080/"} id="9F_FnEhahYTv" outputId="de1cc965-6157-445f-9c03-a56a34ce6180"
from tensorflow.keras import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D

base_model = EfficientNetB6(weights='imagenet', include_top=False,
                            input_shape=(528, 528,3))

# freeze extraction layers
base_model.trainable = False

# add custom top layers
x = base_model.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(4, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)

# confirm unfrozen layers
for layer in model.layers:
    if layer.trainable==True:
        print(layer)

# + colab={"base_uri": "https://localhost:8080/"} id="Qq-TTQ2QlI0R" outputId="1b02e1c8-c86e-4b16-d3b0-62b4bcc5185f"
# create callbacks fr training
my_callbacks = tf.keras.callbacks.ModelCheckpoint('weights.h5', save_best_only = True,
                                                  save_weights_only = True)

#compile the model
model.compile(optimizer='adam', loss='categorical_crossentropy',metrics=['accuracy'])

# train the model on the new data for a few epochs
history = model.fit(train_ds, 
                    epochs=10,
                    validation_data=val_ds,
                    callbacks=[my_callbacks])

# + id="c1HKWa1PxYgg"
# save results
results = history.history

# + colab={"base_uri": "https://localhost:8080/", "height": 246} id="9Xbw9942rOHy" outputId="839858a1-0ae9-4ff8-8d01-42a9339afbcb"
# plot results

n_epochs = len(results['loss'])

plt.figure(figsize=[14,4])
plt.grid(True)
plt.subplot(1,2,1)
plt.plot(range(1, n_epochs+1), results['loss'], label='Training')
plt.plot(range(1, n_epochs+1), results['val_loss'], label='Validation')
plt.xlabel('Epoch'); plt.ylabel('Loss'); plt.title('Loss')
plt.legend()
plt.grid(True)
plt.subplot(1,2,2)
plt.plot(range(1, n_epochs+1), results['accuracy'], label='Training')
plt.plot(range(1, n_epochs+1), results['val_accuracy'], label='Validation')
plt.xlabel('Epoch'); plt.ylabel('Accuracy'); plt.title('Accuracy')
plt.legend()
plt.grid(True)
plt.show()

# + id="yHQZ5xghrPNa"
model.load_weights('weights.h5')

# + colab={"base_uri": "https://localhost:8080/"} id="XL0HMdxCrYez" outputId="2612f2cd-f9a9-4cd7-cb34-e617956f3468"
predictions = model.evaluate(test_ds)

# + colab={"base_uri": "https://localhost:8080/"} id="OInK4dAzrZTo" outputId="062d5111-925b-4cd4-aba1-d3049ca559a8"
# make predictions on the unseen data
predictions = model.predict(test_ds)
predictions

# + colab={"base_uri": "https://localhost:8080/"} id="-pTbp1yssCqG" outputId="5d4b2574-b5f8-4a1b-ed65-2d81920a12f7"
# save the index of the highest probability
predictions = predictions.argmax(axis=1)
predictions

# + colab={"base_uri": "https://localhost:8080/"} id="nQ8FznuArir7" outputId="4977501c-6bf0-42b0-d5b5-6c061a51b619"
# get the actual values

test_images = list(test_ds.unbatch().as_numpy_iterator())
y_true = np.array([i[1] for i in test_images])
y_true = y_true.argmax(axis=1)
y_true

# + colab={"base_uri": "https://localhost:8080/"} id="BGDuKZTvsWU4" outputId="87ffebcc-a0d7-432c-f467-120ff5e3a693"
# calculate f1_score
from sklearn.metrics import f1_score
f1_score(y_true,predictions,average='macro')

# + colab={"base_uri": "https://localhost:8080/"} id="bV8ukWhSsoaF" outputId="4fc16d88-e6d6-4388-a093-76c5ace707a0"
# get the confusion matrix
from sklearn.metrics import confusion_matrix
confusion_matrix(y_true,predictions)

# + [markdown] id="ghMF-ERxzQJ_"
# **This looks like a perfect model with perfect accuracy**

# + colab={"base_uri": "https://localhost:8080/"} id="gCCJPdSBzPKC" outputId="40115543-daad-4ced-b49e-510359aff0ab"
# Save the model as .pkl

import pickle
pickle.dump(model, open('EfficientNetB6_1.0.pkl', 'wb'))

# + id="i2u2eaLwzlPU"


StarcoderdataJupyterScriptsDedupFiltered