text_plain_output_1.png
from keras.applications.inception_v3 import InceptionV3, preprocess_input

from keras.callbacks import ModelCheckpoint

from keras.layers import Dropout, Dense, Flatten, GlobalAveragePooling2D

from keras.models import Sequential, Model

from keras.optimizers import SGD

from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img

from keras.utils import np_utils

import cv2

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

main_folder = '../input/celeba-dataset/'
images_folder = main_folder + 'img_align_celeba/img_align_celeba/'
example_pic = images_folder + '000506.jpg'
training_sample = 10000
validation_sample = 2000
test_sample = 2000
img_width = 178
img_height = 218
batch_size = 16
num_epochs = 5

df_attr = pd.read_csv(main_folder + 'list_attr_celeba.csv')
df_attr.set_index('image_id', inplace=True)
df_attr.replace(to_replace=-1, value=0, inplace=True)

df_attr.columns

df_attr.isnull().sum()

df_attr.shape

img = load_img(example_pic)
df_attr.loc[example_pic.split('/')[-1]][['Smiling', 'Male', 'Young']]

df_partition = pd.read_csv(main_folder + 'list_eval_partition.csv')

df_partition.sample(100)

df_partition.set_index('image_id', inplace=True)
df_par_attr = df_partition.join(df_attr['Male'], how='inner')

df_par_attr.shape

def load_reshape_img(fname):
    img = load_img(fname)
    x = img_to_array(img) / 255.0
    x = x.reshape((1,) + x.shape)
    return x

def generate_df(partition, attr, num_samples):
    df_ = df_par_attr[(df_par_attr['partition'] == partition) & (df_par_attr[attr] == 0)].sample(int(num_samples / 2))
    df_ = pd.concat([df_, df_par_attr[(df_par_attr['partition'] == partition) & (df_par_attr[attr] == 1)].sample(int(num_samples / 2))])
    if partition != 2:
        x_ = np.array([load_reshape_img(images_folder + fname) for fname in df_.index])
        x_ = x_.reshape(x_.shape[0], 218, 178, 3)
        y_ = np_utils.to_categorical(df_[attr], 2)
    else:
        x_ = []
        y_ = []
        for index, target in df_.iterrows():
            im = cv2.imread(images_folder + index)
            im = cv2.resize(cv2.cvtColor(im, cv2.COLOR_BGR2RGB), (img_width, img_height)).astype(np.float32) / 255.0
            im = np.expand_dims(im, axis=0)
            x_.append(im)
            y_.append(target[attr])
    return (x_, y_)

datagen = ImageDataGenerator(rotation_range=30, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
img = load_img(example_pic)
x = img_to_array(img) / 255.0
x = x.reshape((1,) + x.shape)
i = 0
for batch in datagen.flow(x, batch_size=1):
    if i == 9:
        break
    i = i + 1

x_train, y_train = generate_df(0, 'Male', training_sample)
train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input, rotation_range=30, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
train_datagen.fit(x_train)
train_generator = train_datagen.flow(x_train, y_train, batch_size=batch_size)

inc_model = InceptionV3(weights='../input/inceptionv3/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5', include_top=False, input_shape=(img_height, img_width, 3))

x = inc_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(512, activation='relu')(x)
predictions = Dense(2, activation='softmax')(x)

model_ = Model(inputs=inc_model.input, outputs=predictions)
for layer in model_.layers[:52]:
    layer.trainable = False
model_.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy'])

checkpointer = ModelCheckpoint(filepath='weights.best.inc.male.hdf5', verbose=1, save_best_only=True)

hist = model_.fit_generator(train_generator, validation_data=(x_valid, y_valid), steps_per_epoch=training_sample / batch_size, epochs=num_epochs, callbacks=[checkpointer], verbose=1)

plt.figure(figsize=(18, 4))
plt.plot(hist.history['loss'], label='train')
plt.plot(hist.history['val_loss'], label='validation')
plt.legend()
plt.title('loss function')
plt.show()