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

# + colab={} colab_type="code" id="NX8qPBwYbSHx"
#Essential libraries
import numpy as np
import h5py
import matplotlib.pyplot as plt

#Deep learning framework
import tensorflow as tf
from tensorflow import keras

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense , Conv2D , Flatten ,MaxPool2D

# + colab={"base_uri": "https://localhost:8080/", "height": 35} colab_type="code" id="xu6GjbeOb59U" outputId="44035818-343c-4520-af66-8ab7cd894ab1"
tf.__version__


# + colab={} colab_type="code" id="V7ZkWSp1496D"
# %cd /content/drive/My Drive/datasets
# ls

# + colab={} colab_type="code" id="226tjqqShfNb"
#Utility Functions

def load_dataset():
    train_dataset = h5py.File('train_signs.h5', "r")
    train_set_x_orig = np.array(train_dataset["train_set_x"][:]) # your train set features
    train_set_y_orig = np.array(train_dataset["train_set_y"][:]) # your train set labels

    test_dataset = h5py.File('test_signs.h5', "r")
    test_set_x_orig = np.array(test_dataset["test_set_x"][:]) # your test set features
    test_set_y_orig = np.array(test_dataset["test_set_y"][:]) # your test set labels

    classes = np.array(test_dataset["list_classes"][:]) # the list of classes
    
    train_set_y_orig = train_set_y_orig.reshape((1, train_set_y_orig.shape[0]))
    test_set_y_orig = test_set_y_orig.reshape((1, test_set_y_orig.shape[0]))
    
    return train_set_x_orig, train_set_y_orig, test_set_x_orig, test_set_y_orig, classes



def convert_to_one_hot(Y, C):
    Y = np.eye(C)[Y.reshape(-1)].T
    return Y


# + colab={} colab_type="code" id="h50C6ddeimH1"
X_train_orig, Y_train_orig, X_test_orig, Y_test_orig, classes = load_dataset()

# + colab={"base_uri": "https://localhost:8080/", "height": 126} colab_type="code" id="ULAju10jzg1O" outputId="89cd5ce8-3755-4dab-83fc-247d76eefb44"
X_train = X_train_orig/255.
X_test = X_test_orig/255.
Y_train = convert_to_one_hot(Y_train_orig, 6).T
Y_test = convert_to_one_hot(Y_test_orig, 6).T
print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))
conv_layers = {}

# + colab={"base_uri": "https://localhost:8080/", "height": 286} colab_type="code" id="Ur_pUhEoij8L" outputId="0ee231ca-963e-4c72-a712-3275493450ca"
# Example of a picture
index = 6
plt.imshow(X_train_orig[index])
print ("y = " + str(np.squeeze(Y_train_orig[:, index])))

# + colab={} colab_type="code" id="WtN3_lzxilPt"
model = Sequential()
model.add(Conv2D(32, input_shape=(64, 64, 3), kernel_size=3,
                 padding = 'same',activation='relu'))
model.add(MaxPool2D(pool_size = (8,8),strides = (8,8),padding = 'same'))

model.add(Conv2D(16, kernel_size = 3,activation='relu'))
model.add(MaxPool2D(pool_size=(4,4),strides = (4,4) ,padding = "same"))
model.add(Flatten())
model.add(Dense(6,activation='softmax'))

model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])

# + colab={"base_uri": "https://localhost:8080/", "height": 381} colab_type="code" id="8ob5wKDTtUAR" outputId="2f31ffe8-bb6a-4136-c500-ca5f79d5782a"
model.summary()

# + colab={} colab_type="code" id="-orsm7Y8JIKf"
import os
checkpoint_path = "/content/chk/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)

# + colab={"base_uri": "https://localhost:8080/", "height": 35} colab_type="code" id="14WPE2edJL3Z" outputId="0c60c2cb-a8fc-4632-9099-050e4ec86587"
checkpoint_dir

# + colab={} colab_type="code" id="6kDcMXMKHy1T"
from tensorflow.keras.callbacks import EarlyStopping,ModelCheckpoint

#checkpoint path /content/chk


earlystopping = EarlyStopping(monitor='val_loss',patience=5)
modelcheckpoint = ModelCheckpoint(filepath= checkpoint_path,save_weights_only=True,verbose=1)

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} colab_type="code" id="q3rOaML6-kpD" outputId="dfbc66da-b6f4-413d-8eaf-795a34b2cd63"
history = model.fit(X_train,Y_train,epochs=100,validation_data= (X_test,Y_test),callbacks=[modelcheckpoint])

# + colab={} colab_type="code" id="tqYjpo4fxsGg"
model.evaluate(X_train,Y_train)


# + colab={} colab_type="code" id="1CWHojOb-Cxc"
def plot_loss(loss,val_loss):
  plt.figure()
  plt.plot(loss)
  plt.plot(val_loss)
  plt.title('Model loss')
  plt.ylabel('Loss')
  plt.xlabel('Epoch')
  plt.legend(['Train', 'Test'], loc='upper right')
  plt.show()

def plot_accuracy(acc,val_acc):
  plt.figure()
  plt.plot(acc)
  plt.plot(val_acc)
  plt.title('Model accuracy')
  plt.ylabel('acc')
  plt.xlabel('Epoch')
  plt.legend(['Train','Test'],loc='upper right')
  plt.show()


# + colab={"base_uri": "https://localhost:8080/", "height": 295} colab_type="code" id="sdq6tfo0Bp8l" outputId="fa543aed-6259-4176-af9f-1f22fe028b9e"
# Plot train vs test loss during training
plot_loss(history.history['loss'], history.history['val_loss'])

# + colab={"base_uri": "https://localhost:8080/", "height": 295} colab_type="code" id="UWfRLofnB7RT" outputId="7da9d1b6-7592-4699-919d-96e0b91fabd6"
# Plot train vs test accuracy during training
plot_accuracy(history.history['accuracy'],history.history['val_accuracy'])

# + colab={"base_uri": "https://localhost:8080/", "height": 295} colab_type="code" id="e3_ix8NSCBdP" outputId="882b460f-cf82-41b9-8562-d1cb18eed99c"
# Plot train vs test accuracy during training
plot_accuracy(history.history['accuracy'],history.history['val_accuracy'])

# + colab={} colab_type="code" id="oVponRJ7EXUA"
#to save model
# !pip install -q pyyaml h5py

# + colab={} colab_type="code" id="36MJ0MFsFKS5"
pred = model.predict(X_test)

p = [np.argmax(preds) for preds in pred]

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} colab_type="code" id="piyun-m5Ha3F" outputId="500af7be-21c0-4dd0-d4da-526b7e5db104"
p


# + colab={} colab_type="code" id="hFsxy-2TJsDl"
def create_model():
  model = Sequential()
  model.add(Conv2D(32, input_shape=(64, 64, 3), kernel_size=3,
                 padding = 'same',activation='relu'))
  model.add(MaxPool2D(pool_size = (8,8),strides = (8,8),padding = 'same'))

  model.add(Conv2D(16, kernel_size = 3,activation='relu'))
  model.add(MaxPool2D(pool_size=(4,4),strides = (4,4) ,padding = "same"))
  model.add(Flatten())
  model.add(Dense(6,activation='softmax'))

  model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=['accuracy'])

  return model


# + colab={} colab_type="code" id="_o5-VXhWJvLZ"
Model = create_model()

# + colab={"base_uri": "https://localhost:8080/", "height": 381} colab_type="code" id="ZTD8Md6RLa2R" outputId="91bc8204-3f02-452a-9438-01ae065df521"
Model.summary()

# + colab={"base_uri": "https://localhost:8080/", "height": 35} colab_type="code" id="tqvbpyzKL4Bt" outputId="86dfcaa8-e718-4ca4-9f5e-09f6351cbe2e"
latest = tf.train.latest_checkpoint(checkpoint_dir)
latest

# + colab={"base_uri": "https://localhost:8080/", "height": 35} colab_type="code" id="AmSk2J_JLcyK" outputId="accaa318-250e-44c7-8b7e-648d4a0e890d"
Model.load_weights(latest)

# + colab={"base_uri": "https://localhost:8080/", "height": 342} colab_type="code" id="_RNwHHg9MHPF" outputId="ccceb6db-10f6-4c34-c689-407ec3612c79"
Model.evaluate(X_test,Y_test)

# + colab={} colab_type="code" id="JLGXpfw_MPXj"
model.save_weights('/content/cdir/my_checkpoint')

# + colab={} colab_type="code" id="sO2Kq-VYNLYi"
M = create_model()

# + colab={"base_uri": "https://localhost:8080/", "height": 381} colab_type="code" id="iBye96yQNU9e" outputId="3c57afe9-f9b5-40d5-e415-97bd07894a9b"
M.summary()

# + colab={"base_uri": "https://localhost:8080/", "height": 35} colab_type="code" id="rchGa06vNWpC" outputId="0a10ce64-979d-4f91-ce16-fd1012568afa"
M.load_weights('/content/cdir/my_checkpoint')

# + colab={"base_uri": "https://localhost:8080/", "height": 54} colab_type="code" id="R4PkdLmqNrM0" outputId="869ad0a5-8bbd-4ff2-d752-15c16cc6e07d"
loss,acc = model.evaluate(X_test,  Y_test, verbose=2)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))

# + colab={"base_uri": "https://localhost:8080/", "height": 35} colab_type="code" id="j5-LAJ74OEKm" outputId="50b92d03-be5d-4b2f-d0a1-bc839b7cbbca"
# ls

# + colab={} colab_type="code" id="UGuXVhafOogJ"
# !pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive 
from google.colab import auth 
from oauth2client.client import GoogleCredentials

# + colab={} colab_type="code" id="9UB-216LO5F3"
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()                       
drive = GoogleDrive(gauth)

# + colab={} colab_type="code" id="5HAYEPGlO_UA"
model.save('model.h5')
model_file = drive.CreateFile({'title' : 'model.h5'})                       
model_file.SetContentFile('model.h5')                       
model_file.Upload()

# + colab={"base_uri": "https://localhost:8080/", "height": 35} colab_type="code" id="MjeRQxLtPuIZ" outputId="bae45110-3ea9-403f-cd5f-2fee480587ac"
# download to google drive                       
drive.CreateFile({'id': model_file.get('id')})

# + colab={} colab_type="code" id="VoI7abZHP0-p"


StarcoderdataJupyterScriptsDedupFiltered