Kaggle competition: Google QUEST Q&A Labeling
<SOS> metric: MCSpearmanR Kaggle data source: google-quest-q&a-labeling<normalization>
import random
import html
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from tqdm.notebook import tqdm
import tensorflow as tf
import tensorflow.keras.backend as K
import os
from scipy.stats import spearmanr
from scipy.optimize import minimize
from transformers import *
from tensorflow.keras.layers import Flatten, Dense, Dropout, GlobalAveragePooling1D
from tensorflow.keras.models import Model
from sklearn.model_selection import KFold
from scipy.stats import spearmanr
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )<normalization>
%matplotlib inline
Image('.. /input/google-qna-ensemble-architecture/architecture.png' )
def disable_grad(model):
for parameter in model.parameters() :
parameter.requires_grad = False
return model
def normalize(img):
y, x, _ = img.shape
if y > x and x < 256:
ratio_x = x / y
ratio_y = y / x
return cv2.resize(img,(256, int(ratio_y * 256)))
 elif y < x and y < 256:
ratio_x = x / y
ratio_y = y / x
return cv2.resize(img,(int(ratio_x * 256), 256))
else:
return cv2.resize(img,(256, 256))
def weight_preds(preds, weights):
final_preds = []
for i in range(len(preds)) :
for j in range(len(preds[i])) :
if len(final_preds)!= len(preds[i]):
final_preds.append(preds[i][j] * weights[i])
 else:
final_preds[j] += preds[i][j] * weights[i]
return torch.FloatTensor(final_preds)
 def predict_faces(models, x, weigths, n):
x = torch.tensor(x, device=device ).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 = 0
preds = []
for i in range(len(models)) :
preds.append(models[i](x ).squeeze() [:n])
 del x
y_pred = torch.sigmoid(weight_preds(preds, weigths)).mean().item()
return y_pred<load_pretrained>
warnings.filterwarnings('ignore')
 os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
frames_per_video = 32
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>
seed = 13
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed )
class MetaModel(nn.Module):
def __init__(self, models=None, device='cuda:0', extended=False):
super(MetaModel, self ).__init__()
self.extended = extended
self.device = device
self.models = models
self.len = len(models)
 if self.extended:
self.bn = nn.BatchNorm1d(self.len)
 self.relu = nn.ReLU()
self.dropout = nn.Dropout(0.2)
 self.fc = nn.Linear(self.len, 1)
 def forward(self, x):
x = torch.cat(tuple(x), dim=1)
 if self.extended:
x = self.bn(x)
 x = self.relu(x)
 x = self.fc(x)
 return x<define_variables>
def get_data() :
print('getting test and train data...')
 path = '.. /input/google-quest-challenge/'
train = pd.read_csv(path+'train.csv')
 test = pd.read_csv(path+'test.csv')
 submission = pd.read_csv(path+'sample_submission.csv')
 y = train[train.columns[11:]]
X = train[['question_title', 'question_body', 'answer']]
X_test = test[['question_title', 'question_body', 'answer']]
X.question_body = X.question_body.apply(html.unescape)
 X.question_title = X.question_title.apply(html.unescape)
 X.answer = X.answer.apply(html.unescape)
 X_test.question_body = X_test.question_body.apply(html.unescape)
 X_test.question_title = X_test.question_title.apply(html.unescape)
 X_test.answer = X_test.answer.apply(html.unescape)
 return X, X_test, y, train, test
MODELS_PATH = "/kaggle/input/deepfake-detection-model-20k/"
WEIGTHS_EXT = '.pth'
models = []
weigths = []
raw_data_stack = \
[
['0.8548137313946486 0.3376769562025044', 'efficientnet-b2'],
['EfficientNetb3 0.8573518024606384 0.34558522378585194', 'efficientnet-b3'],
['EfficientNetb4 0.8579110384582294 0.3383911053075265', 'efficientnet-b4'],
['EfficientNet6 0.8602770369095758 0.33193617861157143', 'efficientnet-b6'],
['EfficientNetb0 t2 0.8616966359803837 0.3698434531609828', 'efficientnet-b0'],
['EfficientNetb1 t2 0.8410909403768391 0.36058002083572327', 'efficientnet-b1'],
['EfficientNetb2 t2 0.8659554331928073 0.35598630783834084', 'efficientnet-b2'],
['EfficientNetb3 t2 0.8486191172674868 0.3611779548592305', 'efficientnet-b3'],
['EfficientNetb3 0.8635894347414609 0.328333642473084', 'efficientnet-b3'],
['EfficientNetb6 0.8593736556826981 0.32286693639934694', 'efficientnet-b6'],
['tf_efficientnet_b1_ns 0.8571367116923342 0.3341234226295108', 'tf_efficientnet_b1_ns'],
['tf_efficientnet_b3_ns 0.8712466660930913 0.3277394129117183', 'tf_efficientnet_b3_ns'],
['tf_efficientnet_b4_ns 0.8708595027101437 0.3152573955405342', 'tf_efficientnet_b4_ns'],
['tf_efficientnet_b6_ns 0.8733115374688118 0.3156576980666498', 'tf_efficientnet_b6_ns'],
]
stack_models = []
for raw_model in raw_data_stack:
checkpoint = torch.load(MODELS_PATH + raw_model[0] + WEIGTHS_EXT, map_location=device)
 if '-' in raw_model[1]:
model = EfficientNet.from_name(raw_model[1])
 model._fc = nn.Linear(model._fc.in_features, 1)
 else:
model = timm.create_model(raw_model[1], pretrained=False)
 model.classifier = nn.Linear(model.classifier.in_features, 1)
 model.load_state_dict(checkpoint)
 _ = model.eval()
_ = disable_grad(model)
 model = model.to(device)
 stack_models.append(model)
 del checkpoint, model
meta_models = \
[
['MetaModel 0.30638167556896007', slice(4, 8), False, 0.37780],
['MetaModel 0.2919331893755284', slice(0, 4), False, 0.33357],
['MetaModel 0.30281482560578044', slice(0, 8, None), True, 0.34077],
['MetaModel 0.26302117601197256', slice(0, 10, None), False, 0.35134],
['MetaModel 0.256337642808031', slice(10, 14, None), False, 0.32698],
['MetaModel 0.264787397152165', slice(0, 14, None), False, 0.34974]
]
for meta_raw in meta_models:
checkpoint = torch.load(MODELS_PATH + meta_raw[0] + WEIGTHS_EXT, map_location=device)
 model = MetaModel(models=raw_data_stack[meta_raw[1]], extended=meta_raw[2] ).to(device)
 model.load_state_dict(checkpoint)
 _ = model.eval()
_ = disable_grad(model)
 model.to(device)
 models.append(model)
 weigths.append(meta_raw[3])
 del model, checkpoint
total = sum([1-score for score in weigths])
 weigths = [(1-score)/ total for score in weigths] 
<categorify>
def get_tokenizer(model_name):
print(f'getting tokenizer for {model_name}...')
 if model_name == 'xlnet-base-cased':
tokenizer = XLNetTokenizer.from_pretrained('xlnet-base-cased')
 elif model_name == 'roberta-base':
tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
 elif model_name == 'bert-base-uncased':
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
 return tokenizer
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 = normalize(face)
 resized_face = torchvision.transforms.CenterCrop(( input_size, input_size))(Image.fromarray(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))
del faces
if n > 0:
x = torch.tensor(x, device=device ).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 = 0
stacked_preds = []
preds = []
for i in range(len(stack_models)) :
stacked_preds.append(stack_models[i](x ).squeeze() [:n].unsqueeze(dim=1))
for i in range(len(models)) :
preds.append(models[i](stacked_preds[meta_models[i][1]]))
del x, stacked_preds
y_pred = torch.sigmoid(weight_preds(preds, weigths)).mean().item()
del preds
return y_pred
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.5<predict_on_test>
def fix_length(tokens, max_sequence_length=512, q_max_len=254, a_max_len=254, model_type='questions'):
if model_type == 'questions':
length = len(tokens)
 if length > max_sequence_length:
tokens = tokens[:max_sequence_length-1]
return tokens
else:
question_tokens, answer_tokens = tokens
q_len = len(question_tokens)
 a_len = len(answer_tokens)
 if q_len + a_len + 3 > max_sequence_length:
if a_max_len <= a_len and q_max_len <= q_len:
q_new_len_head = q_max_len//2
question_tokens = question_tokens[:q_new_len_head] + question_tokens[-q_new_len_head:]
a_new_len_head = a_max_len//2
answer_tokens = answer_tokens[:a_new_len_head] + answer_tokens[-a_new_len_head:]
elif q_len <= a_len and q_len < q_max_len:
a_max_len = a_max_len +(q_max_len - q_len - 1)
 a_new_len_head = a_max_len//2
answer_tokens = answer_tokens[:a_new_len_head] + answer_tokens[-a_new_len_head:]
elif a_len < q_len:
q_max_len = q_max_len +(a_max_len - a_len - 1)
 q_new_len_head = q_max_len//2
question_tokens = question_tokens[:q_new_len_head] + question_tokens[-q_new_len_head:]
return question_tokens, answer_tokens
def predict_on_video_single(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 = cv2.resize(face,(input_size, input_size))
if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
del faces
if n > 0:
x = torch.tensor(x, device=device ).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() :
stacked_preds = []
preds = []
for i in range(len(stack_models)) :
stacked_preds.append(stack_models[i](x ).squeeze() [:n].unsqueeze(dim=1))
del x
y_pred = torch.sigmoid(models[-1](stacked_preds)).mean().item()
return y_pred
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.5<categorify>
def transformer_inputs(title, question, answer, tokenizer, model_type='questions', MAX_SEQUENCE_LENGTH = 512):
if model_type == 'questions':
question = f"{title} [SEP] {question}"
question_tokens = tokenizer.tokenize(question)
 question_tokens = fix_length(question_tokens, model_type=model_type)
 ids_q = tokenizer.convert_tokens_to_ids(["[CLS]"] + question_tokens)
 padded_ids =(ids_q + [tokenizer.pad_token_id] *(MAX_SEQUENCE_LENGTH - len(ids_q)))[:MAX_SEQUENCE_LENGTH]
token_type_ids =([0] * MAX_SEQUENCE_LENGTH)[:MAX_SEQUENCE_LENGTH]
attention_mask =([1] * len(ids_q)+ [0] *(MAX_SEQUENCE_LENGTH - len(ids_q)))[:MAX_SEQUENCE_LENGTH]
return padded_ids, token_type_ids, attention_mask
else:
question = f"{title} [SEP] {question}"
question_tokens = tokenizer.tokenize(question)
 answer_tokens = tokenizer.tokenize(answer)
 question_tokens, answer_tokens = fix_length(tokens=(question_tokens, answer_tokens), model_type=model_type)
 ids = tokenizer.convert_tokens_to_ids(["[CLS]"] + question_tokens + ["[SEP]"] + answer_tokens + ["[SEP]"])
 padded_ids = ids + [tokenizer.pad_token_id] *(MAX_SEQUENCE_LENGTH - len(ids))
token_type_ids = [0] *(1 + len(question_tokens)+ 1)+ [1] *(len(answer_tokens)+ 1)+ [0] *(MAX_SEQUENCE_LENGTH - len(ids))
attention_mask = [1] * len(ids)+ [0] *(MAX_SEQUENCE_LENGTH - len(ids))
return padded_ids, token_type_ids, attention_mask
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>
def input_data(df, tokenizer, model_type='questions'):
print(f'generating {model_type} input for transformer...')
 input_ids, input_token_type_ids, input_attention_masks = [], [], []
for title, body, answer in tqdm(zip(df["question_title"].values, df["question_body"].values, df["answer"].values)) :
ids, type_ids, mask = transformer_inputs(title, body, answer, tokenizer, model_type=model_type)
 input_ids.append(ids)
 input_token_type_ids.append(type_ids)
 input_attention_masks.append(mask)
 return(
 np.asarray(input_ids, dtype=np.int32),
np.asarray(input_attention_masks, dtype=np.int32),
np.asarray(input_token_type_ids, dtype=np.int32))
speed_test = False<predict_on_test>
def get_model(name):
if name == 'xlnet-base-cased':
config = XLNetConfig.from_pretrained('xlnet-base-cased', output_hidden_states=True)
 model = TFXLNetModel.from_pretrained('xlnet-base-cased', config=config)
 elif name == 'roberta-base':
config = RobertaConfig.from_pretrained('roberta-base', output_hidden_states=True)
 model = TFRobertaModel.from_pretrained('roberta-base', config=config)
 elif name == 'bert-base-uncased':
config = BertConfig.from_pretrained('bert-base-uncased', output_hidden_states=True)
 model = TFBertModel.from_pretrained('bert-base-uncased', config=config)
 return model
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 min.Average per video: %f sec." %(elapsed / 60, elapsed / len(speedtest_videos)) )<predict_on_test>
def create_model(name='xlnet-base-cased', model_type='questions'):
print(f'creating model {name}...')
 K.clear_session()
max_seq_length = 512
input_tokens = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32, name="input_tokens")
 input_mask = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32, name="input_mask")
 input_segment = tf.keras.layers.Input(shape=(max_seq_length,), dtype=tf.int32, name="input_segment")
 model = get_model(name)
 if(name == 'xlnet-base-cased'):
sequence_output, hidden_states = model([input_tokens, input_mask])
 elif(name=='roberta-base' and model_type!='questions'):
sequence_output, pooler_output, hidden_states = model([input_tokens, input_mask])
 else:
sequence_output, pooler_output, hidden_states = model([input_tokens, input_mask, input_segment])
 h12 = tf.reshape(hidden_states[-1][:,0],(-1,1,768))
h11 = tf.reshape(hidden_states[-2][:,0],(-1,1,768))
h10 = tf.reshape(hidden_states[-3][:,0],(-1,1,768))
h09 = tf.reshape(hidden_states[-4][:,0],(-1,1,768))
concat_hidden = tf.keras.layers.Concatenate(axis=2 )([h12, h11, h10, h09])
 x = GlobalAveragePooling1D()(concat_hidden)
 x = Dropout(0.2 )(x)
 if model_type == 'answers':
output = Dense(9, activation='sigmoid' )(x)
 elif model_type == 'questions':
output = Dense(21, activation='sigmoid' )(x)
 else:
output = Dense(30, activation='sigmoid' )(x)
 if(name == 'xlnet-base-cased')or(name=='roberta-base' and model_type!='questions'):
model = Model(inputs=[input_tokens, input_mask], outputs=output)
 else:
model = Model(inputs=[input_tokens, input_mask, input_segment], outputs=output)
 return model
predictions = predict_on_video_set(test_videos, num_workers=4 )<save_to_csv>
class data_generator:
def __init__(self, X, X_test, tokenizer, type_):
tokens, masks, segments = input_data(X_test, tokenizer, type_)
 self.test_data = {'input_tokens': tokens,
'input_mask': masks,
'input_segment': segments}
self.tokens, self.masks, self.segments = input_data(X, tokenizer, type_)
 def generate_data(tr, cv, name='xlnet-base-cased', model_type='questions'):
if name!='xlnet-base-cased':
train_data = {'input_tokens': self.tokens[tr],
'input_mask': self.masks[tr],
'input_segment': self.segments[tr]}
cv_data = {'input_tokens': self.tokens[cv],
'input_mask': self.masks[cv],
'input_segment': self.segments[cv]}
else:
train_data = {'input_tokens': self.tokens[tr],
'input_mask': self.masks[tr]}
cv_data = {'input_tokens': self.tokens[cv],
'input_mask': self.masks[cv]}
if model_type=='questions':
y_tr = y.values[tr, 21:]
y_cv = y.values[cv, 21:]
elif model_type=='answers':
y_tr = y.values[tr, 21:]
y_cv = y.values[cv, 21:]
else:
y_tr = y.values[tr]
y_cv = y.values[cv]
return train_data, cv_data, y_tr, y_cv
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )<install_modules>
def optimize_ranks(preds, unique_labels):
print(f'optimizing the predicted values...')
 new_preds = np.zeros(preds.shape)
 for i in range(preds.shape[1]):
interpolate_bins = np.digitize(preds[:, i], bins=unique_labels, right=False)
 if len(np.unique(interpolate_bins)) == 1:
new_preds[:, i] = preds[:, i]
else:
new_preds[:, i] = unique_labels[interpolate_bins]
return new_preds
!rm -r reader && rm install.sh<install_modules>
def get_exp_labels(train):
X = train.iloc[:, 11:]
unique_labels = np.unique(X.values)
 denominator = 60
q = np.arange(0, 101, 100 / denominator)
 exp_labels = np.percentile(unique_labels, q)
 return exp_labels
%%capture
!pip install /kaggle/input/facenet-pytorch-vggface2/facenet_pytorch-2.2.9-py3-none-any.whl
!pip install /kaggle/input/xt-training/pynvml-8.0.4-py3-none-any.whl
!pip install /kaggle/input/xt-training/xt_training-1.4.0-py3-none-any.whl
!pip install /kaggle/input/imageio-ffmpeg/imageio_ffmpeg-0.3.0-py3-none-manylinux2010_x86_64.whl
!pip install /kaggle/input/imutils/imutils-0.5.3/
!cp -R /kaggle/input/xtract-ai-dfdc/dfdc./<import_modules>
def compute_spearmanr_ignore_nan(trues, preds):
rhos = []
for tcol, pcol in zip(np.transpose(trues), np.transpose(preds)) :
rhos.append(spearmanr(tcol, pcol ).correlation)
 return np.nanmean(rhos )
import os
import glob
import numpy as np
import pandas as pd
import torch
from torch.utils.data import DataLoader, Subset
from torch import optim
from tqdm.notebook import tqdm
from matplotlib import pyplot as plt
import albumentations as A
from tqdm.notebook import tqdm
from xt_training import metrics, Runner
from xt_training.runner import Logger
from dfdc.datasets.video_dataset import VideoDataset
from dfdc.models.video_models import FaceSequenceClassifier, FaceClassifier<train_model>
def rhos(y, y_pred):
return tf.py_function(compute_spearmanr_ignore_nan,(y, y_pred), tf.double )
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
 print('Running on device: {}'.format(device))<load_pretrained>
def fit_model(model, model_name, model_type, data_gen, file_path, train, use_saved_weights=True):
path = '.. /input/google-qna-predicted-data/'
if use_saved_weights:
print(f'getting saved weights for {model_name}...')
 model.load_weights(path+file_path)
 else:
print(f'fitting data on {model_name}...')
 optimizer = tf.keras.optimizers.Adam(learning_rate=0.00002)
 model.compile(loss='binary_crossentropy', optimizer=optimizer, metrics=[rhos])
 kf = KFold(n_splits=5, random_state=42)
 for tr, cv in kf.split(np.arange(train.shape[0])) :
tr_data, cv_data, y_tr, y_cv = data_gen.generate_data(tr, cv, model_name, model_type)
 model.fit(tr_data, y_tr, epochs=1, batch_size=4, validation_data=(cv_data, y_cv))
model.save_weights(file_path)
 return model
class Ensemble(torch.nn.Module):
def __init__(self, unpack=None, permute=False, mapping=None, **kwargs):
super().__init__()
self.permute = permute
self.mapping = mapping
if unpack is None:
self.unpack = lambda x, i: x
else:
self.unpack = unpack
for name, model in kwargs.items() :
self.add_module(name, model)
 def forward(self, x):
out = []
for i, layer in enumerate(self._modules.values()):
if self.mapping is None:
in_i = i
else:
in_i = self.mapping[i]
x_i = self.unpack(x, in_i)
 out.append(layer(x_i))
out = torch.stack(out)
 if self.permute:
out = out.permute(( 1, 0, 2))
return out<load_pretrained>
def get_weighted_avg(model_predictions):
xlnet_q, xlnet_a, roberta_q, roberta_a, roberta_qa, bert_q, bert_a, bert_qa = model_predictions
xlnet_concat = np.concatenate(( xlnet_q, xlnet_a), axis=1)
 bert_concat = np.concatenate(( bert_q, bert_a), axis=1)
 roberta_concat = np.concatenate(( roberta_q, roberta_a), axis=1)
 predict =(roberta_qa + bert_qa + xlnet_concat + bert_concat + roberta_concat)/5
return predict
face_model1 = FaceClassifier(pretrained=False, base_model='resnext')
 face_model1.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/face_model_best_alltrain_moreaug_20200317.pt'))
face_model1.classifier.fc = torch.nn.Sequential()
face_model2 = FaceClassifier(pretrained=False, base_model='resnet')
 face_model2.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/face_model_best_lessaug_resnet101_20200321.pt'))
face_model2.classifier.fc = torch.nn.Sequential()
face_model3 = FaceClassifier(pretrained=False, base_model='resnext')
 face_model3.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/face_model_best_20200314.pt'))
face_model3.classifier.fc = torch.nn.Sequential()
face_model4 = FaceClassifier(pretrained=False, base_model='resnext')
 face_model4.load_state_dict(torch.load('/kaggle/input/face-sequence-classifier/face_model_best_20200223.pt'))
face_model4.classifier.fc = torch.nn.Sequential()
face_model = Ensemble(
 unpack=None,
m1=face_model1,
m2=face_model2,
m3=face_model3,
m4=face_model4, 
)
 face_model.to(device)
 face_model.eval()
@torch.no_grad()
def face_model_transform(x):
return face_model(x.to(device))<create_dataframe>
def get_predictions(predictions_present=True, model_saved_weights_present=True):
msw = model_saved_weights_present
X, X_test, y, train, test = get_data()
path = '.. /input/google-qna-predicted-data/'
model_names = ['xlnet-base-cased', 'roberta-base', 'bert-base-uncased']
model_types = ['questions', 'answers', 'questions_answers']
saved_weights_names = ['xlnet_q.h5', 'xlnet_a.h5', 'roberta_q.h5', 'roberta_a.h5',
'roberta_qa.h5', 'bert_q.h5', 'bert_a.h5', 'bert_qa.h5']
saved_model_predictions = [path+'xlnet_q.csv', path+'xlnet_a.csv', path+'roberta_q.csv', path+'roberta_a.csv',
path+'roberta_qa.csv', path+'bert_q.csv', path+'bert_a.csv', path+'bert_qa.csv']
model_predictions = []
if predictions_present:
model_predictions = [pd.read_csv(file_name ).values for file_name in saved_model_predictions]
else:
i = 0
for name_ in model_names:
for type_ in model_types:
if name_ == 'xlnet-base-cased' and type_ == 'questions_answers':
continue
print('-'*100)
 model = create_model(name_, type_)
 tokenizer = get_tokenizer(name_)
 data_gen = data_generator(X, X_test, tokenizer, type_)
 model = fit_model(model, name_, type_, data_gen, saved_weights_names[i], train, msw)
 print(f'getting target predictions from {name_}...')
 model_predictions.append(model.predict(data_gen.test_data))
i+=1
predicted_labels = get_weighted_avg(model_predictions)
 exp_labels = get_exp_labels(train)
 optimized_predicted_labels = optimize_ranks(predicted_labels, exp_labels)
 df = pd.concat([test['qa_id'], pd.DataFrame(optimized_predicted_labels, columns=train.columns[11:])], axis=1)
 print('done...!')
 return df
test_trans = A.ReplayCompose([
A.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)) ,
A.Resize(160, 160, always_apply=True)
 ])
 video_root = '/kaggle/input/deepfake-detection-challenge/'
test_dataset = VideoDataset(
 video_root,
transform=test_trans,
out_transform=face_model_transform,
is_test=True,
sample_frames=-1,
shuffle=False,
stride=10,
n_frames=-1,
device=device,
reader='imutils',
path_include='test_videos/', 
)
 batch_size = 1
num_workers = 0
test_loader = DataLoader(
 test_dataset,
batch_size=batch_size,
num_workers=num_workers
)<load_pretrained>
submission = get_predictions(predictions_present=True)
<define_variables><EOS> 
submission = pd.read_csv('.. /input/google-qna-predicted-data/output.csv')
 sample_submission = pd.read_csv('.. /input/google-quest-challenge/sample_submission.csv')
 sample_submission = pd.read_csv('.. /input/google-quest-challenge/sample_submission.csv')
 id_in_sub = set(submission.qa_id)
 id_in_sample_submission = set(sample_submission.qa_id)
 diff = id_in_sample_submission - id_in_sub
sample_submission = pd.concat([submission, sample_submission[sample_submission.qa_id.isin(diff)]] ).reset_index(drop=True)
 sample_submission.to_csv("submission.csv", index=False )