Kaggle competition: Facial Keypoints Detection
<SOS> metric: RMSE Kaggle data source: facial-keypoints-detection<string_transform>
%matplotlib inline
def clean_sentences(df):
reviews = []
for sent in tqdm(df['Phrase']):
review_text = re.sub("[^a-zA-Z]"," ", sent)
 words = word_tokenize(review_text.lower())
 lemmatizer = WordNetLemmatizer()
lemma_words = [lemmatizer.lemmatize(i)for i in words]
reviews.append(lemma_words)
 return(reviews )<string_transform>
train_zip_path = '/kaggle/input/facial-keypoints-detection/training.zip'
test_zip_path = '/kaggle/input/facial-keypoints-detection/test.zip'
Id_table_path = '/kaggle/input/facial-keypoints-detection/IdLookupTable.csv'
sample_sub_path = '/kaggle/input/facial-keypoints-detection/SampleSubmission.csv'
extracted_files_path = '/kaggle/working'
%%time
train_sentences = clean_sentences(train)
 test_sentences = clean_sentences(test)
 print(len(train_sentences))
print(len(test_sentences))<string_transform>
with zipfile.ZipFile(train_zip_path, 'r')as zip_ref:
zip_ref.extractall(extracted_files_path)
 with zipfile.ZipFile(test_zip_path, 'r')as zip_ref:
zip_ref.extractall(extracted_files_path )
print(train['Phrase'][0])
 print(' '.join(train_sentences[0]))<categorify>
train_csv = pd.read_csv(extracted_files_path + '/training.csv')
 test_csv = pd.read_csv(extracted_files_path + '/test.csv')
 looktable_csv = pd.read_csv(Id_table_path )
target = train.Sentiment.values
y_target = to_categorical(target)
 num_classes = y_target.shape[1]<import_modules>
feature_8 = ['left_eye_center_x', 'left_eye_center_y',
'right_eye_center_x','right_eye_center_y',
'nose_tip_x', 'nose_tip_y',
'mouth_center_bottom_lip_x',
'mouth_center_bottom_lip_y', 'Image']
train_8_csv = train_csv[feature_8].dropna().reset_index()
train_30_csv = train_csv.dropna().reset_index()
from sklearn.model_selection import train_test_split<import_modules>
def str_to_array(pd_series):

 data_size = len(pd_series)
 X = np.zeros(shape=(data_size,96,96,1), dtype=np.float32)
 for i in tqdm(range(data_size)) :
img_str = pd_series[i]
img_list = img_str.split(' ')
 img_array = np.array(img_list, dtype=np.float32)
 img_array = img_array.reshape(96,96,1)
 X[i] = img_array
return X
from sklearn.model_selection import train_test_split<split>
X_train_30 = str_to_array(train_30_csv['Image'])
 labels_30 = train_30_csv.drop(['index','Image'], axis=1)
 y_train_30 = labels_30.to_numpy(dtype=np.float32)
 print('X_train with 30 feature shape: ', X_train_30.shape)
 print('y_train with 30 feature shape: ', y_train_30.shape )
X_train, X_val, y_train, y_val = train_test_split(train_sentences,
y_target,
test_size = 0.2,
stratify = y_target )<count_unique_values>
X_train_8 = str_to_array(train_8_csv['Image'])
 labels_8 = train_8_csv.drop(['index','Image'], axis=1)
 y_train_8 = labels_8.to_numpy(dtype=np.float32)
 print('X_train with 8 feature shape: ', X_train_8.shape)
 print('y_train with 8 feature shape: ', y_train_8.shape )
unique_words = set()
len_max = 0
for sent in tqdm(X_train):
unique_words.update(sent)
 if(len_max < len(sent)) :
len_max = len(sent)
 print('Number of vocabs: ', len(list(unique_words)))
 print('Max length of text is: ', len_max )<define_variables>
def create_model(output_n = 30):

 model = keras.models.Sequential([
keras.layers.InputLayer(input_shape=[96,96,1]),
keras.layers.Conv2D(filters=32, kernel_size=[5,5], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=32, kernel_size=[5,5], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(pool_size=[2,2]),
keras.layers.Conv2D(filters=64, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=64, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(pool_size=[2,2]),
keras.layers.Conv2D(filters=128, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=128, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(pool_size=[2,2]),
keras.layers.Conv2D(filters=256, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=256, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.MaxPool2D(pool_size=[2,2]),
keras.layers.Conv2D(filters=512, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.Conv2D(filters=512, kernel_size=[3,3], padding='same', use_bias=False),
keras.layers.LeakyReLU(alpha =.1),
keras.layers.BatchNormalization() ,
keras.layers.Flatten() ,
keras.layers.Dense(units=512, activation='relu'),
keras.layers.Dropout (.1),
keras.layers.Dense(units=output_n),
])
 model.compile(optimizer = 'adam' , loss = "mean_squared_error", metrics=["mae"])
 return model
vocab_size = len(list(unique_words))
embedding_dim = 300
max_length = len_max
trunc_type='post'
padding_type='post'
oov_tok = "<OOV>"<string_transform>
model_30 = create_model(output_n=30)
 model_8 = create_model(output_n=8 )
%%time
tokenizer = Tokenizer(num_words = vocab_size,
',
oov_token = oov_tok,
char_level = False)
 tokenizer.fit_on_texts(list(X_train))
X_train = tokenizer.texts_to_sequences(X_train)
 X_train = pad_sequences(X_train,
maxlen = max_length,
padding = padding_type,
truncating = trunc_type)
 X_val = tokenizer.texts_to_sequences(X_val)
 X_val = pad_sequences(X_val,
maxlen = max_length,
padding = padding_type,
truncating = trunc_type)
 X_test = tokenizer.texts_to_sequences(test_sentences)
 X_test = pad_sequences(X_test,
maxlen = max_length,
padding = padding_type,
truncating = trunc_type )<choose_model_class>
LR_callback = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', patience=4, verbose=10, factor=.4, min_lr=.00001)
 EarlyStop_callback = keras.callbacks.EarlyStopping(patience=15, restore_best_weights=True )
model = tf.keras.Sequential([
tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length = max_length),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(128, dropout = 0.8, recurrent_dropout=0.8, return_sequences=True)) ,
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, dropout = 0.5, recurrent_dropout=0.5, return_sequences=False)) ,
tf.keras.layers.Dense(128, activation = 'relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(num_classes, activation = 'softmax')
 ])
 model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
 model.summary()<choose_model_class>
history = model_30.fit(X_train_30, y_train_30, validation_split=.1, batch_size=64, epochs=100, callbacks=[LR_callback,EarlyStop_callback] )
early_stopping = EarlyStopping(min_delta = 0.001,
mode = 'max',
monitor = 'val_acc',
patience = 2)
 callback = [early_stopping]<train_model>
history = model_8.fit(X_train_8, y_train_8, validation_split=.1, batch_size=64, epochs=100, callbacks=[LR_callback,EarlyStop_callback] )
%%time
num_epochs = 5
history = model.fit(X_train,
y_train,
validation_data =(X_val, y_val),
epochs = num_epochs,
batch_size = 256,
verbose = 1,
callbacks = callback )<define_variables>
X_test = str_to_array(test_csv['Image'])
 print('X_test shape: ', X_test.shape )
test_id = test['PhraseId']<predict_on_test>
y_hat_30 = model_30.predict(X_test)
 y_hat_8 = model_8.predict(X_test)
 print('Predictions shape', y_hat_30.shape)
 print('Predictions shape', y_hat_8.shape )
%%time
y_pred = np.argmax(model.predict(X_test), axis=-1 )<save_to_csv>
feature_8_ind = [0, 1, 2, 3, 20, 21, 28, 29]
for i in range(8):
print('Copy "{}" feature column from y_hat_8 --> y_hat_30'.format(feature_8[i]))
y_hat_30[:,feature_8_ind[i]] = y_hat_8[:,i]
submission = pd.DataFrame({'PhraseId': test_id, 'Sentiment': y_pred})
 submission.to_csv('movie_review_prediction_5EP_MLBDLSTM_submission.csv', index=False)
 submission.head()<set_options>
required_features = list(looktable_csv['FeatureName'])
 imageID = list(looktable_csv['ImageId']-1)
 feature_to_num = dict(zip(required_features[0:30], range(30)) )
py.init_notebook_mode(connected=True)
 pd.set_option('max_columns', 50 )<define_variables>
feature_ind = []
for f in required_features:
feature_ind.append(feature_to_num[f] )
datadir = Path('/kaggle/input/google-cloud-ncaa-march-madness-2020-division-1-mens-tournament')
 stage1dir = datadir/'MDataFiles_Stage1'<load_from_csv>
required_pred = []
for x,y in zip(imageID,feature_ind):
required_pred.append(y_hat_30[x, y] )
<load_from_csv><EOS> 
rowid = looktable_csv['RowId']
loc30 = pd.Series(required_pred,name = 'Location')
 submission = pd.concat([rowid,loc30],axis = 1)
 submission.to_csv('Merged_Predictions.csv',index = False )