Kaggle competition: Google QUEST Q&A Labeling
model = models.resnet152
WORK_DIR = os.getcwd()
IMAGE_DIR = Path('.. /input/')
 image_size=224
batch_size=32<load_pretrained>
krig.seed_everything()
fnames = get_image_files(IMAGE_DIR/'train')
 fnames[:5]<load_pretrained>
pd.set_option('use_inf_as_na', True)
 pd.set_option('display.max_columns', 999)
 pd.set_option('display.max_rows', 999 )
pattern_get_class = re.compile(r'/([^/]+)\.\d+\.jpg$')
 data = ImageDataBunch.from_name_re(path = IMAGE_DIR,
fnames = fnames,
pat = pattern_get_class,
ds_tfms=get_transforms() ,
test ='test',
size=image_size,
bs=batch_size,
num_workers=0 ).normalize(imagenet_stats)
 data<define_variables>
IS_KAGGLE = True
QUESTION_MODEL_NAME = 'question_bert_base_uncased_20200210_191831'
ANSWER_MODEL_NAME = 'answer_bert_base_uncased_20200210_202316'
MAX_SEQUENCE_LENGTH = 512
STRIDE = 50
WINDOW_LENGTH = 100
QUESTION_LABELS = [
'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_LABELS = [
'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'
]
QUESTION_FIRST_SEQUENCE = ['category', 'question_title']
ANSWER_FIRST_SEQUENCE = ['category', 'question_title']
QUESTION_SECOND_SEQUENCE = ['question_body']
ANSWER_SECOND_SEQUENCE = ['answer']
BASE_DIR = '.. /resources'
if IS_KAGGLE:
BASE_DIR = '.. /input/googlequestchallenge/kaggle-google-quest-challenge-1.0/resources'
data.show_batch(rows=3 )<choose_model_class>
corrs = pd.read_csv(f'{BASE_DIR}/{QUESTION_MODEL_NAME}/corrs.csv')
 print(f'q mean(corr)={corrs["corr"].mean() :.4f}')
 corrs.head(len(QUESTION_LABELS))
learn = create_cnn(data, model, metrics=accuracy, model_dir=WORK_DIR )<train_model>
corrs = pd.read_csv(f'{BASE_DIR}/{ANSWER_MODEL_NAME}/corrs.csv')
 print(f'a mean(corr)={corrs["corr"].mean() :.4f}')
 corrs.head(len(ANSWER_LABELS))
show_doc(learn.fit_one_cycle )<train_model>
hist = pd.read_csv(f'{BASE_DIR}/{QUESTION_MODEL_NAME}/history.csv')
 hist.head(len(hist))
learn.fit_one_cycle(1 )<save_model>
hist = pd.read_csv(f'{BASE_DIR}/{ANSWER_MODEL_NAME}/history.csv')
 hist.head(len(hist))
learn.save('stage-1' )<find_best_params>
%%time
test = pd.read_csv('.. /input/google-quest-challenge/test.csv')
 test.info()
learn.unfreeze()
learn.lr_find()
learn.recorder.plot()<train_model>
%%time
tokenizer = BertTokenizer.from_pretrained(f'{BASE_DIR}/{QUESTION_MODEL_NAME}')
 ds = gqc.Dataset(key_column='qa_id')
 ds.preprocess(test, tokenizer, first_seq_columns=QUESTION_FIRST_SEQUENCE,
second_seq_columns=QUESTION_SECOND_SEQUENCE,
max_sequence_length=MAX_SEQUENCE_LENGTH, window_length=WINDOW_LENGTH, stride=STRIDE)
 x_test = ds.inputs()
input_ids = pd.DataFrame(x_test[0])
 input_ids.head(10 )
learn.load('stage-1')
 learn.unfreeze()
learn.fit_one_cycle(4, max_lr=slice(1e-6,1e-4))<save_model>
%%time
y_pred = model.predict(x_test)
 print(f'y_pred.shape={np.shape(y_pred)}' )
learn.save('stage-2' )<choose_model_class>
q = pd.DataFrame(y_pred, columns=QUESTION_LABELS)
 q['qa_id'] = ds.df['qa_id'].values
q = q.groupby(['qa_id'], as_index=False)[QUESTION_LABELS].median()
q.info()
interp = ClassificationInterpretation.from_learner(learn )<import_modules>
%%time
tokenizer = BertTokenizer.from_pretrained(f'{BASE_DIR}/{ANSWER_MODEL_NAME}')
 ds = gqc.Dataset(key_column='qa_id')
 ds.preprocess(test, tokenizer, first_seq_columns=ANSWER_FIRST_SEQUENCE,
second_seq_columns=ANSWER_SECOND_SEQUENCE,
max_sequence_length=MAX_SEQUENCE_LENGTH, window_length=WINDOW_LENGTH, stride=STRIDE)
 x_test = ds.inputs()
input_ids = pd.DataFrame(x_test[0])
 input_ids.head(10 )
if 'DatasetType' not in sys.modules: 
<predict_on_test>
%%time
y_pred = model.predict(x_test)
 print(f'y_pred.shape={np.shape(y_pred)}' )
try:
preds, _ = learn.TTA(ds_type=DatasetType.Test)
 except:
preds, _ = learn.get_preds(DatasetType.Test)
 else:
print('Predict with TTA done.' )<define_variables>
a = pd.DataFrame(y_pred, columns=ANSWER_LABELS)
 a['qa_id'] = ds.df['qa_id'].values
a = a.groupby(['qa_id'], as_index=False)[ANSWER_LABELS].median()
a.info()
print(data.classes)
 dict_label_order = {label:order for order,label in enumerate(data.classes)}
print(dict_label_order )<sort_values>
qa = pd.concat([q, a], axis=1)
 qa.head()
n_dogs = dict_label_order['dog']
n_dogs<data_type_conversions>
sub = pd.read_csv('.. /input/google-quest-challenge/sample_submission.csv')
 sub.iloc[:, 1:] = qa[QUESTION_LABELS + ANSWER_LABELS].values
gqc.check_submission(sub, shape=(476, 31), exclude={'qa_id'})
 sub.head()
prob_dogs = preds[:,n_dogs].numpy()
prob_dogs<define_variables>
sub.to_csv('submission.csv', index=False )
ids = [int(file.stem)for file in data.test_ds.x.items]
ids[:10]<create_dataframe>
sub.to_csv('submission.csv', index=False )
<save_to_csv><EOS> 
!pip list