# basic imports
import numpy as np
#import matplotlib.pyplot as plt
#import os
#import cv2
import pickle
import tensorflow as tf
from tensorflow.keras.models import Sequential
import tensorflow.keras.layers as tfl
#from tensorflow.keras.callbacks import TensorBoard, LearningRateScheduler, EarlyStopping
#import time


#model name
#NAME="devnagri-{}".format(int(time.time()))

# TensorBoard
#tensorboard=TensorBoard(log_dir='logs/{}'.format(NAME))

# GPU
gpu_options=tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=0.333)
sess=tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))

# load the directory where image(or dataset) folders are

DATADIR='C:/IDK/ML/Devnagri/DevanagariHandwrittenCharacterDataset/Train'
CATEGORIES=["0","1","2","3","4","5","6","7","8","9",
           "adna","ba","bha","cha","chha","chhya","da","daa","dha","dhaa","ga",
           "gha","gya","ha","ja","jha","ka","kha","kna","ksha","la","ma","na",
           "pa","pha","ra","sa","sh","t","ta","tha","thaa","tra","waw","yaw","yna"]


X=pickle.load(open("X.pickle","rb"))
y=pickle.load(open("y.pickle","rb"))


#X=np.array(X).reshape((-1,32,32,1))
y=np.array(y)
y=tf.keras.utils.to_categorical(y, num_classes=46, dtype='float32')


#building the model

model=Sequential()
# layer 1
model.add(tfl.Conv2D(64,(3,3)))
model.add(tfl.Activation('relu'))
model.add(tfl.MaxPooling2D(pool_size=(2,2)))
#layer 2
model.add(tfl.Conv2D(64,(3,3)))
model.add(tfl.Activation('relu'))
model.add(tfl.MaxPooling2D(pool_size=(2,2)))
#layer 3
model.add(tfl.Conv2D(64,(3,3)))
model.add(tfl.Activation('relu'))
model.add(tfl.MaxPooling2D(pool_size=(2,2)))
# Dense 1
model.add(tfl.Flatten())
model.add(tfl.Dense(64))
model.add(tfl.Activation('relu'))
# Dense 2
model.add(tfl.Flatten())
model.add(tfl.Dense(64))
model.add(tfl.Activation('relu'))
# o/p layer
model.add(tfl.Dense(46))        # 1 for binary class
model.add(tfl.Activation('softmax'))

# compilation, loss, accuracy, optimizer
model.compile(loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
             optimizer=tf.keras.optimizers.Adam(),metrics=['accuracy','AUC'])


# early=tf.keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0.0000001, patience=5)

# fitting
model.fit(X,y,validation_split=0.1,batch_size=200,epochs=10)#,callbacks=[tensorboard,early])

model.save('devnagri-script-detection.model')

tensorflow.compat.v1.ConfigProto tensorflow.keras.layers.Activation tensorflow.compat.v1.GPUOptions tensorflow.keras.losses.CategoricalCrossentropy tensorflow.keras.layers.Dense tensorflow.keras.layers.Conv2D tensorflow.keras.layers.MaxPooling2D tensorflow.keras.optimizers.Adam numpy.array tensorflow.keras.models.Sequential tensorflow.keras.utils.to_categorical tensorflow.keras.layers.Flatten
[(21, 'tensorflow.compat.v1.GPUOptions', 'tf.compat.v1.GPUOptions', ([], {'per_process_gpu_memory_fraction': '(0.333)'}), True, 'import tensorflow as tf\n'), (38, 'numpy.array', 'np.array', (['y'], {}), True, 'import numpy as np\n'), (39, 'tensorflow.keras.utils.to_categorical', 'tf.keras.utils.to_categorical', (['y'], {'num_classes': '(46)', 'dtype': '"""float32"""'}), True, 'import tensorflow as tf\n'), (44, 'tensorflow.keras.models.Sequential', 'Sequential', ([], {}), False, 'from tensorflow.keras.models import Sequential\n'), (46, 'tensorflow.keras.layers.Conv2D', 'tfl.Conv2D', (['(64)', '(3, 3)'], {}), True, 'import tensorflow.keras.layers as tfl\n'), (47, 'tensorflow.keras.layers.Activation', 'tfl.Activation', (['"""relu"""'], {}), True, 'import tensorflow.keras.layers as tfl\n'), (48, 'tensorflow.keras.layers.MaxPooling2D', 'tfl.MaxPooling2D', ([], {'pool_size': '(2, 2)'}), True, 'import tensorflow.keras.layers as tfl\n'), (50, 'tensorflow.keras.layers.Conv2D', 'tfl.Conv2D', (['(64)', '(3, 3)'], {}), True, 'import tensorflow.keras.layers as tfl\n'), (51, 'tensorflow.keras.layers.Activation', 'tfl.Activation', (['"""relu"""'], {}), True, 'import tensorflow.keras.layers as tfl\n'), (52, 'tensorflow.keras.layers.MaxPooling2D', 'tfl.MaxPooling2D', ([], {'pool_size': '(2, 2)'}), True, 'import tensorflow.keras.layers as tfl\n'), (54, 'tensorflow.keras.layers.Conv2D', 'tfl.Conv2D', (['(64)', '(3, 3)'], {}), True, 'import tensorflow.keras.layers as tfl\n'), (55, 'tensorflow.keras.layers.Activation', 'tfl.Activation', (['"""relu"""'], {}), True, 'import tensorflow.keras.layers as tfl\n'), (56, 'tensorflow.keras.layers.MaxPooling2D', 'tfl.MaxPooling2D', ([], {'pool_size': '(2, 2)'}), True, 'import tensorflow.keras.layers as tfl\n'), (58, 'tensorflow.keras.layers.Flatten', 'tfl.Flatten', ([], {}), True, 'import tensorflow.keras.layers as tfl\n'), (59, 'tensorflow.keras.layers.Dense', 'tfl.Dense', (['(64)'], {}), True, 'import tensorflow.keras.layers as tfl\n'), (60, 'tensorflow.keras.layers.Activation', 'tfl.Activation', (['"""relu"""'], {}), True, 'import tensorflow.keras.layers as tfl\n'), (62, 'tensorflow.keras.layers.Flatten', 'tfl.Flatten', ([], {}), True, 'import tensorflow.keras.layers as tfl\n'), (63, 'tensorflow.keras.layers.Dense', 'tfl.Dense', (['(64)'], {}), True, 'import tensorflow.keras.layers as tfl\n'), (64, 'tensorflow.keras.layers.Activation', 'tfl.Activation', (['"""relu"""'], {}), True, 'import tensorflow.keras.layers as tfl\n'), (66, 'tensorflow.keras.layers.Dense', 'tfl.Dense', (['(46)'], {}), True, 'import tensorflow.keras.layers as tfl\n'), (67, 'tensorflow.keras.layers.Activation', 'tfl.Activation', (['"""softmax"""'], {}), True, 'import tensorflow.keras.layers as tfl\n'), (22, 'tensorflow.compat.v1.ConfigProto', 'tf.compat.v1.ConfigProto', ([], {'gpu_options': 'gpu_options'}), True, 'import tensorflow as tf\n'), (70, 'tensorflow.keras.losses.CategoricalCrossentropy', 'tf.keras.losses.CategoricalCrossentropy', ([], {'from_logits': '(True)'}), True, 'import tensorflow as tf\n'), (71, 'tensorflow.keras.optimizers.Adam', 'tf.keras.optimizers.Adam', ([], {}), True, 'import tensorflow as tf\n')]