Kaggle competition: Lyft Motion Prediction for Autonomous Vehicles
<SOS> metric: PostProcessorKernel Kaggle data source: lyft-motion-prediction-autonomous-vehicles<drop_column>
import pandas as pd
import tensorflow as tf
import numpy as np
from kaggle_datasets import KaggleDatasets
from l5kit.evaluation import write_pred_csv
import time
print(group['players in group'].nlargest(5))
del match,group<count_unique_values>
sub = pd.read_csv('.. /input/lyft-motion-prediction-autonomous-vehicles/multi_mode_sample_submission.csv')
 TEST_DATA_DIR = 'lyft-test-tfrecords'
TEST_GCS_PATH = KaggleDatasets().get_gcs_path(TEST_DATA_DIR)
 test_files = tf.io.gfile.glob(TEST_GCS_PATH + '/test' +'/shard*.tfrecord' )
subset = train[train['matchId']=='41a634f62f86b7']
sub_grp = subset[subset['groupId']=='128b07271aa012']
print('matchId=='41a634f62f86b7' & groupId=='128b07271aa012'')
 print('-'*50)
 print('players:',len(subset))
print('groups:',subset['groupId'].nunique())
 print('numGroups:',subset['numGroups'].unique())
 print('maxPlace:',subset['maxPlace'].unique())
 print('-'*50)
 print('max-group players:',len(sub_grp))
print('max-group winPlacePerc:',sub_grp['winPlacePerc'].unique())
 print('-'*50)
 print('winPlacePerc:',subset['winPlacePerc'].sort_values().unique() )<groupby>
feature_descriptions = {
'image': tf.io.FixedLenFeature([], tf.string),
'target_positions': tf.io.FixedLenFeature([], tf.string),
'target_availabilities': tf.io.FixedLenFeature([], tf.string),
'target_yaws': tf.io.FixedLenFeature([], tf.string),
'world_from_agent' : tf.io.FixedLenFeature([], tf.string),
'history_positions': tf.io.FixedLenFeature([], tf.string),
'history_yaws': tf.io.FixedLenFeature([], tf.string),
'history_availabilities': tf.io.FixedLenFeature([], tf.string),
'world_to_image': tf.io.FixedLenFeature([], tf.string),
'track_id': tf.io.FixedLenFeature([], tf.string),
'timestamp': tf.io.FixedLenFeature([], tf.string),
'centroid': tf.io.FixedLenFeature([], tf.string),
'yaw': tf.io.FixedLenFeature([], tf.string),
'extent': tf.io.FixedLenFeature([], tf.string),
}
feature_dtypes = {
'image': tf.uint8,
'target_positions': tf.float32,
'target_availabilities': tf.float32,
'target_yaws': tf.float32,
'world_from_agent' : tf.float64,
'history_positions': tf.float32,
'history_yaws':tf.float32,
'history_availabilities': tf.float32,
'world_to_image': tf.float64,
'track_id': tf.int64,
'timestamp': tf.int64,
'centroid': tf.float64,
'yaw': tf.float64,
'extent': tf.float32,
}
train.groupby('matchId' ).agg({'matchDuration':'var'} ).fillna(0 ).query('matchDuration != 0' )<count_values>
AUTO = tf.data.experimental.AUTOTUNE
GLOBAL_BATCH_SIZE = 64*strategy.num_replicas_in_sync
IMG_SIZE = 224
CHANNEL_DIM = 25
TOTAL_STEPS = np.int(np.ceil(len(sub)/ GLOBAL_BATCH_SIZE))
cols = ['killPoints','rankPoints','winPoints']
querys = ['rankPoints <= 0 & killPoints != 0', 'rankPoints <= 0 & winPoints != 0',
'killPoints == 0 & rankPoints != 0', 'killPoints == 0 & winPoints != 0',
'winPoints == 0 & rankPoints != 0', 'winPoints == 0 & killPoints != 0']
for q in querys:
print('count(%s):' % q, len(train[cols].query(q)) )<count_unique_values>
def padded_batch(images, timestamp , track_id, world_from_agent, target, avail):
if tf.shape(timestamp)[0] != GLOBAL_BATCH_SIZE:
images = tf.concat([images , tf.zeros(( GLOBAL_BATCH_SIZE - tf.shape(images)[0] , IMG_SIZE, IMG_SIZE, CHANNEL_DIM)) ] , axis = 0)
 timestamp = tf.concat([timestamp , tf.zeros(( GLOBAL_BATCH_SIZE - tf.shape(timestamp)[0]), tf.int64)] , axis = 0)
 track_id = tf.concat([track_id , tf.zeros(( GLOBAL_BATCH_SIZE - tf.shape(track_id)[0]), dtype = tf.int64)] , axis = 0)
 world_from_agent = tf.concat([world_from_agent , tf.zeros(( GLOBAL_BATCH_SIZE - tf.shape(world_from_agent)[0] , 3, 3), tf.float64)] , axis = 0)
 return images , timestamp , track_id , world_from_agent , target , avail
def read_unlabeled_tfrecord(example):
example = tf.io.parse_single_example(example, feature_descriptions)
 data = {k:tf.io.parse_tensor(example[k], feature_dtypes[k])for k in example}
image = tf.image.convert_image_dtype(data['image'], dtype = tf.float32)
 image = tf.transpose(tf.squeeze(image), [1,2,0])
 image = tf.reshape(image,(IMG_SIZE, IMG_SIZE, CHANNEL_DIM))
timestamp = tf.squeeze(data['timestamp'])
 track_id = tf.squeeze(data['track_id'])
 world_from_agent = tf.squeeze(data['world_from_agent'])
 target = tf.squeeze(data['target_positions'])
 avail = tf.squeeze(data['target_availabilities'])
 return image , timestamp , track_id , world_from_agent , target, avail
def load_dataset(filenames, ordered = True, training = False):
dataset = tf.data.TFRecordDataset(filenames,compression_type = 'GZIP')
 dataset = dataset.map(read_unlabeled_tfrecord, num_parallel_calls = AUTO ).batch(GLOBAL_BATCH_SIZE,)
 dataset = dataset.map(padded_batch , num_parallel_calls = AUTO)
 return dataset
<count_unique_values>
def get_dataset(files):
dataset = load_dataset(files , training = False)
 dataset = dataset.prefetch(AUTO)
 test_dataset = strategy.experimental_distribute_dataset(dataset)
 return test_dataset
print('match count:', train['matchId'].nunique())
 maxPlacePerc = train.groupby('matchId')['winPlacePerc'].max()
print('match [not contains 1st place]:', len(maxPlacePerc[maxPlacePerc != 1]))
del maxPlacePerc
edge = train[(train['maxPlace'] > 1)&(train['numGroups'] == 1)]
print('match [maxPlace>1 & numGroups==1]:', len(edge.groupby('matchId')))
 print(' - unique winPlacePerc:', edge['winPlacePerc'].unique() )<concatenate>
def transform_points(points, transf_matrix):
transf_matrix = tf.expand_dims(transf_matrix , axis = -1)
 assert len(points.shape)== len(transf_matrix.shape)== 4,(
 f"dimensions mismatch, both points({points.shape})and "
f"transf_matrix({transf_matrix.shape})needs to be tensors of rank 4." 
)
 if points.shape[3] not in [2, 3]:
raise AssertionError(f"Points input should be(N, 2)or(N, 3)shape, received {points.shape}")
 assert points.shape[3] == transf_matrix.shape[2] - 1, "points dim should be one less than matrix dim"
points = tf.cast(points , tf.float64)
 points = tf.matmul(points , tf.transpose(transf_matrix[:, :-1, :-1, :] , perm = [0,3,2,1]))
return tf.cast(points , tf.float32 )
pd.concat([train[train['winPlacePerc'] == 1].head(5),
train[train['winPlacePerc'] == 0].head(5)],
keys=['winPlacePerc_1', 'winPlacePerc_0'] )<concatenate>
def modified_resnet50() :
pretrained_model = tf.keras.applications.ResNet50(include_top=False, weights='imagenet', input_shape =(None, None, 3))
modified_model = tf.keras.applications.ResNet50(include_top=False, weights= None, input_shape =(None, None, 25))
for pretrained_model_layer , modified_model_layer in zip(pretrained_model.layers , modified_model.layers):
layer_to_modify = ['conv1_conv']
if pretrained_model_layer.name in layer_to_modify :
kernel = pretrained_model_layer.get_weights() [0]
bias = pretrained_model_layer.get_weights() [1]
weights = np.concatenate(( kernel[:, :, -1: ,:] , np.tile(kernel , [1, 1, 8, 1]),), axis= -2)
 modified_model_layer.set_weights(( weights , bias))
else:
modified_model_layer.set_weights(pretrained_model_layer.get_weights())
 return modified_model
all_data = train.append(test, sort=False ).reset_index(drop=True)
 del train, test
gc.collect()<groupby>
class LyftModel(tf.keras.Model):
def __init__(self ,num_modes = 3,future_pred_frames = 50 ,):
super(LyftModel , self ).__init__()
self.conv1 = tf.keras.layers.Conv2D(3,kernel_size=1,use_bias=False,padding="same" ,)
 self.bn1 = tf.keras.layers.BatchNormalization()
self.relu = tf.keras.layers.ReLU()
self.model = tf.keras.applications.ResNet50(include_top=False, weights='imagenet',)
 self.gap = tf.keras.layers.GlobalAveragePooling2D()
self.dropout = tf.keras.layers.Dropout(0.2)
 self.future_len = num_modes * future_pred_frames * 2
self.future_pred_frames = future_pred_frames
self.num_modes = num_modes
self.dense1 = tf.keras.layers.Dense(self.future_len + self.num_modes ,)
 def call(self, inputs):
x = self.conv1(inputs)
 x = self.bn1(x)
 x = self.relu(x)
 x = self.model(x)
 x = self.gap(x)
 x = self.dropout(x)
 x = self.dense1(x)
 batch_size, _ = x.shape
pred , confidence = tf.split(x , num_or_size_splits = [self.future_len, self.num_modes], axis = 1)
 assert confidence.shape ==(batch_size , self.num_modes), f'confidence shape got {confidence.shape}'
pred = tf.reshape(pred , shape =(batch_size ,self.num_modes, self.future_pred_frames , 2))
confidence = tf.nn.softmax(confidence , axis = 1)
 return pred , confidence
match = all_data.groupby('matchId')
 all_data['killPlacePerc'] = match['kills'].rank(pct=True ).values<data_type_conversions>
def get_model() :
with strategy.scope() :
model = LyftModel()
model.build(( GLOBAL_BATCH_SIZE , None, None, CHANNEL_DIM))
model.summary()
model.load_weights('.. /input/lyft-resnet-model/epoch 45 and val_loss 23.7635 model.h5')
 transf_points = lambda pred , world_from_agent : transform_points(pred, world_from_agent)
 return model , transf_points
distance =(all_data['rideDistance'] + all_data['walkDistance'] + all_data['swimDistance'])
 all_data['zombi'] =(( distance == 0)&(all_data['kills'] == 0)
 &(all_data['weaponsAcquired'] == 0)
 &(all_data['matchType'].str.contains('solo')) ).astype(int)
 all_data['cheater'] =(( all_data['kills'] / distance >= 1)
 |(all_data['kills'] > 30)|(all_data['roadKills'] > 10)).astype(int)
 del distance
pd.concat([all_data['zombi'].value_counts() , all_data['cheater'].value_counts() ], axis=1 ).T<feature_engineering>
@tf.function
def test_step(image , world_from_agent):
pred , confidence = model(image , training = False)
 pred = transf_points(pred , world_from_agent)
 return pred , confidence
all_data.drop(['rankPoints','killPoints','winPoints'], axis=1, inplace=True )<data_type_conversions>
def model_predict(test_dataset):
start_time = epoch_start_time = time.time()
future_coord_offset = []
timestamps = []
track_ids = []
confs = []
print(f'predicting {TOTAL_STEPS} steps')
 for step,(images, timestamp, track_id , world_from_agent , target , avail),in enumerate(test_dataset):
pred , confidence = strategy.run(test_step, args=(images, world_from_agent))
global_batch_pred = np.concatenate([pred.values[i].numpy() for i in range(strategy.num_replicas_in_sync)], axis = 0)
 global_batch_confs = np.concatenate([confidence.values[i].numpy() for i in range(strategy.num_replicas_in_sync)], axis = 0)
 global_batch_timestamps = np.concatenate([timestamp.values[i].numpy() for i in range(strategy.num_replicas_in_sync)], axis = 0)
 global_batch_track_ids = np.concatenate([track_id.values[i].numpy() for i in range(strategy.num_replicas_in_sync)], axis = 0)
 future_coord_offset.append(global_batch_pred)
 confs.append(global_batch_confs)
 timestamps.append(global_batch_timestamps)
 track_ids.append(global_batch_track_ids)
 print('=' , end = ' ' , flush = True)
 epoch_time = time.time() - epoch_start_time
print('time: {:0.1f}s'.format(epoch_time))
return future_coord_offset, timestamps , track_ids, confs
def fillInf(df, val):
numcols = df.select_dtypes(include='number' ).columns
cols = numcols[numcols != 'winPlacePerc']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
for c in cols: df[c].fillna(val, inplace=True )<feature_engineering>
test_dataset = get_dataset(test_files)
 model , transf_points = get_model()
future_coord_offset, timestamps , track_ids , confs = model_predict(test_dataset )
<drop_column><EOS> 
pred_path = 'submission.csv'
test_length = len(sub)
 write_pred_csv(pred_path,
timestamps = np.concatenate(timestamps, axis = 0)[0:test_length],
track_ids = np.concatenate(track_ids , axis = 0)[0:test_length],
coords = np.concatenate(future_coord_offset , axis = 0)[0:test_length],
confs = np.concatenate(confs , axis = 0)[0:test_length],
)