Kaggle competition: Google QUEST Q&A Labeling
<SOS> metric: MCSpearmanR Kaggle data source: google-quest-q&a-labeling<data_type_conversions>

 pyLDAvis.enable_notebook()
warnings.filterwarnings('ignore' )
pred_class =test_1.round().astype(int )<groupby>
sample = pd.read_csv('/kaggle/input/google-quest-challenge/sample_submission.csv')
 sample.head(3 )
np.sum(pred_class,axis=0 )<train_model>
train = pd.read_csv('/kaggle/input/google-quest-challenge/train.csv')
 train.head(3 )
class DataGenerator(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, list_IDs, df, target_df=None, mode='fit',
base_path=train_path,
batch_size=1, dim=(256, 1600), n_channels=3,
n_classes=4, random_state=1994, shuffle=True):
self.dim = dim
self.batch_size = batch_size
self.df = df
self.mode = mode
self.base_path = base_path
self.target_df = target_df
self.list_IDs = list_IDs
self.n_channels = n_channels
self.n_classes = n_classes
self.shuffle = shuffle
self.random_state = random_state
self.on_epoch_end()
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.list_IDs)/ self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
list_IDs_batch = [self.list_IDs[k] for k in indexes]
X = self.__generate_X(list_IDs_batch)
 if self.mode == 'fit':
y = self.__generate_y(list_IDs_batch)
 return X, y
elif self.mode == 'predict':
return X
else:
raise AttributeError('The mode parameter should be set to "fit" or "predict".')
 def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
np.random.shuffle(self.indexes)
 def __generate_X(self, ID):
'Generates data containing batch_size samples'
img_i = np.empty(( self.batch_size,*self.dim,self.n_channels),dtype=np.uint8)
 for i,index in enumerate(ID):
im_name = self.df['Image_id'].loc[index]
img_path = f"{self.base_path}/{im_name}"
img =cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)
 img_i[i,]=img
return img_i
def __generate_y(self, ID):
img_i = np.empty(( self.batch_size,*self.dim,self.n_classes),dtype=np.uint8)
 for i,index in enumerate(ID):
im_name = self.df['Image_id'].loc[index]
image_df = self.target_df[self.target_df['Image_id'] == im_name]
rles = image_df['EncodedPixels'].values
img =build_masks(rles, input_shape=self.dim)
 img_i[i,]=img
return img_i
def __check_input__(self,index):
X,y=self.__getitem__(index)
 indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
list_IDs_batch = [self.list_IDs[k] for k in indexes]
ID=list_IDs_batch[0]
X=X[0]
y=y[0]
im_name=self.df['Image_id'].loc[ID]
img= cv2.imread(train_path+im_name)
 image_df = self.target_df[self.target_df['Image_id'] == im_name]
rles = image_df['EncodedPixels'].values
masks = build_masks(rles, input_shape=self.dim)
 fig,axis = plt.subplots(nrows=1,ncols=1, figsize=(20, 20))
for i in range(1):
try:
img_i=mask_image(X[i].astype(np.uint8),y[i].astype(np.uint8))
axis[i].imshow(img_i)
 except IndexError:
print("IndexError : so skip")
 img_i=mask_image(X,y)
 axis.imshow(img_i,cmap='gray')
 break
except TypeError:
img_i=mask_image(X,y)
 print("i")
 axis.imshow(img_i,cmap='gray')
 print("TypeError(axis[i] is fails): so skip")
 break
img_i=mask_image(img.astype(np.uint8),masks.astype(np.uint8))
plt.figure(figsize=(10,10))
plt.imshow(img_i )<filter>
test = pd.read_csv('/kaggle/input/google-quest-challenge/test.csv')
 test.head(3 )
non_missing_train_idx = mask_count[mask_count['mask'] > 0] 
<split>
targets = [
'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'
]
BATCH_SIZE = 10
train_idx, val_idx = train_test_split(
 non_missing_train_idx.index,
random_state=25,
test_size=0.2 
)
 train_generator = DataGenerator(
 train_idx,
df=non_missing_train_idx,
target_df=train_df,
batch_size=BATCH_SIZE,
n_classes=4,
mode='fit' 
)
 val_generator = DataGenerator(
 val_idx,
df=non_missing_train_idx,
target_df=train_df,
batch_size=BATCH_SIZE,
n_classes=4 
)
 train_generator.__check_input__(0 )<compute_test_metric>
stopwords=stopwords.words('english')
 train['que_stopwords']=train['question_body'].apply(lambda x : [x for x in x.split() if x in stopwords])
 train['ans_stopwords']=train['answer'].apply(lambda x: [x for x in x.split() if x in stopwords] )
def dice_coef(y_true, y_pred, smooth= 1e-6):
y_pred = K.round(y_pred)
 intersection = K.sum(y_true * y_pred, axis=(1,2))
return K.mean(( 2.* intersection + smooth)/(K.sum(y_true, axis=(1,2)) + K.sum(y_pred, axis=(1,2)) + smooth))
def dice_loss(y_true, y_pred,smooth = 1e-9):
intersection = K.sum(y_true * y_pred, axis=(1,2))
score =(2.* intersection + smooth)/(K.sum(y_true, axis=(1,2)) + K.sum(y_pred, axis=(1,2)) + smooth)
 return K.mean(1-score)
 def bce_dice_loss(y_true, y_pred):
return dice_loss(y_true, y_pred )<compute_test_metric>
def common_ngrams(col,common=10):
corpus=[]
for question in train[col].values:
words=[str(x[0]+' '+x[1])for x in ngrams(question.split() ,2)]
corpus.append(words)
 flatten=[x for one in corpus for x in one]
counter=Counter(flatten)
 most_common=counter.most_common(common)
 string,value=zip(*(most_common))
return string,value
def dice_coef(y_true, y_pred, smooth= 1e-6):
y_pred = K.round(y_pred)
 intersection = K.sum(y_true * y_pred,axis=(1,2))
return K.mean(( 2.* intersection + smooth)/(K.sum(y_true, axis=(1,2)) + K.sum(y_pred, axis=(1,2)) + smooth))
opt=keras.optimizers.Adam(lr=1e-4)
<load_pretrained>
np.set_printoptions(suppress=True )
model_seg = load_model('/kaggle/input/seg-model/seg_model.h5')
 model_seg.load_weights('/kaggle/input/16000-model/1600.h5' )<groupby>
PATH = '.. /input/google-quest-challenge/'
BERT_PATH = '.. /input/bert-base-from-tfhub/bert_en_uncased_L-12_H-768_A-12'
tokenizer = FullTokenizer(BERT_PATH+'/assets/vocab.txt', True)
 MAX_SEQUENCE_LENGTH = 512
df_train = pd.read_csv(PATH+'train.csv')
 df_test = pd.read_csv(PATH+'test.csv')
 df_sub = pd.read_csv(PATH+'sample_submission.csv')
 print('train shape =', df_train.shape)
 print('test shape =', df_test.shape)
 output_categories = list(df_train.columns[11:])
 input_categories = list(df_train.columns[[1,2,5]])
 print('
output categories:
\t', output_categories)
 print('
input categories:
\t', input_categories )
np.sum(pred_class,axis=0 )<feature_engineering>
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
def _trim_input(title, question, answer, max_sequence_length,
t_max_len=30, q_max_len=239, a_max_len=239):
t = tokenizer.tokenize(title)
 q = tokenizer.tokenize(question)
 a = tokenizer.tokenize(answer)
 t_len = len(t)
 q_len = len(q)
 a_len = len(a)
 if(t_len+q_len+a_len+4)> max_sequence_length:
if t_max_len > t_len:
t_new_len = t_len
a_max_len = a_max_len + floor(( t_max_len - t_len)/2)
 q_max_len = q_max_len + ceil(( t_max_len - t_len)/2)
 else:
t_new_len = t_max_len
if a_max_len > a_len:
a_new_len = a_len
q_new_len = q_max_len +(a_max_len - a_len)
 elif q_max_len > q_len:
a_new_len = a_max_len +(q_max_len - q_len)
 q_new_len = q_len
else:
a_new_len = a_max_len
q_new_len = q_max_len
if t_new_len+a_new_len+q_new_len+4 != max_sequence_length:
raise ValueError("New sequence length should be %d, but is %d"
%(max_sequence_length,(t_new_len+a_new_len+q_new_len+4)))
 t = t[:t_new_len]
q = q[:q_new_len]
a = a[:a_new_len]
return t, q, a
def _convert_to_bert_inputs(title, question, answer, tokenizer, max_sequence_length):

 stoken = ["[CLS]"] + title + ["[SEP]"] + question + ["[SEP]"] + answer + ["[SEP]"]
input_ids = _get_ids(stoken, tokenizer, max_sequence_length)
 input_masks = _get_masks(stoken, max_sequence_length)
 input_segments = _get_segments(stoken, max_sequence_length)
 return [input_ids, input_masks, input_segments]
def compute_input_arays(df, columns, tokenizer, max_sequence_length):
input_ids, input_masks, input_segments = [], [], []
for _, instance in tqdm(df[columns].iterrows()):
t, q, a = instance.question_title, instance.question_body, instance.answer
t, q, a = _trim_input(t, q, a, max_sequence_length)
 ids, masks, segments = _convert_to_bert_inputs(t, q, a, tokenizer, max_sequence_length)
 input_ids.append(ids)
 input_masks.append(masks)
 input_segments.append(segments)
 return [np.asarray(input_ids, dtype=np.int32),
np.asarray(input_masks, dtype=np.int32),
np.asarray(input_segments, dtype=np.int32)]
def compute_output_arrays(df, columns):
return np.asarray(df[columns] )
sub_df = pd.read_csv('.. /input/severstal-steel-defect-detection/sample_submission.csv')
 sub_df['Image_id'] = sub_df['ImageId_ClassId'].apply(lambda x: x.split('_')[0])
 test_imgs = pd.DataFrame(sub_df['Image_id'].unique() , columns=['Image_id'])
 test_imgs=test_imgs
filtered_mask = sub_df['Image_id'].isin(test_imgs["Image_id"].values)
 filtered_sub_df = sub_df[filtered_mask].copy()
null_sub_df = sub_df[~filtered_mask].copy()
null_sub_df['EncodedPixels'] = null_sub_df['EncodedPixels'].apply(
 lambda x: ' ')
 filtered_sub_df.reset_index(drop=True, inplace=True)
 test_imgs.reset_index(drop=True, inplace=True)
 print(filtered_sub_df.shape)
 print(null_sub_df.shape)
 filtered_sub_df.head() 
<categorify>
def compute_spearmanr(trues, preds):
rhos = []
for col_trues, col_pred in zip(trues.T, preds.T):
rhos.append(
 spearmanr(col_trues, col_pred + np.random.normal(0, 1e-7, col_pred.shape[0])).correlation)
 return np.nanmean(rhos)
 class CustomCallback(tf.keras.callbacks.Callback):
def __init__(self, valid_data, test_data, batch_size=16, fold=None):
self.valid_inputs = valid_data[0]
self.valid_outputs = valid_data[1]
self.test_inputs = test_data
self.batch_size = batch_size
self.fold = fold
def on_train_begin(self, logs={}):
self.valid_predictions = []
self.test_predictions = []
def on_epoch_end(self, epoch, logs={}):
self.valid_predictions.append(
 self.model.predict(self.valid_inputs, batch_size=self.batch_size))
rho_val = compute_spearmanr(
 self.valid_outputs, np.average(self.valid_predictions, axis=0))
print("
validation rho: %.4f" % rho_val)
 if self.fold is not None:
self.model.save_weights(f'bert-base-{fold}-{epoch}.h5py')
 self.test_predictions.append(
 self.model.predict(self.test_inputs, batch_size=self.batch_size)
)
 def bert_model() :
input_word_ids = tf.keras.layers.Input(
(MAX_SEQUENCE_LENGTH,), dtype=tf.int32, name='input_word_ids')
 input_masks = tf.keras.layers.Input(
(MAX_SEQUENCE_LENGTH,), dtype=tf.int32, name='input_masks')
 input_segments = tf.keras.layers.Input(
(MAX_SEQUENCE_LENGTH,), dtype=tf.int32, name='input_segments')
 bert_layer = hub.KerasLayer(BERT_PATH, trainable=True)
 _, sequence_output = bert_layer([input_word_ids, input_masks, input_segments])
 x = tf.keras.layers.GlobalAveragePooling1D()(sequence_output)
 x = tf.keras.layers.Dropout(0.2 )(x)
 out = tf.keras.layers.Dense(30, activation="sigmoid", name="dense_output" )(x)
 model = tf.keras.models.Model(
 inputs=[input_word_ids, input_masks, input_segments], outputs=out)
 return model
def train_and_predict(model, train_data, valid_data, test_data,
learning_rate, epochs, batch_size, loss_function, fold):
custom_callback = CustomCallback(
 valid_data=(valid_data[0], valid_data[1]),
test_data=test_data,
batch_size=batch_size,
fold=None)
 optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
 model.compile(loss=loss_function, optimizer=optimizer)
 model.fit(train_data[0], train_data[1], epochs=epochs,
batch_size=batch_size, callbacks=[custom_callback])
 return custom_callback
all_class=pred_class.flatten()
all_class
filtered_sub_df['EncodedPixels']=all_class<define_variables>
gkf = GroupKFold(n_splits=10 ).split(X=df_train.question_body, groups=df_train.question_body)
 test_inputs = compute_input_arays(df_test, input_categories, tokenizer, MAX_SEQUENCE_LENGTH )
def sliding_window(img, sz=256):
total=7
x=0
img=img[0]
new_img = np.empty(( total,256,256,3),dtype=np.uint8)
 for count in range(total-1):
new_img[count,]=img[:, x:x+sz, :]
x=x+sz
new_img[-1,]=img[:, -256:, :]
return new_img<train_model>
models = []
for i in range(5):
model_path = f'.. /input/bertuned-f{i}/bertuned_f{i}.h5'
model = bert_model()
model.load_weights(model_path)
 models.append(model)
class DataGenerator2(keras.utils.Sequence):
'Generates data for Keras'
def __init__(self, list_IDs, df, target_df=None, mode='fit',
base_path=train_path,
batch_size=1, dim=(256, 1600), n_channels=3,
n_classes=4, random_state=1994, shuffle=True):
self.dim = dim
self.batch_size = batch_size
self.df = df
self.mode = mode
self.base_path = base_path
self.target_df = target_df
self.list_IDs = list_IDs
self.n_channels = n_channels
self.n_classes = n_classes
self.shuffle = shuffle
self.random_state = random_state
self.on_epoch_end()
def __len__(self):
'Denotes the number of batches per epoch'
return int(np.floor(len(self.list_IDs)// self.batch_size))
def __getitem__(self, index):
'Generate one batch of data'
indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
list_IDs_batch = [self.list_IDs[k] for k in indexes]
X = self.__generate_X(list_IDs_batch)
 if self.mode == 'fit':
y = self.__generate_y(list_IDs_batch)
 return crop_rand_pattern(X, y)
 elif self.mode == 'predict':
return sliding_window(X)
 elif self.mode=='val':
y = self.__generate_y(list_IDs_batch)
 return sliding_window_predict(X, y)
 else:
raise AttributeError('The mode parameter should be set to "fit" or "predict".')
 def on_epoch_end(self):
'Updates indexes after each epoch'
self.indexes = np.arange(len(self.list_IDs))
if self.shuffle == True:
np.random.shuffle(self.indexes)
 def __generate_X(self, ID):
'Generates data containing batch_size samples'
img_i = np.empty(( self.batch_size,*self.dim,self.n_channels),dtype=np.uint8)
 for i,index in enumerate(ID):
im_name = self.df['Image_id'].loc[index]
img_path = f"{self.base_path}/{im_name}"
img =cv2.cvtColor(cv2.imread(img_path), cv2.COLOR_BGR2RGB)
 img_i[i,]=img
return img_i
def __generate_y(self, ID):
img_i = np.empty(( self.batch_size,*self.dim,self.n_classes),dtype=np.uint8)
 for i,index in enumerate(ID):
im_name = self.df['Image_id'].loc[index]
image_df = self.target_df[self.target_df['Image_id'] == im_name]
rles = image_df['EncodedPixels'].values
img =build_masks(rles, input_shape=self.dim)
 img_i[i,]=img
return img_i
def __check_input__(self,index):
X,y=self.__getitem__(index)
 indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]
list_IDs_batch = [self.list_IDs[k] for k in indexes]
ID=list_IDs_batch
X=X
y=y
for i,ID in enumerate(list_IDs_batch):
im_name=self.df['Image_id'].loc[ID]
img= cv2.imread(train_path+im_name)
 image_df = self.target_df[self.target_df['Image_id'] == im_name]
rles = image_df['EncodedPixels'].values
masks = build_masks(rles, input_shape=self.dim)
 img_i=mask_image(img.astype(np.uint8),masks.astype(np.uint8))
plt.figure(figsize=(10,10))
plt.imshow(img_i)
 plt.show()
col=y.shape[0]
fig,axis = plt.subplots(nrows=1,ncols=col, figsize=(20, 20))
for i in range(col):
try:
img_i=mask_image(X[i].astype(np.uint8),y[i].astype(np.uint8))
axis[i].imshow(img_i)
 except IndexError:
print("IndexError : so skip")
 img_i=mask_image(X[i].astype(np.uint8),y[i].astype(np.uint8))
axis.imshow(img_i,cmap='gray')
 break
except TypeError:
img_i=mask_image(X[i].astype(np.uint8),y[i].astype(np.uint8))
print("i")
 axis.imshow(img_i,cmap='gray')
 print("TypeError(axis[i] is fails): so skip")
 break<load_pretrained>
model_path = f'.. /input/bertf1e15/Full-0.h5'
model = bert_model()
model.load_weights(model_path)
 models.append(model )
model_seg_2 = load_model('/kaggle/input/seg-model-256-256/seg_model.h5')
 model_seg_2.load_weights('/kaggle/input/seg-weights-898/bce_dice_loss_bce_from_8963_exp_4.h5' )<define_variables>
for i in range(2):
model_path = f'.. /input/bertmodelpretrained/bert-{i}.h5'
model = bert_model()
model.load_weights(model_path)
 models.append(model )
test_df = []
for i in range(0, test_imgs.shape[0], 300):
batch_idx = list(
 range(i, min(test_imgs.shape[0], i + 300)) 
)
 test_generator = DataGenerator(
 batch_idx,
df=test_imgs,
shuffle=False,
mode='predict',
base_path='.. /input/severstal-steel-defect-detection/test_images',
target_df=test_imgs,
batch_size=1,
n_classes=4 
)
 batch_pred_masks = model_seg.predict_generator(
 test_generator,
workers=1,
verbose=1,
use_multiprocessing=False 
)
 for j, b in tqdm(enumerate(batch_idx)) :
filename = test_imgs['Image_id'].iloc[b]
image_df = filtered_sub_df[filtered_sub_df['Image_id'] == filename].copy()
pred_masks=batch_pred_masks[j,].round().astype(int)
 value = np.sum(pred_masks,axis=(1,0))
c1=np.array(value*image_df['EncodedPixels'])
 pred_masks=batch_pred_masks[j,]
pred_masks[np.where(pred_masks>0.6)]=1
pred_masks[np.where(pred_masks<=0.6)]=0
pred_rles = build_rles(pred_masks)
 pred_rles=np.array(pred_rles)
 pred_rles[c1==0]=''
image_df['EncodedPixels'] = pred_rles
test_df.append(image_df)
 gc.collect()<concatenate>
for i in range(7):
model_path = f'.. /input/pretrained-bert/bert-{i}.h5'
model = bert_model()
model.load_weights(model_path)
 models.append(model )
test_df = pd.concat(test_df)
 print(test_df.shape)
 test_df.head()<save_to_csv>
for i in range(7):
model_path = f'.. /input/bert-base-tf2-0-training/bert-{i}.h5'
model = bert_model()
model.load_weights(model_path)
 models.append(model )
test_df[['ImageId_ClassId', 'EncodedPixels']].to_csv('submission.csv', index=False )<feature_engineering>
test_predictions = []
a=np.array([0,2])
 a[np.where(a>0.7)]=10
a<import_modules>
for model in models:
test_predictions.append(model.predict(test_inputs, batch_size=8))
nltk.download("punkt")
<set_options>
final_predictions = np.mean(test_predictions, axis=0 )
%matplotlib inline
warnings.filterwarnings('ignore' )<load_from_csv>
n=df_test['url'].apply(lambda x:(( 'ell.stackexchange.com' in x)or('english.stackexchange.com' in x)) ).tolist()
spelling=[]
for x in n:
if x:
spelling.append(0.5)
 else:
spelling.append(0.)
df_train = pd.read_csv('.. /input/donorschoose-application-screening/train.csv')
 df_test = pd.read_csv('.. /input/donorschoose-application-screening/test.csv')
 df_resources = pd.read_csv('.. /input/donorschoose-application-screening/resources.csv' )<split>
df_sub['question_type_spelling']=spelling
df_sub.iloc[:, 1:] = final_predictions
df_sub.to_csv('submission.csv', index=False )