Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<set_options>
%matplotlib inline
def set_seed(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 set_seed(42 )<init_hyperparams>
train_df = pd.read_csv('.. /input/train.csv')
 test_df = pd.read_csv('.. /input/test.csv')
 combine = [train_df, test_df]
cfg = {
'format_version': 4,
'data_path': "/kaggle/input/lyft-motion-prediction-autonomous-vehicles",
'model_params': {
'model_architecture': 'resnet34',
'history_num_frames': 10,
'history_step_size': 1,
'history_delta_time': 0.55,
'future_num_frames': 50,
'future_step_size': 1,
'future_delta_time': 0.55,
'model_name': "model_resnet34_output",
'lr': 1e-3,
'weight_path': ".. /input/colab-resnet/full_train_output_sampled_512_8652492800k.pth",
'train': False,
'predict': True
},
'raster_params': {
'raster_size': [224, 224],
'pixel_size': [0.5, 0.5],
'ego_center': [0.25, 0.5],
'map_type': 'py_semantic',
'satellite_map_key': 'aerial_map/aerial_map.png',
'semantic_map_key': 'semantic_map/semantic_map.pb',
'dataset_meta_key': 'meta.json',
'filter_agents_threshold': 0.5
},
'train_data_loader': {
'key': 'scenes/train.zarr',
'batch_size': 16,
'shuffle': True,
'num_workers': 4
},
'test_data_loader': {
'key': 'scenes/test.zarr',
'batch_size': 32,
'shuffle': False,
'num_workers': 4
},
'train_params': {
'max_num_steps': 9001,
'checkpoint_every_n_steps': 500,
}
}<feature_engineering>
print("Percentage of survival in the train set: {}%".format(round(sum(train_df.Survived)/train_df.Survived.count() , 2)) )
DIR_INPUT = cfg["data_path"]
os.environ["L5KIT_DATA_FOLDER"] = DIR_INPUT
dm = LocalDataManager(None )<create_dataframe>
train_df[['Pclass', 'Survived']].groupby(['Pclass'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
train_cfg = cfg["train_data_loader"]
rasterizer = build_rasterizer(cfg, dm)
 train_zarr = ChunkedDataset(dm.require(train_cfg["key"])).open()
train_dataset = AgentDataset(cfg, train_zarr, rasterizer)
 train_dataloader = DataLoader(train_dataset, shuffle=train_cfg["shuffle"], batch_size=train_cfg["batch_size"],
num_workers=train_cfg["num_workers"])
 print("==================================TRAIN DATA==================================")
 print(train_dataset )<create_dataframe>
train_df[["Sex", "Survived"]].groupby(['Sex'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
test_cfg = cfg["test_data_loader"]
rasterizer = build_rasterizer(cfg, dm)
 test_zarr = ChunkedDataset(dm.require(test_cfg["key"])).open()
test_mask = np.load(f"{DIR_INPUT}/scenes/mask.npz")["arr_0"]
test_dataset = AgentDataset(cfg, test_zarr, rasterizer, agents_mask=test_mask)
 test_dataloader = DataLoader(test_dataset,shuffle=test_cfg["shuffle"],batch_size=test_cfg["batch_size"],
num_workers=test_cfg["num_workers"])
 print("==================================TEST DATA==================================")
 print(test_dataset )<choose_model_class>
train_df[["SibSp", "Survived"]].groupby(['SibSp'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
class LyftMultiModel(nn.Module):
def __init__(self, cfg: Dict, num_modes=3):
super().__init__()
architecture = cfg["model_params"]["model_architecture"]
backbone = eval(architecture )(pretrained=True, progress=True)
 self.backbone = backbone
num_history_channels =(cfg["model_params"]["history_num_frames"] + 1)* 2
num_in_channels = 3 + num_history_channels
self.backbone.conv1 = nn.Conv2d(
 num_in_channels,
self.backbone.conv1.out_channels,
kernel_size=self.backbone.conv1.kernel_size,
stride=self.backbone.conv1.stride,
padding=self.backbone.conv1.padding,
bias=False, 
)
 if architecture == "resnet50":
backbone_out_features = 2048
else:
backbone_out_features = 512
self.future_len = cfg["model_params"]["future_num_frames"]
num_targets = 2 * self.future_len
self.head = nn.Sequential(
 nn.Linear(in_features=backbone_out_features, out_features=4096), 
)
 self.num_preds = num_targets * num_modes
self.num_modes = num_modes
self.logit = nn.Linear(4096, out_features=self.num_preds + num_modes)
 def forward(self, x):
x = self.backbone.conv1(x)
 x = self.backbone.bn1(x)
 x = self.backbone.relu(x)
 x = self.backbone.maxpool(x)
 x = self.backbone.layer1(x)
 x = self.backbone.layer2(x)
 x = self.backbone.layer3(x)
 x = self.backbone.layer4(x)
 x = self.backbone.avgpool(x)
 x = torch.flatten(x, 1)
 x = self.head(x)
 x = self.logit(x)
 bs, _ = x.shape
pred, confidences = torch.split(x, self.num_preds, dim=1)
 pred = pred.view(bs, self.num_modes, self.future_len, 2)
 assert confidences.shape ==(bs, self.num_modes)
 confidences = torch.softmax(confidences, dim=1)
 return pred, confidences<compute_train_metric>
train_df[["Parch", "Survived"]].groupby(['Parch'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
def forward(data, model, device, criterion = pytorch_neg_multi_log_likelihood_batch):
inputs = data["image"].to(device)
 target_availabilities = data["target_availabilities"].to(device)
 targets = data["target_positions"].to(device)
 preds, confidences = model(inputs)
 loss = criterion(targets, preds, confidences, target_availabilities)
 return loss, preds, confidences<choose_model_class>
train_df[["Embarked", "Survived"]].groupby(['Embarked'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model = LyftMultiModel(cfg)
 print(f'device {device}')
 weight_path = cfg["model_params"]["weight_path"]
if weight_path:
checkpoint = torch.load(weight_path)
 model.load_state_dict(checkpoint['state_dict'])
 model.cuda()
optimizer = optim.AdamW(model.parameters() , lr=cfg["model_params"]["lr"], betas=(0.9, 0.999), eps=1e-05, weight_decay=0.001)
 optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
 for state in optimizer.state.values() :
for k, v in state.items() :
if isinstance(v, torch.Tensor):
state[k] = v.cuda() 
<init_hyperparams>
for dataset in combine:
df = dataset.groupby(['Sex', 'Pclass'] ).size().unstack(0)
 df['fem_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]]))
print(df)
 print("_"*40 )
if cfg["model_params"]["train"]:
tr_it = iter(train_dataloader)
 progress_bar = tqdm(range(cfg["train_params"]["max_num_steps"]))
num_iter = cfg["train_params"]["max_num_steps"]
losses_train = []
iterations = []
metrics = []
times = []
model_name = cfg["model_params"]["model_name"]
start = time.time()
for i in progress_bar:
try:
data = next(tr_it)
 except StopIteration:
tr_it = iter(train_dataloader)
 data = next(tr_it)
 model.train()
torch.set_grad_enabled(True)
 loss, _, _ = forward(data, model, device)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
losses_train.append(loss.item())
 progress_bar.set_description(f"loss: {loss.item() } loss(avg): {np.mean(losses_train)}")
 if i % cfg['train_params']['checkpoint_every_n_steps'] == 0:
torch.save(model.state_dict() , f'{model_name}_{i}.pth')
 iterations.append(i)
 metrics.append(np.mean(losses_train))
times.append(( time.time() -start)/60)
 results = pd.DataFrame({'iterations': iterations, 'metrics(avg)': metrics, 'elapsed_time(mins)': times})
 results.to_csv(f"train_metrics_{model_name}_{num_iter}.csv", index = False)
 print(f"Total training time is {(time.time() -start)/60} mins")
 print(results.head() )<save_to_csv>
train_df[["Sex", "Pclass", "Survived"]].groupby(['Sex', 'Pclass'],
as_index=True ).mean()
pred_path = 'submission.csv'
write_pred_csv(pred_path,
timestamps=np.concatenate(timestamps),
track_ids=np.concatenate(agent_ids),
coords=np.concatenate(future_coords_offsets_pd),
confs = np.concatenate(confidences_list)
 )<import_modules>
for dataset in combine:
df = dataset.groupby(['Sex', 'Parch'] ).size().unstack(0 ).fillna(0)
 df['male_perc'] =(df[df.columns[1]]/(df[df.columns[0]] + df[df.columns[1]]))
df['fem_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]]))
print(df)
 print("_"*40 )
warnings.filterwarnings("ignore" )<import_modules>
train_df[["Sex", "Parch", "Survived"]].groupby(['Sex', 'Parch'],
as_index=True ).mean()
l5kit.__version__<set_options>
for dataset in combine:
df = dataset.groupby(['Sex', 'SibSp'] ).size().unstack(0 ).fillna(0)
 df['male_perc'] =(df[df.columns[1]]/(df[df.columns[0]] + df[df.columns[1]]))
df['fem_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]]))
print(df)
 print("_"*40 )
def set_seed(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 set_seed(42 )<init_hyperparams>
train_df[["Sex", "SibSp", "Survived"]].groupby(['Sex', 'SibSp'],
as_index=True ).mean()
cfg = {
'format_version': 4,
'data_path': "/kaggle/input/lyft-motion-prediction-autonomous-vehicles",
'model_params': {
'model_architecture': 'resnet34',
'history_num_frames': 10,
'history_step_size': 1,
'history_delta_time': 0.1,
'future_num_frames': 50,
'future_step_size': 1,
'future_delta_time': 0.1,
'model_name': "model_resnet34_output",
'lr': 1e-3,
'weight_path': "/kaggle/input/lyft-pretrained-model-hv/model_multi_update_lyft_public.pth",
'train': False,
'predict': True
},
'raster_params': {
'raster_size': [224, 224],
'pixel_size': [0.5, 0.5],
'ego_center': [0.25, 0.5],
'map_type': 'py_semantic',
'satellite_map_key': 'aerial_map/aerial_map.png',
'semantic_map_key': 'semantic_map/semantic_map.pb',
'dataset_meta_key': 'meta.json',
'filter_agents_threshold': 0.5
},
'train_data_loader': {
'key': 'scenes/train.zarr',
'batch_size': 16,
'shuffle': True,
'num_workers': 4
},
'test_data_loader': {
'key': 'scenes/test.zarr',
'batch_size': 32,
'shuffle': False,
'num_workers': 4
},
'train_params': {
'max_num_steps': 101,
'checkpoint_every_n_steps': 20,
}
}<feature_engineering>
for dataset in combine:
df = dataset.groupby(['Sex', 'Embarked'] ).size().unstack(0)
 df['male_perc'] =(df[df.columns[1]]/(df[df.columns[0]] + df[df.columns[1]]))
df['fem_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]]))
print(df)
 print("_"*40 )
DIR_INPUT = cfg["data_path"]
os.environ["L5KIT_DATA_FOLDER"] = DIR_INPUT
dm = LocalDataManager(None )<create_dataframe>
train_df[["Embarked", "Sex", "Survived"]].groupby(['Sex', 'Embarked'],
as_index=True ).mean()
train_cfg = cfg["train_data_loader"]
rasterizer = build_rasterizer(cfg, dm)
 train_zarr = ChunkedDataset(dm.require(train_cfg["key"])).open()
train_dataset = AgentDataset(cfg, train_zarr, rasterizer)
 train_dataloader = DataLoader(train_dataset, shuffle=train_cfg["shuffle"], batch_size=train_cfg["batch_size"],
num_workers=train_cfg["num_workers"])
 print("==================================TRAIN DATA==================================")
 print(train_dataset )<create_dataframe>
for dataset in combine:
df = dataset.groupby(['Pclass', 'Embarked'] ).size().unstack(0)
 df['first_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]] + df[df.columns[2]]))
df['second_perc'] =(df[df.columns[1]]/(df[df.columns[0]] + df[df.columns[1]] + df[df.columns[2]]))
df['third_perc'] =(df[df.columns[2]]/(df[df.columns[0]] + df[df.columns[1]] + df[df.columns[2]]))
print(df)
 print("_"*40 )
test_cfg = cfg["test_data_loader"]
rasterizer = build_rasterizer(cfg, dm)
 test_zarr = ChunkedDataset(dm.require(test_cfg["key"])).open()
test_mask = np.load(f"{DIR_INPUT}/scenes/mask.npz")["arr_0"]
test_dataset = AgentDataset(cfg, test_zarr, rasterizer, agents_mask=test_mask)
 test_dataloader = DataLoader(test_dataset,shuffle=test_cfg["shuffle"],batch_size=test_cfg["batch_size"],
num_workers=test_cfg["num_workers"])
 print("==================================TEST DATA==================================")
 print(test_dataset )<choose_model_class>
train_df[["Embarked", "Pclass", "Survived"]].groupby(['Embarked', 'Pclass'],
as_index=True ).mean()
class LyftMultiModel(nn.Module):
def __init__(self, cfg: Dict, num_modes=3):
super().__init__()
architecture = cfg["model_params"]["model_architecture"]
backbone = eval(architecture )(pretrained=True, progress=True)
 self.backbone = backbone
num_history_channels =(cfg["model_params"]["history_num_frames"] + 1)* 2
num_in_channels = 3 + num_history_channels
self.backbone.conv1 = nn.Conv2d(
 num_in_channels,
self.backbone.conv1.out_channels,
kernel_size=self.backbone.conv1.kernel_size,
stride=self.backbone.conv1.stride,
padding=self.backbone.conv1.padding,
bias=False, 
)
 if architecture == "resnet50":
backbone_out_features = 2048
else:
backbone_out_features = 512
self.future_len = cfg["model_params"]["future_num_frames"]
num_targets = 2 * self.future_len
self.head = nn.Sequential(
 nn.Linear(in_features=backbone_out_features, out_features=4096), 
)
 self.num_preds = num_targets * num_modes
self.num_modes = num_modes
self.logit = nn.Linear(4096, out_features=self.num_preds + num_modes)
 def forward(self, x):
x = self.backbone.conv1(x)
 x = self.backbone.bn1(x)
 x = self.backbone.relu(x)
 x = self.backbone.maxpool(x)
 x = self.backbone.layer1(x)
 x = self.backbone.layer2(x)
 x = self.backbone.layer3(x)
 x = self.backbone.layer4(x)
 x = self.backbone.avgpool(x)
 x = torch.flatten(x, 1)
 x = self.head(x)
 x = self.logit(x)
 bs, _ = x.shape
pred, confidences = torch.split(x, self.num_preds, dim=1)
 pred = pred.view(bs, self.num_modes, self.future_len, 2)
 assert confidences.shape ==(bs, self.num_modes)
 confidences = torch.softmax(confidences, dim=1)
 return pred, confidences<compute_train_metric>
for dataset in combine:
fil1 =(dataset.Cabin.isnull())
 fil2 =(dataset.Cabin.notnull())
 dataset.loc[fil1, 'Cabin'] = 0
dataset.loc[fil2, 'Cabin'] = 1
dataset.Cabin = pd.to_numeric(dataset['Cabin'])
 print(train_df.Cabin.value_counts())
 print("_"*40)
 print(test_df.Cabin.value_counts() )
def forward(data, model, device, criterion = pytorch_neg_multi_log_likelihood_batch):
inputs = data["image"].to(device)
 target_availabilities = data["target_availabilities"].to(device)
 targets = data["target_positions"].to(device)
 preds, confidences = model(inputs)
 loss = criterion(targets, preds, confidences, target_availabilities)
 return loss, preds, confidences<choose_model_class>
train_df[['Cabin', 'Survived']].groupby(['Cabin'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 model = LyftMultiModel(cfg)
 weight_path = cfg["model_params"]["weight_path"]
if weight_path:
model.load_state_dict(torch.load(weight_path))
model.to(device)
 optimizer = optim.Adam(model.parameters() , lr=cfg["model_params"]["lr"])
 print(f'device {device}' )<train_model>
for dataset in combine:
df = dataset.groupby(['Sex', 'Cabin'] ).size().unstack(0)
 df['fem_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]]))
df['male_perc'] =(df[df.columns[1]]/(df[df.columns[0]] + df[df.columns[1]]))
print(df)
 print("_"*40 )
print(model )<init_hyperparams>
train_df[["Cabin", "Sex", "Survived"]].groupby(['Sex', 'Cabin'],
as_index=True ).mean()
if cfg["model_params"]["train"]:
tr_it = iter(train_dataloader)
 progress_bar = tqdm(range(cfg["train_params"]["max_num_steps"]))
num_iter = cfg["train_params"]["max_num_steps"]
losses_train = []
iterations = []
metrics = []
times = []
model_name = cfg["model_params"]["model_name"]
start = time.time()
for i in progress_bar:
try:
data = next(tr_it)
 except StopIteration:
tr_it = iter(train_dataloader)
 data = next(tr_it)
 model.train()
torch.set_grad_enabled(True)
 loss, _, _ = forward(data, model, device)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
losses_train.append(loss.item())
 progress_bar.set_description(f"loss: {loss.item() } loss(avg): {np.mean(losses_train)}")
 if i % cfg['train_params']['checkpoint_every_n_steps'] == 0:
torch.save(model.state_dict() , f'{model_name}_{i}.pth')
 iterations.append(i)
 metrics.append(np.mean(losses_train))
times.append(( time.time() -start)/60)
 results = pd.DataFrame({'iterations': iterations, 'metrics(avg)': metrics, 'elapsed_time(mins)': times})
 results.to_csv(f"train_metrics_{model_name}_{num_iter}.csv", index = False)
 print(f"Total training time is {(time.time() -start)/60} mins")
 print(results.head() )<save_to_csv>
for dataset in combine:
df = dataset.groupby(['Cabin', 'Pclass'] ).size().unstack(0)
 df['miss_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]]))
print(df)
 print("_"*40 )
pred_path = 'submission.csv'
write_pred_csv(pred_path,
timestamps=np.concatenate(timestamps),
track_ids=np.concatenate(agent_ids),
coords=np.concatenate(future_coords_offsets_pd),
confs = np.concatenate(confidences_list)
 )<import_modules>
train_df[["Cabin", "Pclass", "Survived"]].groupby(['Pclass', 'Cabin'],
as_index=True ).mean()
TL_FACE_DTYPE,
filter_agents_by_labels,
filter_tl_faces_by_frames,
get_agents_slice_from_frames,
get_tl_faces_slice_from_frames, 
)
 def generate_kinetic_agent_sample(
 state_index: int,
frames: np.ndarray,
agents: np.ndarray,
tl_faces: np.ndarray,
selected_track_id: Optional[int],
raster_size: Tuple[int, int],
pixel_size: np.ndarray,
ego_center: np.ndarray,
history_num_frames: int,
history_step_size: int,
future_num_frames: int,
future_step_size: int,
filter_agents_threshold: float,
rasterizer: Optional[Rasterizer] = None,
perturbation: Optional[Perturbation] = None, 
)-> dict:

 agent_velocity = None
history_slice = get_history_slice(state_index, history_num_frames, history_step_size, include_current_state=True)
 future_slice = get_future_slice(state_index, future_num_frames, future_step_size)
 history_frames = frames[history_slice].copy()
future_frames = frames[future_slice].copy()
sorted_frames = np.concatenate(( history_frames[::-1], future_frames))
agent_slice = get_agents_slice_from_frames(sorted_frames[0], sorted_frames[-1])
 agents = agents[agent_slice].copy()
history_frames["agent_index_interval"] -= agent_slice.start
future_frames["agent_index_interval"] -= agent_slice.start
history_agents = filter_agents_by_frames(history_frames, agents)
 future_agents = filter_agents_by_frames(future_frames, agents)
 try:
tl_slice = get_tl_faces_slice_from_frames(history_frames[-1], history_frames[0])
 history_frames["traffic_light_faces_index_interval"] -= tl_slice.start
history_tl_faces = filter_tl_faces_by_frames(history_frames, tl_faces[tl_slice].copy())
 except ValueError:
history_tl_faces = [np.empty(0, dtype=TL_FACE_DTYPE)for _ in history_frames]
if perturbation is not None:
history_frames, future_frames = perturbation.perturb(
 history_frames=history_frames, future_frames=future_frames 
)
 cur_frame = history_frames[0]
cur_agents = history_agents[0]
if selected_track_id is None:
agent_centroid = cur_frame["ego_translation"][:2]
agent_velocity = np.array([0, 0], dtype=np.float32)
 agent_yaw = rotation33_as_yaw(cur_frame["ego_rotation"])
 agent_extent = np.asarray(( EGO_EXTENT_LENGTH, EGO_EXTENT_WIDTH, EGO_EXTENT_HEIGHT))
selected_agent = None
else:
try:
agent = filter_agents_by_track_id(
 filter_agents_by_labels(cur_agents, filter_agents_threshold), selected_track_id 
)[0]
except IndexError:
raise ValueError(f" track_id {selected_track_id} not in frame or below threshold")
 agent_centroid = agent["centroid"]
agent_velocity = agent["velocity"]
agent_yaw = float(agent["yaw"])
 agent_extent = agent["extent"]
selected_agent = agent
input_im =(
 None
if not rasterizer
else rasterizer.rasterize(history_frames, history_agents, history_tl_faces, selected_agent)
)
 world_to_image_space = world_to_image_pixels_matrix(
 raster_size,
pixel_size,
ego_translation_m=agent_centroid,
ego_yaw_rad=agent_yaw,
ego_center_in_image_ratio=ego_center, 
)
 future_coords_offset, future_vel_offset, future_accel_offset, _, future_yaws_offset, future_availability = _create_targets_for_deep_prediction(
 future_num_frames, future_frames, selected_track_id, future_agents, agent_centroid[:2], agent_velocity[:2],
agent_yaw, 
)
 history_coords_offset, history_vel_offset, history_accel_offset, history_jerk_offset, history_yaws_offset, \
history_availability = _create_targets_for_deep_prediction(history_num_frames + 1, history_frames,
selected_track_id, history_agents, agent_centroid[:2], agent_velocity[:2], agent_yaw, 
)
 estimated_future_positions = _extrapolate_targets(future_num_frames, history_num_frames, history_coords_offset,
history_vel_offset, history_accel_offset, history_jerk_offset)
 error_arr = np.abs(estimated_future_positions - future_coords_offset[10:])
 error = np.average(error_arr)
 return {
"image": input_im,
"target_positions": future_coords_offset,
"target_velocities": future_vel_offset,
"target_accelerations": future_accel_offset,
"target_yaws": future_yaws_offset,
"target_availabilities": future_availability,
"history_positions": history_coords_offset,
"history_velocities": history_vel_offset,
"history_accelerations": history_accel_offset,
"history_yaws": history_yaws_offset,
"history_availabilities": history_availability,
"estimated_future_positions": estimated_future_positions,
"world_to_image": world_to_image_space,
"centroid": agent_centroid,
"yaw": agent_yaw,
"extent": agent_extent,
}
def _extrapolate_targets(future_num_frames: int, history_num_frames: int, position_history: np.ndarray,
vel_history: np.ndarray, accel_history: np.ndarray, jerk_history: np.ndarray)-> np.ndarray:

 dt = 0.1
start_time = len(position_history)* dt
predict_frames = future_num_frames - history_num_frames
time_arr = np.arange(dt, predict_frames * dt + dt, dt)
 avg_vel = np.array([np.average(vel_history.T[0]), np.average(vel_history.T[1])])
 avg_acc = np.array([np.average(accel_history.T[0]), np.average(accel_history.T[1])])
 avg_jrk = np.array([np.average(jerk_history.T[0]), np.average(jerk_history.T[1])])
 pos = np.array([position_history[-1]] ).T
vel = np.array([avg_vel] ).T
accel = np.array([avg_acc] ).T
jerk = np.array([avg_jrk] ).T
res = pos * np.ones(time_arr.shape)+ vel * time_arr +(accel * time_arr ** 2)/2 +(jerk * time_arr ** 3)/ 6
return res.T
def _create_targets_for_deep_prediction(
 num_frames: int,
frames: np.ndarray,
selected_track_id: Optional[int],
agents: List[np.ndarray],
agent_current_centroid: np.ndarray,
agent_current_velocity: np.ndarray,
agent_current_yaw: float, 
)-> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray, np.ndarray]:

 num_avail = 0
num_unavail = 0
last_avail_index = -1
last_unavail_index = -1
coords_offset = np.zeros(( num_frames, 2), dtype=np.float32)
 vel_offset = np.zeros(( num_frames, 2), dtype=np.float32)
 accel_offset = np.zeros(( num_frames, 2), dtype=np.float32)
 jerk_offset = np.zeros(( num_frames, 2), dtype=np.float32)
 yaws_offset = np.zeros(( num_frames, 1), dtype=np.float32)
 availability = np.zeros(( num_frames,), dtype=np.float32)
 for i,(frame, agents)in enumerate(zip(frames, agents)) :
if selected_track_id is None:
agent_centroid = frame["ego_translation"][:2]
agent_yaw = rotation33_as_yaw(frame["ego_rotation"])
 agent_velocity = np.array([0, 0], dtype=np.float32)
 num_avail = num_avail + 1
else:
try:
agent = filter_agents_by_track_id(agents, selected_track_id)[0]
num_avail = num_avail + 1
except IndexError:
availability[i] = 0.0
last_unavail_index = i
num_unavail = num_unavail + 1
continue
agent_centroid = agent["centroid"]
agent_velocity = agent["velocity"]
agent_yaw = agent["yaw"]
coords_offset[i] = agent_centroid - agent_current_centroid
if num_avail == 1:
vel_offset[i] = agent_current_velocity
accel_offset[i] = np.array([0, 0], dtype=np.float32)
 jerk_offset[i] = np.array([0, 0], dtype=np.float32)
 elif last_unavail_index == -1:
dx = coords_offset[i] - coords_offset[i - 1]
dt = 0.1
vel_offset[i] = dx / dt
accel_offset[i] =(vel_offset[i] - vel_offset[i - 1])/ dt
jerk_offset[i] =(accel_offset[i] - accel_offset[i - 1])/ dt
elif last_unavail_index == i - 1:
prev_vel = vel_offset[last_avail_index]
prev_pos = coords_offset[last_avail_index]
pos = coords_offset[i]
dx = pos - prev_pos
dt =(i - last_avail_index)* 0.1
vel =(dx / dt)* - prev_vel
vel_offset[i] = vel
accel_offset[i] =(((dx - prev_vel * dt)* 2)**(1/2)) / dt
jerk_offset[i] =(accel_offset[i] - accel_offset[last_avail_index])/ dt
else:
dx = coords_offset[i] - coords_offset[i - 1]
dt = 0.1
vel_offset[i] = dx / dt
accel_offset[i] =(agent_velocity ** 2 - vel_offset[i - 1] ** 2)/(2 * dx)
 jerk_offset[i] =(accel_offset[i] - accel_offset[i - 1])/ dt
yaws_offset[i] = agent_yaw - agent_current_yaw
availability[i] = 1.0
last_avail_index = i
return coords_offset, vel_offset, accel_offset, jerk_offset, yaws_offset, availability<define_variables>
train_df[train_df.Embarked.isnull() ]
MIN_FRAME_HISTORY = 10
MIN_FRAME_FUTURE = 1
class KineticDataset(AgentDataset):
def __init__(
 self,
cfg: dict,
zarr_dataset: ChunkedDataset,
rasterizer: Rasterizer,
perturbation: Optional[Perturbation] = None,
agents_mask: Optional[np.ndarray] = None,
min_frame_history: int = MIN_FRAME_HISTORY,
min_frame_future: int = MIN_FRAME_FUTURE, 
):
assert perturbation is None, "KineticDataset does not support perturbation(yet)"
super(KineticDataset, self ).__init__(cfg, zarr_dataset, rasterizer, perturbation, agents_mask,
min_frame_history, min_frame_future)
 self.sample_function = partial(
 generate_kinetic_agent_sample,
raster_size=cast(Tuple[int, int], tuple(cfg["raster_params"]["raster_size"])) ,
pixel_size=np.array(cfg["raster_params"]["pixel_size"]),
ego_center=np.array(cfg["raster_params"]["ego_center"]),
history_num_frames=cfg["model_params"]["history_num_frames"],
history_step_size=cfg["model_params"]["history_step_size"],
future_num_frames=cfg["model_params"]["future_num_frames"],
future_step_size=cfg["model_params"]["future_step_size"],
filter_agents_threshold=cfg["raster_params"]["filter_agents_threshold"],
rasterizer=rasterizer,
perturbation=perturbation, 
)
 def get_frame(self, scene_index: int, state_index: int, track_id: Optional[int] = None)-> dict:

 frames = self.dataset.frames[get_frames_slice_from_scenes(self.dataset.scenes[scene_index])]
data = self.sample_function(state_index, frames, self.dataset.agents, self.dataset.tl_faces, track_id)
 image = data["image"].transpose(2, 0, 1)
 target_positions = np.array(data["target_positions"], dtype=np.float32)
 target_yaws = np.array(data["target_yaws"], dtype=np.float32)
 history_positions = np.array(data["history_positions"], dtype=np.float32)
 estimated_positions = np.array(data["estimated_future_positions"], dtype=np.float32)
 timestamp = frames[state_index]["timestamp"]
track_id = np.int64(-1 if track_id is None else track_id)
 return {
"image": image,
"target_positions": target_positions,
"target_yaws": target_yaws,
"target_availabilities": data["target_availabilities"],
"history_positions": history_positions,
"history_availabilities": data["history_availabilities"],
"estimated_future_positions": estimated_positions,
"world_to_image": data["world_to_image"],
"track_id": track_id,
"timestamp": timestamp,
"centroid": data["centroid"],
"yaw": data["yaw"],
"extent": data["extent"],
}<import_modules>
test_df[test_df.Fare.isnull() ]
def trim_network_at_index(network: nn.Module, index: int = -1)-> nn.Module:

 assert index < 0, f"Param index must be negative.Received {index}."
return nn.Sequential(*list(network.children())[:index])
 class MnasBackbone(nn.Module):

 def __init__(self, num_in_channels: int = 3):

 super().__init__()
model = mnasnet1_0(pretrained=False)
 layer = model.layers[0]
model.layers[0] = nn.Conv2d(
 in_channels=num_in_channels,
out_channels=layer.out_channels,
kernel_size=layer.kernel_size,
stride=layer.stride,
padding=layer.padding,
bias=False, 
)
 self.backbone = trim_network_at_index(model, -1)
 def forward(self, input_tensor: torch.Tensor)-> torch.Tensor:

 backbone_features = self.backbone(input_tensor)
 return backbone_features.mean([2, 3])
<categorify>
fil =(( train_df.Pclass == 1)&(train_df.SibSp == 0)&(train_df.Parch == 0)
 &(train_df.Sex == 'female'))
mis = train_df[fil].Embarked.mode()
print(mis)
 fil = train_df.Embarked.isnull()
train_df.loc[fil, 'Embarked'] = 'C'
print("_"*40)
 print(train_df.Embarked.value_counts(dropna = False))
ASV_DIM = 3
def calculate_backbone_feature_dim(backbone, input_shape: Tuple[int, int, int])-> int:

 tensor = torch.ones(1, *input_shape)
 output_feat = backbone.forward(tensor)
 return output_feat.shape[-1]
class LyftNet(nn.Module):

 def __init__(self, backbone: nn.Module, num_modes: int, num_targets: int, num_kinetic_dim: int = 3,
n_hidden_layers: int = 4096, input_shape: Tuple[int, int, int] =(3, 500, 500)) :

 super().__init__()
self.ASV_DIM = num_kinetic_dim
self.backbone = backbone
self.num_modes = num_modes
backbone_feature_dim = calculate_backbone_feature_dim(backbone, input_shape)
 self.fc1 = nn.Linear(backbone_feature_dim + self.ASV_DIM, n_hidden_layers, bias=True)
 predictions_per_mode = num_targets
self.num_pred = predictions_per_mode * num_modes
self.future_len = int(num_targets / 2)
 self.norm = nn.BatchNorm1d(self.ASV_DIM)
 self.fc2 = nn.Linear(n_hidden_layers, int(self.num_pred + num_modes))
def forward(self, image_tensor: torch.Tensor,
agent_state_vector: torch.Tensor)-> torch.Tensor:

 backbone_features = self.backbone(image_tensor)
 features = torch.cat([backbone_features, self.norm(agent_state_vector)], dim=1)
 predictions = self.fc2(self.fc1(features))
bs, _ = predictions.shape
pred, confidences = torch.split(predictions, self.num_pred, dim=1)
 pred = pred.view(bs, self.num_modes, self.future_len, 2)
 assert confidences.shape ==(bs, self.num_modes)
 confidences = torch.softmax(confidences, dim=1)
 if math.isnan(confidences[0][0].item()):
print("Oh no, not the naan")
 print(agent_state_vector)
 return pred, confidences<set_options>
fil =(( test_df.Pclass == 3)&(test_df.SibSp == 0)&(test_df.Parch == 0)
 &(test_df.Cabin == 0)&(test_df.Sex == 'male')&(test_df.Embarked == 'S'))
mis = round(test_df[fil].Fare.median() , 4)
 print(mis)
 fil = test_df.Fare.isnull()
test_df.loc[fil, 'Fare'] = mis
print("_"*40)
 print(test_df.Fare.isnull().value_counts(dropna = False))
class LyftManager:
def __init__(self, config, data_path, device, num_modes=3, verbose=False):
self.cfg = config
self.data_path = data_path
self.device = device
self.verbose = verbose
num_history_channels =(self.cfg["model_params"]["history_num_frames"] + 1)* 2
num_in_channels = 3 + num_history_channels
self.backbone = MnasBackbone(num_in_channels=num_in_channels)
 raster_size = self.cfg["raster_params"]["raster_size"]
num_targets = 2 * self.cfg["model_params"]["future_num_frames"]
self.future_len = self.cfg["model_params"]["future_num_frames"]
print("Number of Targets = ", self.future_len)
 self.num_targets = num_targets
self.num_preds = num_targets * num_modes
self.num_modes = num_modes
self.model = LyftNet(self.backbone, num_modes=num_modes, num_kinetic_dim=2 *(self.future_len + 1),
num_targets=num_targets, input_shape=(num_in_channels, raster_size[0], raster_size[1]))
self.model.to(device=self.device)
 def train(self, iterations, lr=1e-3, file_name="lyft-net.pth"):
os.environ["L5KIT_DATA_FOLDER"] = self.data_path
dm = LocalDataManager(None)
 cfg = self.cfg
print(cfg)
 train_cfg = cfg["train_data_loader"]
rasterizer = build_rasterizer(cfg, dm)
 train_zarr = ChunkedDataset(dm.require(train_cfg["key"])).open()
if self.verbose:
print("Dataset Chunked")
 train_dataset = KineticDataset(cfg, train_zarr, rasterizer)
 if self.verbose:
print("Agent Dataset Retrieved")
 train_dataloader = DataLoader(train_dataset,
shuffle=train_cfg["shuffle"],
batch_size=train_cfg["batch_size"],
num_workers=train_cfg["num_workers"])
 print(train_dataset)
 optimizer = optim.Adam(self.model.parameters() , lr=lr)
 criterion = nn.PoissonNLLLoss()
tr_it = iter(train_dataloader)
 progress_bar = tqdm(range(iterations))
losses_train = []
rolling_avg = []
for i in progress_bar:
try:
data = next(tr_it)
 except StopIteration:
tr_it = iter(train_dataloader)
 data = next(tr_it)
 self.model.train()
torch.set_grad_enabled(True)
 position_tensor = data["target_positions"].to(self.device)
 history_position_tensor = data["history_positions"].to(self.device)
 estimated_future_positions = data["estimated_future_positions"].to(self.device)
 imageTensor = data["image"].to(self.device)
 if self.verbose:
print("Image Tensor: ", imageTensor.shape)
 state_vector = torch.cat([estimated_future_positions, history_position_tensor], 1 ).to(self.device)
 state_vector = torch.flatten(state_vector, 1 ).to(self.device)
 if self.verbose:
print("State Vector: ", state_vector.shape)
 pred, conf = self.model.forward(imageTensor, state_vector)
 if self.verbose:
print("Prediction: ", pred.shape)
 print("Probability: ", conf.shape)
 target_availabilities = data["target_availabilities"].unsqueeze(-1 ).to(self.device)
 flattenedTargets = torch.flatten(target_availabilities, 1, 2)
 loss = pytorch_neg_multi_log_likelihood_batch(position_tensor, pred, conf, flattenedTargets)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
losses_train.append(loss.item())
 rolling_avg.append(np.mean(losses_train))
progress_bar.set_description(f"loss: {loss.item() } loss(avg): {np.mean(losses_train)}")
 print("Done Training")
 torch.save(self.model.state_dict() , f"/home/michael/Workspace/Lyft/model/{file_name}")
 def evaluate(self, data_path, file_name="submission.csv"):
os.environ["L5KIT_DATA_FOLDER"] = data_path
dm = LocalDataManager(None)
 cfg = self.cfg
test_cfg = cfg["test_data_loader"]
rasterizer = build_rasterizer(cfg, dm)
 test_zarr = ChunkedDataset(dm.require(test_cfg["key"])).open()
test_mask = np.load(f"{data_path}/scenes/mask.npz")["arr_0"]
test_dataset = KineticDataset(cfg, test_zarr, rasterizer, agents_mask=test_mask)
 test_dataloader = DataLoader(test_dataset,
shuffle=test_cfg["shuffle"],
batch_size=test_cfg["batch_size"],
num_workers=test_cfg["num_workers"])
 test_dataloader = test_dataloader
print(test_dataloader)
 self.model.eval()
torch.set_grad_enabled(False)
 criterion = nn.MSELoss(reduction="none")
 future_coords_offsets_pd = []
timestamps = []
pred_coords = []
confidences_list = []
agent_ids = []
progress_bar = tqdm(test_dataloader)
 for data in progress_bar:
history_position_tensor = data["history_positions"].to(self.device)
 estimated_future_positions = data["estimated_future_positions"].to(self.device)
 imageTensor = data["image"].to(self.device)
 if self.verbose:
print("Image Tensor: ", imageTensor.shape)
 state_vector = torch.cat([estimated_future_positions, history_position_tensor], 1 ).to(self.device)
 state_vector = torch.flatten(state_vector, 1 ).to(self.device)
 if self.verbose:
print("State Vector: ", state_vector.shape)
 pred, confidences = self.model.forward(imageTensor, state_vector)
 timestamps.append(data["timestamp"].numpy().copy())
 agent_ids.append(data["track_id"].numpy().copy())
 pred_coords.append(pred.cpu().numpy().copy())
 confidences_list.append(confidences.cpu().numpy().copy())
 pred_path = f"{os.getcwd() }/{file_name}"
write_pred_csv(pred_path,
timestamps=np.concatenate(timestamps),
track_ids=np.concatenate(agent_ids),
coords=np.concatenate(pred_coords),
confs=np.concatenate(confidences_list))
torch.cuda.empty_cache()
def load(self, checkpoint_path:str):
state_dict = torch.load(checkpoint_path, map_location=self.device)
 self.model.load_state_dict(state_dict)
<load_pretrained>
for dataset in combine:
dataset['MisAge'] = 0
fil =(dataset.Age.isnull())
 dataset.loc[fil, 'MisAge'] = 1
print(train_df.MisAge.value_counts())
 print("_"*40)
 print(test_df.MisAge.value_counts())
 print("_"*40)
 print("_"*40)
 train_df[['MisAge', 'Survived']].groupby(['MisAge'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
data_path = "/kaggle/input/lyft-motion-prediction-autonomous-vehicles"
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 config = load_config_data("/kaggle/input/lyftnet/config_lyftnet.yaml")
 config["test_data_loader"]["batch_size"] = 16
config["test_data_loader"]["num_workers"] = 8
checkpoint_path = "/kaggle/input/lyftnet/lyft-net.pth"
manager = LyftManager(config=config, data_path=data_path, device=device, num_modes=3, verbose=False)
 manager.load(checkpoint_path)
 manager.evaluate(data_path=data_path )<load_from_csv>
for dataset in combine:
df = dataset.groupby(['MisAge', 'Sex'] ).size().unstack(0)
 df['miss_perc'] =(df[df.columns[1]]/(df[df.columns[0]] + df[df.columns[1]]))
df['nomiss_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]]))
print(df)
 print("_"*40 )
<import_modules>
train_df[["Sex", "MisAge", "Survived"]].groupby(['Sex', 'MisAge'],
as_index=True ).mean()
warnings.filterwarnings("ignore")
 print(l5kit.__version__ )<set_options>
for dataset in combine:
df = dataset.groupby(['MisAge', 'Pclass'] ).size().unstack(0)
 df['miss_perc'] =(df[df.columns[1]]/(df[df.columns[0]] + df[df.columns[1]]))
df['nomiss_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]]))
print(df)
 print("_"*40 )
def memory(verbose=True):
mem = psutil.virtual_memory()
gb = 1024*1024*1024
if verbose:
print('Physical memory:',
'%.2f GB(used),'%(( mem.total - mem.available)/ gb),
'%.2f GB(available)'%(( mem.available)/ gb), '/',
'%.2f GB'%(mem.total / gb))
return(mem.total - mem.available)/ gb
def gc_memory(verbose=True):
m = gc.collect()
if verbose:
print('GC:', m, end=' | ')
 memory()
memory() ;<set_options>
train_df[["Pclass", "MisAge", "Survived"]].groupby(['Pclass', 'MisAge'],
as_index=True ).mean()
def set_seed(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 set_seed(42 )<init_hyperparams>
train_df[["Cabin", "MisAge", "Survived"]].groupby(['Cabin', 'MisAge'],
as_index=True ).mean()
cfg = {
'format_version': 4,
'data_path': '/kaggle/input/lyft-motion-prediction-autonomous-vehicles',
'model_params': {
'first_layer_bias': False,
'pretrained': True,
'multi_mode': True,
'model_architecture': 'resnet101',
'history_num_frames': 10,
'history_step_size': 1,
'history_delta_time': 0.1,
'future_num_frames': 50,
'future_step_size': 1,
'future_delta_time': 0.1,
'model_name': "model_resnet34_output",
'lr': 1e-5,
'weight_path': '.. /input/resnet101-best-weights-epoch2/model_state_280000.pth',
'train': False,
'predict': True,
},
'raster_params': {
'raster_size': [224, 224],
'pixel_size': [0.5, 0.5],
'ego_center': [0.25, 0.5],
'map_type': 'py_semantic',
'satellite_map_key': 'aerial_map/aerial_map.png',
'semantic_map_key': 'semantic_map/semantic_map.pb',
'dataset_meta_key': 'meta.json',
'filter_agents_threshold': 0.5,
},
'train_data_loader': {
'key': 'scenes/train.zarr',
'batch_size': 16,
'shuffle': True,
'num_workers': 4,
},
'test_data_loader': {
'key': 'scenes/test.zarr',
'batch_size': 32,
'shuffle': False,
'num_workers': 4,
},
'train_params': {
'steps': 1000,
'update_steps': 100,
'checkpoint_steps': 1000,
},
'combine': True
}
DIR_INPUT = cfg["data_path"]
os.environ["L5KIT_DATA_FOLDER"] = DIR_INPUT
dm = LocalDataManager()
rasterizer = build_rasterizer(cfg, dm)
 test_cfg = cfg["test_data_loader"]
test_zarr = ChunkedDataset(dm.require(test_cfg["key"])).open(cached=False)
 test_mask = np.load(f"{DIR_INPUT}/scenes/mask.npz")["arr_0"]
test_dataset = AgentDataset(cfg, test_zarr, rasterizer, agents_mask=test_mask)
 test_dataloader = DataLoader(test_dataset, shuffle=test_cfg["shuffle"],
batch_size=test_cfg["batch_size"], num_workers=test_cfg["num_workers"])
 print(test_dataset)
 def pytorch_neg_multi_log_likelihood_batch(
 gt: Tensor, pred: Tensor, confidences: Tensor, avails: Tensor 
)-> Tensor:

 assert len(pred.shape)== 4, f"expected 3D(MxTxC)array for pred, got {pred.shape}"
batch_size, num_modes, future_len, num_coords = pred.shape
assert gt.shape ==(batch_size, future_len, num_coords), f"expected 2D(Time x Coords)array for gt, got {gt.shape}"
assert confidences.shape ==(batch_size, num_modes), f"expected 1D(Modes)array for gt, got {confidences.shape}"
assert torch.allclose(torch.sum(confidences, dim=1), confidences.new_ones(( batch_size,))), "confidences should sum to 1"
assert avails.shape ==(batch_size, future_len), f"expected 1D(Time)array for gt, got {avails.shape}"
assert torch.isfinite(pred ).all() , "invalid value found in pred"
assert torch.isfinite(gt ).all() , "invalid value found in gt"
assert torch.isfinite(confidences ).all() , "invalid value found in confidences"
assert torch.isfinite(avails ).all() , "invalid value found in avails"
gt = torch.unsqueeze(gt, 1)
 avails = avails[:, None, :, None]
error = torch.sum(((gt - pred)* avails)** 2, dim=-1)
 with np.errstate(divide="ignore"):
error = torch.log(confidences)- 0.5 * torch.sum(error, dim=-1)
 max_value, _ = error.max(dim=1, keepdim=True)
 error = -torch.log(torch.sum(torch.exp(error - max_value), dim=-1, keepdim=True)) - max_value
return torch.mean(error)
 def pytorch_neg_multi_log_likelihood_single(
 gt: Tensor, pred: Tensor, avails: Tensor 
)-> Tensor:

 batch_size, future_len, num_coords = pred.shape
confidences = pred.new_ones(( batch_size, 1))
return pytorch_neg_multi_log_likelihood_batch(gt, pred.unsqueeze(1), confidences, avails)
 class LyftMultiModel(nn.Module):
def __init__(self, conf: Dict):
super().__init__()
self.future_num_frames = cfg["model_params"]["future_num_frames"]
target_count = 2 * self.future_num_frames
if cfg["model_params"]["multi_mode"]:
self.multi_mode = True
target_count += 1
target_count *= 3
else:
self.multi_mode = False
history_channel_count =(conf["model_params"]["history_num_frames"] + 1)* 2
total_channel_count = 3 + history_channel_count
architecture = cfg["model_params"]["model_architecture"]
if architecture == "resnet50":
backbone = resnet50(pretrained=cfg["model_params"]["pretrained"])
 elif architecture == "resnet101":
backbone = resnet101(pretrained=cfg["model_params"]["pretrained"])
 elif architecture == "resnet34":
backbone = resnet34(pretrained=cfg["model_params"]["pretrained"])
 if architecture in ["resnet50", "resnet101", "resnet34"]:
backbone.conv1 = nn.Conv2d(
 total_channel_count,
backbone.conv1.out_channels,
kernel_size=backbone.conv1.kernel_size,
stride=backbone.conv1.stride,
padding=backbone.conv1.padding,
bias=cfg["model_params"]["first_layer_bias"], 
)
 if architecture == "resnet34":
backbone.fc = nn.Linear(in_features=512, out_features=target_count)
 else:
backbone.fc = nn.Linear(in_features=2048, out_features=target_count)
 self.backbone = backbone
def forward(self, x):
y = self.backbone(x)
 if self.multi_mode:
batches, _ = y.shape
pred, confidences = torch.split(y, self.future_num_frames * 3 * 2, dim=1)
 pred = pred.view(batches, 3, self.future_num_frames, 2)
 confidences = torch.softmax(confidences, dim=1)
 return pred, confidences
else:
return y
def forward(data, model, device, criterion=pytorch_neg_multi_log_likelihood_batch, compute_loss=True):
inputs = data["image"].to(device)
 target_availabilities = data["target_availabilities"].to(device)
 targets = data["target_positions"].to(device)
 preds, confidences = model(inputs)
 loss = criterion(targets, preds, confidences, target_availabilities)if compute_loss else 0
return loss, preds, confidences
def execute(path):
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 model = LyftMultiModel(cfg)
 weight_path = cfg["model_params"]["weight_path"]
if weight_path:
model.load_state_dict(torch.load(weight_path))
model.to(device)
 optimizer = optim.Adam(model.parameters() , lr=cfg["model_params"]["lr"])
 print(f'device {device}')
 if cfg["model_params"]["predict"]:
model.eval()
torch.set_grad_enabled(False)
 future_coords_offsets_pd = []
timestamps = []
confidences_list = []
agent_ids = []
memorys_pred = []
t0 = time.time()
times_pred = []
iterations_pred = []
for i, data in enumerate(tqdm_notebook(test_dataloader, mininterval=5.)):
_, preds, confidences = forward(data, model, device, compute_loss=False)
 preds = torch.einsum('bmti,bji->bmtj',
preds.double() ,
data["world_from_agent"].to(device)[:, :2, :2] ).cpu().numpy()
future_coords_offsets_pd.append(preds.copy())
 confidences_list.append(confidences.cpu().numpy().copy())
 timestamps.append(data["timestamp"].numpy().copy())
 agent_ids.append(data["track_id"].numpy().copy())
 if i%50 == 0:
t =(( time.time() - t0)/ 60)
 print('%4d'%i, '%6.2fmins'%t, end=' | ')
 mem = memory()
iterations_pred.append(i)
 memorys_pred.append(mem)
 times_pred.append(t)
 print('Total timespent: %6.2fmins'%(( time.time() - t0)/ 60))
memory()
pred_path = path
write_pred_csv(
 pred_path,
timestamps=np.concatenate(timestamps),
track_ids=np.concatenate(agent_ids),
coords=np.concatenate(future_coords_offsets_pd),
confs=np.concatenate(confidences_list), 
)
 df_sub = pd.read_csv(pred_path)
 display(df_sub )<feature_engineering>
fil =(train_df.Age.isnull())
 print("By class:")
 print(train_df[fil].Pclass.value_counts())
 print("_"*40)
 print(train_df[train_df.MisAge == 0].Pclass.value_counts())
 print("_"*40)
 print("_"*40)
 print("By sex:")
 print(train_df[fil].Sex.value_counts())
 print("_"*40)
 print(train_df[train_df.MisAge == 0].Sex.value_counts())
 print("_"*40)
 print("_"*40)
 print("By parents and children:")
 print(train_df[fil].Parch.value_counts())
 print("_"*40)
 print(train_df[train_df.MisAge == 0].Parch.value_counts())
 print("_"*40)
 print("_"*40)
 print("By spouse and siblings:")
 print(train_df[fil].SibSp.value_counts())
 print("_"*40)
 print(train_df[train_df.MisAge == 0].SibSp.value_counts() )
if cfg['combine']:
execute('submission_1.csv')
 cfg["model_params"]["weight_path"] = '.. /input/resnet34-bestscore-epoch-weights/model_state_last.pth'
cfg['model_params']['model_architecture'] = 'resnet34'
execute('submission_2.csv')
 else:
execute('submission.csv')
<define_variables>
fil =(test_df.Age.isnull())
 print("By class:")
 print(test_df[fil].Pclass.value_counts())
 print("_"*40)
 print(test_df[test_df.MisAge == 0].Pclass.value_counts())
 print("_"*40)
 print("_"*40)
 print("By sex:")
 print(test_df[fil].Sex.value_counts())
 print("_"*40)
 print(test_df[test_df.MisAge == 0].Sex.value_counts())
 print("_"*40)
 print("_"*40)
 print("By parents and children:")
 print(test_df[fil].Parch.value_counts())
 print("_"*40)
 print(test_df[test_df.MisAge == 0].Parch.value_counts())
 print("_"*40)
 print("_"*40)
 print("By spouse and siblings:")
 print(test_df[fil].SibSp.value_counts())
 print("_"*40)
 print(test_df[test_df.MisAge == 0].SibSp.value_counts() )
if cfg['combine']:
pd.options.display.max_columns=305
paths = [
"submission_1.csv",
"submission_2.csv",
]
weights = [0.2, 0.8]
conf_cols = np.array(["conf_0", "conf_1", "conf_2"])
 xy_cols = [[],[],[]]
for i in range(50):
for j in range(3):
xy_cols[j].append(f"coord_x{j}{i}")
 xy_cols[j].append(f"coord_y{j}{i}")
 xy_cols[0][:10]
COLUMNS = ["timestamp", "track_id"] + list(conf_cols)+ xy_cols[0] + xy_cols[1] + xy_cols[2]
def sort_df(df, sort_timestamp_track_id=True):
conf_orders = np.argsort(-df[conf_cols].values,1)
 XY = np.stack([df[xy_cols[0]].values,df[xy_cols[1]].values, df[xy_cols[2]].values], axis=1)
 XY = XY[np.arange(len(XY)) [:, None], conf_orders]
df2 = pd.DataFrame(columns = COLUMNS)
 df2["timestamp"] = df["timestamp"].values
df2["track_id"] = df["track_id"].values
df2[xy_cols[0] + xy_cols[1] + xy_cols[2]] = XY.reshape(-1,300)
 df2[conf_cols] = df[conf_cols].values[np.arange(len(df)) [:, None], conf_orders]
if sort_timestamp_track_id:
df2.sort_values(["timestamp", "track_id"], inplace=True)
 df2.reset_index(inplace=True, drop=True)
 return df2
df = None
for path,w in zip(paths,weights):
print(w, path)
 temp = pd.read_csv(path)
 temp = sort_df(temp)
 temp[COLUMNS[5:]] *= w
if df is None:
df = temp
else:
df[COLUMNS[2:]] += temp[COLUMNS[2:]]
df[conf_cols] /= df[conf_cols].sum(1 ).values[:, None]
sample = pd.read_csv(".. /input/lyft-motion-prediction-autonomous-vehicles/multi_mode_sample_submission.csv")
 df = sample[["timestamp", "track_id"]].merge(df, on=["timestamp", "track_id"])
 sample.shape, df.shape
df.to_csv("submission.csv", index=False, float_format='%.6f' )<import_modules>
for df in combine:
df['Title'] = df.Name.str.extract('([A-Za-z]+)\.', expand=False)
 pd.crosstab(train_df['Title'], train_df['Sex'] )
warnings.filterwarnings("ignore" )<import_modules>
for df in combine:
df['Title'] = df['Title'].replace(['Mme', 'Countess','Dona'], 'Mrs')
 df['Title'] = df['Title'].replace(['Capt', 'Col','Don', 'Jonkheer', 'Rev',
'Major', 'Sir'], 'Mr')
 df['Title'] = df['Title'].replace(['Mlle', 'Lady','Ms'], 'Miss')
 df.loc[(df.Sex == 'male')&(df.Title == 'Dr'), 'Title'] = 'Mr'
df.loc[(df.Sex == 'female')&(df.Title == 'Dr'), 'Title'] = 'Mrs'
pd.crosstab(train_df['Title'], train_df['Sex'] )
l5kit.__version__<set_options>
fil =(test_df.Age.isnull())
 print("By title:")
 print(test_df[fil].Title.value_counts())
 print("_"*40)
 print(test_df[test_df.MisAge == 0].Title.value_counts())
 print("_"*40)
 print("_"*40)
 fil =(train_df.Age.isnull())
 print("By class:")
 print(train_df[fil].Title.value_counts())
 print("_"*40)
 print(train_df[train_df.MisAge == 0].Title.value_counts() )
def set_seed(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 set_seed(42 )<init_hyperparams>
np.random.seed(452 )
cfg = {
'format_version': 4,
'data_path': "/kaggle/input/lyft-motion-prediction-autonomous-vehicles",
'model_params': {
'model_architecture': 'resnet34',
'history_num_frames': 10,
'history_step_size': 1,
'history_delta_time': 0.5,
'future_num_frames': 50,
'future_step_size': 1,
'future_delta_time': 0.5,
'model_name': "model_resnet34_output",
'lr': 1e-3,
'weight_path': "/kaggle/input/lyft-pretrained-model-hv/model_multi_update_lyft_public.pth",
'train': False,
'predict': True
},
'raster_params': {
'raster_size': [224, 224],
'pixel_size': [0.5, 0.5],
'ego_center': [0.25, 0.5],
'map_type': 'py_semantic',
'satellite_map_key': 'aerial_map/aerial_map.png',
'semantic_map_key': 'semantic_map/semantic_map.pb',
'dataset_meta_key': 'meta.json',
'filter_agents_threshold': 0.5
},
'train_data_loader': {
'key': 'scenes/train.zarr',
'batch_size': 16,
'shuffle': True,
'num_workers': 4
},
'test_data_loader': {
'key': 'scenes/test.zarr',
'batch_size': 32,
'shuffle': False,
'num_workers': 4
},
'train_params': {
'max_num_steps': 101,
'checkpoint_every_n_steps': 20,
}
}<feature_engineering>
for df in combine:
titles = list(set(df.Title))
classes = list(set(df.Pclass))
for title in titles:
for cl in classes:
fil =(df.Title == title)&(df.Pclass == cl)
 med_age = df[fil].Age.dropna().median()
var_age = med_age / 5
mis_age = df[fil].MisAge.sum()
df.loc[fil &(df.Age.isnull()), 'Age'] = np.random.randint(int(med_age - var_age - 1),
int(med_age + var_age), mis_age)
 train_df.Age.describe()
DIR_INPUT = cfg["data_path"]
os.environ["L5KIT_DATA_FOLDER"] = DIR_INPUT
dm = LocalDataManager(None )<create_dataframe>
for dataset in combine:
dataset['Sex'] = dataset['Sex'].map({'male':1 , 'female':2} ).astype(int)
 train_df.sample(5 )
train_cfg = cfg["train_data_loader"]
rasterizer = build_rasterizer(cfg, dm)
 train_zarr = ChunkedDataset(dm.require(train_cfg["key"])).open()
train_dataset = AgentDataset(cfg, train_zarr, rasterizer)
 train_dataloader = DataLoader(train_dataset, shuffle=train_cfg["shuffle"], batch_size=train_cfg["batch_size"],
num_workers=train_cfg["num_workers"])
 print("==================================TRAIN DATA==================================")
 print(train_dataset )<create_dataframe>
for dataset in combine:
dataset['Embarked'] = dataset['Embarked'].map({'S':1 , 'C':2, 'Q':3} ).astype(int)
 train_df.sample(5 )
test_cfg = cfg["test_data_loader"]
rasterizer = build_rasterizer(cfg, dm)
 test_zarr = ChunkedDataset(dm.require(test_cfg["key"])).open()
test_mask = np.load(f"{DIR_INPUT}/scenes/mask.npz")["arr_0"]
test_dataset = AgentDataset(cfg, test_zarr, rasterizer, agents_mask=test_mask)
 test_dataloader = DataLoader(test_dataset,shuffle=test_cfg["shuffle"],batch_size=test_cfg["batch_size"],
num_workers=test_cfg["num_workers"])
 print("==================================TEST DATA==================================")
 print(test_dataset )<choose_model_class>
train_df[['Title', 'Survived']].groupby(['Title'], as_index=True ).mean().sort_values(by='Survived',
ascending=False )
class LyftMultiModel(nn.Module):
def __init__(self, cfg: Dict, num_modes=3):
super().__init__()
architecture = cfg["model_params"]["model_architecture"]
backbone = eval(architecture )(pretrained=True, progress=True)
 self.backbone = backbone
num_history_channels =(cfg["model_params"]["history_num_frames"] + 1)* 2
num_in_channels = 3 + num_history_channels
self.backbone.conv1 = nn.Conv2d(
 num_in_channels,
self.backbone.conv1.out_channels,
kernel_size=self.backbone.conv1.kernel_size,
stride=self.backbone.conv1.stride,
padding=self.backbone.conv1.padding,
bias=False, 
)
 if architecture == "resnet50":
backbone_out_features = 2048
else:
backbone_out_features = 512
self.future_len = cfg["model_params"]["future_num_frames"]
num_targets = 2 * self.future_len
self.head = nn.Sequential(
 nn.Linear(in_features=backbone_out_features, out_features=4096), 
)
 self.num_preds = num_targets * num_modes
self.num_modes = num_modes
self.logit = nn.Linear(4096, out_features=self.num_preds + num_modes)
 def forward(self, x):
x = self.backbone.conv1(x)
 x = self.backbone.bn1(x)
 x = self.backbone.relu(x)
 x = self.backbone.maxpool(x)
 x = self.backbone.layer1(x)
 x = self.backbone.layer2(x)
 x = self.backbone.layer3(x)
 x = self.backbone.layer4(x)
 x = self.backbone.avgpool(x)
 x = torch.flatten(x, 1)
 x = self.head(x)
 x = self.logit(x)
 bs, _ = x.shape
pred, confidences = torch.split(x, self.num_preds, dim=1)
 pred = pred.view(bs, self.num_modes, self.future_len, 2)
 assert confidences.shape ==(bs, self.num_modes)
 confidences = torch.softmax(confidences, dim=1)
 return pred, confidences<compute_train_metric>
train_df.Title.value_counts()
def forward(data, model, device, criterion = pytorch_neg_multi_log_likelihood_batch):
inputs = data["image"].to(device)
 target_availabilities = data["target_availabilities"].to(device)
 targets = data["target_positions"].to(device)
 preds, confidences = model(inputs)
 loss = criterion(targets, preds, confidences, target_availabilities)
 return loss, preds, confidences<choose_model_class>
for df in combine:
df['Title'] = df['Title'].map({'Mr':1 , 'Mrs':2, 'Miss':3, 'Master':4} ).astype(int)
 train_df.sample(5 )
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 model = LyftMultiModel(cfg)
 weight_path = cfg["model_params"]["weight_path"]
if weight_path:
model.load_state_dict(torch.load(weight_path))
model.to(device)
 optimizer = optim.Adam(model.parameters() , lr=cfg["model_params"]["lr"])
 print(f'device {device}' )<train_model>
for df in combine:
df['IsAlone'] = 0
fil =(df.SibSp == 0)&(df.Parch == 0)
 df.loc[fil, 'IsAlone'] = 1
print(train_df.IsAlone.value_counts())
 print("_"*40)
 print(test_df.IsAlone.value_counts() )
print(model )<init_hyperparams>
train_df[['IsAlone', 'Survived']].groupby(['IsAlone'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
if cfg["model_params"]["train"]:
tr_it = iter(train_dataloader)
 progress_bar = tqdm(range(cfg["train_params"]["max_num_steps"]))
num_iter = cfg["train_params"]["max_num_steps"]
losses_train = []
iterations = []
metrics = []
times = []
model_name = cfg["model_params"]["model_name"]
start = time.time()
for i in progress_bar:
try:
data = next(tr_it)
 except StopIteration:
tr_it = iter(train_dataloader)
 data = next(tr_it)
 model.train()
torch.set_grad_enabled(True)
 loss, _, _ = forward(data, model, device)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
losses_train.append(loss.item())
 progress_bar.set_description(f"loss: {loss.item() } loss(avg): {np.mean(losses_train)}")
 if i % cfg['train_params']['checkpoint_every_n_steps'] == 0:
torch.save(model.state_dict() , f'{model_name}_{i}.pth')
 iterations.append(i)
 metrics.append(np.mean(losses_train))
times.append(( time.time() -start)/60)
 results = pd.DataFrame({'iterations': iterations, 'metrics(avg)': metrics, 'elapsed_time(mins)': times})
 results.to_csv(f"train_metrics_{model_name}_{num_iter}.csv", index = False)
 print(f"Total training time is {(time.time() -start)/60} mins")
 print(results.head() )<save_to_csv>
for dataset in combine:
df = dataset.groupby(['Sex', 'IsAlone'] ).size().unstack(0)
 df['fem_perc'] =(df[df.columns[1]]/(df[df.columns[0]] + df[df.columns[1]]))
df['male_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]]))
print(df)
 print("_"*40 )
pred_path = 'submission1.csv'
write_pred_csv(pred_path,
timestamps=np.concatenate(timestamps),
track_ids=np.concatenate(agent_ids),
coords=np.concatenate(future_coords_offsets_pd),
confs = np.concatenate(confidences_list)
 )<import_modules>
train_df[["Sex", "IsAlone", "Survived"]].groupby(['IsAlone','Sex'],
as_index=True ).mean()
import pandas as pd, numpy as np<load_from_csv>
for dataset in combine:
df = dataset.groupby(['Pclass', 'IsAlone'] ).size().unstack(0)
 df['first_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]] + df[df.columns[2]]))
df['second_perc'] =(df[df.columns[1]]/(df[df.columns[0]] + df[df.columns[1]] + df[df.columns[2]]))
df['third_perc'] =(df[df.columns[2]]/(df[df.columns[0]] + df[df.columns[1]] + df[df.columns[2]]))
print(df)
 print("_"*40 )
df = pd.read_csv("./submission1.csv" )<create_dataframe>
train_df[["Pclass", "IsAlone", "Survived"]].groupby(['IsAlone', 'Pclass'],
as_index=True ).mean()
df_issa1 = df.copy()<set_options>
for df in combine:
df['IsKid'] = 0
fil =(df.Age < 16)
 df.loc[fil, 'IsKid'] = 1
print(train_df.IsKid.value_counts())
 print("_"*40)
 print(test_df.IsKid.value_counts() )
pd.options.display.max_columns=305<load_from_csv>
train_df[['IsKid', 'Survived']].groupby(['IsKid'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
df = pd.read_csv(".. /input/lyft-test-set-as-csv/Lyft_test_set.csv")
 print("df.shape:", df.shape)
 df.head(10 )<feature_engineering>
bins = [0, 16, 32, 48, 81]
for df in combine:
df['AgeBin'] = pd.cut(df['Age'], bins)
 print(train_df.AgeBin.value_counts())
 print("_"*40)
 print(test_df.AgeBin.value_counts() )
def get_models(path):
models = {}
path = Path(path)
 for model in path.glob("lgbm*"):
model_name = get_model_name(model.stem)
 shift = int(model_name.split("shift_")[1])
 meta = path.joinpath("meta_shift_{:02d}.json".format(shift))
with meta.open() as f:
train_cols = json.load(f)["TRAIN_COLS"]
models[model_name] = {"model": model.as_posix() , "train_cols": train_cols}
return models<load_pretrained>
train_df[['AgeBin', 'Survived']].groupby(['AgeBin'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
models = get_models(".. /input/lyft-models/lgbm_06")
 len(models )<define_variables>
bins = [0, 16, 32, 48, 81]
names = [0, 1, 2, 3]
for df in combine:
df['AgeBin'] = pd.cut(df['Age'], bins, labels = names)
 df['AgeBin'] = pd.to_numeric(df['AgeBin'])
 print(train_df.AgeBin.value_counts())
 print("_"*40)
 print(test_df.AgeBin.value_counts() )
def make_colnames() :
xcols = ["coord_x{}{}".format(step, rank)for step in range(3)for rank in range(50)]
ycols = ["coord_y{}{}".format(step, rank)for step in range(3)for rank in range(50)]
cols = ["timestamp", "track_id"] + ["conf_0", "conf_1", "conf_2"] + list(it.chain(*zip(xcols, ycols)))
 return cols<prepare_output>
for df in combine:
df['FareCat'] = pd.qcut(df.FarePP, 4)
 print(train_df.FareCat.value_counts())
 print("_"*40)
 print(test_df.FareCat.value_counts() )
def predict(models, df):
sub = np.empty(( len(df), 305))
sub.fill(np.nan)
 sub = pd.DataFrame(sub, columns = make_colnames())
 sub[["timestamp", "track_id"]] = df[["timestamp", "track_id"]]
sub["conf_0"] = 1.0
for shift in range(1, 51):
for suffix in ["x", "y"]:
model_info = models["lgbm_{}_shift_{:02d}".format(suffix, shift)]
model = lgb.Booster(model_file= model_info["model"])
 pred = model.predict(df[model_info["train_cols"]])
 sub["coord_{}0{}".format(suffix, shift-1)] = pred
if not shift%10:
print("shift: {}".format(shift))
sub.fillna(0., inplace=True)
 return sub<predict_on_test>
labels = [0, 1, 2, 3]
for df in combine:
df['FareCat'] = pd.qcut(df.FarePP, 4, labels=labels)
 df['FareCat'] = pd.to_numeric(df['FareCat'])
 print(train_df.FareCat.value_counts())
 print("_"*40)
 print(test_df.FareCat.value_counts() )
sub = predict(models, df )<save_to_csv>
train_df[['FareCat', 'Survived']].groupby(['FareCat'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
sub.to_csv("submission2.csv", index=False )<create_dataframe>
for dataset in combine:
df = dataset.groupby(['Pclass', 'FareCat'] ).size().unstack(0 ).fillna(0)
 df['first_perc'] =(df[df.columns[0]]/(df[df.columns[0]] + df[df.columns[1]] + df[df.columns[2]]))
df['second_perc'] =(df[df.columns[1]]/(df[df.columns[0]] + df[df.columns[1]] + df[df.columns[2]]))
df['third_perc'] =(df[df.columns[2]]/(df[df.columns[0]] + df[df.columns[1]] + df[df.columns[2]]))
print(df)
 print("_"*40 )
df_issa2 = sub.copy()<import_modules>
for df in combine:
df['FamSize'] = 0
df.loc[(df.NumFam > 1), 'FamSize'] = 1
df.loc[(df.NumFam > 3), 'FamSize'] = 2
df.loc[(df.NumFam > 5), 'FamSize'] = 3
print(train_df.FamSize.value_counts())
 print("_"*40)
 print(test_df.FamSize.value_counts() )
import pandas as pd, numpy as np<save_to_csv>
train_df[['FamSize', 'Survived']].groupby(['FamSize'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
df_issa1.to_csv('df_issa1.csv', index=False)
 df_issa2.to_csv('df_issa2.csv', index=False )<feature_engineering>
train_df[["Pclass", "FamSize", "Survived"]].groupby(['FamSize', 'Pclass'],
as_index=True ).mean()
x = 0.7
df = df_issa1.copy()
df['timestamp'] = x*df_issa1['timestamp'] +(1-x)*df_issa2['timestamp']
df['track_id'] = x*df_issa1['track_id'] +(1-x)*df_issa2['track_id']
df.head()<save_to_csv>
train_df[["Sex", "FamSize", "Survived"]].groupby(['FamSize', 'Sex'],
as_index=True ).mean()
df.to_csv('submission.csv', index=False )<load_pretrained>
for df in combine:
df['Se_Cl'] = 0
df.loc[(( df.Sex == 1)&(df.Pclass == 1)) , 'Se_Cl'] = 1
df.loc[(( df.Sex == 1)&(df.Pclass == 2)) , 'Se_Cl'] = 2
df.loc[(( df.Sex == 1)&(df.Pclass == 3)) , 'Se_Cl'] = 3
df.loc[(( df.Sex == 2)&(df.Pclass == 1)) , 'Se_Cl'] = 4
df.loc[(( df.Sex == 2)&(df.Pclass == 2)) , 'Se_Cl'] = 5
df.loc[(( df.Sex == 2)&(df.Pclass == 3)) , 'Se_Cl'] = 6
print(train_df.Se_Cl.value_counts())
 print("_"*40)
 print(test_df.Se_Cl.value_counts())
 print("_"*40)
 print("_"*40)
 train_df[['Se_Cl', 'Survived']].groupby(['Se_Cl'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
if not os.path.exists('/kaggle/input/lyft-motion-prediction-autonomous-vehicles'):
drive.mount('/content/drive')
 !pip install -q kaggle
if not os.path.exists('kaggle.json'):
files.upload()
!mkdir ~/.kaggle
!cp kaggle.json ~/.kaggle/
!chmod 600 ~/.kaggle/kaggle.json
!kaggle config path -p /content
def kaggle_dataset_download_unzip(lfilename,dir):
with zipfile.ZipFile(lfilename)as file:
file.extractall(dir)
 os.remove(lfilename)
 dir='/content/lyft-motion'
if not os.path.exists(dir):
!kaggle datasets download -d aikhmelnytskyy/liftfirst
lfilename='/content/liftfirst.zip'
kaggle_dataset_download_unzip(lfilename,dir+'/scenes')
 !kaggle datasets download -d aikhmelnytskyy/lyftsecond
lfilename='/content/lyftsecond.zip'
kaggle_dataset_download_unzip(lfilename,dir)
 !kaggle datasets download -d huanvo/lyft-pretrained-model-hv
lfilename='/content/lyft-pretrained-model-hv.zip'
kaggle_dataset_download_unzip(lfilename,'/content/lyft-pretrained-model-hv')
 data_path='/content/lyft-motion/'
weight_path='/content/lyft-pretrained-model-hv/model_multi_update_lyft_public.pth'
model_path='/content/drive/My Drive/Models/'
else:
data_path='/kaggle/input/lyft-motion-prediction-autonomous-vehicles'
weight_path='/kaggle/input/lyft-pretrained-model-hv/model_multi_update_lyft_public.pth'
model_path=''<import_modules>
for df in combine:
df['Cl_IA'] = 0
df.loc[(( df.IsAlone == 1)&(df.Pclass == 1)) , 'Cl_IA'] = 1
df.loc[(( df.IsAlone == 1)&(df.Pclass == 2)) , 'Cl_IA'] = 2
df.loc[(( df.IsAlone == 1)&(df.Pclass == 3)) , 'Cl_IA'] = 3
df.loc[(( df.IsAlone == 0)&(df.Pclass == 1)) , 'Cl_IA'] = 4
df.loc[(( df.IsAlone == 0)&(df.Pclass == 2)) , 'Cl_IA'] = 5
df.loc[(( df.IsAlone == 0)&(df.Pclass == 3)) , 'Cl_IA'] = 6
print(train_df.Cl_IA.value_counts())
 print("_"*40)
 print(test_df.Cl_IA.value_counts())
 print("_"*40)
 print("_"*40)
 train_df[['Cl_IA', 'Survived']].groupby(['Cl_IA'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
!pip install -q l5kit
warnings.filterwarnings("ignore" )<import_modules>
for df in combine:
df['Ca_Cl'] = 0
df.loc[(( df.Cabin == 0)&(df.Pclass == 1)) , 'Ca_Cl'] = 1
df.loc[(( df.Cabin == 0)&(df.Pclass == 2)) , 'Ca_Cl'] = 2
df.loc[(( df.Cabin == 0)&(df.Pclass == 3)) , 'Ca_Cl'] = 3
df.loc[(( df.Cabin == 1)&(df.Pclass == 1)) , 'Ca_Cl'] = 4
df.loc[(( df.Cabin == 1)&(df.Pclass == 2)) , 'Ca_Cl'] = 5
df.loc[(( df.Cabin == 1)&(df.Pclass == 3)) , 'Ca_Cl'] = 6
print(train_df.Ca_Cl.value_counts())
 print("_"*40)
 print(test_df.Ca_Cl.value_counts())
 print("_"*40)
 print("_"*40)
 train_df[['Ca_Cl', 'Survived']].groupby(['Ca_Cl'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
l5kit.__version__<set_options>
for df in combine:
df['MA_Cl'] = 0
df.loc[(( df.MisAge == 0)&(df.Pclass == 1)) , 'MA_Cl'] = 1
df.loc[(( df.MisAge == 0)&(df.Pclass == 2)) , 'MA_Cl'] = 2
df.loc[(( df.MisAge == 0)&(df.Pclass == 3)) , 'MA_Cl'] = 3
df.loc[(( df.MisAge == 1)&(df.Pclass == 1)) , 'MA_Cl'] = 4
df.loc[(( df.MisAge == 1)&(df.Pclass == 2)) , 'MA_Cl'] = 5
df.loc[(( df.MisAge == 1)&(df.Pclass == 3)) , 'MA_Cl'] = 6
print(train_df.MA_Cl.value_counts())
 print("_"*40)
 print(test_df.MA_Cl.value_counts())
 print("_"*40)
 print("_"*40)
 train_df[['MA_Cl', 'Survived']].groupby(['MA_Cl'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
def set_seed(seed):
random.seed(seed)
 np.random.seed(seed)
 os.environ["PYTHONHASHSEED"] = str(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 set_seed(42 )<init_hyperparams>
for df in combine:
df['IK_Cl'] = 0
df.loc[(( df.IsKid == 0)&(df.Pclass == 1)) , 'IK_Cl'] = 1
df.loc[(( df.IsKid == 0)&(df.Pclass == 2)) , 'IK_Cl'] = 2
df.loc[(( df.IsKid == 0)&(df.Pclass == 3)) , 'IK_Cl'] = 3
df.loc[(( df.IsKid == 1)&(df.Pclass == 1)) , 'IK_Cl'] = 4
df.loc[(( df.IsKid == 1)&(df.Pclass == 2)) , 'IK_Cl'] = 5
df.loc[(( df.IsKid == 1)&(df.Pclass == 3)) , 'IK_Cl'] = 6
print(train_df.IK_Cl.value_counts())
 print("_"*40)
 print(test_df.IK_Cl.value_counts())
 print("_"*40)
 print("_"*40)
 train_df[['IK_Cl', 'Survived']].groupby(['IK_Cl'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
cfg = {
'format_version': 4,
'data_path': data_path,
'model_params': {
'model_architecture': 'resnet34',
'history_num_frames': 10,
'history_step_size': 1,
'history_delta_time': 0.1,
'future_num_frames': 50,
'future_step_size': 1,
'future_delta_time': 0.1,
'model_name': "model_resnet34_output",
'lr': 1e-3,
'weight_path': weight_path,
'train': False,
'predict': True
},
'raster_params': {
'raster_size': [224, 224],
'pixel_size': [0.5, 0.5],
'ego_center': [0.25, 0.5],
'map_type': 'py_semantic',
'satellite_map_key': 'aerial_map/aerial_map.png',
'semantic_map_key': 'semantic_map/semantic_map.pb',
'dataset_meta_key': 'meta.json',
'filter_agents_threshold': 0.5
},
'train_data_loader': {
'key': 'scenes/train.zarr',
'batch_size': 32,
'shuffle': True,
'num_workers': 4
},
'test_data_loader': {
'key': 'scenes/test.zarr',
'batch_size': 32,
'shuffle': False,
'num_workers': 4
},
'train_params': {
'max_num_steps': 101,
'checkpoint_every_n_steps': 20,
}
}<feature_engineering>
for df in combine:
df["Em_Cl"] = df["Embarked"] * df["Pclass"]
print(train_df.Em_Cl.value_counts())
 print("_"*40)
 print(test_df.Em_Cl.value_counts())
 print("_"*40)
 print("_"*40)
 train_df[['Em_Cl', 'Survived']].groupby(['Em_Cl'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
DIR_INPUT = cfg["data_path"]
os.environ["L5KIT_DATA_FOLDER"] = DIR_INPUT
dm = LocalDataManager(None )<create_dataframe>
for df in combine:
df['Se_Ca'] = 0
df.loc[(( df.Sex == 1)&(df.Cabin == 0)) , 'Se_Ca'] = 1
df.loc[(( df.Sex == 1)&(df.Cabin == 1)) , 'Se_Ca'] = 2
df.loc[(( df.Sex == 2)&(df.Cabin == 0)) , 'Se_Ca'] = 3
df.loc[(( df.Sex == 2)&(df.Cabin == 1)) , 'Se_Ca'] = 4
print(train_df.Se_Ca.value_counts())
 print("_"*40)
 print(test_df.Se_Ca.value_counts())
 print("_"*40)
 print("_"*40)
 train_df[['Se_Ca', 'Survived']].groupby(['Se_Ca'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
train_cfg = cfg["train_data_loader"]
rasterizer = build_rasterizer(cfg, dm)
 train_zarr = ChunkedDataset(dm.require(train_cfg["key"])).open()
train_dataset = AgentDataset(cfg, train_zarr, rasterizer)
 train_dataloader = DataLoader(train_dataset, shuffle=train_cfg["shuffle"], batch_size=train_cfg["batch_size"],
num_workers=train_cfg["num_workers"])
 print("==================================TRAIN DATA==================================")
 print(train_dataset )<create_dataframe>
for df in combine:
df['MA_Ca'] = 0
df.loc[(( df.MisAge == 0)&(df.Cabin == 0)) , 'MA_Ca'] = 1
df.loc[(( df.MisAge == 0)&(df.Cabin == 1)) , 'MA_Ca'] = 2
df.loc[(( df.MisAge == 1)&(df.Cabin == 0)) , 'MA_Ca'] = 3
df.loc[(( df.MisAge == 1)&(df.Cabin == 1)) , 'MA_Ca'] = 4
print(train_df.MA_Ca.value_counts())
 print("_"*40)
 print(test_df.MA_Ca.value_counts())
 print("_"*40)
 print("_"*40)
 train_df[['MA_Ca', 'Survived']].groupby(['MA_Ca'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
test_cfg = cfg["test_data_loader"]
rasterizer = build_rasterizer(cfg, dm)
 test_zarr = ChunkedDataset(dm.require(test_cfg["key"])).open()
test_mask = np.load(f"{DIR_INPUT}/scenes/mask.npz")["arr_0"]
test_dataset = AgentDataset(cfg, test_zarr, rasterizer, agents_mask=test_mask)
 test_dataloader = DataLoader(test_dataset,shuffle=test_cfg["shuffle"],batch_size=test_cfg["batch_size"],
num_workers=test_cfg["num_workers"])
 print("==================================TEST DATA==================================")
 print(test_dataset )<choose_model_class>
for df in combine:
df['Se_IA'] = 0
df.loc[(( df.Sex == 1)&(df.IsAlone == 0)) , 'Se_IA'] = 1
df.loc[(( df.Sex == 1)&(df.IsAlone == 1)) , 'Se_IA'] = 2
df.loc[(( df.Sex == 2)&(df.IsAlone == 0)) , 'Se_IA'] = 3
df.loc[(( df.Sex == 2)&(df.IsAlone == 1)) , 'Se_IA'] = 4
print(train_df.Se_IA.value_counts())
 print("_"*40)
 print(test_df.Se_IA.value_counts())
 print("_"*40)
 print("_"*40)
 train_df[['Se_IA', 'Survived']].groupby(['Se_IA'],
as_index=True ).mean().sort_values(by='Survived',
ascending=False )
class LyftMultiModel(nn.Module):
def __init__(self, cfg: Dict, num_modes=3):
super().__init__()
architecture = cfg["model_params"]["model_architecture"]
backbone = eval(architecture )(pretrained=True, progress=True)
 self.backbone = backbone
num_history_channels =(cfg["model_params"]["history_num_frames"] + 1)* 2
num_in_channels = 3 + num_history_channels
self.backbone.conv1 = nn.Conv2d(
 num_in_channels,
self.backbone.conv1.out_channels,
kernel_size=self.backbone.conv1.kernel_size,
stride=self.backbone.conv1.stride,
padding=self.backbone.conv1.padding,
bias=False, 
)
 if architecture == 'resnet101':
backbone_out_features = 2048
else:
backbone_out_features = 512
self.future_len = cfg["model_params"]["future_num_frames"]
num_targets = 2 * self.future_len
self.head = nn.Sequential(
 nn.Linear(in_features=backbone_out_features, out_features=4096), 
)
 self.num_preds = num_targets * num_modes
self.num_modes = num_modes
self.logit = nn.Linear(4096, out_features=self.num_preds + num_modes)
 def forward(self, x):
x = self.backbone.conv1(x)
 x = self.backbone.bn1(x)
 x = self.backbone.relu(x)
 x = self.backbone.maxpool(x)
 x = self.backbone.layer1(x)
 x = self.backbone.layer2(x)
 x = self.backbone.layer3(x)
 x = self.backbone.layer4(x)
 x = self.backbone.avgpool(x)
 x = torch.flatten(x, 1)
 x = self.head(x)
 x = self.logit(x)
 bs, _ = x.shape
pred, confidences = torch.split(x, self.num_preds, dim=1)
 pred = pred.view(bs, self.num_modes, self.future_len, 2)
 assert confidences.shape ==(bs, self.num_modes)
 confidences = torch.softmax(confidences, dim=1)
 return pred, confidences<compute_train_metric>
warnings.filterwarnings("ignore" )
def forward(data, model, device, criterion = pytorch_neg_multi_log_likelihood_batch):
inputs = data["image"].to(device)
 target_availabilities = data["target_availabilities"].to(device)
 targets = data["target_positions"].to(device)
 preds, confidences = model(inputs)
 loss = criterion(targets, preds, confidences, target_availabilities)
 return loss, preds, confidences<choose_model_class>
features = ['Pclass', 'Sex', 'Cabin', 'Embarked', 'Title', 'AgeBin', 'MisAge', 'IsKid',
'FamSize', 'Se_Cl', 'Cl_IA', 'Se_Ca', 'MA_Ca', 'Se_IA']
y = train_df['Survived'].copy()
X = train_df[features].copy()
test = test_df[features].copy()
X.head()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 model = LyftMultiModel(cfg)
 print(torch.cuda.is_available())
 weight_path = cfg["model_params"]["weight_path"]
if weight_path:
model.load_state_dict(torch.load(weight_path))
model.to(device)
 optimizer = optim.Adam(model.parameters() , lr=cfg["model_params"]["lr"])
 print(f'device {device}' )<train_model>
clf_list = [DecisionTreeClassifier() ,
RandomForestClassifier() ,
AdaBoostClassifier() ,
GradientBoostingClassifier() ,
XGBClassifier() ,
Perceptron() ,
LogisticRegression() ,
SVC() ,
LinearSVC() ,
KNeighborsClassifier() ,
GaussianNB() ,
SGDClassifier()
]
print(model )<init_hyperparams>
mdl = []
bias_acc = []
var_acc = []
bias_f1 = []
var_f1 = []
bias_auc = []
var_auc = []
acc_scorer = make_scorer(f1_score)
 for clf in clf_list:
model = clf.__class__.__name__
res = cross_val_score(clf, X, y, scoring='accuracy', cv = 5)
 score = round(res.mean() * 100, 3)
 var = round(res.std() , 3)
 bias_acc.append(score)
 var_acc.append(var)
 res = cross_val_score(clf, X, y, scoring=acc_scorer, cv = 5)
 score = round(res.mean() * 100, 3)
 var = round(res.std() , 3)
 bias_f1.append(score)
 var_f1.append(var)
 res = cross_val_score(clf, X, y, scoring='roc_auc', cv = 5)
 score = round(res.mean() * 100, 3)
 var = round(res.std() , 3)
 bias_auc.append(score)
 var_auc.append(var)
 mdl.append(model)
 print(model)
 robcon = pd.DataFrame({'Model': mdl, 'Bias_acc':bias_acc,'Variance_acc':var_acc,
'Bias_f1':bias_f1,'Variance_f1':var_f1, 'Bias_auc':bias_auc,'Variance_auc':var_auc,
})
 robcon = robcon[['Model','Bias_acc','Variance_acc', 'Bias_f1','Variance_f1','Bias_auc','Variance_auc' ]]
robcon
if cfg["model_params"]["train"]:
tr_it = iter(train_dataloader)
 progress_bar = tqdm(range(cfg["train_params"]["max_num_steps"]))
num_iter = cfg["train_params"]["max_num_steps"]
losses_train = []
iterations = []
metrics = []
times = []
model_name = cfg["model_params"]["model_name"]
start = time.time()
for i in progress_bar:
try:
data = next(tr_it)
 except StopIteration:
tr_it = iter(train_dataloader)
 data = next(tr_it)
 model.train()
torch.set_grad_enabled(True)
 loss, _, _ = forward(data, model, device)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
losses_train.append(loss.item())
 progress_bar.set_description(f"loss: {loss.item() } loss(avg): {np.mean(losses_train)}")
 if i % cfg['train_params']['checkpoint_every_n_steps'] == 0:
torch.save(model.state_dict() , f'{model_path}{model_name}_{i}.pth')
 iterations.append(i)
 metrics.append(np.mean(losses_train))
times.append(( time.time() -start)/60)
 results = pd.DataFrame({'iterations': iterations, 'metrics(avg)': metrics, 'elapsed_time(mins)': times})
 results.to_csv(f"train_metrics_{model_name}_{num_iter}.csv", index = False)
 print(f"Total training time is {(time.time() -start)/60} mins")
 print(results.head() )<save_to_csv>
print("Best for accuracy")
 print(robcon[['Model','Bias_acc']].sort_values(by= 'Bias_acc', ascending=False ).head(6))
print("_"*40)
 print("Best for f1")
 print(robcon[['Model','Bias_f1']].sort_values(by= 'Bias_f1', ascending=False ).head(6))
print("_"*40)
 print("Best for roc_auc")
 print(robcon[['Model','Bias_auc']].sort_values(by= 'Bias_auc', ascending=False ).head(6))
print("_"*40)
 print("Least variance for accuracy")
 print(robcon[['Model','Variance_acc']].sort_values(by= 'Variance_acc' ).head(6))
print("_"*40)
 print("Least variance for f1")
 print(robcon[['Model','Variance_f1']].sort_values(by= 'Variance_f1' ).head(6))
print("_"*40)
 print("Least variance for roc_auc")
 print(robcon[['Model','Variance_auc']].sort_values(by= 'Variance_auc' ).head(6))
pred_path = 'submission.csv'
write_pred_csv(pred_path,
timestamps=np.concatenate(timestamps),
track_ids=np.concatenate(agent_ids),
coords=np.concatenate(future_coords_offsets_pd),
confs = np.concatenate(confidences_list)
 )<import_modules>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=895 )
import numpy as np
import pandas as pd
import os 
<save_to_csv>
from sklearn.feature_selection import RFECV
SUBMISSION_FOLDER = '.. /input/lyft-submit'
SUBMISSION_FILE = 'ext_submission.csv'
submissions = pd.read_csv(f"{SUBMISSION_FOLDER}/{SUBMISSION_FILE}")
 submissions.to_csv("submission.csv", index=False )<set_options>
FeatSel_log = RFECV(LogisticRegression() , step = 1, scoring = 'roc_auc', cv = 10)
 FeatSel_log.fit(X_train, y_train)
 BestFeat_log = X_train.columns.values[FeatSel_log.get_support() ]
BestFeat_log
pd.set_option('max_columns', 50 )<import_modules>
param_grid = {'C': [0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000],
'tol': [0.000001, 0.00001, 0.0001, 0.001, 0.01, 0.1, 1],
'random_state' : [42]}
grid_log = GridSearchCV(LogisticRegression() , param_grid, cv = 10, scoring= 'roc_auc')
 %time grid_log.fit(X_train[BestFeat_log], y_train)
 best_log = grid_log.best_estimator_
print(best_log)
 print("_"*40)
 print(np.abs(grid_log.best_score_))
print("_"*40)
 predictions = best_log.predict(X_test[BestFeat_log])
 accuracy_score(y_true = y_test, y_pred = predictions )
print("l5kit version:", l5kit.__version__ )<import_modules>
param_grid = {'C': np.arange(1,10),
'tol': [0.0001, 0.001, 0.01, 0.1, 1],
'kernel': ['linear', 'rbf', 'poly', 'sigmoid'],
'random_state': [42]}
grid_SVC = GridSearchCV(SVC() , param_grid, cv = 10, scoring= 'roc_auc')
 %time grid_SVC.fit(X_train, y_train)
 best_SVC = grid_SVC.best_estimator_
print(best_SVC)
 print("_"*40)
 print(np.abs(grid_SVC.best_score_))
predictions = best_SVC.predict(X_test)
 accuracy_score(y_true = y_test, y_pred = predictions )
import torch
from torch.utils.data import DataLoader
from torch.utils.data.dataset import Subset
from torchvision.models import resnet18
from torch import nn
from typing import Dict<choose_model_class>
FeatSel_ada = RFECV(AdaBoostClassifier() , step = 1, scoring = 'roc_auc', cv = 10)
 FeatSel_ada.fit(X_train, y_train)
 BestFeat_ada = X_train.columns.values[FeatSel_ada.get_support() ]
BestFeat_ada
class LyftMultiModel(nn.Module):
def __init__(self, cfg: Dict, num_modes=3):
super().__init__()
backbone = resnet18(pretrained=False, progress=True)
 self.backbone = backbone
num_history_channels =(cfg["model_params"]["history_num_frames"] + 1)* 2
num_in_channels = 3 + num_history_channels
self.backbone.conv1 = nn.Conv2d(
 num_in_channels,
self.backbone.conv1.out_channels,
kernel_size=self.backbone.conv1.kernel_size,
stride=self.backbone.conv1.stride,
padding=self.backbone.conv1.padding,
bias=False, 
)
 backbone_out_features = 512
self.future_len = cfg["model_params"]["future_num_frames"]
num_targets = 2 * self.future_len
self.head = nn.Sequential(
 nn.Linear(in_features=backbone_out_features, out_features=4096), 
)
 self.num_preds = num_targets * num_modes
self.num_modes = num_modes
self.logit = nn.Linear(4096, out_features=self.num_preds + num_modes)
 def forward(self, x):
x = self.backbone.conv1(x)
 x = self.backbone.bn1(x)
 x = self.backbone.relu(x)
 x = self.backbone.maxpool(x)
 x = self.backbone.layer1(x)
 x = self.backbone.layer2(x)
 x = self.backbone.layer3(x)
 x = self.backbone.layer4(x)
 x = self.backbone.avgpool(x)
 x = torch.flatten(x, 1)
 x = self.head(x)
 x = self.logit(x)
 bs, _ = x.shape
pred, confidences = torch.split(x, self.num_preds, dim=1)
 pred = pred.view(bs, self.num_modes, self.future_len, 2)
 assert confidences.shape ==(bs, self.num_modes)
 confidences = torch.softmax(confidences, dim=1)
 return pred, confidences 
<load_pretrained>
param_grid = {'n_estimators': np.arange(50, 500, 50),
'learning_rate': [0.0001, 0.001, 0.01, 0.1, 1, 2],
'algorithm': ['SAMME', 'SAMME.R'],
'random_state': [42]}
grid_ada = GridSearchCV(AdaBoostClassifier() , param_grid, cv = 10, scoring= 'roc_auc')
 %time grid_ada.fit(X_train[BestFeat_ada], y_train)
 best_ada = grid_ada.best_estimator_
print(best_ada)
 print("_"*40)
 print(np.abs(grid_ada.best_score_))
predictions = best_ada.predict(X_test[BestFeat_ada])
 accuracy_score(y_true = y_test, y_pred = predictions )
def save_yaml(filepath, content, width=120):
with open(filepath, 'w')as f:
yaml.dump(content, f, width=width)
 def load_yaml(filepath):
with open(filepath, 'r')as f:
content = yaml.safe_load(f)
 return content
class DotDict(dict):

 __getattr__ = dict.get
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__ 
<predict_on_test>
FeatSel_for = RFECV(RandomForestClassifier() , step = 1, scoring = 'roc_auc', cv = 10)
 FeatSel_for.fit(X_train, y_train)
 BestFeat_for = X_train.columns.values[FeatSel_for.get_support() ]
BestFeat_for
def run_prediction(predictor, data_loader):
predictor.eval()
pred_coords_list = []
confidences_list = []
timestamps_list = []
track_id_list = []
with torch.no_grad() :
dataiter = tqdm(data_loader)
 for data in dataiter:
image = data["image"].to(device)
 pred, confidences = predictor(image)
 pred_coords_list.append(pred.cpu().numpy().copy())
 confidences_list.append(confidences.cpu().numpy().copy())
 timestamps_list.append(data["timestamp"].numpy().copy())
 track_id_list.append(data["track_id"].numpy().copy())
 timestamps = np.concatenate(timestamps_list)
 track_ids = np.concatenate(track_id_list)
 coords = np.concatenate(pred_coords_list)
 confs = np.concatenate(confidences_list)
 return timestamps, track_ids, coords, confs 
<init_hyperparams>
param_grid = {'n_estimators': np.arange(10, 100, 10),
'max_depth': np.arange(2,20),
'max_features' : ['auto', 'log2', None],
'criterion' : ['gini', 'entropy'],
'random_state' : [42]}
grid_forest = GridSearchCV(RandomForestClassifier() , param_grid, cv = 10, scoring= 'roc_auc')
 %time grid_forest.fit(X_train[BestFeat_for], y_train)
 best_forest = grid_forest.best_estimator_
print(best_forest)
 print("_"*40)
 print(np.abs(grid_forest.best_score_))
predictions = best_forest.predict(X_test[BestFeat_for])
 accuracy_score(y_true = y_test, y_pred = predictions )
cfg = {
'format_version': 4,
'model_params': {
'model_architecture': 'resnet50',
'history_num_frames': 10,
'history_step_size': 1,
'history_delta_time': 0.1,
'future_num_frames': 50,
'future_step_size': 1,
'future_delta_time': 0.1
},
'raster_params': {
'raster_size': [448, 448],
'pixel_size': [0.5, 0.5],
'ego_center': [0.25, 0.5],
'map_type': 'py_semantic',
'satellite_map_key': 'aerial_map/aerial_map.png',
'semantic_map_key': 'semantic_map/semantic_map.pb',
'dataset_meta_key': 'meta.json',
'filter_agents_threshold': 0.5
},
'train_data_loader': {
'key': 'scenes/train.zarr',
'batch_size': 12,
'shuffle': True,
'num_workers': 4
},
'valid_data_loader': {
'key': 'scenes/validate.zarr',
'batch_size': 32,
'shuffle': False,
'num_workers': 4
},
'test_data_loader': {
'key': 'scenes/test.zarr',
'batch_size': 8,
'shuffle': False,
'num_workers': 4
},
'train_params': {
'max_num_steps': 10000,
'checkpoint_every_n_steps': 5000,
}
} 
<define_variables>
FeatSel_XGB = RFECV(XGBClassifier() , step = 1, scoring = 'roc_auc', cv = 10)
 FeatSel_XGB.fit(X_train, y_train)
 BestFeat_XGB = X_train.columns.values[FeatSel_XGB.get_support() ]
BestFeat_XGB
flags_dict = {
"debug": False,
"l5kit_data_folder": "/kaggle/input/lyft-motion-prediction-autonomous-vehicles",
"pred_mode": "multi",
"device": "cuda:0",
"out_dir": "results/multi_train",
"epoch": 2,
"snapshot_freq": 50,
}<load_pretrained>
param_grid = {'learning_rate': [0.0001, 0.001, 0.01, 0.1, 1, 2],
'max_depth': np.arange(2,10),
'n_estimators': np.arange(50, 500, 50),
'random_state': [42]}
grid_XGB = GridSearchCV(XGBClassifier() , param_grid, cv = 10, scoring= 'roc_auc')
 %time grid_XGB.fit(X_train[BestFeat_XGB], y_train)
 best_XGB = grid_XGB.best_estimator_
print(best_XGB)
 print("_"*40)
 print(np.abs(grid_XGB.best_score_))
predictions = best_XGB.predict(X_test[BestFeat_XGB])
 accuracy_score(y_true = y_test, y_pred = predictions )
<load_pretrained><EOS> 
submission = pd.DataFrame({
"PassengerId": test_df["PassengerId"],
"Survived": best_XGB.predict(test[BestFeat_XGB])
 })
 submission.to_csv('submission.csv', index=False )