Kaggle competition: Facial Keypoints Detection
<SOS> metric: RMSE Kaggle data source: facial-keypoints-detection<train_model>
%matplotlib inline
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
 history = model.fit(
 x=x_train,
y=y_train,
batch_size=256,
epochs=30,
callbacks=[checkpoint, f1_metrics],
validation_split=0.2
)<save_to_csv>
horizontal_flip = False
rotation_augmentation = True
brightness_augmentation = True
shift_augmentation = True
random_noise_augmentation = True
include_unclean_data = True
sample_image_index = 20
rotation_angles = [12]
pixel_shifts = [12]
NUM_EPOCHS = 80
BATCH_SIZE = 64
model.load_weights('model.h5')
 y_test = model.predict(x_test)
 submission_df = pd.read_csv('.. /input/iwildcam-2019-fgvc6/sample_submission.csv')
 submission_df['Predicted'] = y_test.argmax(axis=1)
 print(submission_df.shape)
 submission_df.head()
submission_df.to_csv('submission.csv',index=False )<set_options>
print("Contents of input/facial-keypoints-detection directory: ")
 !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
warnings.filterwarnings('ignore' )<load_from_csv>
%%time
train_file = 'training.csv'
test_file = 'test.csv'
idlookup_file = '.. /input/facial-keypoints-detection/IdLookupTable.csv'
train_data = pd.read_csv(train_file)
 test_data = pd.read_csv(test_file)
 idlookup_data = pd.read_csv(idlookup_file )
%%time
train_df = pd.read_csv(os.path.join(PATH, 'train.csv'))
test_df = pd.read_csv(os.path.join(PATH, 'test.csv'))<prepare_x_and_y>
print("Length of train data: {}".format(len(train_data)))
 print("Number of Images with missing pixel values: {}".format(len(train_data)- int(train_data.Image.apply(lambda x: len(x.split())).value_counts().values)) )
x_train = np.load('.. /input/reducing-image-sizes-to-32x32/X_train.npy')
 x_test = np.load('.. /input/reducing-image-sizes-to-32x32/X_test.npy')
 y_train = np.load('.. /input/reducing-image-sizes-to-32x32/y_train.npy' )<define_variables>
train_data.isnull().sum()
classes_wild = {0: 'empty', 1: 'deer', 2: 'moose', 3: 'squirrel', 4: 'rodent', 5: 'small_mammal', \
6: 'elk', 7: 'pronghorn_antelope', 8: 'rabbit', 9: 'bighorn_sheep', 10: 'fox', 11: 'coyote', \
12: 'black_bear', 13: 'raccoon', 14: 'skunk', 15: 'wolf', 16: 'bobcat', 17: 'cat',\
18: 'dog', 19: 'opossum', 20: 'bison', 21: 'mountain_goat', 22: 'mountain_lion'}<feature_engineering>
%%time
def load_images(image_data):
images = []
for idx, sample in image_data.iterrows() :
image = np.array(sample['Image'].split(' '), dtype=int)
 image = np.reshape(image,(96,96,1))
images.append(image)
 images = np.array(images)/255.
 return images
def load_keypoints(keypoint_data):
keypoint_data = keypoint_data.drop('Image',axis = 1)
 keypoint_features = []
for idx, sample_keypoints in keypoint_data.iterrows() :
keypoint_features.append(sample_keypoints)
 keypoint_features = np.array(keypoint_features, dtype = 'float')
 return keypoint_features
clean_train_images = load_images(clean_train_data)
 print("Shape of clean_train_images: {}".format(np.shape(clean_train_images)))
 clean_train_keypoints = load_keypoints(clean_train_data)
 print("Shape of clean_train_keypoints: {}".format(np.shape(clean_train_keypoints)))
 test_images = load_images(test_data)
 print("Shape of test_images: {}".format(np.shape(test_images)))
 train_images = clean_train_images
train_keypoints = clean_train_keypoints
fig, axis = plt.subplots()
plot_sample(clean_train_images[sample_image_index], clean_train_keypoints[sample_image_index], axis, "Sample image & keypoints")
 if include_unclean_data:
unclean_train_images = load_images(unclean_train_data)
 print("Shape of unclean_train_images: {}".format(np.shape(unclean_train_images)))
 unclean_train_keypoints = load_keypoints(unclean_train_data)
 print("Shape of unclean_train_keypoints: {}
".format(np.shape(unclean_train_keypoints)))
 train_images = np.concatenate(( train_images, unclean_train_images))
train_keypoints = np.concatenate(( train_keypoints, unclean_train_keypoints))
train_df['classes_wild'] = train_df['category_id'].apply(lambda cw: classes_wild[cw] )<define_variables>
def left_right_flip(images, keypoints):
flipped_keypoints = []
flipped_images = np.flip(images, axis=2)
 for idx, sample_keypoints in enumerate(keypoints):
flipped_keypoints.append([96.-coor if idx%2==0 else coor for idx,coor in enumerate(sample_keypoints)])
 return flipped_images, flipped_keypoints
if horizontal_flip:
flipped_train_images, flipped_train_keypoints = left_right_flip(clean_train_images, clean_train_keypoints)
 print("Shape of flipped_train_images: {}".format(np.shape(flipped_train_images)))
 print("Shape of flipped_train_keypoints: {}".format(np.shape(flipped_train_keypoints)))
 train_images = np.concatenate(( train_images, flipped_train_images))
train_keypoints = np.concatenate(( train_keypoints, flipped_train_keypoints))
fig, axis = plt.subplots()
plot_sample(flipped_train_images[sample_image_index], flipped_train_keypoints[sample_image_index], axis, "Horizontally Flipped" )
train_image_files = list(os.listdir(os.path.join(PATH,'train_images')))
 test_image_files = list(os.listdir(os.path.join(PATH,'test_images')))
 print("Number of image files: train:{} test:{}".format(len(train_image_files), len(test_image_files)) )<train_model>
def rotate_augmentation(images, keypoints):
rotated_images = []
rotated_keypoints = []
print("Augmenting for angles(in degrees): ")
 for angle in rotation_angles:
for angle in [angle,-angle]:
print(f'{angle}', end=' ')
 M = cv2.getRotationMatrix2D(( 48,48), angle, 1.0)
 angle_rad = -angle*pi/180.
 for image in images:
rotated_image = cv2.warpAffine(image, M,(96,96), flags=cv2.INTER_CUBIC)
 rotated_images.append(rotated_image)
 for keypoint in keypoints:
rotated_keypoint = keypoint - 48.
 for idx in range(0,len(rotated_keypoint),2):
rotated_keypoint[idx] = rotated_keypoint[idx]*cos(angle_rad)-rotated_keypoint[idx+1]*sin(angle_rad)
 rotated_keypoint[idx+1] = rotated_keypoint[idx]*sin(angle_rad)+rotated_keypoint[idx+1]*cos(angle_rad)
 rotated_keypoint += 48.
 rotated_keypoints.append(rotated_keypoint)
 return np.reshape(rotated_images,(-1,96,96,1)) , rotated_keypoints
if rotation_augmentation:
rotated_train_images, rotated_train_keypoints = rotate_augmentation(clean_train_images, clean_train_keypoints)
 print("
Shape of rotated_train_images: {}".format(np.shape(rotated_train_images)))
 print("Shape of rotated_train_keypoints: {}
".format(np.shape(rotated_train_keypoints)))
 train_images = np.concatenate(( train_images, rotated_train_images))
train_keypoints = np.concatenate(( train_keypoints, rotated_train_keypoints))
fig, axis = plt.subplots()
plot_sample(rotated_train_images[sample_image_index], rotated_train_keypoints[sample_image_index], axis, "Rotation Augmentation" )
%%time
train_file_names = list(train_df['file_name'])
 print("Matching train image names: {}".format(len(set(train_file_names ).intersection(train_image_files))))<train_model>
def alter_brightness(images, keypoints):
altered_brightness_images = []
inc_brightness_images = np.clip(images*1.2, 0.0, 1.0)
 dec_brightness_images = np.clip(images*0.6, 0.0, 1.0)
 altered_brightness_images.extend(inc_brightness_images)
 altered_brightness_images.extend(dec_brightness_images)
 return altered_brightness_images, np.concatenate(( keypoints, keypoints))
if brightness_augmentation:
altered_brightness_train_images, altered_brightness_train_keypoints = alter_brightness(clean_train_images, clean_train_keypoints)
 print(f"Shape of altered_brightness_train_images: {np.shape(altered_brightness_train_images)}")
 print(f"Shape of altered_brightness_train_keypoints: {np.shape(altered_brightness_train_keypoints)}")
 train_images = np.concatenate(( train_images, altered_brightness_train_images))
train_keypoints = np.concatenate(( train_keypoints, altered_brightness_train_keypoints))
fig, axis = plt.subplots()
plot_sample(altered_brightness_train_images[sample_image_index], altered_brightness_train_keypoints[sample_image_index], axis, "Increased Brightness")
 fig, axis = plt.subplots()
plot_sample(altered_brightness_train_images[len(altered_brightness_train_images)//2+sample_image_index], altered_brightness_train_keypoints[len(altered_brightness_train_images)//2+sample_image_index], axis, "Decreased Brightness" )
%%time
test_file_names = list(test_df['file_name'])
 print("Matching test image names: {}".format(len(set(test_file_names ).intersection(test_image_files))))<count_unique_values>
model = Sequential()
model.add(Convolution2D(32,(3,3), padding='same', use_bias=False, input_shape=(96,96,1)))
 model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(32,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(64,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(64,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(96,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(96,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(128,(3,3),padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(128,(3,3),padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(256,(3,3),padding='same',use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(256,(3,3),padding='same',use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Convolution2D(512,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Convolution2D(512,(3,3), padding='same', use_bias=False))
model.add(LeakyReLU(alpha = 0.1))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dense(512,activation='relu'))
model.add(Dropout(0.1))
model.add(Dense(30))
model.summary()
cnt_classes_images = train_df.classes_wild.nunique()
print("There are {} classes of images".format(cnt_classes_images))
pd.DataFrame(train_df.classes_wild.value_counts() ).transpose()<feature_engineering>
%%time
if os.path.exists('.. /input/data-augmentation-for-facial-keypoint-detection/best_model.hdf5'):
model = load_model('.. /input/data-augmentation-for-facial-keypoint-detection/best_model.hdf5')
 checkpointer = ModelCheckpoint(filepath = 'best_model.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=NUM_EPOCHS, batch_size=BATCH_SIZE, validation_split=0.05, callbacks=[checkpointer] )
try:
train_df['date_time'] = pd.to_datetime(train_df['date_captured'], errors='coerce')
 train_df["year"] = train_df['date_time'].dt.year
train_df["month"] = train_df['date_time'].dt.month
train_df["day"] = train_df['date_time'].dt.day
train_df["hour"] = train_df['date_time'].dt.hour
train_df["minute"] = train_df['date_time'].dt.minute
except Exception as ex:
print("Exception:".format(ex))<define_variables>
%%time
checkpointer = ModelCheckpoint(filepath = 'best_model.hdf5', monitor='mae', verbose=1, save_best_only=True, mode='min')
 model.fit(train_images, train_keypoints, epochs=NUM_EPOCHS, batch_size=BATCH_SIZE, callbacks=[checkpointer] )
IMAGE_PATH = os.path.join(PATH,'train_images/')
 draw_category_images('classes_wild' )<data_type_conversions>
%%time
model = load_model('best_model.hdf5')
 test_preds = model.predict(test_images )
x_train = x_train.astype('float32')
 x_test = x_test.astype('float32')
 x_train /= 255.
 x_test /= 255 .<compute_train_metric>
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('submission.csv',index = False )