Kaggle competition: Facial Keypoints Detection
nn = NeuralNetworkModel(data, test, target, features, categoricals=categoricals, n_splits=10,
cv_method="StratifiedKFold", group=None, task="classification", scaler="MinMax", verbose=True )<train_model>
Train_Dir = '.. /input/training/training.csv'
Test_Dir = '.. /input/test/test.csv'
lookid_dir = '.. /input/IdLookupTable.csv'
train_data = pd.read_csv(Train_Dir)
 test_data = pd.read_csv(Test_Dir)
 lookid_data = pd.read_csv(lookid_dir)
 os.listdir('.. /input')
 train_data.fillna(method = 'ffill',inplace = True)
 imag = []
for i in range(0,7049):
img = train_data['Image'][i].split(' ')
 img = ['0' if x == '' else x for x in img]
imag.append(img)
 image_list = np.array(imag,dtype = 'float')
 X_train = image_list.reshape(-1,96,96)
 training = train_data.drop('Image',axis = 1)
 y_train = []
for i in range(0,7049):
y = training.iloc[i,:]
y_train.append(y)
 Y_train = np.array(y_train,dtype = 'float' )
lgbm = LgbModel(data, test, target, features, categoricals=categoricals, n_splits=10,
cv_method="StratifiedKFold", group=None, task="classification", scaler=None, verbose=True )<create_dataframe>
catb = CatbModel(data, test, target, features, categoricals=categoricals, n_splits=10,
cv_method="StratifiedKFold", group=None, task="classification", scaler=None, verbose=True )<load_from_csv>
def log_test(net,test_iter,ctx):
test_l = 0
for X,y in test_iter:
X, y = X.as_in_context(ctx), y.as_in_context(ctx)
 l = loss(net(X),y)
 test_l = l.mean().asscalar()
return test_l
def train(net, X_train, Y_train,num_epochs, trainer, batch_size, ctx):
train_files,val_files = train_test_split(range(len(X_train)) ,test_size=0.1,shuffle=True)
 train_data,train_label = nd.array(X_train[train_files]),nd.array(Y_train[train_files])
 val_data,val_label = nd.array(X_train[val_files]),nd.array(Y_train[val_files])
 train_iter = gdata.DataLoader(gdata.ArrayDataset(train_data,train_label),batch_size,shuffle=True)
 test_iter = gdata.DataLoader(gdata.ArrayDataset(val_data,val_label),batch_size)
 train_ls,test_ls=[],[]
print('train on:',ctx)
 for epoch in range(num_epochs):
start = time.time()
train_sum_l =0
for X,y in train_iter:
X, y = X.expand_dims(axis=1 ).as_in_context(ctx), y.as_in_context(ctx)
 with autograd.record() :
l = loss(net(X),y)
 l.backward()
trainer.step(batch_size)
 train_loss = loss(net(train_data.expand_dims(axis=1 ).as_in_context(ctx)) ,train_label.as_in_context(ctx)).mean().asscalar()
train_ls.append(train_loss)
 print()
if val_files:
test_ls.append(loss(net(val_data.expand_dims(axis=1 ).as_in_context(ctx)) ,val_label.as_in_context(ctx)).mean().asscalar())
 else:
test_ls.append(train_loss)
 print('epoch %d, train loss %.4f, test loss %.3f, time %.1f sec' %(epoch + 1,train_ls[-1], test_ls[-1], time.time() - start))
return train_ls, test_ls
submission_df = pd.read_csv('.. /input/google-cloud-ncaa-march-madness-2020-division-1-womens-tournament/WSampleSubmissionStage1_2020.csv')
 submission_df['Pred'] = 0.7 * lgbm.y_pred + 0.2 * catb.y_pred + 0.1 * nn.y_pred
submission_df<save_to_csv>
ctx=mx.gpu(0)
 resnet = vision.resnet34_v1(pretrained=False, ctx=mx.cpu() )
submission_df.to_csv('submission.csv', index=False )<import_modules>
fine_net = resnet.features
fine_net.add(nn.Dense(30))
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import random
import os<set_options>
lr, num_epochs = 0.001, 500
batch_size=128
loss = gloss.L2Loss()
net = fine_net
net.initialize(force_reinit=True,ctx=ctx, init=init.Xavier())
 net.collect_params().reset_ctx(ctx)
 trainer = gluon.Trainer(net.collect_params() ,'adam',{'learning_rate':lr} )
print("TF version: ", tf.__version__)
 if tf.__version__ < "2.0.0":
tf.enable_eager_execution()
print("Eager execution enabled.")
 else:
print("Eager execution enabled by default.")
 if tf.test.gpu_device_name() :
print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))
else:
print("Please install GPU version of TF" )<define_variables>
train_ls,test_ls = train(net, X_train, Y_train,num_epochs, trainer, batch_size, ctx )
def setseed(seed = 0):
random.seed(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 os.environ['TF_DETERMINISTIC_OPS'] = '1'
SEED = 0
setseed(SEED)
 setseed()<load_from_csv>
Y_test = net(nd.array(X_test ).expand_dims(axis=1 ).as_in_context(ctx))
Y_test[0]
<import_modules><EOS> 
lookid_list = list(lookid_data['FeatureName'])
 imageID = list(lookid_data['ImageId']-1)
 pre_list = list(Y_test.asnumpy())
 rowid = lookid_data['RowId']
rowid=list(rowid)
 feature = []
for f in list(lookid_data['FeatureName']):
feature.append(lookid_list.index(f))
preded = []
for x,y in zip(imageID,feature):
preded.append(pre_list[x][y] if pre_list[x][y]<96 else 96)
 rowid = pd.Series(rowid,name = 'RowId')
 loc = pd.Series(preded,name = 'Location')
 submission = pd.concat([rowid,loc],axis = 1)
 submission.to_csv('resnet18_submission_0616.csv',index = False )