# ---
# 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
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/sweeneyngo/furspect/blob/master/multiclassification.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + colab={"resources": {"http://localhost:8080/nbextensions/google.colab/files.js": {"data": "<KEY>", "headers": [["content-type", "application/javascript"]], "ok": true, "status": 200, "status_text": "OK"}}, "base_uri": "https://localhost:8080/", "height": 144} id="NJmLYwl9vXfh" outputId="7a35375c-f8ed-414f-a085-00e44b7905d8"
from google.colab import drive
drive.mount('/content/drive', force_remount=True)

# ! pip install -q kaggle
from google.colab import files 
files.upload()
# ! mkdir ~/.kaggle 
# ! cp kaggle.json ~/.kaggle/
# ! chmod 600 ~/.kaggle/kaggle.json
# !kaggle datasets download -d alessiocorrado99/animals10

# + id="pqD7L4vlz7hW"
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
import cv2
import os

# + id="lRUe-7d30EvL"
# !unzip animals10.zip

# + id="dVPvxwl3NODw"
# !rm -rf animals10.zip

# + colab={"base_uri": "https://localhost:8080/"} id="ZXxm4gYC1Qme" outputId="57cbc620-a5f7-462e-c22e-d350be5f2032"
# main folder
classes = os.listdir('raw-img')
classes

# + id="NDGmCJ2F1pxi"
# organize dataset into a useful structure
from shutil import copyfile 

from random import seed    
from random import random

# create directories
subdirs = ['train', 'validation']

for subdir in subdirs:

  # create label subdirectories
  for label in classes:
    new = os.path.join(subdir, label)
    os.makedirs(new, exist_ok=True)

# + id="rSsJTyAX2fbz"
# seed random number generator
seed(1)

for label in classes:

  head = os.path.join('raw-img', label)

  for file in os.listdir(head):
    src = os.path.join(head, file)
    dst = os.path.join(subdirs[0], label, file)

    if random() < 0.2:
      dst = os.path.join(subdirs[1], label, file)

    print(f'Travelling from {src} to {dst}!')
    copyfile(src, dst)
    


# + colab={"base_uri": "https://localhost:8080/", "height": 286} id="3De_pD0q38RB" outputId="3e8fff09-5d50-4484-fe11-15623a233e90"
# sample image

path = os.path.join('validation', 'elefante')
files = os.listdir(path)
img = cv2.imread(os.path.join(path, files[0]))
plt.imshow(img, cmap=plt.cm.binary)

# + colab={"base_uri": "https://localhost:8080/", "height": 661} id="JzrXZ3ZKfEjw" outputId="861d1fab-4aee-4104-a74d-9f4bd4e67863"
import os
from matplotlib import pyplot as plt

def display_examples(folder):
    """
        Display 25 images from the images array with its corresponding labels
    """
    path = os.path.join('train/', folder)
    fig = plt.figure(figsize=(10,10))
    fig.suptitle("Some examples of images of the dataset", fontsize=16)
    for i in range(25):
        plt.subplot(5,5,i+1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        images = os.listdir(path)
        idx = np.random.randint(0, len(images))
        arr = cv2.imread(os.path.join(path, images[idx]))
        arr = cv2.cvtColor(arr, cv2.COLOR_BGR2RGB)
        plt.imshow(arr, cmap=plt.cm.binary)
        plt.xlabel(folder)
    plt.show()
    
display_examples('furry')

# + colab={"base_uri": "https://localhost:8080/"} id="DVsyWwdm6IYr" outputId="0691c8a4-1400-4f08-ae2d-e935c30e38cd"
from keras.preprocessing.image import ImageDataGenerator

path = os.path.join('')
# create data generators
train_datagen = ImageDataGenerator(rotation_range=15,
                                rescale=1./255,
                                shear_range=0.1,
                                zoom_range=0.2,
                                horizontal_flip=True,
                                width_shift_range=0.1,
                                height_shift_range=0.1)

# keep test images unmodified
val_datagen = ImageDataGenerator(rescale=1.0/255.0)

# prepare iterators
train_it = train_datagen.flow_from_directory(os.path.join(path, 'train'),
    class_mode='categorical', batch_size=32, target_size=(200, 200))
val_it = val_datagen.flow_from_directory(os.path.join(path, 'validation'),
    class_mode='categorical', batch_size=32, target_size=(200, 200))

print('Initiated data generators.')

# + colab={"base_uri": "https://localhost:8080/"} id="Wjh-LWkaYyiN" outputId="9d55ed13-b8d4-472a-c0fe-49b9bad04c06"
from keras.preprocessing.image import ImageDataGenerator
import os
path = os.path.join('')



datagen = ImageDataGenerator(rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        rotation_range=30, 
        width_shift_range=0.1,
        height_shift_range=0.1,
        samplewise_center = True,
        )

val_datagen = ImageDataGenerator(rescale=1./255,
        samplewise_center = True,
        )
# datagen = ImageDataGenerator(featurewise_center=True)
# # specify imagenet mean values for centering
# datagen.mean = [123.68, 116.779, 103.939]

# # create data generators
# train_datagen = ImageDataGenerator(rescale=1.0/255.0)

# # keep test images unmodified
# val_datagen = ImageDataGenerator(rescale=1.0/255.0)

# prepare iterators
# train_it = train_datagen.flow_from_directory(os.path.join(path, 'train'),
#     class_mode='categorical', batch_size=32, target_size=(200, 200))
# val_it = val_datagen.flow_from_directory(os.path.join(path, 'validation'),
#     class_mode='categorical', batch_size=32, target_size=(200, 200))

train_it = datagen.flow_from_directory(os.path.join(path, 'train'),
    class_mode='categorical', interpolation = 'lanczos', batch_size=32, target_size=(224, 224))
val_it = val_datagen.flow_from_directory(os.path.join(path, 'validation'),
    class_mode='categorical', interpolation = 'lanczos', batch_size=32, target_size=(224, 224))


print('Initiated data generators.')

# + [markdown] id="zkh540gNfkEI"
# **kerasTuner modelling**

# + colab={"base_uri": "https://localhost:8080/"} id="IPp2PXA6_m-H" outputId="dbd4fa6f-7d89-4897-9bc0-97c6d18f00f2"
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Conv2D, MaxPooling2D, BatchNormalization

from tensorflow.keras.optimizers import SGD
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# !pip install -q -U keras-tuner

import kerastuner as kt

def model_builder(hp):

  model = Sequential()

  model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(200, 200, 3)))
  model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
  model.add(MaxPooling2D((2, 2), padding='same'))
  model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
  model.add(MaxPooling2D((2, 2), padding='same'))
  model.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
  model.add(MaxPooling2D((2, 2), padding='same'))
      
  model.add(Flatten())
  model.add(keras.layers.Dense(units=128, activation='relu', kernel_initializer='he_uniform'))
  model.add(keras.layers.Dense(units=128, activation='relu', kernel_initializer='he_uniform'))
  model.add(Dense(11, activation='softmax'))

  # compile model
  # opt = SGD(lr=0.001, momentum=0.9)

  # hp_learning_rate = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])

  model.compile(optimizer=keras.optimizers.Adam(learning_rate=1e-5), loss='categorical_crossentropy', metrics=['accuracy']) #1e-4
  model.summary()
  return model



# + [markdown] id="ot4KCQ3xfqxH"
# **Manual modelling**

# + id="-SrWxjz_XD6d"
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Conv2D, MaxPooling2D, BatchNormalization

from tensorflow.keras.optimizers import SGD
from tensorflow.keras.preprocessing.image import ImageDataGenerator

model = Sequential()

model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(200, 200, 3)))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(MaxPooling2D((2, 2), padding='same'))
model.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
model.add(MaxPooling2D((2, 2), padding='same'))
  

model.add(Flatten())
model.add(keras.layers.Dense(units=128, activation='relu', kernel_initializer='he_uniform'))
model.add(Dense(11, activation='softmax'))

# compile model
# opt = SGD(lr=0.001, momentum=0.9)

# hp_learning_rate = hp.Choice('learning_rate', values=[1e-2, 1e-3, 1e-4])

model.compile(optimizer=keras.optimizers.Adam(learning_rate=1e-4), loss='categorical_crossentropy', metrics=['accuracy']) #1e-4
model.summary()

# + colab={"base_uri": "https://localhost:8080/"} id="fx73y76vrTZe" outputId="5b3a9f1f-e79e-4891-a12c-09a795bd224d"
# add new classifier layers
from tensorflow.keras import layers
from keras.applications.vgg16 import VGG16
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Conv2D, MaxPooling2D, BatchNormalization

from tensorflow.keras.optimizers import SGD
from tensorflow.keras.preprocessing.image import ImageDataGenerator


base = VGG16(include_top=False, input_shape=(224, 224, 3))

model = Sequential()
model.add(base)
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dense(11, activation='softmax'))

for layer in base.layers:
  layer.trainable = False

model.layers[0].trainable = False

model.compile(loss='categorical_crossentropy', optimizer='adam',
              metrics=['accuracy'])
model.build(input_shape=(None, 224, 224, 3))
# define new model
model.summary()

# + id="SX3-SQJZkt3y"
tuner = kt.Hyperband(model_builder,
                     objective='val_accuracy',
                     max_epochs=10, overwrite = True)

# + id="3SriAaT_W3Ku"
tuner.search(train_it, steps_per_epoch=len(train_it) // 32,
      validation_data=val_it, validation_steps=len(val_it) // 32, epochs=50, verbose=1, callbacks=[callbacks])

# Get the optimal hyperparameters
best_hps = tuner.get_best_hyperparameters(num_trials=1)[0]
best_hps['conv_layers']

# + id="GIqOLA47iq64"
best_model = tuner.get_best_models()[0]
best_model.save('furspect_c.h5')

# + id="U7LgAl_rBoXa"
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
earlystop = EarlyStopping(monitor='val_loss', patience = 10)
learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_loss', patience=1, verbose = 1,factor = 0.2, min_lr = 1e-8, min_delta=0.0001)
# mc = ModelCheckpoint('furspect_best.h5', monitor='val_accuracy', mode='max', verbose=1)
callbacks = [earlystop, learning_rate_reduction]

# + colab={"background_save": true, "base_uri": "https://localhost:8080/"} id="heOL_u3fByuc" outputId="7d1fe0c3-f983-4f12-c058-3effc8f04c94"
from tensorflow.keras.models import load_model

# model = load_model('furspect_c.h5')
history = model.fit(train_it,
      validation_data=val_it, epochs=50, verbose=1, callbacks=[callbacks], shuffle=False)
  # 

# + colab={"base_uri": "https://localhost:8080/"} id="b7rEAfXgB0FN" outputId="a1b56593-ae5d-44a0-b4f3-db9ef79f4daf"
    # evaluate model
    _, acc = model.evaluate(val_it, steps=len(val_it), verbose=1)
    print('> %.3f' % (acc * 100.0))

# + colab={"base_uri": "https://localhost:8080/", "height": 350} id="g3lTEmXwO7U2" outputId="6fc4824b-001e-489a-cf70-f9ed8f1467cd"
from matplotlib import pyplot

loss_vals = history.history['loss']
val_loss_vals = history.history['val_loss']



epochs = range(1, len(history.history['accuracy'])+1)

f, ax = plt.subplots(nrows=1,ncols=2,figsize=(18,5))
    
ax[0].plot(epochs, loss_vals,  marker='o', label='Training Loss')
ax[0].plot(epochs, val_loss_vals, marker='o', color = 'green', label='Validation Loss')
ax[0].set_title('Training & Validation Loss')
ax[0].set_xlabel('Epochs')
ax[0].set_ylabel('Loss')
ax[0].legend(loc='best')
ax[0].grid(True)

acc_vals = history.history['accuracy']
val_acc_vals = history.history['val_accuracy']

ax[1].plot(epochs, acc_vals, marker='o', label='Training Accuracy')
ax[1].plot(epochs, val_acc_vals, marker='o', color = 'green', label='Validation Accuracy')
ax[1].set_title('Training & Validation Accuracy')
ax[1].set_xlabel('Epochs')
ax[1].set_ylabel('Accuracy')
ax[1].legend(loc='best')
ax[1].grid(True)

plt.show()
# pyplot.savefig(filename + '_plot.png')
pyplot.close()

# + colab={"base_uri": "https://localhost:8080/", "height": 372} id="pBmmjDfWjmaN" outputId="2d1fd5c3-d626-432e-98c9-df67fdc599c7"
# # !pip install scikit-image
# # !pip install opencv-python

from PIL import Image
import numpy as np
from skimage import transform
from tensorflow.keras.models import load_model
from matplotlib import pyplot as plt
import cv2

path = 'train'
folder = os.listdir(os.path.join(path))
chosen = folder[np.random.randint(0, 11)]
dir = os.path.join(path, chosen)
print(dir)
images = os.listdir(dir)
# # load all images into a list
index = np.random.randint(0, len(images))
file = images[index]

print(file)


plt.figure()
arr = cv2.imread(os.path.join(dir, file))
arr = cv2.cvtColor(arr, cv2.COLOR_BGR2RGB)

plt.imshow(arr, cmap=plt.cm.binary)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.title(f'Image: {chosen}')
plt.show()

np_image = Image.open(os.path.join(dir, file))
np_image = np.array(np_image).astype('float32')/255
np_image = transform.resize(np_image, (200, 200, 3))
np_image = np.expand_dims(np_image, axis=0)
res = model.predict(np_image, batch_size=10)
classes = ['cane', 'cavallo', 'elefante', 'farfalla', 'furry', 'gallina', 'gatto', 'mucca', 'pecora', 'ragno', 'scoiattolo']
print(res[0][:])
print(np.argmax(res[0][:]))
print(classes[(np.argmax(res[0][:]))])




# + [markdown] id="S0CM-PG0U4NU"
# **Conjoining furry directory**

# + id="s-J20hbCM9HM"
# !unzip drive/MyDrive/furry.zip

# + id="WePnBfsxVjK3"
label = 'furry'
for subdir in ['train', 'validation']:
  new = os.path.join(subdir, label)
  os.makedirs(new, exist_ok=True)

# + id="MT4GZKQNU_z9"
seed(1)
head = os.path.join(label)

for file in os.listdir(head):
  src = os.path.join(head, file)
  dst = os.path.join('train', label, file)

  if random() < 0.2:
    dst = os.path.join('validation', label, file)

  print(f'Travelling from {src} to {dst}!')
  copyfile(src, dst)

# + id="mkNKswEWtaZI"
a = 'untitled_project'
for trial in os.listdir(a):
  b = os.path.join(a, trial)
  os.rmdir(b)

# + id="IuV5hYWW51o3"
for file in os.listdir(os.path.join('tuner_dir/furspect_'))

# + [markdown] id="8APKGdP7U65V"
# ## **Analysis**

# + id="OccqW-I5OIzO"
train_classes = to_categorical(train_it.classes, num_classes=len(train_it.class_indices))

# + colab={"base_uri": "https://localhost:8080/"} id="NspwLGxnRAp2" outputId="9808f391-0784-45b3-f3ef-bf3194cb2182"
val_classes = to_categorical(val_it.classes, num_classes=len(val_it.class_indices))
val_classes

# + colab={"base_uri": "https://localhost:8080/"} id="2iRltLy2N7ft" outputId="2234d92e-c3af-49d9-c19c-6b760a42e78f"
preds = np.round(model.predict(val_it, 0))
preds

# + colab={"base_uri": "https://localhost:8080/"} id="qlLb0aRrORI1" outputId="366379bf-8dff-4488-bbde-8d8f01dd028a"
model.evaluate(val_it, steps=len(val_it), verbose=1)

# + colab={"base_uri": "https://localhost:8080/"} id="d0SjteSjNjgt" outputId="025c033a-1815-4025-f11a-f8b01c3c7a96"
from sklearn import metrics
class_report = metrics.classification_report(val_classes, preds, target_names=classes)
print(class_report)

# + id="DsrUILlsRaUJ"
import pandas as pd
categorical_val_classes = pd.DataFrame(val_classes).idxmax(axis=1)
categorical_preds = pd.DataFrame(preds).idxmax(axis=1)

# + colab={"base_uri": "https://localhost:8080/"} id="kxRJZw19ReSq" outputId="3e320eca-d980-46f2-895a-bbc410c5a26b"
cm = metrics.confusion_matrix(categorical_val_classes, categorical_preds)
cm

# + id="H0mGtswNR3BX"
import matplotlib.pyplot as plt
import itertools

def plot_cm(cm, classes,
  normalize=False,
  title="confusion_matrix",
  cmap=plt.cm.Blues):

  #Add Normalization Option
  if normalize:
    cm = cm.astype("float") / cm.sum(axis=1)[:, np.newaxis]
    print("Normalized confusion matrix")
  else:
    print("Unnormalized confusion matrix")
 
  plt.imshow(cm, interpolation="nearest", cmap=cmap)
  # plt.imshow(cm)
  plt.title(title)
  plt.colorbar()
  tick_marks = np.arange(len(classes))
  plt.xticks(tick_marks, classes, rotation=45)
  plt.yticks(tick_marks, classes)
 
  fmt = ".2f" if normalize else "d"
  thresh = cm.max() / 2.
  for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
    plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")

  plt.ylabel("True label")
  plt.xlabel("Predicted label") 


# + colab={"base_uri": "https://localhost:8080/", "height": 344} id="G3I5fuj2SEnm" outputId="2cb3885e-4e7e-4817-f617-878fa4f17546"
plot_cm(cm, classes, normalize=True)

# + id="4mVTiKFREdju"
from sklearn.metrics import classification_report, confusion_matrix, roc_curve, auc, roc_auc_score
from sklearn.preprocessing import LabelBinarizer
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sn

print('Classification Report')
target_names = list(train.class_indices.keys())
print(classification_report(train.classes, y_pred, target_names=target_names))

fpr_keras, tpr_keras, thresholds_keras = roc_curve(train.classes, y_pred)
auc_keras = auc(fpr_keras, tpr_keras)

plt.figure(1)
plt.plot([0, 1], [0, 1], 'k--')
plt.plot(fpr_keras, tpr_keras, label='area = {:.3f}'.format(auc_keras))
plt.xlabel('False positive rate')
plt.ylabel('True positive rate')
plt.title('ROC curve')
plt.legend(loc='best')
plt.show()

StarcoderdataJupyterScriptsDedupFiltered