Kaggle competition: Gendered Pronoun Resolution
class FocalLoss(nn.Module):
def __init__(self, gamma=2):
super().__init__()
self.gamma = gamma
def forward(self, input, target):
if not(target.size() == input.size()):
raise ValueError("Target size({})must be the same as input size({})" 
.format(target.size() , input.size()))
max_val =(-input ).clamp(min=0)
 loss = input - input * target + max_val + \ 
(( -max_val ).exp() +(-input - max_val ).exp() ).log()
invprobs = F.logsigmoid(-input *(target * 2.0 - 1.0))
loss =(invprobs * self.gamma ).exp() * loss
return loss.sum(dim=1 ).mean()<compute_train_metric>
x = np.load('.. /input/prepare-for-submission/0.npy' )
def f1_(preds, targs, start=0.17, end=0.24, step=0.01):
with warnings.catch_warnings() :
warnings.simplefilter("ignore")
 return max([fbeta_score(targs,(preds>th), 1, average='samples')
 for th in np.arange(start,end,step)] )<choose_model_class>
def logloss(y, y_):
return tf.losses.log_loss(y,y_)
 model = load_model(mf, custom_objects={'logloss':logloss} )
learn = Learner.from_model_data(m, md, tmp_name=TMP, models_name=MODEL)
 learn.crit = FocalLoss()
learn.opt_fn = optim.Adam
learn.metrics = [f1_]<define_search_space>
import gc
lr = 1E-2<train_model>
y1 = model.predict(x )
learn.fit(lr,1,cycle_len=30,use_clr_beta=(10,10, 0.85, 0.9))<save_model>
del x
gc.collect()
learn.save('128' )<import_modules>
x = np.load('.. /input/prepare-for-submission-2/1.npy')
 y2 = model.predict(x)
 del x
gc.collect()
import sys
import numpy as np
import keras
from keras.utils import Sequence
from PIL import Image
from matplotlib import pyplot as plt
import pandas as pd
from tqdm import tqdm
import os
import imgaug as ia
from imgaug import augmenters as iaa
import cv2<define_variables>
x = np.load('.. /input/prepare-for-submission-3/2.npy')
 y3 = model.predict(x)
 del x
gc.collect()
BATCH_SIZE = 128
SEED = 777
SHAPE =(192, 192, 4)
 DIR = '.. /input'
VAL_RATIO = 0.1
THRESHOLD = 0.05
ia.seed(SEED )<load_from_csv>
y = np.concatenate([y1, y2, y3] )
def getTrainDataset() :
path_to_train = DIR + '/train/'
data = pd.read_csv(DIR + '/train.csv')
 paths = []
labels = []
for name, lbl in zip(data['Id'], data['Target'].str.split(' ')) :
y = np.zeros(28)
 for key in lbl:
y[int(key)] = 1
paths.append(os.path.join(path_to_train, name))
labels.append(y)
 return np.array(paths), np.array(labels)
 def getTestDataset() :
path_to_test = DIR + '/test/'
data = pd.read_csv(DIR + '/sample_submission.csv')
 paths = []
labels = []
for name in data['Id']:
y = np.ones(28)
 paths.append(os.path.join(path_to_test, name))
labels.append(y)
 return np.array(paths), np.array(labels)
<define_variables>
df = pd.DataFrame()
df['ID'] = pd.read_csv('.. /input/gendered-pronoun-resolution/test_stage_2.tsv', delimiter='\t')['ID']
class ProteinDataGenerator(keras.utils.Sequence):
def __init__(self, paths, labels, batch_size, shape, shuffle = False, use_cache = False, augment = False):
self.paths, self.labels = paths, labels
self.batch_size = batch_size
self.shape = shape
self.shuffle = shuffle
self.use_cache = use_cache
self.augment = augment
if use_cache == True:
self.cache = np.zeros(( paths.shape[0], shape[0], shape[1], shape[2]), dtype=np.float16)
 self.is_cached = np.zeros(( paths.shape[0]))
self.on_epoch_end()
def __len__(self):
return int(np.ceil(len(self.paths)/ float(self.batch_size)))
 def __getitem__(self, idx):
indexes = self.indexes[idx * self.batch_size :(idx+1)* self.batch_size]
paths = self.paths[indexes]
X = np.zeros(( paths.shape[0], self.shape[0], self.shape[1], self.shape[2]))
if self.use_cache == True:
X = self.cache[indexes]
for i, path in enumerate(paths[np.where(self.is_cached[indexes] == 0)]):
image = self.__load_image(path)
 self.is_cached[indexes[i]] = 1
self.cache[indexes[i]] = image
X[i] = image
else:
for i, path in enumerate(paths):
X[i] = self.__load_image(path)
 y = self.labels[indexes]
if self.augment == True:
seq = iaa.Sequential([
iaa.OneOf([
iaa.Fliplr(0.5),
iaa.Crop(percent=(0, 0.1)) ,
iaa.Sometimes(0.5,
iaa.GaussianBlur(sigma=(0, 0.5)) 
),
iaa.ContrastNormalization(( 0.75, 1.5)) ,
iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.05*255), per_channel=0.5),
iaa.Multiply(( 0.8, 1.2), per_channel=0.2),
iaa.Affine(
 scale={"x":(0.8, 1.2), "y":(0.8, 1.2)},
translate_percent={"x":(-0.2, 0.2), "y":(-0.2, 0.2)},
rotate=(-180, 180),
shear=(-8, 8)
)
 ])], random_order=True)
 X = np.concatenate(( X, seq.augment_images(X), seq.augment_images(X), seq.augment_images(X)) , 0)
 y = np.concatenate(( y, y, y, y), 0)
 return X, y
def on_epoch_end(self):
self.indexes = np.arange(len(self.paths))
if self.shuffle == True:
np.random.shuffle(self.indexes)
 def __iter__(self):

 for item in(self[i] for i in range(len(self))):
yield item
def __load_image(self, path):
R = Image.open(path + '_red.png')
 G = Image.open(path + '_green.png')
 B = Image.open(path + '_blue.png')
 Y = Image.open(path + '_yellow.png')
 im = np.stack((
np.array(R),
np.array(G),
np.array(B),
np.array(Y)) , -1)
 im = cv2.resize(im,(SHAPE[0], SHAPE[1]))
im = np.divide(im, 255)
 return im<set_options>
df_s = pd.read_csv('.. /input/gendered-pronoun-resolution/sample_submission_stage_2.csv' )
set_random_seed(SEED )<compute_test_metric>
df['A'] = y[:,0]
def f1(y_true, y_pred):
y_pred = K.cast(K.greater(K.clip(y_pred, 0, 1), THRESHOLD), K.floatx())
 tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 tn = K.sum(K.cast(( 1-y_true)*(1-y_pred), 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K.epsilon())
 r = tp /(tp + fn + K.epsilon())
 f1 = 2*p*r /(p+r+K.epsilon())
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return K.mean(f1)
 def f1_loss(y_true, y_pred):
tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)
 tn = K.sum(K.cast(( 1-y_true)*(1-y_pred), 'float'), axis=0)
 fp = K.sum(K.cast(( 1-y_true)*y_pred, 'float'), axis=0)
 fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)
 p = tp /(tp + fp + K.epsilon())
 r = tp /(tp + fn + K.epsilon())
 f1 = 2*p*r /(p+r+K.epsilon())
 f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)
 return 1-K.mean(f1 )<define_search_model>
df['B'] = y[:,1]
df['NEITHER'] = y[:,2]
<choose_model_class><EOS> 
df.to_csv('submission.csv', index=False )