Kaggle competition: Peking University/Baidu - Autonomous Driving
<SOS> metric: PKUAutoDrivingAP Kaggle data source: peking-university/baidu-autonomous-driving<create_dataframe>
PATH = '.. /input/pku-autonomous-driving/'
os.listdir(PATH )
targets = ["age","domain1_var1","domain1_var2","domain2_var1","domain2_var2"]
models = ['enet','lasso','ridge', 'svr', 'bagging_regressor', 'bayesian_ridge', 'ardregression', 'kernel_ridge', 'nusvr', 'linear_svr', 'power0', 'power1', 'power2']
data_preprocessings = ['scale','standarize','minmax']
no_folds = 7
results_test_1 = pd.DataFrame()
results_test_2 = pd.DataFrame()
results_train = df[['Id','fold_column']+targets]
results_test_1['Id'] = test_df_1['Id']
results_test_2['Id'] = test_df_2['Id']
w = {"age":0.3, "domain1_var1":0.175, "domain1_var2":0.175, "domain2_var1":0.175,"domain2_var2":0.175}
for target in targets:
for model in models:
for pp in data_preprocessings:
print("Model: ",model," | ","Target: ",target," | ","Preprocessing: ",pp)
 y_oof = np.zeros(df.shape[0])
 y_test_1 = np.zeros(( test_df.shape[0], 7))
y_test_2 = np.zeros(( test_df.shape[0], 7))
for i in range(no_folds):
gc.collect()
train_df = df[df['fold_column']!=i]
val_df = df[df['fold_column']==i]
train_df = train_df[train_df[target].notnull() ]
parameter = parameters[model][target][pp]
if pp == 'scale':
train_df[fnc_features] *= 1/parameter[0]
val_df[fnc_features] *= 1/parameter[0]
t1 = test_df_1[features]
t1[fnc_features] *= 1/parameter[0]
t2 = test_df_2[features]
t2[fnc_features] *= 1/parameter[0]
t1 = t1.values
t2 = t2.values
parameter = parameter[1:]
if pp == 'standarize':
scaler = StandardScaler()
scaler.fit(train_df[features])
 train_df[features] = scaler.transform(train_df[features])
 val_df[features] = scaler.transform(val_df[features])
 t1 = scaler.transform(test_df_1[features])
 t2 = scaler.transform(test_df_2[features])
 if pp == 'minmax':
scaler = MinMaxScaler()
scaler.fit(train_df[features])
 train_df[features] = scaler.transform(train_df[features])
 val_df[features] = scaler.transform(val_df[features])
 t1 = scaler.transform(test_df_1[features])
 t2 = scaler.transform(test_df_2[features])
 if model == 'svr':
M = SVR(C=parameter[0], cache_size=3000.0)
 if model == 'enet':
M = ElasticNet(alpha = parameter[0], l1_ratio = parameter[1], normalize = False, max_iter = 5000, tol = 1e-5)
 if model == 'lasso':
M = Lasso(alpha = parameter[0], normalize = False, max_iter = 5000, tol = 1e-5)
 if model == 'ridge':
M = Ridge(alpha = parameter[0], normalize = False, max_iter = 5000, tol = 1e-5)
 if model == 'bagging_regressor':
M = BaggingRegressor(Ridge(alpha = parameter[0]), n_estimators=parameter[1], random_state=42,
max_samples=parameter[2], max_features=parameter[3])
 if model == 'bayesian_ridge':
M = BayesianRidge(n_iter=1000, tol=10e-05)
 if model == 'ardregression':
M = ARDRegression()
if model == 'kernel_ridge':
M = KernelRidge(alpha= parameter[0],kernel ='rbf')
 if model == 'nusvr':
M = NuSVR(nu=parameter[0], C=parameter[1])
 if model == 'linear_svr':
M = LinearSVR(C=parameter[0])
 if model in ['power0', 'power1', 'power2']:
M = TweedieRegressor(power = int(model[-1]), alpha = parameter[0])
 M.fit(train_df[features].values, train_df[target].values)
 y_oof[val_df.index] = M.predict(val_df[features])
 y_test_1[:, i] = M.predict(t1)
 y_test_2[:, i] = M.predict(t2)
 results_train[target+'.'+model+'.'+pp] = y_oof
results_test_1[target+'.'+model+'.'+pp] = y_test_1.mean(axis=1)
 results_test_2[target+'.'+model+'.'+pp] = y_test_2.mean(axis=1)
 score = metric(df[df[target].notnull() ][target].values, results_train[df[target].notnull() ][target+'.'+model+'.'+pp].values)
 print('Score: ',score)
 print()<prepare_x_and_y>
SWITCH_LOSS_EPOCH = 5
print(torch.__version__ )
for model in ['multi_enet', 'multi_lasso']:
for pp in data_preprocessings:
print("Model: ",model," | ","Preprocessing: ",pp)
 y_oof = np.zeros(( df.shape[0], 5))
y_test_1 = np.zeros(( test_df.shape[0], 5))
y_test_2 = np.zeros(( test_df.shape[0], 5))
for fold_no in range(7):
gc.collect()
train_df = df[df['fold_column']!=fold_no]
val_df = df[df['fold_column']==fold_no]
for t in target:
train_df = train_df[train_df[t].notnull() ]
parameter = parameters[model][pp]
if pp == 'scaling':
train_df[fnc_features] *= 1/parameter[0]
val_df[fnc_features] *= 1/parameter[0]
t1 = test_df_1[features]
t1[fnc_features] *= 1/parameter[0]
t2 = test_df_2[features]
t2[fnc_features] *= 1/parameter[0]
t1 = t1.values
t2 = t2.values
parameter = parameter[1:]
if pp == 'standarize':
scaler = StandardScaler()
scaler.fit(train_df[features])
 train_df[features] = scaler.transform(train_df[features])
 val_df[features] = scaler.transform(val_df[features])
 t1 = scaler.transform(test_df_1[features])
 t2 = scaler.transform(test_df_2[features])
 if pp == 'minmax':
scaler = MinMaxScaler()
scaler.fit(train_df[features])
 train_df[features] = scaler.transform(train_df[features])
 val_df[features] = scaler.transform(val_df[features])
 t1 = scaler.transform(test_df_1[features])
 t2 = scaler.transform(test_df_2[features])
 if model == 'multi_enet':
M = MultiTaskElasticNet(alpha = parameter[0], l1_ratio = parameter[1],
normalize = False,
max_iter = 5000, tol = 1e-5)
 if model == 'multi_lasso':
M = MultiTaskLasso(alpha = parameter[0],
normalize = False,
max_iter = 5000, tol = 1e-5)
 M.fit(train_df[features].values, train_df[target].values)
 y_oof[val_df.index] = M.predict(val_df[features])
 y_test_1 += M.predict(t1)
 y_test_2 += M.predict(t2)
 for x, t in enumerate(target):
results_train[t+'.'+model+'.'+pp] = y_oof[:,x]
results_test_1[t+'.'+model+'.'+pp] = y_test_1[:,x]/7
results_test_2[t+'.'+model+'.'+pp] = y_test_2[:,x]/7
mae, score = metric(results_train[df[t].notnull() ][t].values, results_train[df[t].notnull() ][t+'.'+model+'.'+pp].values)
 print('Feature: ', t, " | ", 'Score: ', score, " | ", 'MAE: ', mae)
 print()<save_to_csv>
train = pd.read_csv(PATH + 'train.csv')
 test = pd.read_csv(PATH + 'sample_submission.csv')
 bad_list = ['ID_1a5a10365',
'ID_1db0533c7',
'ID_53c3fe91a',
'ID_408f58e9f',
'ID_4445ae041',
'ID_bb1d991f6',
'ID_c44983aeb',
'ID_f30ebe4d4']
train = train.loc[~train['ImageId'].isin(bad_list)]
camera_matrix = np.array([[2304.5479, 0, 1686.2379],
[0, 2305.8757, 1354.9849],
[0, 0, 1]], dtype=np.float32)
 camera_matrix_inv = np.linalg.inv(camera_matrix)
 train.head()
results_train.to_csv('OOF_preds.csv')
 results_test_1.to_csv('Test_preds_1.csv')
 results_test_2.to_csv('Test_preds_2.csv' )<compute_train_metric>
def imread(path, fast_mode=False):
img = cv2.imread(path)
 if not fast_mode and img is not None and len(img.shape)== 3:
img = np.array(img[:, :, ::-1])
 return img
img = imread(PATH + 'train_images/ID_8a6e65317' + '.jpg')
 IMG_SHAPE = img.shape
oof = pd.read_csv('OOF_preds.csv')
 for i in sorted(oof.columns):
t = i.split('.')[0]
if(t in targets)and i!=t:
mae, score = metric(oof[oof[t].notnull() ][t].values, oof[oof[t].notnull() ][i].values)
 print("Target", t ,' | ',i ,' | ','MAE', mae, '|', 'Score', score )<load_from_csv>
def str2coords(s, names=['id', 'yaw', 'pitch', 'roll', 'x', 'y', 'z']):

 coords = []
for l in np.array(s.split() ).reshape([-1, 7]):
coords.append(dict(zip(names, l.astype('float'))))
if 'id' in coords[-1]:
coords[-1]['id'] = int(coords[-1]['id'])
 return coords
test_1 = pd.read_csv('Test_preds_1.csv')
 test_2 = pd.read_csv('Test_preds_1.csv' )<define_variables>
inp = train['PredictionString'][0]
print('Example input:
', inp)
 print()
print('Output:
', str2coords(inp))
model_names = ['bayesian_ridge', 'bagging_regressor', 'ElasticNet', 'Ridge', 'SVR', 'NuSVR']<prepare_x_and_y>
def rotate(x, angle):
x = x + angle
x = x -(x + np.pi)//(2 * np.pi)* 2 * np.pi
return x
results_test_1 = pd.DataFrame()
results_test_2 = pd.DataFrame()
results_train = oof[['Id','fold']+targets]
results_test_1['Id'] = test_1['Id']
results_test_2['Id'] = test_2['Id']
w = {'age': 0.3,'domain1_var1': 0.175,'domain1_var2': 0.175,'domain2_var1': 0.175,'domain2_var2': 0.175}
overal_score = 0
for target in targets:
print("Target",target)
 all_scores = []
y_oof = np.zeros(oof.shape[0])
 y_test_1 = np.zeros(( test_1.shape[0], len(model_names)))
 y_test_2 = np.zeros(( test_2.shape[0], len(model_names)))
 features = [i for i in oof.columns if(target in i)and(i!=target)]
y_oof = np.zeros(( oof.shape[0],len(model_names)))
 for k,model in enumerate([BayesianRidge() , BaggingRegressor(Ridge()),ElasticNet(max_iter=5000, tol=10e-05), Ridge(tol=10e-05), SVR(max_iter=5000, tol=10e-05), NuSVR(tol=10e-05)]):
for fold_no in range(7):
gc.collect()
train_df = oof[oof['fold']!=fold_no]
val_df = oof[oof['fold']==fold_no]
train_df = train_df[train_df[target].notnull() ]
model.fit(train_df[features].values, train_df[target].values)
 y_oof[val_df.index,k] = model.predict(val_df[features])
 y_test_1[:, k] += model.predict(test_1[features])/7
y_test_2[:, k] += model.predict(test_2[features])/7
mae, score = metric(oof[oof[target].notnull() ][target].values, y_oof[oof[target].notnull() ,k])
 all_scores.append(mae)
 print(model_names[k],mae,score)
 weights = 0 - np.array(all_scores)
 weights = 0.001+weights - weights.min()
weights = weights/weights.sum()
TTTT = pd.DataFrame(np.corrcoef(y_oof.T))
TTTT.index = model_names
TTTT.columns = model_names
display(TTTT)
 print(weights)
 results_train[target] =(y_oof*weights ).sum(axis=1)
 results_test_1[target] =(y_test_1*weights ).sum(axis=1)
 results_test_2[target] =(y_test_2*weights ).sum(axis=1)
 mae, score = metric(oof[oof[target].notnull() ][target].values, results_train[oof[target].notnull() ][target].values)
 print('Target: ', target, " | ",'Score: ',score," | ","MAE: ", mae)
 overal_score += w[target]*score
print()
print('Overall Score: ', overal_score )<save_to_csv>
def get_img_coords(s):

 coords = str2coords(s)
 xs = [c['x'] for c in coords]
ys = [c['y'] for c in coords]
zs = [c['z'] for c in coords]
P = np.array(list(zip(xs, ys, zs)) ).T
img_p = np.dot(camera_matrix, P ).T
img_p[:, 0] /= img_p[:, 2]
img_p[:, 1] /= img_p[:, 2]
img_xs = img_p[:, 0]
img_ys = img_p[:, 1]
img_zs = img_p[:, 2]
return img_xs, img_ys
plt.figure(figsize=(14,14))
plt.imshow(imread(PATH + 'train_images/' + train['ImageId'][2217] + '.jpg'))
plt.scatter(*get_img_coords(train['PredictionString'][2217]), color='red', s=100);
results_train.to_csv('OOF_preds.csv')
 results_test_1.to_csv('Test_preds_1.csv')
 results_test_2.to_csv('Test_preds_2.csv' )<data_type_conversions>
def euler_to_Rot(yaw, pitch, roll):
Y = np.array([[cos(yaw), 0, sin(yaw)],
[0, 1, 0],
[-sin(yaw), 0, cos(yaw)]])
 P = np.array([[1, 0, 0],
[0, cos(pitch), -sin(pitch)],
[0, sin(pitch), cos(pitch)]])
 R = np.array([[cos(roll), -sin(roll), 0],
[sin(roll), cos(roll), 0],
[0, 0, 1]])
 return np.dot(Y, np.dot(P, R))
sub_df = pd.melt(results_test_1[["Id", "age", "domain1_var1", "domain1_var2", "domain2_var1", "domain2_var2"]], id_vars=["Id"], value_name="Predicted")
 sub_df["Id"] = sub_df["Id"].astype("str")+ "_" + sub_df["variable"].astype("str")
 sub_df = sub_df.drop("variable", axis=1 ).sort_values("Id")
 sub_df<save_to_csv>
IMG_WIDTH = 1536
IMG_HEIGHT = 512
MODEL_SCALE = 8
def _regr_preprocess(regr_dict):
for name in ['x', 'y', 'z']:
regr_dict[name] = regr_dict[name] / 100
regr_dict['roll'] = rotate(regr_dict['roll'], np.pi)
 regr_dict['pitch_sin'] = sin(regr_dict['pitch'])
 regr_dict['pitch_cos'] = cos(regr_dict['pitch'])
 regr_dict.pop('pitch')
 regr_dict.pop('id')
 return regr_dict
def _regr_back(regr_dict):
for name in ['x', 'y', 'z']:
regr_dict[name] = regr_dict[name] * 100
regr_dict['roll'] = rotate(regr_dict['roll'], -np.pi)
 pitch_sin = regr_dict['pitch_sin'] / np.sqrt(regr_dict['pitch_sin']**2 + regr_dict['pitch_cos']**2)
 pitch_cos = regr_dict['pitch_cos'] / np.sqrt(regr_dict['pitch_sin']**2 + regr_dict['pitch_cos']**2)
 regr_dict['pitch'] = np.arccos(pitch_cos)* np.sign(pitch_sin)
 return regr_dict
def preprocess_image(img):
img = img[img.shape[0] // 2:]
bg = np.ones_like(img)* img.mean(1, keepdims=True ).astype(img.dtype)
 bg = bg[:, :img.shape[1] // 4]
img = np.concatenate([bg, img, bg], 1)
 img = cv2.resize(img,(IMG_WIDTH, IMG_HEIGHT))
return(img / 255 ).astype('float32')
 def get_mask_and_regr(img, labels):
mask = np.zeros([IMG_HEIGHT // MODEL_SCALE, IMG_WIDTH // MODEL_SCALE], dtype='float32')
 regr_names = ['x', 'y', 'z', 'yaw', 'pitch', 'roll']
regr = np.zeros([IMG_HEIGHT // MODEL_SCALE, IMG_WIDTH // MODEL_SCALE, 7], dtype='float32')
 coords = str2coords(labels)
 xs, ys = get_img_coords(labels)
 for x, y, regr_dict in zip(xs, ys, coords):
x, y = y, x
x =(x - img.shape[0] // 2)* IMG_HEIGHT /(img.shape[0] // 2)/ MODEL_SCALE
x = np.round(x ).astype('int')
 y =(y + img.shape[1] // 4)* IMG_WIDTH /(img.shape[1] * 1.5)/ MODEL_SCALE
y = np.round(y ).astype('int')
 if x >= 0 and x < IMG_HEIGHT // MODEL_SCALE and y >= 0 and y < IMG_WIDTH // MODEL_SCALE:
mask[x, y] = 1
regr_dict = _regr_preprocess(regr_dict)
 regr[x, y] = [regr_dict[n] for n in sorted(regr_dict)]
return mask, regr
sub_df.to_csv("submission_test_1.csv", index=False )<data_type_conversions>
class CarDataset(Dataset):

 def __init__(self, dataframe, root_dir, training=True, transform=None):
self.df = dataframe
self.root_dir = root_dir
self.transform = transform
self.training = training
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
idx, labels = self.df.values[idx]
img_name = self.root_dir.format(idx)
 img0 = imread(img_name, True)
 img = preprocess_image(img0)
 img = np.rollaxis(img, 2, 0)
 if self.training:
mask, regr = get_mask_and_regr(img0, labels)
 regr = np.rollaxis(regr, 2, 0)
 else:
mask, regr = 0, 0
return [img, mask, regr]
sub_df = pd.melt(results_test_2[["Id", "age", "domain1_var1", "domain1_var2", "domain2_var1", "domain2_var2"]], id_vars=["Id"], value_name="Predicted")
 sub_df["Id"] = sub_df["Id"].astype("str")+ "_" + sub_df["variable"].astype("str")
 sub_df = sub_df.drop("variable", axis=1 ).sort_values("Id")
 sub_df<save_to_csv>
train_images_dir = PATH + 'train_images/{}.jpg'
test_images_dir = PATH + 'test_images/{}.jpg'
df_train=pd.read_pickle('.. /input/carpose/' + 'df_train.pkl')
 df_dev=pd.read_pickle('.. /input/carpose/' + 'df_dev.pkl')
 df_test=pd.read_pickle('.. /input/carpose/' + 'df_test.pkl')
 train_dataset = CarDataset(df_train, train_images_dir)
 dev_dataset = CarDataset(df_dev, train_images_dir)
 test_dataset = CarDataset(df_test, test_images_dir )
sub_df.to_csv("submission_test_2.csv", index=False )<load_from_csv>
BATCH_SIZE = 2
train_loader = DataLoader(dataset=train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2)
 dev_loader = DataLoader(dataset=dev_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=2)
 test_loader = DataLoader(dataset=test_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=2 )
r1 = pd.read_csv('submission_test_1.csv' ).set_index('Id')
 r2 = pd.read_csv('submission_test_2.csv' ).set_index('Id')
 p = pd.read_csv('.. /input/classifier9siteprobs/prob_8.csv' ).drop(columns = ['Unnamed: 0'] ).set_index('Id')
 results = []
for i in tqdm(r1.index):
prob = p.loc[int(i.split('_')[0])]['prob']
if prob>=0.7:
prob = 1
if prob<=0.3:
prob = 0
value = r1.loc[i]['Predicted']*(1-prob)+ r2.loc[i]['Predicted']*(prob)
 results.append({'Id':i, 'Predicted':value})
 r = pd.DataFrame(results)
 r.to_csv("submission.csv", index=False )<import_modules>
class double_conv(nn.Module):

 def __init__(self, in_ch, out_ch):
super(double_conv, self ).__init__()
self.conv = nn.Sequential(
 nn.Conv2d(in_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
nn.Conv2d(out_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True)
)
 def forward(self, x):
x = self.conv(x)
 return x
class up(nn.Module):
def __init__(self, in_ch, out_ch, bilinear=True):
super(up, self ).__init__()
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
 else:
self.up = nn.ConvTranspose2d(in_ch//2, in_ch//2, 2, stride=2)
 self.conv = double_conv(in_ch, out_ch)
 def forward(self, x1, x2=None):
x1 = self.up(x1)
 diffY = x2.size() [2] - x1.size() [2]
diffX = x2.size() [3] - x1.size() [3]
x1 = F.pad(x1,(diffX // 2, diffX - diffX//2,
diffY // 2, diffY - diffY//2))
if x2 is not None:
x = torch.cat([x2, x1], dim=1)
 else:
x = x1
x = self.conv(x)
 return x
def get_mesh(batch_size, shape_x, shape_y):
mg_x, mg_y = np.meshgrid(np.linspace(0, 1, shape_y), np.linspace(0, 1, shape_x))
mg_x = np.tile(mg_x[None, None, :, :], [batch_size, 1, 1, 1] ).astype('float32')
 mg_y = np.tile(mg_y[None, None, :, :], [batch_size, 1, 1, 1] ).astype('float32')
 mesh = torch.cat([torch.tensor(mg_x ).to(device), torch.tensor(mg_y ).to(device)], 1)
 return mesh
import numpy as np
import pandas as pd
from pathlib import Path
from matplotlib import pyplot as plt
import seaborn as sns
from sklearn.model_selection import KFold, StratifiedKFold
from fastai.tabular import *
from fastai import *
import os, shutil
import sys<set_options>
__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
'resnet152', 'resnext50_32x4d', 'resnext101_32x8d',
'wide_resnet50_2', 'wide_resnet101_2']
model_urls = {
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
'resnext50_32x4d': 'https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pth',
'resnext101_32x8d': 'https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pth',
'wide_resnet50_2': 'https://download.pytorch.org/models/wide_resnet50_2-95faca4d.pth',
'wide_resnet101_2': 'https://download.pytorch.org/models/wide_resnet101_2-32ee1156.pth',
}
def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):

 return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=dilation, groups=groups, bias=False, dilation=dilation)
 def conv1x1(in_planes, out_planes, stride=1):

 return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
 class BasicBlock(nn.Module):
expansion = 1
__constants__ = ['downsample']
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1, norm_layer=None):
super(BasicBlock, self ).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
if groups != 1 or base_width != 64:
raise ValueError('BasicBlock only supports groups=1 and base_width=64')
 if dilation > 1:
raise NotImplementedError("Dilation > 1 not supported in BasicBlock")
 self.conv1 = conv3x3(inplanes, planes, stride)
 self.bn1 = norm_layer(planes)
 self.relu = nn.ReLU(inplace=True)
 self.conv2 = conv3x3(planes, planes)
 self.bn2 = norm_layer(planes)
 self.downsample = downsample
self.stride = stride
def forward(self, x):
identity = x
out = self.conv1(x)
 out = self.bn1(out)
 out = self.relu(out)
 out = self.conv2(out)
 out = self.bn2(out)
 if self.downsample is not None:
identity = self.downsample(x)
 out += identity
out = self.relu(out)
 return out
class Bottleneck(nn.Module):
expansion = 4
__constants__ = ['downsample']
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1, norm_layer=None):
super(Bottleneck, self ).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
width = int(planes *(base_width / 64.))* groups
self.conv1 = conv1x1(inplanes, width)
 self.bn1 = norm_layer(width)
 self.conv2 = conv3x3(width, width, stride, groups, dilation)
 self.bn2 = norm_layer(width)
 self.conv3 = conv1x1(width, planes * self.expansion)
 self.bn3 = norm_layer(planes * self.expansion)
 self.relu = nn.ReLU(inplace=True)
 self.downsample = downsample
self.stride = stride
def forward(self, x):
identity = x
out = self.conv1(x)
 out = self.bn1(out)
 out = self.relu(out)
 out = self.conv2(out)
 out = self.bn2(out)
 out = self.relu(out)
 out = self.conv3(out)
 out = self.bn3(out)
 if self.downsample is not None:
identity = self.downsample(x)
 out += identity
out = self.relu(out)
 return out
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes=1000, zero_init_residual=False,
groups=1, width_per_group=64, replace_stride_with_dilation=None,
norm_layer=None):
super(ResNet, self ).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self._norm_layer = norm_layer
self.inplanes = 64
self.dilation = 1
if replace_stride_with_dilation is None:
replace_stride_with_dilation = [False, False, False]
if len(replace_stride_with_dilation)!= 3:
raise ValueError("replace_stride_with_dilation should be None "
"or a 3-element tuple, got {}".format(replace_stride_with_dilation))
self.groups = groups
self.base_width = width_per_group
self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3,
bias=False)
 self.bn1 = norm_layer(self.inplanes)
 self.relu = nn.ReLU(inplace=True)
 self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
 self.layer1 = self._make_layer(block, 64, layers[0])
 self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
dilate=replace_stride_with_dilation[0])
 self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
dilate=replace_stride_with_dilation[1])
 self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
dilate=replace_stride_with_dilation[2])
 self.avgpool = nn.AdaptiveAvgPool2d(( 1, 1))
self.fc = nn.Linear(512 * block.expansion, num_classes)
 for m in self.modules() :
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
 elif isinstance(m,(nn.BatchNorm2d, nn.GroupNorm)) :
nn.init.constant_(m.weight, 1)
 nn.init.constant_(m.bias, 0)
 if zero_init_residual:
for m in self.modules() :
if isinstance(m, Bottleneck):
nn.init.constant_(m.bn3.weight, 0)
 elif isinstance(m, BasicBlock):
nn.init.constant_(m.bn2.weight, 0)
 def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
norm_layer = self._norm_layer
downsample = None
previous_dilation = self.dilation
if dilate:
self.dilation *= stride
stride = 1
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
 conv1x1(self.inplanes, planes * block.expansion, stride),
norm_layer(planes * block.expansion), 
)
 layers = []
layers.append(block(self.inplanes, planes, stride, downsample, self.groups,
self.base_width, previous_dilation, norm_layer))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, groups=self.groups,
base_width=self.base_width, dilation=self.dilation,
norm_layer=norm_layer))
return nn.Sequential(*layers)

def forward(self, x):
conv1 = F.relu(self.bn1(self.conv1(x)) , inplace=True)
 conv1 = F.max_pool2d(conv1, 3, stride=2, padding=1)
 feats4 = self.layer1(conv1)
 feats8 = self.layer2(feats4)
 feats16 = self.layer3(feats8)
 feats32 = self.layer4(feats16)
 return feats8, feats16, feats32
def _resnet(arch, block, layers, pretrained, progress, **kwargs):
model = ResNet(block, layers, **kwargs)
 if pretrained:
state_dict = load_state_dict_from_url(model_urls[arch],
progress=progress)
 model.load_state_dict(state_dict)
 return model
def resnet18(pretrained=False, progress=True, **kwargs):
r
return _resnet('resnet18', BasicBlock, [2, 2, 2, 2], pretrained, progress,
**kwargs)
 def resnet34(pretrained=False, progress=True, **kwargs):
r
return _resnet('resnet34', BasicBlock, [3, 4, 6, 3], pretrained, progress,
**kwargs)
 def resnet50(pretrained=False, progress=True, **kwargs):
r
return _resnet('resnet50', Bottleneck, [3, 4, 6, 3], pretrained, progress,
**kwargs)
 def resnet101(pretrained=False, progress=True, **kwargs):
r
return _resnet('resnet101', Bottleneck, [3, 4, 23, 3], pretrained, progress,
**kwargs)
 def resnet152(pretrained=False, progress=True, **kwargs):
r
return _resnet('resnet152', Bottleneck, [3, 8, 36, 3], pretrained, progress,
**kwargs)
 def resnext50_32x4d(pretrained=False, progress=True, **kwargs):
r
kwargs['groups'] = 32
kwargs['width_per_group'] = 4
return _resnet('resnext50_32x4d', Bottleneck, [3, 4, 6, 3],
pretrained, progress, **kwargs)
 def resnext101_32x8d(pretrained=False, progress=True, **kwargs):
r
kwargs['groups'] = 32
kwargs['width_per_group'] = 8
return _resnet('resnext101_32x8d', Bottleneck, [3, 4, 23, 3],
pretrained, progress, **kwargs)
 def wide_resnet50_2(pretrained=False, progress=True, **kwargs):
r
kwargs['width_per_group'] = 64 * 2
return _resnet('wide_resnet50_2', Bottleneck, [3, 4, 6, 3],
pretrained, progress, **kwargs)
 def wide_resnet101_2(pretrained=False, progress=True, **kwargs):
r
kwargs['width_per_group'] = 64 * 2
return _resnet('wide_resnet101_2', Bottleneck, [3, 4, 23, 3],
pretrained, progress, **kwargs )
logging.getLogger().setLevel(logging.NOTSET)
<define_variables>
class CentResnet(nn.Module):

 def __init__(self, n_classes):
super(CentResnet, self ).__init__()
self.base_model = base_model
self.lat8 = nn.Conv2d(512, 256, 1)
 self.lat16 = nn.Conv2d(1024, 256, 1)
 self.lat32 = nn.Conv2d(2048, 256, 1)
 self.bn8 = nn.GroupNorm(16, 256)
 self.bn16 = nn.GroupNorm(16, 256)
 self.bn32 = nn.GroupNorm(16, 256)
 self.conv0 = double_conv(5, 64)
 self.conv1 = double_conv(64, 128)
 self.conv2 = double_conv(128, 512)
 self.conv3 = double_conv(512, 1024)
 self.mp = nn.MaxPool2d(2)
 self.up1 = up(1282 , 512)
 self.up2 = up(512 + 512, 256)
 self.outc = nn.Conv2d(256, n_classes, 1)
 def forward(self, x):
batch_size = x.shape[0]
mesh1 = get_mesh(batch_size, x.shape[2], x.shape[3])
 x0 = torch.cat([x, mesh1], 1)
 x1 = self.mp(self.conv0(x0))
x2 = self.mp(self.conv1(x1))
x3 = self.mp(self.conv2(x2))
x4 = self.mp(self.conv3(x3))
feats8, feats16, feats32 = self.base_model(x)
 lat8 = F.relu(self.bn8(self.lat8(feats8)))
 lat16 = F.relu(self.bn16(self.lat16(feats16)))
 lat32 = F.relu(self.bn32(self.lat32(feats32)))
 mesh2 = get_mesh(batch_size, lat32.shape[2], lat32.shape[3])
 feats = torch.cat([lat32, mesh2], 1)
 x = self.up1(feats, x4)
 x = self.up2(x, x3)
 x = self.outc(x)
 return x
kaggle_path = Path('.')
 kaggle_input_path = Path('/kaggle/input/trends-assessment-prediction')
 cluster_input_path = Path('/kaggle/input/trends-cluster-sfnc-groups')
 av_input_path = Path('/kaggle/input/trends-av-probs-of-test-and-s2')
 img_53000_path = Path('/kaggle/input/fork-of-trends-image-features-53-100')
<define_variables>
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 print(device)
 n_epochs = 10
model = CentResnet(8 ).to(device)
 optimizer = optim.AdamW(model.parameters() , lr=0.001)
 exp_lr_scheduler = optim.lr_scheduler.CyclicLR(optimizer,base_lr=0.001,cycle_momentum=False,max_lr=0.0013,step_size_up=2000 )
INCLUDE_FNC_DATA = True
INCLUDE_FNC_CLUSTERS = False
INCLUDE_FNC_DIST_CCENTER = False
INCLUDE_FNC_CLUSTERS_2 = True
INCLUDE_FNC_DIST_CCENTER_2 = True
INCLUDE_IMG_53000 = False
INCLUDE_AV_DATA = False
IMPUTATION_STRAT = 'IGNORE_ON_TRAIN'
LOSS_WEIGHTS = [0.3, 0.175, 0.175, 0.175, 0.175]
BS = 128
DEP_VAR = ['age','domain1_var1','domain1_var2', 'domain2_var1', 'domain2_var2']<load_from_csv>
def criterion(prediction, mask, regr,weight=0.4, size_average=True):
pred_mask = torch.sigmoid(prediction[:, 0])
 mask_loss = mask * torch.log(pred_mask + 1e-12)+(1 - mask)* torch.log(1 - pred_mask + 1e-12)
 mask_loss = -mask_loss.mean(0 ).sum()
pred_regr = prediction[:, 1:]
regr_loss =(torch.abs(pred_regr - regr ).sum(1)* mask ).sum(1 ).sum(1)/ mask.sum(1 ).sum(1)
 regr_loss = regr_loss.mean(0)
 loss = mask_loss +regr_loss
if not size_average:
loss *= prediction.shape[0]
return loss ,mask_loss , regr_loss
l_data = pd.read_csv(kaggle_input_path/'loading.csv' ).drop('IC_20',axis=1)
 if INCLUDE_FNC_DATA:
f_data = pd.read_csv(kaggle_input_path/'fnc.csv')
 l_data = l_data.merge(f_data, on='Id', how = 'inner')
 if INCLUDE_FNC_CLUSTERS:
c_data = pd.read_csv(cluster_input_path/'sfnc_group_clusters.csv')
 l_data = l_data.merge(c_data, on='Id', how = 'inner')
 if INCLUDE_FNC_DIST_CCENTER:
cc_data = pd.read_csv(cluster_input_path/'sfnc_dist_to_cluster_center.csv')
 l_data = l_data.merge(cc_data, on='Id', how = 'inner')
 if INCLUDE_FNC_CLUSTERS_2:
c2_data = pd.read_csv(cluster_input_path/'sfnc_group_clusters_2c.csv')
 temp_col = []
for c in c2_data.columns:
if c != 'Id':
temp_col += [c+'_2']
else:
temp_col += [c]
c2_data.columns = temp_col
l_data = l_data.merge(c2_data, on='Id', how = 'inner')
 if INCLUDE_FNC_DIST_CCENTER_2:
cc2_data = pd.read_csv(cluster_input_path/'sfnc_dist_to_cluster_center_2c.csv')
 temp_col = []
for c in cc2_data.columns:
if c != 'Id':
temp_col += [c+'_2']
else:
temp_col += [c]
cc2_data.columns = temp_col
l_data = l_data.merge(cc2_data, on='Id', how = 'inner')
 if INCLUDE_IMG_53000:
i_data = pd.read_csv(img_53000_path/'train_features.csv')
 i_data = i_data.append(pd.read_csv(img_53000_path/'test_features.csv'))
l_data = l_data.merge(i_data, on='Id', how = 'inner')
 if INCLUDE_AV_DATA:
av_data = pd.read_csv(av_input_path/'test_s2_probs.csv')
 l_data = l_data.merge(av_data, on='Id', how = 'inner')
 y_data = pd.read_csv(kaggle_input_path/'train_scores.csv')
 idx_site2 = pd.read_csv(kaggle_input_path/'reveal_ID_site2.csv')
<data_type_conversions>
def train(epoch, history=None):
model.train()
t = tqdm(train_loader)
 for batch_idx,(img_batch, mask_batch, regr_batch)in enumerate(t):
img_batch = img_batch.to(device)
 mask_batch = mask_batch.to(device)
 regr_batch = regr_batch.to(device)
 optimizer.zero_grad()
output = model(img_batch)
 if epoch < SWITCH_LOSS_EPOCH :
loss,mask_loss, regr_loss = criterion(output, mask_batch, regr_batch,1)
 else:
loss,mask_loss, regr_loss = criterion(output, mask_batch, regr_batch,0.5)
 t.set_description(f'train_loss(l={loss:.3f} )(m={mask_loss:.2f} )(r={regr_loss:.4f}')
 if history is not None:
history.loc[epoch + batch_idx / len(train_loader), 'train_loss'] = loss.data.cpu().numpy()
loss.backward()
optimizer.step()
exp_lr_scheduler.step()
print('Train Epoch: {} \tLR: {:.6f}\tLoss: {:.6f}\tMaskLoss: {:.6f}\tRegLoss: {:.6f}'.format(
 epoch,
optimizer.state_dict() ['param_groups'][0]['lr'],
loss.data,
mask_loss.data,
regr_loss.data))
def evaluate(epoch, history=None):
model.eval()
loss = 0
valid_loss = 0
valid_mask_loss = 0
valid_regr_loss = 0
with torch.no_grad() :
for img_batch, mask_batch, regr_batch in dev_loader:
img_batch = img_batch.to(device)
 mask_batch = mask_batch.to(device)
 regr_batch = regr_batch.to(device)
 output = model(img_batch)
 if epoch < SWITCH_LOSS_EPOCH :
loss,mask_loss, regr_loss= criterion(output, mask_batch, regr_batch,1, size_average=False)
 valid_loss += loss.data
valid_mask_loss += mask_loss.data
valid_regr_loss += regr_loss.data
else :
loss,mask_loss, regr_loss = criterion(output, mask_batch, regr_batch,0.5, size_average=False)
 valid_loss += loss.data
valid_mask_loss += mask_loss.data
valid_regr_loss += regr_loss.data
valid_loss /= len(dev_loader.dataset)
 valid_mask_loss /= len(dev_loader.dataset)
 valid_regr_loss /= len(dev_loader.dataset)
 if history is not None:
history.loc[epoch, 'dev_loss'] = valid_loss.cpu().numpy()
history.loc[epoch, 'mask_loss'] = valid_mask_loss.cpu().numpy()
history.loc[epoch, 'regr_loss'] = valid_regr_loss.cpu().numpy()
print('Dev loss: {:.4f}'.format(valid_loss))
if IMPUTATION_STRAT == 'IGNORE_ON_TRAIN':
y_data = y_data.fillna(0)
 else:
y_data = y_data.fillna(y_data.mean())
 y_data<merge>
torch.save(model.state_dict() , './resnext50.pth' )
train_df = l_data.merge(y_data, on='Id', how='inner' ).sort_values(by='Id' ).reset_index(drop = True)
 idx_train = train_df.pop('Id')
 train_df<merge>
act = torch.nn.Sigmoid()
logtens = torch.from_numpy(logits)
 probs = act(logtens)
 probs = probs[probs>0]
print(probs )
test = l_data.merge(y_data, on='Id', how='outer', indicator = True)
 test = test[test['_merge'] == 'left_only'].drop(['age',
'domain1_var1',
'domain1_var2',
'domain2_var1',
'domain2_var2',
'_merge'], axis = 1 ).sort_values(by='Id' ).reset_index(drop = True)
 idx_test = test.pop('Id')
 test<feature_engineering>
DISTANCE_THRESH_CLEAR = 2
def convert_3d_to_2d(x, y, z, fx = 2304.5479, fy = 2305.8757, cx = 1686.2379, cy = 1354.9849):
return x * fx / z + cx, y * fy / z + cy
def optimize_xy(r, c, x0, y0, z0):
def distance_fn(xyz):
x, y, z = xyz
x, y = convert_3d_to_2d(x, y, z0)
 y, x = x, y
x =(x - IMG_SHAPE[0] // 2)* IMG_HEIGHT /(IMG_SHAPE[0] // 2)/ MODEL_SCALE
x = np.round(x ).astype('int')
 y =(y + IMG_SHAPE[1] // 4)* IMG_WIDTH /(IMG_SHAPE[1] * 1.5)/ MODEL_SCALE
y = np.round(y ).astype('int')
 return(x-r)**2 +(y-c)**2
res = minimize(distance_fn, [x0, y0, z0], method='Powell')
 x_new, y_new, z_new = res.x
return x_new, y_new, z0
def clear_duplicates(coords):
for c1 in coords:
xyz1 = np.array([c1['x'], c1['y'], c1['z']])
 for c2 in coords:
xyz2 = np.array([c2['x'], c2['y'], c2['z']])
 distance = np.sqrt(((xyz1 - xyz2)**2 ).sum())
 if distance < DISTANCE_THRESH_CLEAR:
if c1['confidence'] < c2['confidence']:
c1['confidence'] = -1
return [c for c in coords if c['confidence'] > 0]
def extract_coords(prediction):
logits = prediction[0]
regr_output = prediction[1:]
points = np.argwhere(logits > -0.5)
 col_names = sorted(['x', 'y', 'z', 'yaw', 'pitch_sin', 'pitch_cos', 'roll'])
 coords = []
for r, c in points:
regr_dict = dict(zip(col_names, regr_output[:, r, c]))
coords.append(_regr_back(regr_dict))
coords[-1]['confidence'] = 1 /(1 + np.exp(-logits[r, c]))
coords[-1]['x'], coords[-1]['y'], coords[-1]['z'] = optimize_xy(r, c, coords[-1]['x'], coords[-1]['y'], coords[-1]['z'])
 coords = clear_duplicates(coords)
 return coords
def coords2str(coords, names=['yaw', 'pitch', 'roll', 'x', 'y', 'z', 'confidence']):
s = []
for c in coords:
for n in names:
s.append(str(c.get(n, 0)))
 return ' '.join(s )
for d in DEP_VAR:
y_data['bin_'+d] = pd.qcut(y_data[d].rank(method='first'), q=4, labels=False)
 y_data['bin_all'] =(y_data['bin_age']+
y_data['bin_domain1_var1']*10+
y_data['bin_domain1_var2']*100+
y_data['bin_domain2_var1']*1000+
y_data['bin_domain2_var2']*10000)
 y_data['bin_age_10'] = pd.qcut(y_data['age'].rank(method='first'), q=10, labels=False)
 y_data['bin_age_7'] = pd.qcut(y_data['age'].rank(method='first'), q=7, labels=False)
 if INCLUDE_AV_DATA:
y_data = y_data.merge(av_data[['Id','is_test_prob']])
 y_data['bin_test_7'] = pd.qcut(y_data['is_test_prob'].rank(method='first'), q=7, labels=False)
 y_data<compute_train_metric>
val_preds = []
dev_loader = DataLoader(dataset=dev_dataset, batch_size=2, shuffle=False, num_workers=2)
 model.eval()
for img, _, _ in tqdm(dev_loader):
with torch.no_grad() :
output = model(img.to(device))
output = output.data.cpu().numpy()
for out in output:
coords = extract_coords(out)
 s = coords2str(coords)
 val_preds.append(s )
def norm_absolute_error(preds, targs):
"Normalized absolute error between `pred` and `targ`."
sg=targs.sign()
y=targs*sg
pred, targ = flatten_check(preds*sg, y)
 return torch.abs(targ - pred ).sum() / targ.sum()
def weighted_nae(preds, targs, details = False):
if IMPUTATION_STRAT == 'IGNORE_ON_TRAIN':
sg = targs.float().sign()
x0 = preds[:,0].float() *sg[:,0]
x1 = preds[:,1].float() *sg[:,1]
x2 = preds[:,2].float() *sg[:,2]
x3 = preds[:,3].float() *sg[:,3]
x4 = preds[:,4].float() *sg[:,4]
else:
sg = 1
x0 = preds[:,0].float()
x1 = preds[:,1].float()
x2 = preds[:,2].float()
x3 = preds[:,3].float()
x4 = preds[:,4].float()
y = targs.float() *sg
return norm_absolute_error(x0,y[:,0]), \
norm_absolute_error(x1,y[:,1]), \
norm_absolute_error(x2,y[:,2]), \
norm_absolute_error(x3,y[:,3]), \
norm_absolute_error(x4,y[:,4]), \
0.3 * norm_absolute_error(x0,y[:,0])+ \
0.175 * norm_absolute_error(x1,y[:,1])+ \
0.175 * norm_absolute_error(x2,y[:,2])+ \
0.175 * norm_absolute_error(x3,y[:,3])+ \
0.175 * norm_absolute_error(x4,y[:,4])
 def plot_diff(y, y_truth):
y_df = pd.DataFrame(y.numpy())
 y_df.columns = DEP_VAR
y_df = y_df.melt()
y_df['Id'] = y_df.index
y_truth_df = pd.DataFrame(y_truth.numpy())
 y_truth_df.columns = DEP_VAR
y_truth_df = y_truth_df.melt()
y_truth_df['Id'] = y_truth_df.index
plot_df = y_truth_df.merge(y_df, on=['variable', 'Id'], how='inner' ).drop('Id', axis = 1)
 plot_df.columns = ['Category', 'Target', 'Prediction']
g = sns.relplot(x="Target", y="Prediction",
col="Category", hue="Category", style="Category",
kind="scatter", data=plot_df)
<categorify>
df_dev['PredictionString'] = val_preds
df_dev.head()
df_dev.to_csv('val_predictions.csv', index=False )
class Metric_idx(Callback):
def __init__(self, idx):
super().__init__()
self.idx = idx
def on_epoch_begin(self, **kwargs):
self.targs, self.preds = Tensor([]), Tensor([])
 def on_batch_end(self, last_output:Tensor, last_target:Tensor, **kwargs):
last_output = last_output[self.idx]
last_target = last_target[self.idx]
self.preds = torch.cat(( self.preds, last_output.float().cpu()))
self.targs = torch.cat(( self.targs, last_target.float().cpu()))
def _norm_absolute_error(self):
return norm_absolute_error(self.preds, self.targs)
 def on_epoch_end(self, last_metrics, **kwargs):
return add_metrics(last_metrics, self._norm_absolute_error())
 Metric_age = partial(Metric_idx,0)
 Metric_domain1_var1 = partial(Metric_idx,1)
 Metric_domain1_var2 = partial(Metric_idx,2)
 Metric_domain2_var1 = partial(Metric_idx,3)
 Metric_domain2_var2 = partial(Metric_idx,4)
 class Metric_total(Callback):
def __init__(self):
super().__init__()
self.age = Metric_idx(0)
 self.domain1_var1 = Metric_idx(1)
 self.domain1_var2 = Metric_idx(2)
 self.domain2_var1 = Metric_idx(3)
 self.domain2_var2 = Metric_idx(4)
 def on_epoch_begin(self, **kwargs):
self.age.on_epoch_begin(**kwargs)
 self.domain1_var1.on_epoch_begin(**kwargs)
 self.domain1_var2.on_epoch_begin(**kwargs)
 self.domain2_var1.on_epoch_begin(**kwargs)
 self.domain2_var2.on_epoch_begin(**kwargs)
 def on_batch_end(self, last_output:Tensor, last_target:Tensor, **kwargs):
self.age.on_batch_end(last_output, last_target, **kwargs)
 self.domain1_var1.on_batch_end(last_output, last_target, **kwargs)
 self.domain1_var2.on_batch_end(last_output, last_target, **kwargs)
 self.domain2_var1.on_batch_end(last_output, last_target, **kwargs)
 self.domain2_var2.on_batch_end(last_output, last_target, **kwargs)
 def on_epoch_end(self, last_metrics, **kwargs):
return add_metrics(last_metrics,
0.3 * self.age._norm_absolute_error() +
0.175*self.domain1_var1._norm_absolute_error() +
0.175*self.domain1_var2._norm_absolute_error() +
0.175*self.domain2_var1._norm_absolute_error() +
0.175*self.domain2_var2._norm_absolute_error()
)<init_hyperparams>
predictions = []
test_loader = DataLoader(dataset=test_dataset, batch_size=2, shuffle=False, num_workers=2)
 model.eval()
for img, _, _ in tqdm(test_loader):
with torch.no_grad() :
output = model(img.to(device))
output = output.data.cpu().numpy()
for out in output:
coords = extract_coords(out)
 s = coords2str(coords)
 predictions.append(s )
class Loss_combine(nn.Module):
def __init__(self, loss_weights = [0.4,0.15,0.15,0.15,0.15], loss_base = 'MSE'):
super().__init__()
self.loss_base = loss_base
self.loss_weights = loss_weights
self.fw = Tensor(LOSS_WEIGHTS ).cuda()
def forward(self, input, target,reduction='mean'):
x0,x1,x2,x3,x4 = input.T
if IMPUTATION_STRAT == 'IGNORE_ON_TRAIN':
sg = target.float().sign()
x0,x1,x2,x3,x4 = x0.float() *sg[:,0],x1.float() *sg[:,1],x2.float() *sg[:,2],x3.float() *sg[:,3],x4.float() *sg[:,4]
else:
sg = 1
x0,x1,x2,x3,x4 = x0.float() ,x1.float() ,x2.float() ,x3.float() ,x4.float()
y = target.float() *sg
loss1 = 0
loss2 = 0
if self.loss_base in('MSE','MIX'):
loss_func = F.mse_loss
loss1 =(self.fw*(F.mse_loss(input*sg,y,reduction='none' ).sum(dim=0)) /(( y**2.5 ).sum(dim=0)))*3
loss1 = loss1.sum()
if self.loss_base in('L1','MIX'):
loss_func = F.l1_loss
reduction = 'sum'
loss2 = self.fw[0]*loss_func(x0,y[:,0],reduction=reduction)/sum(y[:,0])+ \
self.fw[1]*loss_func(x1,y[:,1],reduction=reduction)/sum(y[:,1])+ \
self.fw[2]*loss_func(x2,y[:,2],reduction=reduction)/sum(y[:,2])+ \
self.fw[3]*loss_func(x3,y[:,3],reduction=reduction)/sum(y[:,3])+ \
self.fw[4]*loss_func(x4,y[:,4],reduction=reduction)/sum(y[:,4])
 loss2 = loss2/4
return loss1 + loss2 
<choose_model_class>
test = pd.read_csv(PATH + 'sample_submission.csv')
 test['PredictionString'] = predictions
test.to_csv('predictions.csv', index=False)
 test.head()
class TailBlock(nn.Module):
def __init__(self, bs):
super(TailBlock, self ).__init__()
self.bs = bs
self.lstm = nn.GRU(input_size = 53, hidden_size = 20, num_layers = 64, bidirectional=False, dropout = 0.3)
 self.linear = nn.Linear(40, 8)
 self.hidden_cell = torch.zeros(128,bs, 20 ).cuda()
def forward(self,x_cont):
print(x_cont.size())
 x_non_lstm_feat = x_cont[:,:-5300]
x53 = x_cont[:,-5300:].view(-1, 100, 53)
 print('x53',x53.shape)
 lstm_out, self.hidden_cell = self.lstm(x53.view(100, self.bs, -1), self.hidden_cell)
 print('lout',lstm_out.shape)
 x_lstm_out = self.linear(torch.cat([x_non_lstm_feat.view(1,-1), x_lstm_out.view(1,-1)], dim=1))
return x_lstm_out<choose_model_class>
def expand_df(df, PredictionStringCols):
df = df.dropna().copy()
df['NumCars'] = [int(( x.count(' ')+1)/7)for x in df['PredictionString']]
image_id_expanded = [item for item, count in zip(df['ImageId'], df['NumCars'])for i in range(count)]
prediction_strings_expanded = df['PredictionString'].str.split(' ',expand = True ).values.reshape(-1,7 ).astype(float)
 prediction_strings_expanded = prediction_strings_expanded[~np.isnan(prediction_strings_expanded ).all(axis=1)]
df = pd.DataFrame(
 {
'ImageId': image_id_expanded,
PredictionStringCols[0]:prediction_strings_expanded[:,0],
PredictionStringCols[1]:prediction_strings_expanded[:,1],
PredictionStringCols[2]:prediction_strings_expanded[:,2],
PredictionStringCols[3]:prediction_strings_expanded[:,3],
PredictionStringCols[4]:prediction_strings_expanded[:,4],
PredictionStringCols[5]:prediction_strings_expanded[:,5],
PredictionStringCols[6]:prediction_strings_expanded[:,6]
})
 return df
def str2coords(s, names):
coords = []
for l in np.array(s.split() ).reshape([-1, 7]):
coords.append(dict(zip(names, l.astype('float'))))
return coords
def TranslationDistance(p,g, abs_dist = False):
dx = p['x'] - g['x']
dy = p['y'] - g['y']
dz = p['z'] - g['z']
diff0 =(g['x']**2 + g['y']**2 + g['z']**2)**0.5
diff1 =(dx**2 + dy**2 + dz**2)**0.5
if abs_dist:
diff = diff1
else:
diff = diff1/diff0
return diff
def RotationDistance(p, g):
true=[ g['pitch'] ,g['yaw'] ,g['roll'] ]
pred=[ p['pitch'] ,p['yaw'] ,p['roll'] ]
q1 = R.from_euler('xyz', true)
 q2 = R.from_euler('xyz', pred)
 diff = R.inv(q2)* q1
W = np.clip(diff.as_quat() [-1], -1., 1.)
W =(acos(W)*360)/pi
if W > 180:
W = 360 - W
return W
class NeckBlock(nn.Module):
def __init__(self, nf, ps=0.3):
super(NeckBlock,self ).__init__()
self.sa = SelfAttention(256)
 self.bn1 = nn.BatchNorm1d(nf)
 self.d1 = nn.Dropout(ps)
 self.l1 = nn.Linear(nf, 1)
 self.act1 = nn.LeakyReLU(0.1, inplace=True)
 self.bn2 = nn.BatchNorm1d(nf)
 self.d2 = nn.Dropout(ps)
 self.l2 = nn.Linear(nf, 4)
 self.act2 = nn.LeakyReLU(0.1, inplace=True)
 def forward(self, x):
ident = x
x0 = self.sa(x)
 x1 = self.bn1(x0)
 x1 = self.d1(x1)
 x1 = self.l1(x1)
 x1 = self.act1(x1+ident)
 x2 = self.bn2(x0)
 x2 = self.d2(x2)
 x2 = self.l2(x2)
 x2 = self.act2(x2+ident)
 return x1, xn 
<define_variables>
thres_tr_list = [0.1, 0.09, 0.08, 0.07, 0.06, 0.05, 0.04, 0.03, 0.02, 0.01]
thres_ro_list = [50, 45, 40, 35, 30, 25, 20, 15, 10, 5]
def check_match(idx):
keep_gt=False
thre_tr_dist = thres_tr_list[idx]
thre_ro_dist = thres_ro_list[idx]
train_dict = {imgID:str2coords(s, names=['carid_or_score', 'pitch', 'yaw', 'roll', 'x', 'y', 'z'])for imgID,s in zip(train_df['ImageId'],train_df['PredictionString'])}
valid_dict = {imgID:str2coords(s, names=['pitch', 'yaw', 'roll', 'x', 'y', 'z', 'carid_or_score'])for imgID,s in zip(valid_df['ImageId'],valid_df['PredictionString'])}
result_flg = []
scores = []
MAX_VAL = 10**10
for img_id in valid_dict:
for pcar in sorted(valid_dict[img_id], key=lambda x: -x['carid_or_score']):
min_tr_dist = MAX_VAL
min_idx = -1
for idx, gcar in enumerate(train_dict[img_id]):
tr_dist = TranslationDistance(pcar,gcar)
 if tr_dist < min_tr_dist:
min_tr_dist = tr_dist
min_ro_dist = RotationDistance(pcar,gcar)
 min_idx = idx
if min_tr_dist < thre_tr_dist and min_ro_dist < thre_ro_dist:
if not keep_gt:
train_dict[img_id].pop(min_idx)
 result_flg.append(1)
 else:
result_flg.append(0)
 scores.append(pcar['carid_or_score'])
 return result_flg, scores
<load_pretrained><EOS> 
valid_df = pd.read_csv('val_predictions.csv')
 expanded_valid_df = expand_df(valid_df, ['pitch','yaw','roll','x','y','z','Score'])
 valid_df = valid_df.fillna('')
 train_df = pd.read_csv('.. /input/pku-autonomous-driving/train.csv')
 train_df = train_df[train_df.ImageId.isin(valid_df.ImageId.unique())]
expanded_train_df = expand_df(train_df, ['model_type','pitch','yaw','roll','x','y','z'])
 max_workers = 10
n_gt = len(expanded_train_df)
 ap_list = []
p = Pool(processes=max_workers)
 for result_flg, scores in p.imap(check_match, range(10)) :
if np.sum(result_flg)> 0:
n_tp = np.sum(result_flg)
 recall = n_tp/n_gt
ap = average_precision_score(result_flg, scores)*recall
else:
ap = 0
ap_list.append(ap)
 map = np.mean(ap_list)
 print('map:', map )