"""
# Import Library
"""
# 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 in 

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 "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

from keras.preprocessing.image import ImageDataGenerator, load_img
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import random

import os
print(os.listdir("../input"))

# Any results you write to the current directory are saved as output.
"""
# Prepare Traning Data
"""
filenames = os.listdir("../input/train/train")
categories = []
for filename in filenames:
    category = filename.split('.')[0]
    if category == 'dog':
        categories.append(1)
    else:
        categories.append(0)

df = pd.DataFrame({
    'filename': filenames,
    'category': categories
})
df.head()
"""
# See sample image
"""
sample = random.choice(filenames)
image = load_img("../input/train/train/"+sample)
plt.imshow(image)
"""
# Build Model
[Reference](https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html)
"""
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Activation, Dropout, Flatten, Dense

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(256, 256, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# the model so far outputs 3D feature maps (height, width, features)

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

model.summary()
"""
### Prepare Test and Train Data
"""
train_df, validate_df = train_test_split(df, test_size=0.1)
train_df = train_df.reset_index()
validate_df = validate_df.reset_index()

# validate_df = validate_df.sample(n=10).reset_index() # use for fast testing code purpose
# train_df = train_df.sample(n=100).reset_index() # use for fast testing code purpose

total_train = train_df.shape[0]
batch_size=15
"""
# Traning Generator
"""
train_datagen = ImageDataGenerator(
    horizontal_flip=True,
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    rotation_range=20
)

train_generator = train_datagen.flow_from_dataframe(
    train_df, 
    "../input/train/train/", 
    x_col='filename',
    y_col='category',
    class_mode='binary',
    batch_size=batch_size
)
"""
### Validation Generator
"""
validation_datagen = ImageDataGenerator(rescale = 1./255)
validation_generator = validation_datagen.flow_from_dataframe(
    validate_df, 
    "../input/train/train/", 
    x_col='filename',
    y_col='category',
    class_mode='binary',
    batch_size=batch_size
)
"""
# See sample generated images
"""
plt.figure(figsize=(12, 12))
for X_batch, y_batch in train_generator:
    for i in range(0, 9):
        plt.subplot(3, 3, i+1)
        image = X_batch[i]
        plt.imshow(image)
    plt.tight_layout()
    plt.show()
    break
"""
# Fit Model
"""
model.fit_generator(
    train_generator, 
    epochs=30,
    validation_data=validation_generator,
    steps_per_epoch=total_train//batch_size
)
"""
# Save Model
"""
model.save_weights("model.h5")
"""
# Prepare Testing Data
"""
test_filenames = os.listdir("../input/test1/test1")
test_df = pd.DataFrame({
    'filename': test_filenames
})
# test_df = test_df.sample(n=10).reset_index() 
nb_samples = test_df.shape[0]

"""
# Create Testing Generator
"""
test_gen = ImageDataGenerator(rescale=1./255)
test_generator = test_gen.flow_from_dataframe(
    test_df, 
    "../input/test1/test1/", 
    x_col='filename',
    class_mode=None,
    batch_size=batch_size,
    shuffle=False
)
"""
# Predict
"""
predict = model.predict_generator(test_generator, steps=np.ceil(nb_samples/batch_size)).astype('int64')
test_df['category'] = predict
sample_test = test_df.sample(n=9).reset_index()
sample_test.head()
plt.figure(figsize=(12, 12))
for index, row in sample_test.iterrows():
    filename = row['filename']
    category = row['category']
    img = load_img("../input/test1/test1/"+filename, target_size=(256, 256))
    plt.subplot(3, 3, index+1)
    plt.imshow(img)
    plt.xlabel(filename + '(' + "{}".format(category) + ')')
plt.tight_layout()
plt.show()
"""
# Submission
"""
submission_df = test_df.copy()
submission_df['id'] = submission_df['filename'].str.split('.').str[0]
submission_df['label'] = submission_df['category']
submission_df.drop(['filename', 'category'], axis=1, inplace=True)
submission_df.to_csv('submission.csv', index=False)