# ---
# 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="z_nQ9lDK_0Ot"
#@title
#Tanveer_1728042
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

# + id="S7Anl7i6Agxa"
import random as rn
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, concatenate, MaxPooling2D, GlobalAveragePooling2D
from tensorflow.keras.callbacks import ModelCheckpoint, TensorBoard
from tensorflow.keras.utils import to_categorical
import os
import pathlib
import cv2
from tqdm import tqdm
import os
from zipfile import ZipFile
from PIL import Image
from os import listdir
from os.path import join
import pandas
from sklearn.model_selection import train_test_split
from sklearn.metrics import *
from sklearn import preprocessing


np.random.seed(42)

# + id="3Lf3XvMtAqjP"
# hyper-parameters
batch_size = 32

# categories of images
num_classes = 2

# number of training epochs
epochs = 100

IMG_SIZE = 100


# + id="6VziBpjNA5-V"
def load_data2():
    """This function loads dataset, normalized, and labels one-hot encoded"""
    train_data_dir = pathlib.Path('/content/drive/MyDrive/Final(8th)/Paddy Classification/Train')
    test_data_dir = pathlib.Path('/content/drive/MyDrive/Final(8th)/Paddy Classification/Test')    
    train_folders = os.listdir(train_data_dir)
    test_folders = os.listdir(test_data_dir)

    train_image_names = []
    test_image_names = []
    train_labels = []
    train_images = []
    test_labels = []
    test_images = []

    size = 100, 100

    for folder in train_folders:
        for file in os.listdir(os.path.join(train_data_dir,folder)):
            if file.endswith("jpg") or file.endswith("jpeg") or file.endswith("png") or file.endswith("JPG"):
                train_image_names.append(file)
                train_labels.append(folder)
                img = cv2.imread(os.path.join(train_data_dir,folder,file))
                im = cv2.resize(img,size)
                train_images.append(im)
            else:
                continue

    for folder in test_folders:
        for file in os.listdir(os.path.join(test_data_dir,folder)):
            if file.endswith("jpg") or file.endswith("jpeg") or file.endswith("png") or file.endswith("JPG"):
                test_image_names.append(file)
                test_labels.append(folder)
                img = cv2.imread(os.path.join(test_data_dir,folder,file))
                im = cv2.resize(img,size)
                test_images.append(im)
            else:
                continue

    X_train = np.array(train_images)
    X_test = np.array(test_images)
 
    print("Training samples:", X_train.shape[0])
    print("Testing samples:", X_test.shape[0])
    
    X_train = X_train.astype("float32")
    X_test = X_test.astype("float32")
    X_train /= 255
    X_test /= 255

    train_label_dummies = pandas.get_dummies(train_labels)
    test_label_dummies = pandas.get_dummies(test_labels)

    trainlabels = train_label_dummies.values.argmax(1)
    testlabels = test_label_dummies.values.argmax(1)

    X_train = np.array(X_train)
    y_train = np.array(trainlabels)


    X_test = np.array(X_test)
    y_test = np.array(testlabels)

    print('OK')
    return(X_train, y_train),(X_test, y_test), (train_image_names, test_image_names)


# + id="oeRu-vw7C0Qy"
def create_model():
  model = Sequential()
  model.add(Conv2D(224, (3, 3), activation='relu', input_shape=(100, 100, 3)))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25)) 

  model.add(Conv2D(64, (3, 3), activation='relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25)) 

  model.add(Conv2D(32, (3, 3), activation='relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25)) 

  model.add(Conv2D(128, (3, 3), activation='relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25))

  model.add(Conv2D(16, (3, 3), activation='relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25))  
    
  model.add(Flatten())
  model.add(Dense(500, activation='softmax'))
  model.add(Dropout(0.25)) 
  model.add(Dense(num_classes, activation='softmax'))
  model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
  return model


# + id="CYCu_g2dAgiO" colab={"base_uri": "https://localhost:8080/"} outputId="ee41b202-533b-41ab-ffbf-f9808d423c7e"
if __name__ == "__main__":

    # load the data
    (X_train, y_train), (X_test, y_test), (train_image_names, test_image_names) = load_data2()
	
    X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, stratify=y_train, test_size=0.1, random_state=1)
    #print("Validation samples:", X_val.shape[0])
    
    # construct the model
    model = create_model()
    
    # train
    model.fit(X_train, y_train,
            batch_size=batch_size,
            epochs=epochs,
            steps_per_epoch=10,
            validation_steps=100,
            validation_data=(X_val, y_val))
    validation_steps = 20
    
    # evaluate
    loss0, accuracy0 = model.evaluate(X_val, y_val, steps = 20)

    print("Validation Loss: {:.2f}".format(loss0))
    print("Validation Accuracy: {:.2f}".format(accuracy0))

# test item prediction
testLabelPredicted = model.predict(X_test)
testLabelPredicted =  np.rint(testLabelPredicted.argmax(axis=-1))
#print(test_image_names)
testLabelGold = y_test
#print(testLabelGold)

# Evaluation
results = confusion_matrix(testLabelGold, testLabelPredicted) 

# + id="ZK8k_AHjZi8s" colab={"base_uri": "https://localhost:8080/"} outputId="dfcd44d7-b7b7-4d28-d4be-66272d924f2c"
from google.colab import drive
drive.mount('/content/drive')

# + id="3EBAFUVIL5Hl" colab={"base_uri": "https://localhost:8080/"} outputId="72da3a0f-8077-46ab-9b59-777857b9dbeb"
# test item prediction
testLabelPredicted = model.predict(X_test)
testLabelPredicted =  np.rint(testLabelPredicted.argmax(axis=-1))
#print(test_image_names)
testLabelGold = y_test
#print(testLabelGold)

# Evaluation
results = confusion_matrix(testLabelGold, testLabelPredicted) 
    
print ('Confusion Matrix :')
print (results) 

print ('Recall Score :',recall_score(testLabelGold, testLabelPredicted, average='micro'))
print ('Precision Score :',precision_score(testLabelGold, testLabelPredicted, average='micro'))
print ('F1 Score :',f1_score(testLabelGold, testLabelPredicted, average='micro'))
print ('Accuracy :',accuracy_score(testLabelGold, testLabelPredicted))

# + id="DrZ90uLeeHYJ" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="d330faa7-c59c-42d3-f5be-1e16f3efbd50"
from keras.utils.vis_utils import plot_model
plot_model(model, show_shapes=True,
               to_file=os.path.join('model.png'))

# + id="jtVi4hbgecq1" colab={"base_uri": "https://localhost:8080/"} outputId="473a274b-d611-4f19-81e8-717c53771c8c"
print(model.summary())

# + id="FdC37Oxjf1ES"
labels=['Paddy','No Paddy']

# + id="Tu9KbZcxe2LP" colab={"base_uri": "https://localhost:8080/", "height": 871} outputId="edebf72d-00fe-4edd-9c9b-70f3b80febd3"
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sbn
cm = confusion_matrix(testLabelGold, testLabelPredicted, normalize='true')
df_cm = pd.DataFrame(cm, range(2), range(2))
plt.figure(figsize=(15,15))
sbn.set(font_scale=1.4) # for label size
sbn.heatmap(df_cm, annot=True, annot_kws={"size": 16}, xticklabels=labels, yticklabels=labels)

plt.show()

StarcoderdataJupyterScriptsDedupFiltered