Kaggle competition: Facial Keypoints Detection
model2.save('my_model2.h5')
 model2 = load_model('my_model2.h5' )<define_variables>
class FlippedImageDataGenerator(ImageDataGenerator):
flip_indices = [ 
(0, 2),(1, 3), 
(4, 8),(5, 9),(6, 10),(7, 11), 
(12, 16),(13, 17),(14, 18),(15, 19), 
(22, 24),(23, 25),
]
def next(self):
X_batch, y_batch = super(FlippedImageDataGenerator, self ).next()
batch_size = X_batch.shape[0]
indices = np.random.choice(batch_size, batch_size/2, replace=False)
 X_batch[indices] = X_batch[indices, :, :, ::-1]
if y_batch is not None:
y_batch[indices, ::2] = y_batch[indices, ::2] * -1
for a, b in self.flip_indices:
y_batch[indices, a], y_batch[indices, b] =(
 y_batch[indices, b], y_batch[indices, a] 
)
 return X_batch, y_batch<split>
!ls.. /input/facial-keypoints-detection/
print("
Extracting.zip dataset files to working directory...")
 !unzip -u.. /input/facial-keypoints-detection/test.zip
!unzip -u.. /input/facial-keypoints-detection/training.zip
print("
Current working directory:")
 !pwd
print("
Contents of working directory:")
 !ls
X, y = load2d()
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42 )<train_on_grid>
%%time
train_csv = 'training.csv'
test_csv = 'test.csv'
idlookup_file = '.. /input/facial-keypoints-detection/IdLookupTable.csv'
train = pd.read_csv(train_csv)
 test = pd.read_csv(test_csv)
 idlookup_data = pd.read_csv(idlookup_file )
model3 = CNN()
flipgen = FlippedImageDataGenerator()
hist3 = model3.fit_generator(flipgen.flow(X_train, y_train),
samples_per_epoch=X_train.shape[0],
nb_epoch=300,
validation_data=(X_val, y_val)) 
<load_pretrained>
train.isnull().sum()
model3.save('my_model3.h5')
 model3 = load_model('my_model3.h5' )<define_variables>
train.isnull().sum()
SPECIALIST_SETTINGS = [
dict(
 columns=(
 'left_eye_center_x', 'left_eye_center_y',
'right_eye_center_x', 'right_eye_center_y', 
),
flip_indices=(( 0, 2),(1, 3)) , 
),
dict(
 columns=(
 'nose_tip_x', 'nose_tip_y', 
),
flip_indices=() , 
),
dict(
 columns=(
 'mouth_left_corner_x', 'mouth_left_corner_y',
'mouth_right_corner_x', 'mouth_right_corner_y',
'mouth_center_top_lip_x', 'mouth_center_top_lip_y', 
),
flip_indices=(( 0, 2),(1, 3)) , 
),
dict(
 columns=(
 'mouth_center_bottom_lip_x',
'mouth_center_bottom_lip_y', 
),
flip_indices=() , 
),
dict(
 columns=(
 'left_eye_inner_corner_x', 'left_eye_inner_corner_y',
'right_eye_inner_corner_x', 'right_eye_inner_corner_y',
'left_eye_outer_corner_x', 'left_eye_outer_corner_y',
'right_eye_outer_corner_x', 'right_eye_outer_corner_y', 
),
flip_indices=(( 0, 2),(1, 3),(4, 6),(5, 7)) , 
),
dict(
 columns=(
 'left_eyebrow_inner_end_x', 'left_eyebrow_inner_end_y',
'right_eyebrow_inner_end_x', 'right_eyebrow_inner_end_y',
'left_eyebrow_outer_end_x', 'left_eyebrow_outer_end_y',
'right_eyebrow_outer_end_x', 'right_eyebrow_outer_end_y', 
),
flip_indices=(( 0, 2),(1, 3),(4, 6),(5, 7)) , 
),
] 
<train_model>
train.fillna(method = 'ffill',inplace = True )
def fit_specialists(freeze=True,
print_every=10,
epochs=100,
prop=0.1,
name_transfer_model="my_model3.h5"):
specialists = OrderedDict()
for setting in SPECIALIST_SETTINGS:
cols = setting['columns']
flip_indices = setting['flip_indices']
X, y = load2d(cols=cols)
 X_train, X_val, y_train, y_val = train_test_split(X, y,
test_size=0.2,
random_state=42)
 model4 = load_model(name_transfer_model)
 if freeze:
for layer in model.layers:
layer.trainable = False
model4.layers.pop()
model4.outputs = [model4.layers[-1].output]
model4.layers[-1].outbound_nodes = []
model4.add(Dense(len(cols)))
 model4.compile(loss='mean_squared_error', optimizer="adam")
 flipgen = FlippedImageDataGenerator()
flipgen.flip_indices = setting['flip_indices']
print(X_train.shape)
 print(y_train.shape)
 print(X_val.shape)
 print(y_val.shape)
 hist_final = model4.fit_generator(flipgen.flow(X_train, y_train),
samples_per_epoch=X_train.shape[0],
nb_epoch=epochs,
validation_data=(X_val, y_val))
specialists[cols] = model4
return(specialists)
<train_model>
train.isnull().sum()
%%time
specialists1 = fit_specialists(freeze=True,
print_every=10,
epochs=100,
name_transfer_model="my_model3.h5" )<predict_on_test>
train.isnull().sum()
X_test,_ = load2d(test=True)
 y_pred3 = model3.predict(X_test)
 landmark_nm = read_csv(os.path.expanduser(FTRAIN)).columns[:-1].values
df_y_pred3 = DataFrame(y_pred3,columns = landmark_nm)
 def predict_specialist(specialists1,X_test):
y_pred_s = []
for columns, value in specialists1.items() :
smodel = value
y_pred = smodel.predict(X_test)
 y_pred = DataFrame(y_pred,columns=columns)
 y_pred_s.append(y_pred)
 df_y_pred_s = concat(y_pred_s,axis=1)
 return(df_y_pred_s)
 df_y_pred_s = predict_specialist(specialists1,X_test)
 y_pred_s = df_y_pred_s.values<feature_engineering>
test.isnull().sum()
FIdLookup = '.. /input/IdLookupTable.csv'
IdLookup = read_csv(os.path.expanduser(FIdLookup))
def prepare_submission(y_pred4,filename):

 ImageId = IdLookup["ImageId"]
FeatureName = IdLookup["FeatureName"]
RowId = IdLookup["RowId"]
submit = []
for rowId,irow,landmark in zip(RowId,ImageId,FeatureName):
submit.append([rowId,y_pred4[landmark].iloc[irow-1]])
 submit = DataFrame(submit,columns=["RowId","Location"])
 submit["Location"] = submit["Location"]*48 + 48
print(submit.shape)
 if filename == "model3":
submit.to_csv("model3.csv",index=False)
 else:
submit.to_csv("special.csv",index=False)
 prepare_submission(df_y_pred_s,"special")
 prepare_submission(df_y_pred3,"model3")
<predict_on_test>
test.isnull().sum()
<create_dataframe>
import tensorflow.keras as keras
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import Input,LeakyReLU, Conv2D,Flatten, BatchNormalization, Dense, Dropout, GlobalAveragePooling2D,MaxPool2D
from tensorflow.keras import optimizers
import tensorflow as tf
from keras.utils import np_utils
from keras import applications
from keras.layers import concatenate
import time
from skimage.transform import resize
df_y_pred_s = df_y_pred_s[df_y_pred3.columns]
df_compare = {}
df_compare["difference"] =(( df_y_pred_s - df_y_pred3)**2 ).mean(axis=1)
 df_compare["RowId"] = range(df_y_pred_s.shape[0])
 df_compare = DataFrame(df_compare)
 df_compare = df_compare.sort_values("difference",ascending=False )<prepare_x_and_y>
model = Sequential()
model.add(Conv2D(32,(5,5), padding='same', use_bias=True, input_shape=(96,96,1)))
 model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(32,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(32,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(64,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(64,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(64,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(96,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(96,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(96,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(128,(5,5),padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(128,(5,5),padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(256,(5,5),padding='same',use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(256,(5,5),padding='same',use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Conv2D(512,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(512,(5,5), padding='same', use_bias=True))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Flatten())
 model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(Dropout(0.1))
model.add(Dense(30))
model.summary()
def fit_specialists(freeze=True,
print_every=10,
epochs=100,
prop=0.1,
name_transfer_model="my_model2.h5"):
specialists = OrderedDict()
for setting in SPECIALIST_SETTINGS:
cols = setting['columns']
flip_indices = setting['flip_indices']
X, y = load2d(cols=cols)
 X_train, X_val, y_train, y_val = train_test_split(X, y,
test_size=0.2,
random_state=42)
 model4 = load_model(name_transfer_model)
 if freeze:
for layer in model.layers:
layer.trainable = False
model4.layers.pop()
model4.outputs = [model4.layers[-1].output]
model4.layers[-1].outbound_nodes = []
model4.add(Dense(len(cols)))
 model4.compile(loss='mean_squared_error', optimizer="adam")
 flipgen = FlippedImageDataGenerator()
flipgen.flip_indices = setting['flip_indices']
print(X_train.shape)
 print(y_train.shape)
 print(X_val.shape)
 print(y_val.shape)
 hist_final = model4.fit_generator(flipgen.flow(X_train, y_train),
samples_per_epoch=X_train.shape[0],
nb_epoch=epochs,
validation_data=(X_val, y_val))
specialists[cols] = model4
return(specialists)
<train_model>
%%time
specialists2 = fit_specialists(freeze=True,
print_every=10,
epochs=100,
name_transfer_model="my_model2.h5" )<predict_on_test>
checkpointer = ModelCheckpoint(filepath = 'best_modelV8.hdf5', monitor='val_mae', verbose=1, save_best_only=True, mode='min')
 model.compile(optimizer="adam", loss='mean_squared_error', metrics=['mae', 'acc'])
 history = model.fit(train_images, train_keypoints, epochs=500, batch_size=256, validation_split=0.1, callbacks=[checkpointer])
X_test,_ = load2d(test=True)
 def predict_specialist(specialists2,X_test):
y_pred_s = []
for columns, value in specialists2.items() :
smodel = value
y_pred = smodel.predict(X_test)
 y_pred = DataFrame(y_pred,columns=columns)
 y_pred_s.append(y_pred)
 df_y_pred_s = concat(y_pred_s,axis=1)
 return(df_y_pred_s)
 df_y_pred_s = predict_specialist(specialists2,X_test)
 y_pred_s = df_y_pred_s.values<predict_on_test>
model.save('best_modelV5x5_V1.hdf5')
y_pred2 = model2.predict(X_test)
 landmark_nm = read_csv(os.path.expanduser(FTRAIN)).columns[:-1].values
df_y_pred2 = DataFrame(y_pred2,columns = landmark_nm )<feature_engineering>
%%time
model = load_model('best_modelV5x5_V1.hdf5')
 test_preds = model.predict(test_images )
FIdLookup = '.. /input/IdLookupTable.csv'
IdLookup = read_csv(os.path.expanduser(FIdLookup))
def prepare_submission(y_pred2,filename):

 ImageId = IdLookup["ImageId"]
FeatureName = IdLookup["FeatureName"]
RowId = IdLookup["RowId"]
submit = []
for rowId,irow,landmark in zip(RowId,ImageId,FeatureName):
submit.append([rowId,y_pred2[landmark].iloc[irow-1]])
 submit = DataFrame(submit,columns=["RowId","Location"])
 submit["Location"] = submit["Location"]*48 + 48
print(submit.shape)
 if filename == "model2":
submit.to_csv("model2.csv",index=False)
 else:
submit.to_csv("special_model2.csv",index=False)
 prepare_submission(df_y_pred_s,"special_model2")
 prepare_submission(df_y_pred2,"model2" )<create_dataframe>
feature_names = list(idlookup_data['FeatureName'])
 image_ids = list(idlookup_data['ImageId']-1)
 row_ids = list(idlookup_data['RowId'])
 feature_list = []
for feature in feature_names:
feature_list.append(feature_names.index(feature))
predictions = []
for x,y in zip(image_ids, feature_list):
predictions.append(test_preds[x][y])
 row_ids = pd.Series(row_ids, name = 'RowId')
 locations = pd.Series(predictions, name = 'Location')
 locations = locations.clip(0.0,96.0)
 submission_result = pd.concat([row_ids,locations],axis = 1)
 submission_result.to_csv('charlin_version_5X5.csv',index = False )
df_y_pred_s = df_y_pred_s[df_y_pred3.columns]
df_compare = {}
df_compare["difference"] =(( df_y_pred_s - df_y_pred2)**2 ).mean(axis=1)
 df_compare["RowId"] = range(df_y_pred_s.shape[0])
 df_compare = DataFrame(df_compare)
 df_compare = df_compare.sort_values("difference",ascending=False )<prepare_x_and_y>
aug = ImageDataGenerator(
 		rotation_range=20,
		zoom_range=0.15,
		width_shift_range=0.2,
		height_shift_range=0.2,
		shear_range=0.15,
		horizontal_flip=True,
		fill_mode="nearest" )
<import_modules>
trainX=np.swapaxes(trainX,0,1)
 trainX=np.swapaxes(trainX,1,2)
 trainX=np.swapaxes(trainX,2,3)
 trainX.shape
<load_from_csv><EOS> 
testX=np.swapaxes(testX,0,1)
 testX=np.swapaxes(testX,1,2)
 testX=np.swapaxes(testX,2,3)
 testX.shape