Kaggle competition: Facial Keypoints Detection
class Metrics(Callback):
def on_train_begin(self, logs={}):
self.val_f1s = []
self.val_recalls = []
self.val_precisions = []
def on_epoch_end(self, epoch, logs={}):
X_val, y_val = self.validation_data[:2]
y_pred = self.model.predict(X_val)
 y_pred_cat = keras.utils.to_categorical(
 y_pred.argmax(axis=1),
num_classes=14 
)
 _val_f1 = f1_score(y_val, y_pred_cat, average='macro')
 _val_recall = recall_score(y_val, y_pred_cat, average='macro')
 _val_precision = precision_score(y_val, y_pred_cat, average='macro')
 self.val_f1s.append(_val_f1)
 self.val_recalls.append(_val_recall)
 self.val_precisions.append(_val_precision)
 print(( f"val_f1: {_val_f1:.4f}"
f" — val_precision: {_val_precision:.4f}"
f" — val_recall: {_val_recall:.4f}"))
return
f1_metrics = Metrics()<choose_model_class>
import pandas as pd
import numpy as np
import keras
from tqdm import tqdm
from keras import backend as K
model_densenet = DenseNet121(
 weights='.. /input/densenet-keras/DenseNet-BC-121-32-no-top.h5',
include_top=False,
input_shape=(32,32,3)
 )<choose_model_class>
lookid_data = pd.read_csv("/kaggle/input/IdLookupTable.csv")
 lookid_data.head()
model = Sequential()
model.add(model_densenet)
 model.add(layers.GlobalAveragePooling2D())
 model.add(layers.Dense(cnt_classes_images, activation='softmax'))<choose_model_class>
samplesubmission = pd.read_csv("/kaggle/input/SampleSubmission.csv")
 samplesubmission.head()
model.compile(
 loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)<choose_model_class>
train = pd.read_csv("/kaggle/input/training/training.csv")
 train.head().T
checkpoint = ModelCheckpoint(
 'model.h5',
monitor='val_acc',
verbose=1,
save_best_only=True,
save_weights_only=False,
mode='auto'
)<train_model>
train.isnull().sum()
BATCH_SIZE = 64
EPOCHS = 35
VALID_SPLIT = 0.1
history = model.fit(
 x=x_train,
y=y_train,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
callbacks=[checkpoint, f1_metrics],
validation_split=VALID_SPLIT
)<predict_on_test>
train.isnull().sum()
model.load_weights('model.h5')
 y_test = model.predict(x_test )<load_from_csv>
train.fillna(method = 'ffill',inplace = True )
submission_df = pd.read_csv(os.path.join(PATH,'sample_submission.csv'))
submission_df['Predicted'] = y_test.argmax(axis=1)
 print(submission_df.shape)
 submission_df.head(3 )<save_to_csv>
X = train.Image.values
del train['Image']
Y = train.values
submission_df.to_csv("submission.csv", index=False )<load_from_csv>
x = []
for i in tqdm(X):
q = [int(j)for j in i.split() ]
x.append(q)
 len(x )
train_data=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()<load_from_csv>
x_train,x_test,y_train,y_test = tts(x,Y,random_state = 69,test_size = 0.1 )
test_data=pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()<count_missing_values>
from keras.layers.advanced_activations import LeakyReLU
from keras.models import Sequential, Model
from keras.layers import Activation, Convolution2D, MaxPooling2D, BatchNormalization, Flatten, Dense, Dropout, Conv2D,MaxPool2D, ZeroPadding2D
train_data.isnull().sum()<count_missing_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()
test_data.isnull().sum()<count_values>
train_data['IsBadBuy'].value_counts()<count_values>
model.compile(optimizer = 'adam',loss = 'mean_squared_error', metrics = ['mae','acc'])
 model.fit(x_train,y_train,batch_size=256, epochs=50,validation_data=(x_test,y_test))
train_data['Model'].value_counts()<drop_column>
model.compile(optimizer = 'adam',loss = 'mean_squared_error', metrics = ['mae'])
 model.fit(x,Y,batch_size=64, epochs=100)
 model.fit(x,Y,batch_size=128, epochs=50)
 model.fit(x,Y,batch_size=256, epochs=50 )
train_data.drop('Model',axis=1 , inplace=True)
 test_data.drop('Model',axis=1 , inplace=True )<count_values>
test = pd.read_csv("/kaggle/input/test/test.csv")
 test.head()
train_data['Trim'].value_counts()<drop_column>
test.isnull().sum()
train_data.drop('Trim' , inplace=True ,axis=1)
 test_data.drop('Trim' , inplace=True , axis=1 )<count_values>
test.isnull().sum()
train_data['SubModel'].value_counts()<drop_column>
test = test.Image.values
x_t = []
for i in tqdm(test):
q = [int(j)for j in i.split() ]
x_t.append(q)
 x_t = np.array(x_t)
 x_t = x_t.reshape(-1, 96,96,1)
 x_t = x_t/255.0
x_t.shape
train_data.drop('SubModel' , axis=1 , inplace=True)
 test_data.drop('SubModel' , axis=1 , inplace=True )<count_values>
pred = model.predict(x_t)
 pred.shape
train_data['Color'].value_counts()<data_type_conversions>
lookid_list = list(lookid_data['FeatureName'])
 imageID = list(lookid_data['ImageId']-1)
 pre_list = list(pred )
train_data['Color'].fillna(value='Color_unknown' , inplace =True)
 test_data['Color'].fillna(value='Color_unknown' , inplace =True )<count_missing_values>
rowid = lookid_data['RowId']
rowid=list(rowid )
print('Number of null values in color column ' + str(train_data['Color'].isnull().sum()))
print('Number of null values in color column ' + str(test_data['Color'].isnull().sum()))<count_values>
preded = []
for x,y in zip(imageID,feature):
preded.append(pre_list[x][y] )
train_data['Transmission'].value_counts() 
<count_values>
submission = pd.concat([rowid,loc],axis = 1 )
test_data['Transmission'].value_counts()<filter>
submission.to_csv('Utkarsh.csv',index = False )