"""
# Memuat Pustaka Dasar
"""
import pandas as pd
import numpy as np
import os
import matplotlib.pyplot as plt
import seaborn as sns
from glob import glob
from PIL import Image
np.random.seed(21)
"""
# Menentukan Direktori Data
"""
dir_jahe_train = "../input/tanaman-umbi/umbi/train/jahe"
dir_kencur_train = "../input/tanaman-umbi/umbi/train/kencur"
dir_kunci_train = "../input/tanaman-umbi/umbi/train/kunci"
dir_kunyit_train = "../input/tanaman-umbi/umbi/train/kunyit"
dir_laos_train = "../input/tanaman-umbi/umbi/train/laos"

dir_jahe_test = "../input/tanaman-umbi/umbi/tests/jahe"
dir_kencur_test = "../input/tanaman-umbi/umbi/tests/kencur"
dir_kunci_test = "../input/tanaman-umbi/umbi/tests/kunci"
dir_kunyit_test = "../input/tanaman-umbi/umbi/tests/kunyit"
dir_laos_test = "../input/tanaman-umbi/umbi/tests/laos"

read = lambda imname: np.asarray(Image.open(imname).convert("RGB"))
"""
# Memuat Citra Latih
"""
img_jahe = [read(os.path.join(dir_jahe_train, filename)) for filename in os.listdir(dir_jahe_train)]
X_jahe_train = np.array(img_jahe, dtype='uint8')

img_kencur = [read(os.path.join(dir_kencur_train, filename)) for filename in os.listdir(dir_kencur_train)]
X_kencur_train = np.array(img_kencur, dtype='uint8')

img_kunci = [read(os.path.join(dir_kunci_train, filename)) for filename in os.listdir(dir_kunci_train)]
X_kunci_train = np.array(img_kunci, dtype='uint8')

img_kunyit = [read(os.path.join(dir_kunyit_train, filename)) for filename in os.listdir(dir_kunyit_train)]
X_kunyit_train = np.array(img_kunyit, dtype='uint8')

img_laos = [read(os.path.join(dir_laos_train, filename)) for filename in os.listdir(dir_laos_train)]
X_laos_train = np.array(img_laos, dtype='uint8')
"""
# Memuat Citra Tes
"""
img_jahe = [read(os.path.join(dir_jahe_test, filename)) for filename in os.listdir(dir_jahe_test)]
X_jahe_test = np.array(img_jahe, dtype='uint8')

img_kencur = [read(os.path.join(dir_kencur_test, filename)) for filename in os.listdir(dir_kencur_test)]
X_kencur_test = np.array(img_kencur, dtype='uint8')

img_kunci = [read(os.path.join(dir_kunci_test, filename)) for filename in os.listdir(dir_kunci_test)]
X_kunci_test = np.array(img_kunci, dtype='uint8')

img_kunyit = [read(os.path.join(dir_kunyit_test, filename)) for filename in os.listdir(dir_kunyit_test)]
X_kunyit_test = np.array(img_kunyit, dtype='uint8')

img_laos = [read(os.path.join(dir_laos_test, filename)) for filename in os.listdir(dir_laos_test)]
X_laos_test = np.array(img_laos, dtype='uint8')
"""
# Membuat Label
"""
y_jahe_train = np.zeros(X_jahe_train.shape[0]) 
y_kencur_train = np.zeros(X_kencur_train.shape[0]) + 1
y_kunci_train = np.zeros(X_kunci_train.shape[0]) + 2
y_kunyit_train = np.zeros(X_kunyit_train.shape[0]) + 3
y_laos_train = np.zeros(X_laos_train.shape[0]) + 4
y_jahe_test = np.zeros(X_jahe_test.shape[0]) 
y_kencur_test = np.zeros(X_kencur_test.shape[0]) + 1
y_kunci_test = np.zeros(X_kunci_test.shape[0]) + 2
y_kunyit_test = np.zeros(X_kunyit_test.shape[0]) + 3
y_laos_test = np.zeros(X_laos_test.shape[0]) + 4
"""
# Menggabungkan Data
"""
X_train = np.concatenate((X_jahe_train, X_kencur_train, X_kunci_train, X_kunyit_train, X_laos_train), axis = 0)
y_train = np.concatenate((y_jahe_train, y_kencur_train, y_kunci_train, y_kunyit_train, y_laos_train), axis = 0)

X_test = np.concatenate((X_jahe_test, X_kencur_test, X_kunci_test, X_kunyit_test, X_laos_test), axis = 0)
y_test = np.concatenate((y_jahe_test, y_kencur_test, y_kunci_test, y_kunyit_test, y_laos_test), axis = 0)
"""
# Mengacak Urutan Data
"""
s = np.arange(X_train.shape[0])
np.random.shuffle(s)
X_train = X_train[s]
y_train = y_train[s]

s = np.arange(X_test.shape[0])
np.random.shuffle(s)
X_test = X_test[s]
y_test = y_test[s]
"""
# Menampilkan 15 Citra Pertama
"""
w=40
h=30
fig=plt.figure(figsize=(12, 8))
columns = 5
rows = 3

for i in range(1, columns*rows +1):
  ax = fig.add_subplot(rows, columns, i)
  if y_train[i] == 0:
    ax.title.set_text('Jahe')
  elif y_train[i] == 1:
    ax.title.set_text('Kencur')    
  elif y_train[i] == 2:
    ax.title.set_text('Kunci')
  elif y_train[i] == 3:
    ax.title.set_text('Kunyit')        
  else:
    ax.title.set_text('Laos')

  plt.imshow(X_train[i], interpolation='nearest')
plt.show()
"""
# Mengkodekan Label
"""
import keras
from keras.utils.np_utils import to_categorical 

y_train = to_categorical(y_train, num_classes=5)
y_test = to_categorical(y_test, num_classes=5)
"""
# Normalisasi Data
"""
X_train = X_train/255
X_test = X_test/255
"""
# Membuat Model CNN Sederhana
"""
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from tensorflow.keras.optimizers import Adam, RMSprop
from keras.callbacks import ReduceLROnPlateau

def CNN_Model(input_shape = (300, 300, 3), num_classes=5):
    
    model = Sequential()
    model.add(Conv2D(64, kernel_size=(3,3), padding='Same', input_shape = input_shape, activation='relu', kernel_initializer = 'glorot_uniform'))
    model.add(Dropout(0.25)) 
        
    model.add(MaxPool2D(pool_size=(2,2)))      
    model.add(Flatten())
    model.add(Dense(128, activation='relu', kernel_initializer='normal'))
    
    model.add(Dense(num_classes, activation = 'softmax'))
    
    model.summary()
    
    optimizer= Adam(learning_rate=0.001)     
    model.compile(optimizer = optimizer, loss='categorical_crossentropy', metrics=["accuracy"])
    
    return model   

model = CNN_Model()
"""
# Melatih Model
"""
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', 
                                            patience=3, 
                                            verbose=1, 
                                            factor=0.5, 
                                            min_lr=0.00001)
history = model.fit(X_train, 
                    y_train, 
                    validation_split=0.2, 
                    epochs=50, callbacks=[learning_rate_reduction],
                    verbose=1)
"""
# Akurasi Model
"""
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])

plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epochs')

plt.legend(['Train', 'Val'], loc='lower right')
plt.show()
"""
# Loss Model
"""
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])

plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epochs')

plt.legend(['Train', 'Val'], loc='upper right')
plt.show()