Kaggle competition: Natural Language Processing with Disaster Tweets
deep_fake_predictor = DeepFakePredictor()<predict_on_test>
!pip install -q tf-models-official==2.3.0
def process_dfs(df, num_workers=2):
def process_df(sub_df):
dataset = DatasetRetriever(sub_df)
 result = deep_fake_predictor.predict(dataset)
 return result
with ThreadPoolExecutor(max_workers=num_workers)as ex:
results = ex.map(process_df, np.split(df, num_workers))
return results<save_to_csv>
train = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 test=pd.read_csv('.. /input/nlp-getting-started/test.csv')
 submission = pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv" )
result.to_csv('submission.csv' )<install_modules>
train['words'] = train['text'].str.split()
train['word_len'] = train['words'].map(lambda x: len(x))
train['word_len'].max()
!pip install.. /input/pytorchcv/pytorchcv-0.0.55-py2.py3-none-any.whl --quiet<import_modules>
test['words'] = test['text'].str.split()
test['word_len'] = test['words'].map(lambda x: len(x))
test['word_len'].max()
device = 'cuda' if torch.cuda.is_available() else 'cpu'<normalization>
ids_with_target_error = [328,443,513,2619,3640,3900,4342,5781,6552,6554,6570,6701,6702,6729,6861,7226]
train.loc[train['id'].isin(ids_with_target_error),'target'] = 0
train[train['id'].isin(ids_with_target_error)]
def gem(x, p=3, eps=1e-6):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./p)
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-6):
super(GeM,self ).__init__()
self.p = Parameter(torch.ones(1)*p)
 self.eps = eps
def forward(self, x):
return gem(x, p=self.p, eps=self.eps)
 def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(self.eps)+ ')'
class MishFunction(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
ctx.save_for_backward(x)
 return x * torch.tanh(F.softplus(x))
@staticmethod
def backward(ctx, grad_output):
x = ctx.saved_variables[0]
sigmoid = torch.sigmoid(x)
 tanh_sp = torch.tanh(F.softplus(x))
return grad_output *(tanh_sp + x * sigmoid *(1 - tanh_sp * tanh_sp))
class Mish(nn.Module):
def forward(self, x):
return MishFunction.apply(x)
 def to_Mish(model):
for child_name, child in model.named_children() :
if isinstance(child, nn.ReLU):
setattr(model, child_name, Mish())
 else:
to_Mish(child)
 class Head(torch.nn.Module):
def __init__(self, in_f, out_f, hidden):
super(Head, self ).__init__()
self.f = nn.Flatten()
self.l = nn.Linear(in_f, hidden)
 self.m = Mish()
self.d = nn.Dropout(0.75)
 self.o = nn.Linear(hidden, out_f)
 self.b1 = nn.BatchNorm1d(in_f)
 self.b2 = nn.BatchNorm1d(hidden)
 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, hidden):
super(FCN, self ).__init__()
self.base = base
self.h1 = Head(in_f, 1, hidden)
 def forward(self, x):
x = self.base(x)
 return self.h1(x)
 net = []
model = get_model("xception", pretrained=False)
 model = nn.Sequential(*list(model.children())[:-1])
 model[0].final_block.pool = nn.Sequential(nn.AdaptiveAvgPool2d(1))
model = FCN(model, 2048, 512)
 model = model.cuda()
model.load_state_dict(torch.load('.. /input/deepfake-models/model2.pth'))
net.append(model)
 model = get_model("efficientnet_b1", pretrained=False)
 model = nn.Sequential(*list(model.children())[:-1])
 model[0].final_pool = nn.Sequential(nn.AdaptiveAvgPool2d(1))
model = FCN(model, 1280, 512)
 model = model.cuda()
model.load_state_dict(torch.load('.. /input/deepfake-models/model (.259 ).pth'))
net.append(model)
 model = get_model("efficientnet_b1", pretrained=False)
 model = nn.Sequential(*list(model.children())[:-1])
 model[0].final_pool = nn.Sequential(nn.AdaptiveAvgPool2d(1))
model = FCN(model, 1280, 512)
 model = model.cuda()
model.load_state_dict(torch.load('.. /input/deepfake-models/model_16 (.2755 ).pth'))
net.append(model)
<choose_model_class>
df_all = pd.concat([train,test])
 df_all.shape
<normalization>
df_all['text'] = df_all['text'].str.lower()
df_all['text'].head(2 )
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )<set_options>
def remove_breaklines(text):
return re.sub('
','',text)
 df_all['text'] = df_all['text'].apply(lambda x: remove_breaklines(x))
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_mobilenet_face(image):
global boxes,scores,num_detections 
(im_height,im_width)=image.shape[:-1]
imgs=np.array([image])
(boxes, scores)= sess.run(
 [boxes_tensor, scores_tensor],
feed_dict={image_tensor: imgs})
 max_=np.where(scores==scores.max())[0][0]
box=boxes[0][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)
 return(left, right, top, bottom)
 def crop_image(frame,bbox):
left, right, top, bottom=bbox
return frame[top:bottom,left:right]
def get_img(frame):
return cv2.resize(crop_image(frame,get_mobilenet_face(frame)) ,(160,160))
def detect_video(video):
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:
pass
if frame_idx >= frame_idxs[i]:
ret, frame = capture.retrieve()
if not ret or frame is None:
pass
else:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 try:
face=get_img(frame)
 except Exception as err:
print(err)
 continue
face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
 imgs.append(face)
 i += 1
if i >= len(frame_idxs):
break
if len(imgs)<frame_count:
return None
return imgs<save_to_csv>
def remove_numbers(text):
return re.sub('\w*\d\w*', '', text)
 df_all['text'] = df_all['text'].apply(lambda x: remove_numbers(x))
probs = np.asarray(probs)
 probs[probs!=probs] = 0.5
plt.hist(probs, 40)
 filenames = [os.path.basename(f)for f in filenames]
submission = pd.DataFrame({'filename': filenames, 'label': probs})
 submission.to_csv('submission.csv', index=False)
 submission<import_modules>
def remove_URL(text):
url = re.compile(r'https?://\S+|www\.\S+')
 return url.sub(r'',text)
 df_all['text'] = df_all['text'].apply(lambda x: remove_URL(x))
from fastai.vision import *<load_from_disk>
def remove_html(text):
html=re.compile(r'<.*?>')
 return html.sub(r'',text)
 df_all['text']=df_all['text'].apply(lambda x : remove_html(x))
train_sample_metadata = pd.read_json('.. /input/deepfake-detection-challenge/train_sample_videos/metadata.json' ).T.reset_index()
train_sample_metadata.columns = ['fname','label','split','original']
train_sample_metadata.head()<filter>
def remove_emoji(text):
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F"
u"\U0001F300-\U0001F5FF"
u"\U0001F680-\U0001F6FF"
u"\U0001F1E0-\U0001F1FF"
u"\U00002702-\U000027B0"
u"\U000024C2-\U0001F251"
"]+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 df_all['text']=df_all['text'].apply(lambda x: remove_emoji(x))
fake_sample_df = train_sample_metadata[train_sample_metadata.label == 'FAKE']
real_sample_df = train_sample_metadata[train_sample_metadata.label == 'REAL']<define_variables>
def remove_punct(text):
table=str.maketrans('','',string.punctuation)
 return text.translate(table)
 df_all['text']=df_all['text'].apply(lambda x : remove_punct(x))
train_dir = Path('/kaggle/input/deepfake-detection-challenge/train_sample_videos/')
 test_dir = Path('/kaggle/input/deepfake-detection-challenge/test_videos/')
 train_video_files = get_files(train_dir, extensions=['.mp4'])
 test_video_files = get_files(test_dir, extensions=['.mp4'] )<define_variables>
abbreviations = {
"$" : " dollar ",
"€" : " euro ",
"4ao" : "for adults only",
"a.m" : "before midday",
"a3" : "anytime anywhere anyplace",
"aamof" : "as a matter of fact",
"acct" : "account",
"adih" : "another day in hell",
"afaic" : "as far as i am concerned",
"afaict" : "as far as i can tell",
"afaik" : "as far as i know",
"afair" : "as far as i remember",
"afk" : "away from keyboard",
"app" : "application",
"approx" : "approximately",
"apps" : "applications",
"asap" : "as soon as possible",
"asl" : "age, sex, location",
"atk" : "at the keyboard",
"ave." : "avenue",
"aymm" : "are you my mother",
"ayor" : "at your own risk",
"b&b" : "bed and breakfast",
"b+b" : "bed and breakfast",
"b.c" : "before christ",
"b2b" : "business to business",
"b2c" : "business to customer",
"b4" : "before",
"b4n" : "bye for now",
"b@u" : "back at you",
"bae" : "before anyone else",
"bak" : "back at keyboard",
"bbbg" : "bye bye be good",
"bbc" : "british broadcasting corporation",
"bbias" : "be back in a second",
"bbl" : "be back later",
"bbs" : "be back soon",
"be4" : "before",
"bfn" : "bye for now",
"blvd" : "boulevard",
"bout" : "about",
"brb" : "be right back",
"bros" : "brothers",
"brt" : "be right there",
"bsaaw" : "big smile and a wink",
"btw" : "by the way",
"bwl" : "bursting with laughter",
"c/o" : "care of",
"cet" : "central european time",
"cf" : "compare",
"cia" : "central intelligence agency",
"csl" : "can not stop laughing",
"cu" : "see you",
"cul8r" : "see you later",
"cv" : "curriculum vitae",
"cwot" : "complete waste of time",
"cya" : "see you",
"cyt" : "see you tomorrow",
"dae" : "does anyone else",
"dbmib" : "do not bother me i am busy",
"diy" : "do it yourself",
"dm" : "direct message",
"dwh" : "during work hours",
"e123" : "easy as one two three",
"eet" : "eastern european time",
"eg" : "example",
"embm" : "early morning business meeting",
"encl" : "enclosed",
"encl." : "enclosed",
"etc" : "and so on",
"faq" : "frequently asked questions",
"fawc" : "for anyone who cares",
"fb" : "facebook",
"fc" : "fingers crossed",
"fig" : "figure",
"fimh" : "forever in my heart",
"ft." : "feet",
"ft" : "featuring",
"ftl" : "for the loss",
"ftw" : "for the win",
"fwiw" : "for what it is worth",
"fyi" : "for your information",
"g9" : "genius",
"gahoy" : "get a hold of yourself",
"gal" : "get a life",
"gcse" : "general certificate of secondary education",
"gfn" : "gone for now",
"gg" : "good game",
"gl" : "good luck",
"glhf" : "good luck have fun",
"gmt" : "greenwich mean time",
"gmta" : "great minds think alike",
"gn" : "good night",
"g.o.a.t" : "greatest of all time",
"goat" : "greatest of all time",
"goi" : "get over it",
"gps" : "global positioning system",
"gr8" : "great",
"gratz" : "congratulations",
"gyal" : "girl",
"h&c" : "hot and cold",
"hp" : "horsepower",
"hr" : "hour",
"hrh" : "his royal highness",
"ht" : "height",
"ibrb" : "i will be right back",
"ic" : "i see",
"icq" : "i seek you",
"icymi" : "in case you missed it",
"idc" : "i do not care",
"idgadf" : "i do not give a damn fuck",
"idgaf" : "i do not give a fuck",
"idk" : "i do not know",
"ie" : "that is",
"i.e" : "that is",
"ifyp" : "i feel your pain",
"IG" : "instagram",
"iirc" : "if i remember correctly",
"ilu" : "i love you",
"ily" : "i love you",
"imho" : "in my humble opinion",
"imo" : "in my opinion",
"imu" : "i miss you",
"iow" : "in other words",
"irl" : "in real life",
"j4f" : "just for fun",
"jic" : "just in case",
"jk" : "just kidding",
"jsyk" : "just so you know",
"l8r" : "later",
"lb" : "pound",
"lbs" : "pounds",
"ldr" : "long distance relationship",
"lmao" : "laugh my ass off",
"lmfao" : "laugh my fucking ass off",
"lol" : "laughing out loud",
"ltd" : "limited",
"ltns" : "long time no see",
"m8" : "mate",
"mf" : "motherfucker",
"mfs" : "motherfuckers",
"mfw" : "my face when",
"mofo" : "motherfucker",
"mph" : "miles per hour",
"mr" : "mister",
"mrw" : "my reaction when",
"ms" : "miss",
"mte" : "my thoughts exactly",
"nagi" : "not a good idea",
"nbc" : "national broadcasting company",
"nbd" : "not big deal",
"nfs" : "not for sale",
"ngl" : "not going to lie",
"nhs" : "national health service",
"nrn" : "no reply necessary",
"nsfl" : "not safe for life",
"nsfw" : "not safe for work",
"nth" : "nice to have",
"nvr" : "never",
"nyc" : "new york city",
"oc" : "original content",
"og" : "original",
"ohp" : "overhead projector",
"oic" : "oh i see",
"omdb" : "over my dead body",
"omg" : "oh my god",
"omw" : "on my way",
"p.a" : "per annum",
"p.m" : "after midday",
"pm" : "prime minister",
"poc" : "people of color",
"pov" : "point of view",
"pp" : "pages",
"ppl" : "people",
"prw" : "parents are watching",
"ps" : "postscript",
"pt" : "point",
"ptb" : "please text back",
"pto" : "please turn over",
"qpsa" : "what happens",
"ratchet" : "rude",
"rbtl" : "read between the lines",
"rlrt" : "real life retweet",
"rofl" : "rolling on the floor laughing",
"roflol" : "rolling on the floor laughing out loud",
"rotflmao" : "rolling on the floor laughing my ass off",
"rt" : "retweet",
"ruok" : "are you ok",
"sfw" : "safe for work",
"sk8" : "skate",
"smh" : "shake my head",
"sq" : "square",
"srsly" : "seriously",
"ssdd" : "same stuff different day",
"tbh" : "to be honest",
"tbs" : "tablespooful",
"tbsp" : "tablespooful",
"tfw" : "that feeling when",
"thks" : "thank you",
"tho" : "though",
"thx" : "thank you",
"tia" : "thanks in advance",
"til" : "today i learned",
"tl;dr" : "too long i did not read",
"tldr" : "too long i did not read",
"tmb" : "tweet me back",
"tntl" : "trying not to laugh",
"ttyl" : "talk to you later",
"u" : "you",
"u2" : "you too",
"u4e" : "yours for ever",
"utc" : "coordinated universal time",
"w/" : "with",
"w/o" : "without",
"w8" : "wait",
"wassup" : "what is up",
"wb" : "welcome back",
"wtf" : "what the fuck",
"wtg" : "way to go",
"wtpa" : "where the party at",
"wuf" : "where are you from",
"wuzup" : "what is up",
"wywh" : "wish you were here",
"yd" : "yard",
"ygtr" : "you got that right",
"ynk" : "you never know",
"zzz" : "sleeping bored and tired"
}
def convert_abbrev(word):
return abbreviations[word] if word in abbreviations.keys() else word
dummy_video_file = train_video_files[0]<set_options>
def convert_abbrev_in_text(text):
tokens = word_tokenize(text)
 tokens = [convert_abbrev(word)for word in tokens]
text = ' '.join(tokens)
 return text
df_all['text']=df_all['text'].apply(lambda x : convert_abbrev_in_text(x))
sys.path.insert(0,'/kaggle/working/reader/python')
 set_bridge('torch')
 device = torch.device("cuda" )<categorify>
df_all['text']=df_all['text'].apply(lambda x : word_tokenize(x))
retinaface_stats = tensor([123,117,104] ).to(device)
 def decord_cpu_video_reader(path, freq=None):
video = VideoReader(str(path), ctx=cpu())
 len_video = len(video)
 if freq: t = video.get_batch(range(0, len(video), freq)).permute(0,3,1,2)
 else: t = video.get_batch(range(len_video))
return t, len_video
def get_decord_video_batch_cpu(path, freq=10, sz=640, stats:Tensor=None, device=defaults.device):
"get resized and mean substracted batch tensor of a sampled video(scale of 255)"
t_raw, len_video = decord_cpu_video_reader(path, freq)
 H,W = t_raw.size(2), t_raw.size(3)
 t = F.interpolate(t_raw.to(device ).to(torch.float32),(sz,sz))
if stats is not None: t -= stats[...,None,None]
return t, t_raw,(H, W )<prepare_output>
stop = set(stopwords.words('english'))
def remove_stopwords(text):
words = [w for w in text if w not in stop]
return ' '.join(words)
 df_all['text']=df_all['text'].apply(lambda x : remove_stopwords(x))
sys.path.insert(0,"/kaggle/input/retina-face-2/Pytorch_Retinaface_2/" )<import_modules>
cl_ch_len = df_all['text'].apply(lambda x: len(x))
cl_wd_len = df_all['text'].str.split().map(lambda x: len(x))
print('Max words length for cleaned tweets: {}'.format(max(cl_wd_len)))
 print('Max characters length for cleaned tweets: {}'.format(max(cl_ch_len)))
 MAX_LEN = max(cl_wd_len )
import os
import torch
import torch.backends.cudnn as cudnn
import numpy as np
from data import cfg_mnet, cfg_re50
from layers.functions.prior_box import PriorBox
from utils.nms.py_cpu_nms import py_cpu_nms
import cv2
from models.retinaface import RetinaFace
from utils.box_utils import decode, decode_landm
import time<define_variables>
def create_corpus(df):
corpus=[]
for tweet in tqdm(df_all['text']):
words=[word.lower() for word in word_tokenize(tweet)if(( word.isalpha() ==1)&(word not in stop)) ]
corpus.append(words)
 return corpus
corpus = create_corpus(df_all)
 corpus[0]
def check_keys(model, pretrained_state_dict):
ckpt_keys = set(pretrained_state_dict.keys())
 model_keys = set(model.state_dict().keys())
 used_pretrained_keys = model_keys & ckpt_keys
unused_pretrained_keys = ckpt_keys - model_keys
missing_keys = model_keys - ckpt_keys
print('Missing keys:{}'.format(len(missing_keys)))
 print('Unused checkpoint keys:{}'.format(len(unused_pretrained_keys)))
 print('Used keys:{}'.format(len(used_pretrained_keys)))
 assert len(used_pretrained_keys)> 0, 'load NONE from pretrained checkpoint'
return True
def remove_prefix(state_dict, prefix):

 print('remove prefix '{}''.format(prefix))
f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix)else x
return {f(key): value for key, value in state_dict.items() }
def load_model(model, pretrained_path, load_to_cpu):
print('Loading pretrained model from {}'.format(pretrained_path))
if load_to_cpu:
pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage)
 else:
device = torch.cuda.current_device()
pretrained_dict = torch.load(pretrained_path, map_location=lambda storage, loc: storage.cuda(device))
if "state_dict" in pretrained_dict.keys() :
pretrained_dict = remove_prefix(pretrained_dict['state_dict'], 'module.')
 else:
pretrained_dict = remove_prefix(pretrained_dict, 'module.')
 check_keys(model, pretrained_dict)
 model.load_state_dict(pretrained_dict, strict=False)
 return model<set_options>
glove_embedding_dict={}
with open('.. /input/glove-global-vectors-for-word-representation/glove.6B.200d.txt','r')as f:
for line in tqdm(f):
values=line.split()
word=values[0]
vectors=np.asarray(values[1:],'float32')
 glove_embedding_dict[word]=vectors
cudnn.benchmark = True<define_variables>
W_E_DIM = 200
def get_model(modelname="mobilenet"):
torch.set_grad_enabled(False)
 cfg = None
cfg_mnet['pretrain'] = False
cfg_re50['pretrain'] = False
if modelname == "mobilenet":
pretrained_path = ".. /input/retina-face-2/Pytorch_Retinaface_2/weights/mobilenet0.25_Final.pth"
cfg = cfg_mnet
if modelname == "resnet50":
pretrained_path = ".. /input/retina-face/Pytorch_Retinaface/weights/Resnet50_Final.pth"
cfg = cfg_re50
net = RetinaFace(cfg=cfg, phase='test')
 net = load_model(net, pretrained_path, False)
 net.eval().to(device)
 return net, cfg<predict_on_test>
tokenizer_obj=Tokenizer()
tokenizer_obj.fit_on_texts(corpus)
 sequences=tokenizer_obj.texts_to_sequences(corpus)
 tweet_pad=pad_sequences(sequences,maxlen=MAX_LEN,truncating='post',padding='post' )
def predict(model, t, sz, cfg,
confidence_threshold = 0.5, top_k = 5, nms_threshold = 0.5, keep_top_k = 5):
"get prediction for a batch t by model with image sz"
resize = 1
scale_rate = 1
im_height, im_width = sz, sz
scale = torch.Tensor([sz, sz, sz, sz])
 scale = scale.to(device)
 locs, confs, landmss = torch.Tensor([]), torch.Tensor([]), torch.Tensor([])
 locs = locs.to(device)
 confs = confs.to(device)
 landmss = landmss.to(device)
 locs_, confs_, landmss_ = model(t)
 locs = torch.cat(( locs, locs_), 0)
 confs = torch.cat(( confs, confs_), 0)
 landmss = torch.cat(( landmss, landmss_), 0)
 bbox_result, landms_result = [], []
priorbox = PriorBox(cfg, image_size=(im_height, im_width))
priors = priorbox.forward()
priors = priors.to(device)
 prior_data = priors.data
for idx in range(t.size(0)) :
loc = locs[idx]
conf = confs[idx]
landms = landmss[idx]
boxes = decode(loc.data.squeeze(0), prior_data, cfg['variance'])
 boxes = boxes * scale / resize
boxes = boxes.cpu().numpy()
scores = conf.squeeze(0 ).data.cpu().numpy() [:, 1]
landms = decode_landm(landms.data.squeeze(0), prior_data, cfg['variance'])
 scale1 = torch.Tensor([t.shape[3], t.shape[2], t.shape[3], t.shape[2],
t.shape[3], t.shape[2], t.shape[3], t.shape[2],
t.shape[3], t.shape[2]])
 scale1 = scale1.to(device)
 landms = landms * scale1 / resize
landms = landms.cpu().numpy()
inds = np.where(scores > confidence_threshold)[0]
boxes = boxes[inds]
landms = landms[inds]
scores = scores[inds]
order = scores.argsort() [::-1][:top_k]
boxes = boxes[order]
landms = landms[order]
scores = scores[order]
dets = np.hstack(( boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
 keep = py_cpu_nms(dets, nms_threshold)
 dets = dets[keep, :]
landms = landms[keep]
dets = dets[:keep_top_k, :]
landms = landms[:keep_top_k, :]
bbox_result.append(dets[:,:-1])
 landms_result.append(landms)
 return bbox_result, landms_result<find_best_params>
word_index=tokenizer_obj.word_index
print('Number of unique words:',len(word_index))
%%time
model, cfg = get_model("mobilenet" )<categorify>
num_words=len(word_index)+1
embedding_matrix=np.zeros(( num_words,W_E_DIM))
for word,i in tqdm(word_index.items()):
if i < num_words:
emb_vec=glove_embedding_dict.get(word)
 if emb_vec is not None:
embedding_matrix[i]=emb_vec
def bboxes_to_original_scale(bboxes, H, W, sz):

 res = []
for bb in bboxes:
h_scale, w_scale = H/sz, W/sz
orig_bboxes =(bb*array([w_scale, h_scale, w_scale, h_scale])[None,...] ).astype(int)
 res.append(orig_bboxes)
 return res<categorify>
train_text = tweet_pad[:train.shape[0]]
test_text = tweet_pad[train.shape[0]:]
X_train,X_dev,Y_train,Y_dev=train_test_split(train_text,train['target'].values,test_size=0.2)
 print('Shape of train',X_train.shape)
 print("Shape of Validation ",X_dev.shape )
def landmarks_to_original_scale(landmarks, H, W, sz):

 res = []
for landms in landmarks:
h_scale, w_scale = H/sz, W/sz
orig_landms =(landms*array([w_scale, h_scale]*5)[None,...] ).astype(int)
 res.append(orig_landms)
 return res<import_modules>
model=Sequential()
embedding=Embedding(num_words,W_E_DIM,embeddings_initializer=Constant(embedding_matrix),
input_length=MAX_LEN,trainable=False)
 model.add(embedding)
 model.add(SpatialDropout1D(0.2))
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
optimzer=Adam(learning_rate=3e-4)
 model.compile(loss='binary_crossentropy',optimizer=optimzer,metrics=['accuracy'])
 model.summary()
from tqdm import tqdm<init_hyperparams>
history=model.fit(X_train,Y_train,batch_size=4,
epochs=10,validation_data=(X_dev,Y_dev),verbose=2)
freq = 5
model_args = dict(confidence_threshold = 0.5, top_k = 5, nms_threshold = 0.5, keep_top_k = 5)
 sz = cfg['image_size']
imgnet_stats = [tensor(o)for o in imagenet_stats]
rescale_param = 1.3<install_modules>
pred = model.predict(test_text)
 pred = pred.round().astype('int' )
!pip install -q.. /input/efficientnetpytorchpip/efficientnet_pytorch-0.6.3/<import_modules>
df_sub = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv')
 df_sub['target'] = pred
df_sub = df_sub[['id', 'target']]
df_sub.to_csv('lstm_submission.csv', index=False, header=True)
 df_sub.head(10 )
from fastai.vision.models.efficientnet import *<categorify>
%%time
bert_url = 'https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/2'
bert_layer = hub.KerasLayer(bert_url, trainable=True, name='Bert_layer' )
class DummyDatabunch:
c = 2
path = '.'
device = defaults.device
loss_func = None
data = DummyDatabunch()<load_pretrained>
def bert_encode(texts, tokenizer, max_len=512):
input_tokens = []
input_masks = []
input_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
input_tokens.append(tokens)
 input_masks.append(pad_masks)
 input_segments.append(segment_ids)
 return np.array(input_tokens), np.array(input_masks), np.array(input_segments )
effnet_model = EfficientNet.from_name("efficientnet-b5", override_params={'num_classes': 2})
 learner = Learner(data, effnet_model); learner.model_dir = '.'
learner.load('.. /input/deepfakerandmergeaugmodels/single_frame_effnetb5_randmerge')
 effnetb5_inference_model = learner.model.eval()<load_pretrained>
def build_model(bert_layer, max_len=512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 pooled_output, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 cls_output = sequence_output[:,0,:]
output = Dense(1, activation='sigmoid' )(cls_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=output)
 model.compile(Adam(lr=1e-5), loss = 'binary_crossentropy',metrics=['accuracy'])
 return model
effnet_model = EfficientNet.from_name("efficientnet-b7", override_params={'num_classes': 2})
 learner = Learner(data, effnet_model); learner.model_dir = '.'
learner.load('.. /input/deepfakerandmergeaugmodels/single_frame_effnetb7_randmerge_fp16')
 effnetb7_inference_model = learner.model.float().eval()<load_pretrained>
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )
learner = cnn_learner(data, models.resnet34, pretrained=False); learner.model_dir = '.'
learner.load('.. /input/deepfakerandmergeaugmodels/single_frame_resnet34_randmerge')
 resnet_inference_model = learner.model.eval()<define_variables>
train_text = df_all[:train.shape[0]].text
test_text = df_all[train.shape[0]:].text
train_input = bert_encode(train_text, tokenizer, max_len=160)
 test_input = bert_encode(test_text, tokenizer, max_len = 160)
 train_labels = train.target.values
predictions = []
video_fnames = []<define_variables>
checkpoint = ModelCheckpoint('model.h5', monitor='val_loss', save_best_only=True)
 train_history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=5,
callbacks=[checkpoint],
batch_size=16
)
fname2pred = dict(zip(video_fnames, predictions))<load_from_csv>
model.load_weights('model.h5')
 test_pred = model.predict(test_input )
submission_df = pd.read_csv("/kaggle/input/deepfake-detection-challenge/sample_submission.csv" )<categorify>
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('bert_submission.csv', index=False )