Kaggle competition: Indoor Location & Navigation
<SOS> metric: IndoorLocalization Kaggle data source: indoor-location-&-navigation<train_model>
!git clone --depth 1 https://github.com/location-competition/indoor-location-competition-20 indoor_location_competition_20
!rm -rf indoor_location_competition_20/data
n_seeds = 5
n_splits = 5
shuffle = True
params = {"n_layers": 4, "rate": 0.25}
callbacks_params = {"patience_for_es": 20, "save_best_only": True}
fit_params = {"epochs": 1_000}<train_on_grid>
import multiprocessing
import numpy as np
import pandas as pd
import scipy.interpolate
import scipy.sparse
from tqdm import tqdm
from indoor_location_competition_20.io_f import read_data_file
import indoor_location_competition_20.compute_f as compute_f
%%time
Y_pred = np.zeros(( train_size, n_classes))
Y_pred = pd.DataFrame(Y_pred, columns=Y.columns, index=Y.index)
 for i in range(n_seeds):
set_seed(seed=i)
 cv = MultilabelStratifiedGroupKFold(
 n_splits=n_splits, random_state=i, shuffle=shuffle 
)
 for j,(train, valid)in enumerate(cv.split(X, Y, groups)) :
model_path = f"model_seed_{i}_fold_{j}.h5"
model = build_model(n_features, n_classes, **params)
 callbacks = build_callbacks(model_path, **callbacks_params)
 model.fit(
 X.iloc[train],
Y.iloc[train],
callbacks=callbacks,
validation_data=(X.iloc[valid], Y.iloc[valid]),
**fit_params, 
)
 model.load_weights(model_path)
 Y_pred.iloc[valid] += model.predict(X.iloc[valid])/ n_seeds
Y_pred[train_features["cp_type"] == "ctl_vehicle"] = 0.0<compute_test_metric>
INPUT_PATH = '.. /input/indoor-location-navigation'
score(Y, Y_pred )<prepare_output>
def compute_rel_positions(acce_datas, ahrs_datas):
step_timestamps, step_indexs, step_acce_max_mins = compute_f.compute_steps(acce_datas)
 headings = compute_f.compute_headings(ahrs_datas)
 stride_lengths = compute_f.compute_stride_length(step_acce_max_mins)
 step_headings = compute_f.compute_step_heading(step_timestamps, headings)
 rel_positions = compute_f.compute_rel_positions(stride_lengths, step_headings)
 return rel_positions
%%time
Y_pred = np.zeros(( test_size, n_classes))
Y_pred = pd.DataFrame(Y_pred, columns=Y.columns, index=X_test.index)
 for i in range(n_seeds):
for j in range(n_splits):
model = tf.keras.models.load_model(
 f"model_seed_{i}_fold_{j}.h5",
compile=False,
custom_objects={"ModelWithMixup": ModelWithMixup}, 
)
 Y_pred += model.predict(X_test)/ n_seeds / n_splits
Y_pred[test_features["cp_type"] == "ctl_vehicle"] = 0.0<save_to_csv>
def correct_path(args):
path, path_df = args
T_ref = path_df['timestamp'].values
xy_hat = path_df[['x', 'y']].values
example = read_data_file(f'{INPUT_PATH}/test/{path}.txt')
 rel_positions = compute_rel_positions(example.acce, example.ahrs)
 if T_ref[-1] > rel_positions[-1, 0]:
rel_positions = [np.array([[0, 0, 0]]), rel_positions, np.array([[T_ref[-1], 0, 0]])]
else:
rel_positions = [np.array([[0, 0, 0]]), rel_positions]
rel_positions = np.concatenate(rel_positions)
 T_rel = rel_positions[:, 0]
delta_xy_hat = np.diff(scipy.interpolate.interp1d(T_rel, np.cumsum(rel_positions[:, 1:3], axis=0), axis=0 )(T_ref), axis=0)
 N = xy_hat.shape[0]
delta_t = np.diff(T_ref)
 alpha =(8.1)**(-2)* np.ones(N)
 beta =(0.3 + 0.3 * 1e-3 * delta_t)**(-2)
 A = scipy.sparse.spdiags(alpha, [0], N, N)
 B = scipy.sparse.spdiags(beta, [0], N-1, N-1)
 D = scipy.sparse.spdiags(np.stack([-np.ones(N), np.ones(N)]), [0, 1], N-1, N)
 Q = A +(D.T @ B @ D)
 c =(A @ xy_hat)+(D.T @(B @ delta_xy_hat))
xy_star = scipy.sparse.linalg.spsolve(Q, c)
 return pd.DataFrame({
'site_path_timestamp' : path_df['site_path_timestamp'],
'floor' : path_df['floor'],
'x' : xy_star[:, 0],
'y' : xy_star[:, 1],
} )
<import_modules><EOS> 
sub = pd.read_csv('.. /input/simple-99-accurate-floor-model/submission.csv')
 tmp = sub['site_path_timestamp'].apply(lambda s : pd.Series(s.split('_')))
 sub['site'] = tmp[0]
sub['path'] = tmp[1]
sub['timestamp'] = tmp[2].astype(float)
 processes = multiprocessing.cpu_count()
with multiprocessing.Pool(processes=processes)as pool:
dfs = pool.imap_unordered(correct_path, sub.groupby('path'))
dfs = tqdm(dfs)
 dfs = list(dfs)
 sub = pd.concat(dfs ).sort_values('site_path_timestamp')
 sub.to_csv('submission.csv', index=False )