Kaggle competition: Deepfake Detection Challenge
<SOS> metric: LogLoss Kaggle data source: deepfake-detection-challenge<save_to_csv>
!pip install.. /input/efficientnet/efficientnet-1.0.0-py3-none-any.whl
model = SVC(C=2, gamma=0.4, kernel='rbf' ).fit(X_train, y_train)
 y_val_pred = model.predict(X_val)
 print(accuracy_score(y_val, y_val_pred), f1_score(y_val, y_val_pred))
print(confusion_matrix(y_val, y_val_pred))
model = SVC(C=2, gamma=0.4, kernel='rbf' ).fit(X, df.target)
 y_test_pred = model.predict(X_test)
 sub_df = pd.read_csv(os.path.join('.. /input/nlp-getting-started/', 'sample_submission.csv'))
sub_df["target"] = y_test_pred
sub_df.to_csv("submission_lda.csv",index=False )<save_to_csv>
sample_submission = pd.read_csv(".. /input/deepfake-detection-challenge/sample_submission.csv")
 detection_graph = tf.Graph()
detection_graph = tf.Graph()
with detection_graph.as_default() :
od_graph_def = tf.compat.v1.GraphDef()
with tf.io.gfile.GFile('.. /input/mobilenet-face/frozen_inference_graph_face.pb', 'rb')as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
 tf.import_graph_def(od_graph_def, name='')
 config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
sess=tf.compat.v1.Session(graph=detection_graph, config=config)
 image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
 boxes_tensor = detection_graph.get_tensor_by_name('detection_boxes:0')
 scores_tensor = detection_graph.get_tensor_by_name('detection_scores:0')
 num_detections = detection_graph.get_tensor_by_name('num_detections:0')
 def get_img(images):
global boxes,scores,num_detections
im_heights,im_widths=[],[]
imgs=[]
for image in images: 
(im_height,im_width)=image.shape[:-1]
imgs.append(image)
 im_heights.append(im_height)
 im_widths.append(im_widths)
 imgs=np.array(imgs)
(boxes, scores_)= sess.run(
 [boxes_tensor, scores_tensor],
feed_dict={image_tensor: imgs})
 finals=[]
for x in range(boxes.shape[0]):
scores=scores_[x]
max_=np.where(scores==scores.max())[0][0]
box=boxes[x][max_]
ymin, xmin, ymax, xmax = box 
(left, right, top, bottom)=(xmin * im_width, xmax * im_width,
ymin * im_height, ymax * im_height)
 left, right, top, bottom = int(left), int(right), int(top), int(bottom)
 finals.append([left, right, top, bottom])
 return finals
def resize_imgs(bbox,imgs,resize=240,pad=40):
finals=[]
for image,(left, right, top, bottom)in zip(imgs,bbox):
finals.append(cv2.cvtColor(cv2.resize(image[max([0,top-pad]):bottom+(pad*2),max([0,left-pad]):right+(pad*2)],(resize,resize)) ,cv2.COLOR_BGR2RGB))
return np.hstack(finals)
 def detect_video(video,frame_count=10):
capture = cv2.VideoCapture(video)
 v_len = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
frame_idxs = np.linspace(0,v_len,frame_count, endpoint=False, dtype=np.int)
 imgs=[]
i=0
for frame_idx in range(int(v_len)) :
ret = capture.grab()
if not ret:
print("Error grabbing frame %d from movie %s" %(frame_idx, video))
if frame_idx >= frame_idxs[i]:
if frame_idx-frame_idxs[i]>20:
return None
ret, frame = capture.retrieve()
if not ret or frame is None:
print("Error retrieving frame %d from movie %s" %(frame_idx, video))
else:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 imgs.append(frame)
 i += 1
if i >= len(frame_idxs):
break
bbox=get_img(imgs)
 if len(bbox)<10:
return None
return imgs,bbox
model = RandomForestClassifier(n_estimators=200, max_depth=None, random_state=42, n_jobs=-1 ).fit(X_train, y_train)
 y_val_pred = model.predict(X_val)
 print(accuracy_score(y_val, y_val_pred), f1_score(y_val, y_val_pred))
print(confusion_matrix(y_val, y_val_pred))
model = RandomForestClassifier(n_estimators=200, max_depth=None, random_state=42, n_jobs=-1 ).fit(X, df.target)
 y_test_pred = model.predict(X_test)
 sub_df = pd.read_csv(os.path.join('.. /input/nlp-getting-started/', 'sample_submission.csv'))
sub_df["target"] = y_test_pred
sub_df.to_csv("submission_lda_rf.csv",index=False )<split>
val_transform = Compose([
Resize(240, 240),
Normalize(mean= (.5,.5,.5), std= (.5,.5,.5)) ,
ToTensorV2() ,
] )
print(len(df["tokens"]))
X_1 = transform(df["tokens"], tf_model, lda_model)
 X_2 = transform(df["tokens"], tf_model, lsi_model)
 X = np.hstack(( X_1, X_2))
print(len(X))
X_test = transform(df_test["tokens"], tf_model, lda_model)
 X_1 = transform(df_test["tokens"], tf_model, lda_model)
 X_2 = transform(df_test["tokens"], tf_model, lsi_model)
 X_test = np.hstack(( X_1, X_2))
X_train, X_val, y_train, y_val = train_test_split(X, df["target"], test_size=0.1, random_state=42)
 X_train.shape<save_to_csv>
model1=torch.jit.load('.. /input/dfdc-ensemble-final-2/dfdc_mixl_0.pt',map_location='cpu' ).cuda()
_=model1.eval()
model2=torch.jit.load('.. /input/dfdc-ensemble-final-2/dfdc_mixxl_0.pt',map_location='cpu' ).cuda()
_=model2.eval()
model3=torch.jit.load('.. /input/dfdc-ensemble-final-2/dfdc_se101_2.pt',map_location='cpu' ).cuda()
_=model3.eval()
model = SVC(C=2, gamma=0.4, kernel='rbf' ).fit(X_train, y_train)
 y_val_pred = model.predict(X_val)
 print(accuracy_score(y_val, y_val_pred), f1_score(y_val, y_val_pred))
print(confusion_matrix(y_val, y_val_pred))
model = SVC(C=2, gamma=0.4, kernel='rbf' ).fit(X, df.target)
 y_test_pred = model.predict(X_test)
 sub_df = pd.read_csv(os.path.join('.. /input/nlp-getting-started/', 'sample_submission.csv'))
sub_df["target"] = y_test_pred
sub_df.to_csv("submission_lda_lsi.csv",index=False )<save_to_csv>
bottleneck = efn.EfficientNetB6(weights=None,include_top=False,pooling='avg')
 inp=Input(( 240,240,3))
x=bottleneck(inp)
 x=Dense(256 )(x)
 x=Dense(128, activation='relu' )(x)
 x=Dense(1,activation='sigmoid' )(x)
 model_b6=Model(inp,x)
 model_b6.load_weights('.. /input/dfdc-ensemble-final-2/nocrop_effb6_dense256_dense128_e5_wrong_activation_weights.h5' )
model = RandomForestClassifier(n_estimators=200, max_depth=None, random_state=42, n_jobs=-1 ).fit(X_train, y_train)
 y_val_pred = model.predict(X_val)
 print(accuracy_score(y_val, y_val_pred), f1_score(y_val, y_val_pred))
print(confusion_matrix(y_val, y_val_pred))
model = RandomForestClassifier(n_estimators=200, max_depth=None, random_state=42, n_jobs=-1 ).fit(X, df.target)
 y_test_pred = model.predict(X_test)
 sub_df = pd.read_csv(os.path.join('.. /input/nlp-getting-started/', 'sample_submission.csv'))
sub_df["target"] = y_test_pred
sub_df.to_csv("submission_lda_lsi_rf.csv",index=False )<concatenate>
model_34 = load_model(".. /input/dfdc-ensemble-final-2/model_b3_300_4.h5")
 model_35 = load_model(".. /input/dfdc-ensemble-final-2/model_35.h5")
 model_b1_34 = load_model(".. /input/dfdc-ensemble-final-2/model_b1_200_5.h5")
 def get_birghtness(img):
return img/img.max()
def process_img(img,flip=False,birghtness=True):
imgs=[]
for x in range(10):
if flip:
imgs.append(get_birghtness(cv2.flip(img[:,x*240:(x+1)*240,:],1)))
 else:
imgs.append(get_birghtness(img[:,x*240:(x+1)*240,:]))
return np.array(imgs )
X_all = pd.concat([df["tokens"], df_test["tokens"]] ).reset_index(drop=True)
 model_w2v = Word2Vec(sentences=X_all, size=50, window=3, min_count=1, workers=-1)
 del X_all<find_best_params>
np.mean([p1,p2] )
vector = model_w2v.wv['armageddon']
print(vector )<define_variables>
print(sum(preds)/len(preds))
for x,y in zip(preds,filenames):
submission.loc[submission['filename']==y,'label']=x
<save_to_csv><EOS> 
submission.to_csv('submission.csv', index=False)
 !rm -r videos