Kaggle competition: Facial Keypoints Detection
<SOS> metric: RMSE Kaggle data source: facial-keypoints-detection<load_from_csv>
mpl.style.use('seaborn-darkgrid' )
tourney_seeds_df = pd.read_csv(stage1dir/'MNCAATourneySeeds.csv')
 tourney_seeds_df<load_from_csv>
df_train = pd.read_csv('.. /input/facial-keypoints-detection/training.zip')
 df_train.head(1 )
regular_season_results_df = pd.read_csv(stage1dir/'MRegularSeasonCompactResults.csv')
 tournament_results_df = pd.read_csv(stage1dir/'MNCAATourneyCompactResults.csv' )<load_from_csv>
feature_col, target_cols = 'Image', list(df_train.drop('Image', axis=1 ).columns )
sample_submission = pd.read_csv(datadir/'MSampleSubmissionStage1_2020.csv')
 sample_submission<load_from_csv>
IMG_WIDTH = 96
IMG_HEIGHT = 96
IMG_CHANNELS = 1
images = np.array(df_train[feature_col].str.split().tolist() , dtype='float' ).reshape(-1, IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS)
 labels = df_train[target_cols].to_numpy()
regular_season_detailed_results_df = pd.read_csv(stage1dir/'MRegularSeasonDetailedResults.csv')
 tournament_detailed_results_df = pd.read_csv(stage1dir/'MNCAATourneyDetailedResults.csv' )<load_from_csv>
normalized_images = images / 255 .
cities_df = pd.read_csv(stage1dir/'Cities.csv')
 mgame_cities_df = pd.read_csv(stage1dir/'MGameCities.csv' )<load_from_csv>
train_images, valid_images, train_labels, valid_labels = train_test_split(normalized_images, labels, test_size=0.1, random_state=7 )
massey_df = pd.read_csv(stage1dir/'MMasseyOrdinals.csv')
 massey_df<load_from_csv>
Input,
Conv2D,
MaxPool2D,
Dense,
BatchNormalization,
ReLU,
Dropout,
Flatten,
Dropout,
Concatenate,
GlobalAvgPool2D 
)
 def inception_module(inputs, f1, f2):
x1 = Conv2D(f1, 3, padding='same' )(inputs)
 x1 = BatchNormalization()(x1)
 x1 = ReLU()(x1)
 x2 = Conv2D(f2, 5, padding='same' )(inputs)
 x2 = BatchNormalization()(x2)
 x2 = ReLU()(x2)
 return Concatenate()([x1, x2] )
event2015_df = pd.read_csv(datadir/'MEvents2015.csv')
<load_from_csv>
def build_model() :
inputs = Input(( 96, 96, 1))
x = inception_module(inputs, 64, 32)
 x = MaxPool2D()(x)
 x = inception_module(x, 64, 32)
 x = MaxPool2D()(x)
 x = inception_module(x, 128, 32)
 x = MaxPool2D()(x)
 x = inception_module(x, 128, 32)
 x = MaxPool2D()(x)
 x = inception_module(x, 256, 64)
 x = MaxPool2D()(x)
 x = Flatten()(x)
 x = Dense(1024, kernel_regularizer=L2(l2=0.05))(x)
 x = BatchNormalization()(x)
 x = ReLU()(x)
 x = Dense(512, kernel_regularizer=L2(l2=0.02))(x)
 x = BatchNormalization()(x)
 x = ReLU()(x)
 x = Dense(128, kernel_regularizer=L2(l2=0.01))(x)
 x = BatchNormalization()(x)
 x = ReLU()(x)
 x = Dense(30 )(x)
 model = tf.keras.Model(inputs, outputs=x)
 return model
model = build_model()
model.summary()
players_df = pd.read_csv(datadir/'MPlayers.csv')
 players_df<load_from_csv>
model.compile(optimizer='adam', loss='mean_squared_error', metrics=['mae'] )
team_coaches_df = pd.read_csv(stage1dir/'MTeamCoaches.csv')
 print('team_coaches_df', team_coaches_df.shape)
 team_coaches_df.iloc[80:85]<load_from_csv>
ckp_filepath = 'trained_models/model'
model_checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath=ckp_filepath,
monitor='val_mae',
mode='auto',
save_best_only=True,
save_weights_only=True)
 reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(factor=0.9, monitor='val_mae',
mode='auto', cooldown=0, patience=5, verbose=1, min_lr=1e-5 )
conferences_df = pd.read_csv(stage1dir/'Conferences.csv')
 team_conferences_df = pd.read_csv(stage1dir/'MTeamConferences.csv' )<filter>
EPOCHS = 300
BATCH_SIZE = 256
history = model.fit(train_images,
train_labels,
validation_data=(valid_images, valid_labels),
batch_size=BATCH_SIZE, epochs=EPOCHS, callbacks=[model_checkpoint, reduce_lr] )
team_conferences_df[team_conferences_df['TeamID'] == 1102]<load_from_csv>
df_test = pd.read_csv('.. /input/facial-keypoints-detection/test.zip')
 df_test.head(1 )
conference_tourney_games_df = pd.read_csv(stage1dir/'MConferenceTourneyGames.csv')
 conference_tourney_games_df<load_from_csv>
test_images = np.array(df_test['Image'].str.split().tolist() , dtype='int' ).reshape(-1, 96, 96, 1 )
secondary_tourney_teams_df = pd.read_csv(stage1dir/'MSecondaryTourneyTeams.csv')
 secondary_tourney_teams_df<load_from_csv>
normalized_test_images = test_images / 255 .
secondary_tourney_results_df = pd.read_csv(stage1dir/'MSecondaryTourneyCompactResults.csv')
 secondary_tourney_results_df<load_from_csv>
model.load_weights(ckp_filepath)
 keypoints_predictions = model.predict(normalized_test_images, batch_size=BATCH_SIZE )
<load_from_csv>
idx = np.random.choice(16, 16)
 show_examples(test_images[idx], keypoints_predictions[idx] )
tourney_slots_df = pd.read_csv(stage1dir/'MNCAATourneySlots.csv')
 tourney_seed_round_slots_df = pd.read_csv(stage1dir/'MNCAATourneySeedRoundSlots.csv' )<filter>
lookup = pd.read_csv('.. /input/facial-keypoints-detection/IdLookupTable.csv')
 lookup.head()
<filter><EOS> 
df_test_predictions = pd.DataFrame(keypoints_predictions, columns=target_cols)
 df_test_predictions.index += 1
lookup['Location'] = lookup.set_index(['ImageId', 'FeatureName'] ).index.map(df_test_predictions.stack() ).values
timestamp = pd.to_datetime('today' ).floor('15min')
 lookup[['RowId', 'Location']].to_csv(f'predictions_{timestamp}.csv', index=False )