Kaggle competition: Kobe Bryant Shot Selection
<SOS> metric: logloss Kaggle data source: kobe-bryant-shot-selection<install_modules>
data = pd.read_csv(".. /input/data.csv")
 print(check_output(["ls", ".. /input"] ).decode("utf8"))
%%capture
!pip install /kaggle/input/facenet-pytorch-vggface2/facenet_pytorch-1.0.1-py3-none-any.whl
!mkdir -p /tmp/.cache/torch/checkpoints/
!cp /kaggle/input/facenet-pytorch-vggface2/20180402-114759-vggface2-logits.pth /tmp/.cache/torch/checkpoints/vggface2_DG3kwML46X.pt
!cp /kaggle/input/facenet-pytorch-vggface2/20180402-114759-vggface2-features.pth /tmp/.cache/torch/checkpoints/vggface2_G5aNV2VSMn.pt<import_modules>
def test_it_RF(data_test):
clf = RandomForestClassifier(n_jobs=-1, n_estimators=70, max_depth=7, random_state=2016)
 return cross_val_score(clf, data_test.drop('shot_made_flag', 1), data_test.shot_made_flag,
scoring='log_loss', cv=10 )
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
print(f'Running on device: {device}' )<train_on_grid>
def sort_encode(df, field):
ct = pd.crosstab(df.shot_made_flag, df[field] ).apply(lambda x:x/x.sum() , axis=0)
 temp = list(zip(ct.values[1, :], ct.columns))
temp.sort()
new_map = {}
for index,(acc, old_number)in enumerate(temp):
new_map[old_number] = index
new_field = field + '_sort_enumerated'
df[new_field] = df[field].map(new_map)
 return new_field
mtcnn = MTCNN(device=device ).eval()
resnet = InceptionResnetV1(pretrained='vggface2', num_classes=2, device=device ).eval()<define_variables>
data['angle'] = 99
data.loc[~(data.loc_x==0), 'angle'] = \
np.arctan(data.loc[~(data.loc_x==0), 'loc_y'] / data.loc[~(data.loc_x==0), 'loc_x'])
 data.loc[data.loc_x == 0, 'angle'] = np.pi / 2
bias = -0.4
weight = 0.068235746
submission = []
for filename, x_i in zip(filenames, X):
if x_i is not None and len(x_i)== 10:
prob = 1 /(1 + np.exp(-(bias +(weight * x_i ).sum())))
 else:
prob = 0.6
submission.append([os.path.basename(filename), prob] )<save_to_csv>
sort_encode(data, 'action_type' )
submission = pd.DataFrame(submission, columns=['filename', 'label'])
 submission.sort_values('filename' ).to_csv('submission.csv', index=False )<install_modules>
sort_encode(data, 'angle_bin' )
!pip install /kaggle/input/dfdcdataset/dlib-19.19.0-cp36-cp36m-linux_x86_64.whl<load_pretrained>
mode = data.action_type_sort_enumerated.mode() [0]
data.action_type_sort_enumerated.fillna(mode, inplace=True)
 mode1 = data.angle_bin_sort_enumerated.mode() [0]
data.angle_bin_sort_enumerated.fillna(mode1, inplace=True )
DATA_PREFIX = '/kaggle/input'
SKIP_FRAMES = 75
detector = dlib.cnn_face_detection_model_v1(os.path.join(DATA_PREFIX, 'dfdcdataset', 'mmod_human_face_detector.dat'))
sp = dlib.shape_predictor(os.path.join(DATA_PREFIX, 'dfdcdataset', 'shape_predictor_5_face_landmarks.dat'))
predictor = dlib.deep_fake_detection_model_v1(os.path.join(DATA_PREFIX, 'dfdcdataset', 'deepfake_detector.dnn'))
def align_face(frame, detection_sp):
x_center = int(( detection_sp.part(0 ).x + detection_sp.part(2 ).x + detection_sp.part(4 ).x)/ 3)
 y_center = int(( detection_sp.part(4 ).y + detection_sp.part(0 ).y + detection_sp.part(2 ).y)/ 3)
 w = 2 * abs(detection_sp.part(0 ).x - detection_sp.part(2 ).x)
 h = w
shape = frame.shape
face_crop = frame[
max(int(y_center - h), 0):min(int(y_center + h), shape[0]),
max(int(x_center - w), 0):min(int(x_center + w), shape[1])
 ]
return cv2.resize(face_crop,(150,150))
def align_face_dlib(frame, detection_sp):
detections = dlib.full_object_detections()
detections.append(detection_sp)
 return dlib.get_face_chips(frame, detections, size=150)[0]
def predict_fake(face):

 return predictor.predict(face)[0]
def process_frame(frame):
labels = []
dets = detector(frame, 1)
 batch_faces = dlib.full_object_detections()
for k,d in enumerate(dets):
face = align_face_dlib(frame, sp(frame, d.rect))
labels.append(predict_fake(face))
return labels
def process_video(video_filename):

 frame_labels = []
frames = []
cap = cv2.VideoCapture(video_filename)
 frame_count = 0
while cap.isOpened() :
ret = cap.grab()
frame_count += 1
if not ret:
break
if frame_count % SKIP_FRAMES:
continue
_, frame = cap.retrieve()
frame_labels.extend(process_frame(frame))
cap.release()
fakeness = statistics.mean(frame_labels)
 fakeness = 0.3 +(fakeness * 0.4)
 return fakeness
def single_log_loss(prediction, groundtruth):
return groundtruth * math.log(prediction)+(1-groundtruth)* math.log(1-prediction)
 def estimate_loss(predictions, all_correct=True):
result = 0
for p in predictions:
if all_correct:
result += single_log_loss(p, 1 if p > 0.5 else 0)
 else:
result += single_log_loss(p, 0 if p > 0.5 else 1)
 return -result/len(predictions)
 predictions = []
filenames = glob.glob(os.path.join(DATA_PREFIX, 'deepfake-detection-challenge/test_videos/*.mp4'))
sub = pd.read_csv(os.path.join(DATA_PREFIX, 'deepfake-detection-challenge/sample_submission.csv'))
sub = sub.set_index('filename', drop=False)
 print('Initialize submission')
 for filename in tqdm.tqdm(filenames):
fn = filename.split('/')[-1]
sub.loc[fn, 'label'] = 0.5
print('CUDA usage: {}'.format(dlib.DLIB_USE_CUDA))
for filename in tqdm.tqdm(filenames):
fn = filename.split('/')[-1]
sub.loc[fn, 'label'] = 0.5
try:
start = timer()
prediction = process_video(filename)
 sub.loc[fn, 'label'] = prediction
sub.to_csv('submission.csv', index=False)
 predictions.append(prediction)
 print('Processed video {}, label={}, time={}'.format(filename, prediction, timedelta(seconds=timer() -start)))
 print('Possible lost: best={}, worse={}'.format(estimate_loss(predictions), estimate_loss(predictions, False)))
 except Exception as error:
print('Failed to process {}'.format(filename))
sub.to_csv('submission.csv', index=False )<set_options>
feature = ['action_type_sort_enumerated', 'shot_distance', 'angle_bin_sort_enumerated', \
'shot_made_flag']
%matplotlib inline
warnings.filterwarnings("ignore" )<define_variables>
data_cv = data.loc[~data.shot_made_flag.isnull() , feature]
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 frame_h = 5
frame_l = 5
len(test_videos )<import_modules>
[x for x in feature]
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )<set_options>
feature1 = feature
feature2 = ['action_type_sort_enumerated','shot_distance','angle_bin_sort_enumerated','shot_id']
train = data.loc[~data.shot_made_flag.isnull() , feature1]
test = data.loc[data.shot_made_flag.isnull() , feature2]
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu<load_pretrained>
clf = RandomForestClassifier(n_jobs=-1, n_estimators=70, max_depth=7, random_state=2016)
 clf.fit(train.drop('shot_made_flag',axis=1), train.shot_made_flag)
 predictions = clf.predict_proba(test.drop('shot_id',axis=1))
submission = pd.DataFrame({'shot_id': test.shot_id, 'shot_made_flag': predictions[:, 1]})
 submission[['shot_id', 'shot_made_flag']].to_csv('submission_RF.csv', index=False )