Kaggle competition: Google QUEST Q&A Labeling
Image('.. /input/deepfake-kernel-data/google_cloud_vm.png' )<define_variables>
train = pd.read_csv(".. /input/google-quest-challenge/train.csv", index_col='qa_id')
 train.shape
Image('.. /input/deepfake-kernel-data/lr_15e-2_epochs_42_patience_5.png' )<define_variables>
test = pd.read_csv(".. /input/google-quest-challenge/test.csv", index_col='qa_id')
 test.shape
Image('.. /input/deepfake-kernel-data/lr_2e-3_epochs_10_patience_5.png' )<define_variables>
target_columns = [
'question_asker_intent_understanding',
'question_body_critical',
'question_conversational',
'question_expect_short_answer',
'question_fact_seeking',
'question_has_commonly_accepted_answer',
'question_interestingness_others',
'question_interestingness_self',
'question_multi_intent',
'question_not_really_a_question',
'question_opinion_seeking',
'question_type_choice',
'question_type_compare',
'question_type_consequence',
'question_type_definition',
'question_type_entity',
'question_type_instructions',
'question_type_procedure',
'question_type_reason_explanation',
'question_type_spelling',
'question_well_written',
'answer_helpful',
'answer_level_of_information',
'answer_plausible',
'answer_relevance',
'answer_satisfaction',
'answer_type_instructions',
'answer_type_procedure',
'answer_type_reason_explanation',
'answer_well_written'
]
Image('.. /input/deepfake-kernel-data/lr_2e-3_epochs_20_patience_5.png' )<define_variables>
y_train = train[target_columns].copy()
x_train = train.drop(target_columns, axis=1)
 del train
x_test = test.copy()
del test
Image('.. /input/deepfake-kernel-data/lr_4e-3_epochs_12_patience_2.png' )<define_variables>
import tensorflow_hub as hub
import tensorflow as tf
Image('.. /input/deepfake-kernel-data/lr_4e-3_epochs_30_patience_2.png' )<define_variables>
copyfile(src = ".. /input/tf-bert-tokenization/tokenization.py", dst = ".. /working/tokenization.py")
Image('.. /input/deepfake-kernel-data/google_cloud_vm_deepfake_training_screenshot.png' )<set_options>
BERT = '.. /input/bert-model'
tokenizer = FullTokenizer(BERT + '/assets/vocab.txt', True )
%matplotlib inline
warnings.filterwarnings("ignore" )<define_variables>
tokenizer.tokenize('Hello world from BERT FullTokenizer!' )
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>
def _get_masks(tokens, max_seq_length):

 if len(tokens)>max_seq_length:
raise IndexError("Token length more than max seq length!")
 return [1]*len(tokens)+ [0] *(max_seq_length - len(tokens))
def _get_segments(tokens, max_seq_length):

 if len(tokens)>max_seq_length:
raise IndexError("Token length more than max seq length!")
 segments = []
first_sep = True
current_segment_id = 0
for token in tokens:
segments.append(current_segment_id)
 if token == "[SEP]":
if first_sep:
first_sep = False
else:
current_segment_id = 1
return segments + [0] *(max_seq_length - len(tokens))
def _get_ids(tokens, tokenizer, max_seq_length):

 token_ids = tokenizer.convert_tokens_to_ids(tokens)
 input_ids = token_ids + [0] *(max_seq_length-len(token_ids))
return input_ids
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )<set_options>
def trim_tokens(t, q, a, max_t, max_q, max_a):
if(len(t)+ len(q)+ len(a)) >(max_t + max_q + max_a):
_max_t = max_t
_max_q = max_q
_max_a = max_a
if len(t)> _max_t:
t = t[:_max_t]
else:
x =(_max_t - len(t)) / 2.
 _max_q += math.ceil(x)
 _max_a += math.floor(x)
 if len(q)> _max_q:
q = q[:_max_q]
else:
_max_a +=(_max_q - len(q))
if len(a)> _max_a:
a = a[:_max_a]
return t,q,a
def make_bert_input(x, max_question_title_length=24, max_question_body_length=242, max_answer_length=242):
max_sequence_length = max_question_title_length + max_question_body_length + max_answer_length + 4
print('Calculated max sequence length:', max_sequence_length)
 input_ids = []
input_masks = []
input_segments = []
for idx, row in x[['question_title', 'question_body', 'answer']].iterrows() :
t,q,a = trim_tokens(tokenizer.tokenize(row.question_title),
tokenizer.tokenize(row.question_body),
tokenizer.tokenize(row.answer),
max_question_title_length,
max_question_body_length,
max_answer_length)
 tokens = ['[CLS]'] + t + ['[SEP]'] + q + ['[SEP]'] + a + ['[SEP]']
input_ids.append(_get_ids(tokens, tokenizer, max_sequence_length))
input_masks.append(_get_masks(tokens, max_sequence_length))
input_segments.append(_get_segments(tokens, max_sequence_length))
return [
np.asarray(input_ids, dtype=np.int32),
np.asarray(input_masks, dtype=np.int32),
np.asarray(input_segments, dtype=np.int32)
 ]
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu<load_pretrained>
max_sequence_length = 512
facedet = BlazeFace().to(gpu)
 facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
 facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
 _ = facedet.train(False )<load_pretrained>
def make_model() :
input_word_ids = tf.keras.layers.Input(shape=(max_sequence_length,), dtype=tf.int32,
name="input_word_ids")
 input_mask = tf.keras.layers.Input(shape=(max_sequence_length,), dtype=tf.int32,
name="input_mask")
 segment_ids = tf.keras.layers.Input(shape=(max_sequence_length,), dtype=tf.int32,
name="segment_ids")
 bert_layer = hub.KerasLayer(BERT, trainable=True)
 pooled_output, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 tmp = tf.keras.layers.GlobalAveragePooling1D()(sequence_output)
 tmp = tf.keras.layers.Dropout(0.2 )(tmp)
 out = tf.keras.layers.Dense(len(target_columns), activation='sigmoid', name='dense_output' )(tmp)
 model = tf.keras.models.Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5))
return model
frames_per_video = 64
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 )<define_variables>
def mean_spearmanr_correlation_score(y_true, y_pred):
return np.mean([spearmanr(y_pred[:, idx] + np.random.normal(0, 1e-7, y_pred.shape[0]),
y_true[:, idx] ).correlation for idx in range(len(target_columns)) ] )
input_size = 224<normalization>
trained_estimators = []
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )<choose_model_class>
n_splits = 5
scores = []
cv = KFold(n_splits=n_splits, random_state=42)
 idx = 1
for train_idx, valid_idx in cv.split(x_train, y_train, groups=x_train.question_body):
x_train_train = x_train.iloc[train_idx]
y_train_train = y_train.iloc[train_idx]
x_train_valid = x_train.iloc[valid_idx]
y_train_valid = y_train.iloc[valid_idx]
K.clear_session()
estimator = make_model()
estimator.fit(make_bert_input(x_train_train), y_train_train, batch_size=8, epochs=5)
 trained_estimators.append(estimator)
 oof_part = estimator.predict(make_bert_input(x_train_valid))
score = mean_spearmanr_correlation_score(y_train_valid.values, oof_part)
 print('Score:', score)
 if not math.isnan(score):
scores.append(score)
 idx += 1
if idx > 3:
break
print('Mean score:', np.mean(scores))
class MyResNeXt(models.resnet.ResNet):
def __init__(self, training=True):
super(MyResNeXt, 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>
y_pred = []
for estimator in trained_estimators:
y_pred.append(estimator.predict(make_bert_input(x_test)) )
checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
 model = MyResNeXt().to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint<predict_on_test>
def blend_by_ranking(data, weights):
out = np.zeros(data.shape[0])
 for idx,column in enumerate(data.columns):
out += weights[idx] * rankdata(data[column].values)
 out /= np.max(out)
 return out
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.481<predict_on_test>
submission = pd.read_csv(".. /input/google-quest-challenge/sample_submission.csv", index_col='qa_id')
 out = pd.DataFrame(index=submission.index)
 for column_idx,column in enumerate(target_columns):
column_data = pd.DataFrame(index=submission.index)
 for prediction_idx,prediction in enumerate(y_pred):
column_data[str(prediction_idx)] = prediction[:, column_idx]
out[column] = blend_by_ranking(column_data, np.ones(column_data.shape[1]))
<define_variables><EOS> 
out.to_csv("submission.csv" )