Kaggle competition: Kobe Bryant Shot Selection
<SOS> metric: logloss Kaggle data source: kobe-bryant-shot-selection<set_options>
pd.options.display.max_rows = 500
pd.options.display.max_columns = 50
warnings.filterwarnings('ignore')
 %matplotlib inline
speed_test = True<predict_on_test>
df = pd.read_csv('.. /input/data.csv')
 sub = pd.read_csv('.. /input/sample_submission.csv' )
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )<predict_on_test>
df_test = df.loc[df.shot_made_flag.isnull() , :]
df_test.index = range(len(df_test))
df.dropna(inplace=True)
 df.index = range(len(df))
predictions = predict_on_video_set(test_videos, num_workers=4 )<save_to_csv>
c= df.loc[(df.loc_x==0)&(df.loc_y==0)]
c.shot_made_flag.value_counts()
submission_df_resnext = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_resnext.to_csv("submission_resnext.csv", index=False )<install_modules>
def color_generator(num_colors):
colors = []
for i in range(num_colors):
colors.append(( np.random.rand() , np.random.rand() , np.random.rand()))
return colors
!pip install.. /input/deepfake-xception-trained-model/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet<define_variables>
colors = color_generator(100 )
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 len(test_videos )<set_options>
df = pd.read_csv('.. /input/data.csv' )
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )<load_pretrained>
df['angle'] = df.apply(lambda row: 90 if row['loc_y']==0 else m.degrees(m.atan(row['loc_x']/abs(row['loc_y']))),axis=1 )
facedet = BlazeFace().to(gpu)
 facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
 facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
 _ = facedet.train(False )<load_pretrained>
df['angle_bin'] = pd.cut(df.angle, 7, labels=range(7))
df['angle_bin'] = df.angle_bin.astype(int )
frames_per_video = 83
video_reader = VideoReader()
video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
 face_extractor = FaceExtractor(video_read_fn, facedet )<define_variables>
df['matchup_code'] = df.matchup.apply(lambda x: 0 if(x.split(' ')[1])=='@' else 1 )
input_size = 150<normalization>
df['action_type'] = df.action_type.apply(lambda x: x.replace('-', ''))
df['action_type'] = df.action_type.apply(lambda x: x.replace('Follow Up', 'followup'))
df['action_type'] = df.action_type.apply(lambda x: x.replace('Finger Roll','fingerroll'))
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )<choose_model_class>
cv = CountVectorizer(max_features=50, stop_words=['shot'] )
model = get_model("xception", pretrained=False)
 model = nn.Sequential(*list(model.children())[:-1])
 class Pooling(nn.Module):
def __init__(self):
super(Pooling, self ).__init__()
self.p1 = nn.AdaptiveAvgPool2d(( 1,1))
self.p2 = nn.AdaptiveMaxPool2d(( 1,1))
def forward(self, x):
x1 = self.p1(x)
 x2 = self.p2(x)
 return(x1+x2)* 0.5
model[0].final_block.pool = nn.Sequential(nn.AdaptiveAvgPool2d(( 1,1)))
 class Head(torch.nn.Module):
def __init__(self, in_f, out_f):
super(Head, self ).__init__()
self.f = nn.Flatten()
self.l = nn.Linear(in_f, 512)
 self.d = nn.Dropout(0.5)
 self.o = nn.Linear(512, out_f)
 self.b1 = nn.BatchNorm1d(in_f)
 self.b2 = nn.BatchNorm1d(512)
 self.r = nn.ReLU()
def forward(self, x):
x = self.f(x)
 x = self.b1(x)
 x = self.d(x)
 x = self.l(x)
 x = self.r(x)
 x = self.b2(x)
 x = self.d(x)
 out = self.o(x)
 return out
class FCN(torch.nn.Module):
def __init__(self, base, in_f):
super(FCN, self ).__init__()
self.base = base
self.h1 = Head(in_f, 1)
 def forward(self, x):
x = self.base(x)
 return self.h1(x)
 net = []
model = FCN(model, 2048)
 model = model.cuda()
model.load_state_dict(torch.load('.. /input/deepfake-xception-trained-model/model.pth'))
net.append(model )<predict_on_test>
shot_features = cv.fit_transform(df['action_type'] ).toarray()
shot_features = pd.DataFrame(shot_features, columns=cv.get_feature_names() )
def predict_on_video(video_path, batch_size):
try:
faces = face_extractor.process_video(video_path)
 face_extractor.keep_only_best_face(faces)
 if len(faces)> 0:
x = np.zeros(( batch_size, input_size, input_size, 3), dtype=np.uint8)
 n = 0
for frame_data in faces:
for face in frame_data["faces"]:
resized_face = isotropically_resize_image(face, input_size)
 resized_face = make_square_image(resized_face)
 if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
if n > 0:
x = torch.tensor(x, device=gpu ).float()
x = x.permute(( 0, 3, 1, 2))
for i in range(len(x)) :
x[i] = normalize_transform(x[i] / 255.)
with torch.no_grad() :
y_pred = model(x)
 y_pred = torch.sigmoid(y_pred.squeeze())
 return np.percentile(y_pred[:n].to('cpu'), 51, interpolation="nearest")
 except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.481<predict_on_test>
df = pd.concat([df,shot_features], axis=1 )
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )<define_variables>
df['game_date'] = pd.to_datetime(df.game_date )
speed_test = True<predict_on_test>
df['game_date_month'] = df.game_date.dt.month
df['game_date_quarter'] = df.game_date.dt.quarter
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )<predict_on_test>
df['time_remaining'] = df.apply(lambda row: row['minutes_remaining']*60+row['seconds_remaining'], axis=1 )
%%time
model.eval()
predictions = predict_on_video_set(test_videos, num_workers=4 )<save_to_csv>
df['timeUnder4'] = df.time_remaining.apply(lambda x: 1 if x<4 else 0 )
submission_df_xception = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df_xception.to_csv("submission_xception.csv", index=False )<create_dataframe>
df['distance_bin'] = pd.cut(df.shot_distance, bins=10, labels=range(10))
submission_df = pd.DataFrame({"filename": test_videos} )<feature_engineering>
ang_dist = df.groupby(['angle_bin', 'distance_bin'])['shot_made_flag'].agg([np.mean],as_index= False ).reset_index()
r1 = 0.32
r2 = 0.68
total = r1 + r2
r11 = r1/total
r22 = r2/total<feature_engineering>
ang_dist['group'] = range(len(ang_dist))
ang_dist.drop('mean', inplace=True, axis=1 )
submission_df["label"] = r22*submission_df_resnext["label"] + r11*submission_df_xception["label"]<save_to_csv>
df = df.merge(ang_dist, 'left', ['angle_bin', 'distance_bin'] )
submission_df.to_csv("submission.csv", index=False )<set_options>
predictors = df.columns.drop(['game_event_id'
, 'shot_made_flag'
, 'game_id'
, 'shot_id'
, 'game_date'
, 'minutes_remaining'
, 'seconds_remaining'
,'lat', 'lon'
, 'playoffs'
, 'team_id', 'team_name'
, 'matchup'
] )
%matplotlib inline 
<define_variables>
predictors = ['action_type', 'combined_shot_type', 'shot_distance',
'shot_zone_basic', 'opponent', 'matchup_code',
'bank', 'driving', 'dunk', 'fadeaway', 'jump', 'pullup', 'running',
'slam', 'turnaround','timeUnder4', 'angle_bin','loc_x', 'loc_y','period', 'season']
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 len(test_videos )<import_modules>
le = LabelEncoder()
for col in predictors:
if df[col].dtype=='object':
df[col] = le.fit_transform(df[col] )
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )<set_options>
df_test = df.loc[df.shot_made_flag.isnull() , :]
df_test.index = range(len(df_test))
df.dropna(inplace=True)
 df.index = range(len(df))
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu<load_pretrained>
xgb = xgboost.XGBClassifier(seed=1, learning_rate=0.01, n_estimators=500,silent=False, max_depth=7, subsample=0.6, colsample_bytree=0.6 )
facedet = BlazeFace().to(gpu)
 facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
 facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
 _ = facedet.train(False )<load_pretrained>
kf = KFold(n=len(df), shuffle=True,n_folds=5, random_state=50 )
frames_per_video = 17
video_reader = VideoReader()
video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
 face_extractor = FaceExtractor(video_read_fn, facedet )<define_variables>
def run_test(predictors):
all_score = []
for train_index, test_index in kf:
xgb.fit(df.loc[train_index, predictors], df.loc[train_index, 'shot_made_flag'])
 score = metrics.log_loss(df.loc[test_index, 'shot_made_flag'], xgb.predict_proba(df.loc[test_index, predictors])[:,1])
 all_score.append(score)
 print(score)
 print('Mean score =', np.array(all_score ).mean() )
input_size = 224<normalization>
run_test(predictors )
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )<choose_model_class>
xgb.fit(df[predictors], df['shot_made_flag'] )
class MyResNeXt(models.resnet.ResNet):
def __init__(self, training=True):
super(MyResNeXt, self ).__init__(block=models.resnet.Bottleneck,
layers=[3, 4, 6, 3],
groups=32,
width_per_group=4)
 self.fc = nn.Linear(2048, 1 )<load_pretrained>
xgb.predict_proba(df_test[predictors])[:,1]
checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
 model = MyResNeXt().to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint<predict_on_test>
preds = xgb.predict_proba(df_test[predictors] )
def predict_on_video(video_path, batch_size):
try:
faces = face_extractor.process_video(video_path)
 face_extractor.keep_only_best_face(faces)
 if len(faces)> 0:
x = np.zeros(( batch_size, input_size, input_size, 3), dtype=np.uint8)
 n = 0
for frame_data in faces:
for face in frame_data["faces"]:
resized_face = isotropically_resize_image(face, input_size)
 resized_face = make_square_image(resized_face)
 if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
if n > 0:
x = torch.tensor(x, device=gpu ).float()
x = x.permute(( 0, 3, 1, 2))
for i in range(len(x)) :
x[i] = normalize_transform(x[i] / 255.)
with torch.no_grad() :
y_pred = model(x)
 y_pred = torch.sigmoid(y_pred.squeeze())
 return y_pred[:n].mean().item()
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.5<predict_on_test>
df_test['shot_made_flag'] = xgb.predict_proba(df_test[predictors])[:,1]
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )<define_variables>
submission = df_test[['shot_id', 'shot_made_flag']]
<predict_on_test><EOS> 
submission.to_csv('last_submission.csv', index=False )