Kaggle competition: Natural Language Processing with Disaster Tweets
input_size = 224<normalization>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from wordcloud import WordCloud
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )<load_pretrained>
df =pd.read_csv('/kaggle/input/nlp-getting-started/train.csv' , encoding='ISO-8859-1')
 df.head()
frames_per_video = 10
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)
<choose_model_class>
df_tweets = df[['text','target']]
class HisResNeXt(models.resnet.ResNet):
def __init__(self, training=True):
super(HisResNeXt, 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>
df_tweets.drop_duplicates(subset=['text'],keep='first',inplace=True)
 df_tweets.info()
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='' )<load_pretrained>
train_df,eval_df = train_test_split(df_tweets,test_size = 0.01 )
checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
 model = HisResNeXt().to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint 
<predict_on_test>
!pip install simpletransformers==0.32.3
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<set_options>
model = ClassificationModel('bert', 'bert-base-cased', num_labels=2, args={'reprocess_input_data': True, 'overwrite_output_dir': True},use_cuda=False)
cm = detection_graph.as_default()
cm.__enter__()<prepare_x_and_y>
train_df2 = pd.DataFrame({
'text': train_df['text'].replace(r'
', ' ', regex=True),
'label': train_df['target']
})
 eval_df2 = pd.DataFrame({
'text': eval_df['text'].replace(r'
', ' ', regex=True),
'label': eval_df['target']
} )
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' )<categorify>
model.train_model(train_df2 )
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)
 image=imgs[x]
finals.append(cv2.cvtColor(cv2.resize(image[max([0,top-40]):bottom+80,max([0,left-40]):right+80],(240,240)) ,cv2.COLOR_BGR2RGB))
return 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
imgs=get_img(imgs)
 if len(imgs)<10:
return None
return np.hstack(imgs )<define_variables>
result, model_outputs, wrong_predictions = model.eval_model(eval_df2 )
res_predictions =[]<predict_on_test>
lst = []
for arr in model_outputs:
lst.append(np.argmax(arr))
for x in tqdm(glob.glob('.. /input/deepfake-detection-challenge/test_videos/*.mp4')) :
try:
filename=x.replace('.. /input/deepfake-detection-challenge/test_videos/','' ).replace('.mp4','.jpg')
 a=detect_video(x)
 y_pred = predict_on_video(x, batch_size=frames_per_video)
 res_predictions.append(y_pred)
 if a is None:
continue
cv2.imwrite('./videos/'+filename,a)
 except Exception as err:
print(err )<choose_model_class>
true = eval_df2['label'].tolist()
predicted = lst
bottleneck_EfficientNetB1 = efn.EfficientNetB1(weights=None,include_top=False,pooling='avg')
 inp=Input(( 10,240,240,3))
x=TimeDistributed(bottleneck_EfficientNetB1 )(inp)
 x = LSTM(128 )(x)
 x = Dense(64, activation='elu' )(x)
 x = Dense(1,activation='sigmoid' )(x)
 model_EfficientNetB1=Model(inp,x)
 bottleneck_Xception = Xception(weights=None,
include_top=False,pooling='avg')
 y=TimeDistributed(bottleneck_Xception )(inp)
 y = LSTM(128 )(y)
 y = Dense(64, activation='elu' )(y)
 y = Dense(1,activation='sigmoid' )(y)
 model_Xception=Model(inp,y )<load_pretrained>
mat = sklearn.metrics.confusion_matrix(true , predicted)
 mat
model_EfficientNetB1.load_weights('.. /input/efficientnetb1dfdc/EfficientNetB1-e_2_b_4_f_30-10.h5')
 model_Xception.load_weights('.. /input/xceptiondfdc/Xception-e_2_b_4_f_30-10.h5' )<statistical_test>
print(sklearn.metrics.classification_report(true,predicted,target_names=['fake','real']))
def get_birghtness(img):
return img/img.max()
def process_img(img,flip=False):
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 )<load_from_csv>
test_df =pd.read_csv('/kaggle/input/nlp-getting-started/test.csv' , encoding='ISO-8859-1')
 test_df.head()
sample_submission = pd.read_csv(".. /input/deepfake-detection-challenge/sample_submission.csv")
 test_files=glob.glob('./videos/*.jpg')
 submission=pd.DataFrame()
submission['filename']=os.listdir(( '.. /input/deepfake-detection-challenge/test_videos/'))
submission['label']=0.5
filenames=[]
batch=[]
batch1=[]
preds=[]<define_variables>
final_prediction = model.predict(list(test_df.text))
new_preds=[]
for x,y in zip(preds,res_predictions):
new_preds.append(x[0]+(0.2*y))
print(sum(new_preds)/len(new_preds))<feature_engineering>
print('Loading in Submission File...')
 submit_df = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 submit_df['target'] = final_prediction[0]
submit_df.to_csv('bert_submit.csv', index=False )
for x,y in zip(new_preds,filenames):
submission.loc[submission['filename']==y,'label']=x<save_to_csv>
print("Finished" )