"""
I am running it for one epoch only. Just due to time constraint.
"""
import numpy as np  # Data manipulation
import pandas as pd # Dataframe manipulation 
import matplotlib.pyplot as plt # Plotting the data and the results
import matplotlib.image as mpimg # For displaying imagees
%matplotlib inline
from keras import models
from keras import layers
import keras.preprocessing  as kp
from keras.preprocessing.image import ImageDataGenerator
from keras import regularizers
from keras import optimizers
train_datagen = ImageDataGenerator( # Data Augumentation for test data
rescale=1./255,
rotation_range=30,
shear_range=0.3,
zoom_range=0.3
)

test_datagen = ImageDataGenerator(rescale=1./255)
train_gen=train_datagen.flow_from_directory('../input/gender-recognition-200k-images-celeba/Dataset/Train',
                                            target_size=(250,250),
                                            batch_size=48,
                                            class_mode='binary')
valid_gen=test_datagen.flow_from_directory('../input/gender-recognition-200k-images-celeba/Dataset/Validation',
                                           target_size=(250,250),
                                           batch_size=48,
                                           class_mode='binary')
from keras.applications import MobileNet
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D


base_model=MobileNet(weights='imagenet',include_top=False) #imports the mobilenet model and discards the last 1000 neuron layer.

x=base_model.output
x=GlobalAveragePooling2D()(x)
x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.
x=Dense(1024,activation='relu')(x) #dense layer 2
x=Dense(512,activation='relu')(x) #dense layer 3
preds=Dense(1,activation='softmax')(x)



# train the model on the new data for a few epochs

model=Model(inputs=base_model.input,outputs=preds)
for i,layer in enumerate(model.layers):
  print(i,layer.name)
for layer in model.layers:
    layer.trainable=False
# or if we want to set the first 20 layers of the network to be non-trainable
for layer in model.layers[:20]:
    layer.trainable=False
for layer in model.layers[20:]:
    layer.trainable=True
# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy',metrics=['acc'])
step_size_train=train_gen.n//train_gen.batch_size
history=model.fit_generator(generator=train_gen,
                   steps_per_epoch=step_size_train,
                   epochs=1,validation_data=valid_gen,validation_steps=50)
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'ro', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()

plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'ro', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.figure()
test_datagen1 = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen1.flow_from_directory(
'../input/gender-recognition-200k-images-celeba/Dataset/Test',
target_size=(150,150),
batch_size=64,
class_mode='binary')
fig,ax=plt.subplots(ncols=2,nrows=4,figsize=(20,20))
img1 = mpimg.imread('../input/gender-recognition-200k-images-celeba/Dataset/Test/Female/160003.jpg')
ax[0][0].imshow(img1)
ax[0][0].set_title("Dataset we trained and tested on.")
img2 = mpimg.imread('../input/gender-classification-dataset/Training/female/131422.jpg.jpg')
ax[0][1].imshow(img2)
ax[0][1].set_title("The completely new dataset.")
img3 =  mpimg.imread('../input/gender-recognition-200k-images-celeba/Dataset/Validation/Female/180019.jpg')
ax[1][0].imshow(img3)
img4= mpimg.imread('../input/gender-classification-dataset/Validation/female/113010.jpg.jpg')
ax[1][1].imshow(img4)
img5 = mpimg.imread('../input/gender-recognition-200k-images-celeba/Dataset/Validation/Male/180028.jpg')
ax[2][0].imshow(img5)
img6 = mpimg.imread('../input/gender-classification-dataset/Validation/male/063517.jpg.jpg')
ax[2][1].imshow(img6)
ax[3][0].imshow(mpimg.imread('../input/gender-recognition-200k-images-celeba/Dataset/Validation/Male/180073.jpg'))
ax[3][1].imshow(mpimg.imread('../input/gender-classification-dataset/Validation/male/063531.jpg.jpg'))
plt.tight_layout()
test_datagen2 = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen2.flow_from_directory(
'../input/gender-classification-dataset/Training',
target_size=(150,150),
batch_size=64,
class_mode='binary')
test_datagen3 = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen1.flow_from_directory(
'../input/gender-classification-dataset/Validation',
target_size=(150,150),
batch_size=64,
class_mode='binary')