# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)



# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))
        break

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
"""
# Lib Imports
"""
from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
from keras.preprocessing.image import ImageDataGenerator

import glob

from keras.applications import Xception
from keras.applications.xception import preprocess_input
"""
# Random Image Show
"""
im_frame = Image.open('../input/expertclass/Trainset/Trainset/Hoegaarden/Hoegaarden_309.png')
np_frame = np.array(im_frame)

plt.imshow(np_frame)
"""
# Loading functions
"""
import fnmatch
from tqdm import tqdm

def get_img_paths(root_paths, format_ = 'png'):
    if root_paths.__class__ not in (list, tuple, set):
        root_paths = [root_paths]
    
    paths = []
    labels = []
    for path in root_paths:
        p, l = _get_img_paths(path, format_)
        paths += p
        labels += l
    
    paths_df = pd.DataFrame({'filename':paths, 'class':labels})
    return paths_df

def _get_img_paths(root_path, format_ = 'png'):
    paths = []
    labels = []
    for dirname, _, filenames in os.walk(root_path):
        for filename in fnmatch.filter(filenames, f'*.{format_}'):
            paths.append(os.path.join(dirname, filename))
            labels.append(dirname.split('/')[-1])
    return paths, labels

def load_images(paths):
    imgs = []
    img_labels = []
    for path in tqdm(paths):    
        imgs.append(np.array(Image.open(path)))
        img_labels.append(path.split('/')[-2])
    
    return tuple(imgs), tuple(img_labels)
#load all data in order to make k-fold with entire data (train + test)
train_path = '../input/expertclass/Trainset/Trainset/'
test_path = '../input/expertclass/Testset/Testset/'
load_images_df = get_img_paths([train_path, test_path])
load_images_df
def img_to_array(load_images_df,augmentation_factor = 2, augment = True):    
    
    if augment == False:
        augmentation_factor = 1
        train_datagen = ImageDataGenerator(preprocessing_function = preprocess_input)
    else:
        train_datagen = ImageDataGenerator(
            channel_shift_range = 10,
            shear_range = 0.2,
            zoom_range = 0.2,
            horizontal_flip = False,
            rotation_range = 45,
            vertical_flip = False,
            preprocessing_function = preprocess_input,    
        )

    batch_gen = train_datagen.flow_from_dataframe(
        load_images_df,
        #color_mode = 'grayscale',
        target_size = (299,299),
        shuffle = False,
        class_mode = 'raw',
        batch_size = len(load_images_df) + 1
    )


    X, y = [], []
    for _ in range(augmentation_factor):
        X_, y_ = batch_gen[0]
        X.append(X_)
        y.append(y_.flatten())

    X, y = np.vstack(X), np.hstack(y)
    return X, y
"""
# Create Visual Features
"""
feature_extractor = Xception(
    include_top=False,
    weights="imagenet",
    input_tensor=None,
    pooling='avg',
    classifier_activation="softmax",
    input_shape = (299,299,3)
)
X, y = img_to_array(load_images_df,augmentation_factor = 3, augment = False)
i = np.random.choice(range(len(X)))
print(y[i])
plt.imshow(X[i])
X_features = feature_extractor.predict(X)
X_features = X_features.reshape(X_features.shape[0], np.prod(X_features.shape[1:]))
import sklearn
sklearn.__version__
"""
# Modelling
"""
from sklearn.decomposition import PCA, KernelPCA
from sklearn.ensemble import RandomForestClassifier, StackingClassifier, AdaBoostClassifier, BaggingClassifier
from sklearn.pipeline import Pipeline
from sklearn.model_selection import RandomizedSearchCV
from sklearn.preprocessing import QuantileTransformer, StandardScaler, RobustScaler, MinMaxScaler
from sklearn.neural_network import MLPClassifier
from sklearn.utils.class_weight import compute_sample_weight
from sklearn.metrics import log_loss

import matplotlib.pyplot as plt
pca = PCA(1000).fit(X_features)
plt.plot(np.arange(0, len(pca.explained_variance_ratio_)),pca.explained_variance_ratio_.cumsum())
plt.title('cumulative explained variance vs n_components')
print(np.argmin(abs(pca.explained_variance_ratio_.cumsum() - 0.8)))
print(f'explained variance: {pca.explained_variance_ratio_.sum()}')
del pca
dnn = MLPClassifier(
    (64,), verbose = 0, validation_fraction = 0.2, tol = 1e-5, early_stopping = True, learning_rate = 'constant',n_iter_no_change = 15, learning_rate_init = 5e-3, alpha = 2e-4, max_iter = 200, batch_size = 'auto')

final_estimator = BaggingClassifier(dnn, n_estimators = 50, max_samples = 1.0, max_features = 1.0)

model_pipeline = Pipeline(
    steps = [                
        ('scaler2', StandardScaler()),        
        ('model', final_estimator)
    ]

)
sample_weight = compute_sample_weight('balanced',y)
model_pipeline.fit(X_features, y)
preds_path = '../input/expertclass/Prediction/Prediction/'
X_pred, _ = img_to_array(get_img_paths(preds_path), augment = False)
X_pred_features = feature_extractor.predict(X_pred)
"""
## Check Greatest losses
"""
train_proba_preds = model_pipeline.predict_proba(X_features)
train_preds = model_pipeline.predict(X_features)
losses = np.array([log_loss(y[i:i+1],train_proba_preds[i:i+1], labels = model_pipeline.classes_) for i in range(len(y))])

indexes = np.argsort(losses,)[::-1]
n = 20
fig,ax = plt.subplots(np.ceil(n/5).astype(int),5)
for i in range(n):
    ax[i//5, i%5].imshow(X[indexes[i]])
    ax[i//5, i%5].set_title(y[indexes[i]] +'-' + train_preds[indexes[i]] +' '+ str(round(np.max(train_proba_preds[indexes[i]])*100, 2)) + '%')
plt.subplots_adjust(left = -2, bottom = -3)
print('Greatest losses ("True label" - "Predicted label" "predicted proba")')
"""
## Make predictions
"""
preds = model_pipeline.predict(X_pred_features)
proba_preds = model_pipeline.predict_proba(X_pred_features)

fig,ax = plt.subplots(8,5)
for i in range(X_pred.shape[0]):
    ax[i//5, i%5].imshow(X_pred[i])
    ax[i//5, i%5].set_title(preds[i] +' '+ str(round(np.max(proba_preds[i])*100, 2)) + '%')
plt.subplots_adjust(left = -2, bottom = -3)
name_mapper = {
    'Budweiser':0,
    'Hoegaarden':1,
    'Leffe':2,
    'Stella':3,
    'Others':4    
}

#sort submissions
ids = (
    get_img_paths(preds_path)['filename']
    .apply(
        lambda x: int((x.split('/')[-1].split('.')[0])))
    .values
)

pd.DataFrame({'ID':ids,'Class':preds}).replace(name_mapper).set_index('ID').to_csv(f'submission_{pd.to_datetime("now").date()}.csv')