Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<import_modules>
gc.enable()
print(os.listdir(".. /input/"))
tqdm.pandas(desc='Progress')
<import_modules>
SEED = 7
np.random.seed(SEED)
 set_random_seed(SEED)
 dir_path = ".. /input/aptos2019-blindness-detection/"
IMG_DIM = 299
BATCH_SIZE = 12
CHANNEL_SIZE = 3
NUM_EPOCHS = 60
TRAIN_DIR = 'train_images'
TEST_DIR = 'test_images'
FREEZE_LAYERS = 2
CLASSS = {0: "No DR", 1: "Mild", 2: "Moderate", 3: "Severe", 4: "Proliferative DR"}
print(os.listdir(".. /input")) 
<define_variables>
df_train = pd.read_csv(os.path.join(dir_path, "train.csv"))
df_test = pd.read_csv(os.path.join(dir_path, "test.csv"))
NUM_CLASSES = df_train['diagnosis'].nunique()
embed_size = 300
max_features = 200000
maxlen = 40
batch_size=1024<set_options>
print("Training set has {} samples and {} classes.".format(df_train.shape[0], df_train.shape[1]))
print("Testing set has {} samples and {} classes.".format(df_test.shape[0], df_test.shape[1]))
def seed_everything(seed=1029):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()<compute_test_metric>
x_train, x_test, y_train, y_test = train_test_split(df_train.id_code, df_train.diagnosis, test_size=0.2,
random_state=SEED, stratify=df_train.diagnosis )
def sigmoid(x):
return 1 /(1 + np.exp(-x))<define_variables>
def check_max_min_img_height_width(df, img_dir):
max_Height , max_Width =0 ,0
min_Height , min_Width =sys.maxsize ,sys.maxsize
for idx, row in df.iterrows() :
imgPath=os.path.join(dir_path,f"{img_dir}/{row['id_code']}.png")
 img=cv2.imread(imgPath)
 H,W=img.shape[:2]
max_Height=max(H,max_Height)
 max_Width =max(W,max_Width)
 min_Height=min(H,min_Height)
 min_Width =min(W,min_Width)
 return max_Height, max_Width, min_Height, min_Width
puncts = [',', '.', '"', ':', ')', '(', '-', '!', '?', '|', ';', "'", '$', '&', '/', '[', ']', '>', '%', '=', '
'·', '_', '{', '}', '©', '^', '®', '`', '<', '→', '°', '€', '™', '›', '♥', '←', '×', '§', '″', '′', 'Â', '█', '½', 'à', '…',
'“', '★', '”', '–', '●', 'â', '►', '−', '¢', '²', '¬', '░', '¶', '↑', '±', '¿', '▾', '═', '¦', '║', '―', '¥', '▓', '—', '‹', '─',
'▒', '：', '¼', '⊕', '▼', '▪', '†', '■', '’', '▀', '¨', '▄', '♫', '☆', 'é', '¯', '♦', '¤', '▲', 'è', '¸', '¾', 'Ã', '⋅', '‘', '∞',
'∙', '）', '↓', '、', '│', '（', '»', '，', '♪', '╩', '╚', '³', '・', '╦', '╣', '╔', '╗', '▬', '❤', 'ï', 'Ø', '¹', '≤', '‡', '√', ]
def clean_text(x):
x = str(x)
 for punct in puncts:
if punct in x:
x = x.replace(punct, f' {punct} ')
 return x
def clean_numbers(x):
if bool(re.search(r'\d', x)) :
x = re.sub('[0-9]{5,}', '
x = re.sub('[0-9]{4}', '
x = re.sub('[0-9]{3}', '
x = re.sub('[0-9]{2}', '
return x
mispell_dict = {"ain't": "is not", "aren't": "are not","can't": "cannot", "'cause": "because", "could've": "could have", "couldn't": "could not", "didn't": "did not", "doesn't": "does not", "don't": "do not", "hadn't": "had not", "hasn't": "has not", "haven't": "have not", "he'd": "he would","he'll": "he will", "he's": "he is", "how'd": "how did", "how'd'y": "how do you", "how'll": "how will", "how's": "how is", "I'd": "I would", "I'd've": "I would have", "I'll": "I will", "I'll've": "I will have","I'm": "I am", "I've": "I have", "i'd": "i would", "i'd've": "i would have", "i'll": "i will", "i'll've": "i will have","i'm": "i am", "i've": "i have", "isn't": "is not", "it'd": "it would", "it'd've": "it would have", "it'll": "it will", "it'll've": "it will have","it's": "it is", "let's": "let us", "ma'am": "madam", "mayn't": "may not", "might've": "might have","mightn't": "might not","mightn't've": "might not have", "must've": "must have", "mustn't": "must not", "mustn't've": "must not have", "needn't": "need not", "needn't've": "need not have","o'clock": "of the clock", "oughtn't": "ought not", "oughtn't've": "ought not have", "shan't": "shall not", "sha'n't": "shall not", "shan't've": "shall not have", "she'd": "she would", "she'd've": "she would have", "she'll": "she will", "she'll've": "she will have", "she's": "she is", "should've": "should have", "shouldn't": "should not", "shouldn't've": "should not have", "so've": "so have","so's": "so as", "this's": "this is","that'd": "that would", "that'd've": "that would have", "that's": "that is", "there'd": "there would", "there'd've": "there would have", "there's": "there is", "here's": "here is","they'd": "they would", "they'd've": "they would have", "they'll": "they will", "they'll've": "they will have", "they're": "they are", "they've": "they have", "to've": "to have", "wasn't": "was not", "we'd": "we would", "we'd've": "we would have", "we'll": "we will", "we'll've": "we will have", "we're": "we are", "we've": "we have", "weren't": "were not", "what'll": "what will", "what'll've": "what will have", "what're": "what are", "what's": "what is", "what've": "what have", "when's": "when is", "when've": "when have", "where'd": "where did", "where's": "where is", "where've": "where have", "who'll": "who will", "who'll've": "who will have", "who's": "who is", "who've": "who have", "why's": "why is", "why've": "why have", "will've": "will have", "won't": "will not", "won't've": "will not have", "would've": "would have", "wouldn't": "would not", "wouldn't've": "would not have", "y'all": "you all", "y'all'd": "you all would","y'all'd've": "you all would have","y'all're": "you all are","y'all've": "you all have","you'd": "you would", "you'd've": "you would have", "you'll": "you will", "you'll've": "you will have", "you're": "you are", "you've": "you have", 'colour': 'color', 'centre': 'center', 'favourite': 'favorite', 'travelling': 'traveling', 'counselling': 'counseling', 'theatre': 'theater', 'cancelled': 'canceled', 'labour': 'labor', 'organisation': 'organization', 'wwii': 'world war 2', 'citicise': 'criticize', 'youtu ': 'youtube ', 'Qoura': 'Quora', 'sallary': 'salary', 'Whta': 'What', 'narcisist': 'narcissist', 'howdo': 'how do', 'whatare': 'what are', 'howcan': 'how can', 'howmuch': 'how much', 'howmany': 'how many', 'whydo': 'why do', 'doI': 'do I', 'theBest': 'the best', 'howdoes': 'how does', 'mastrubation': 'masturbation', 'mastrubate': 'masturbate', "mastrubating": 'masturbating', 'pennis': 'penis', 'Etherium': 'Ethereum', 'narcissit': 'narcissist', 'bigdata': 'big data', '2k17': '2017', '2k18': '2018', 'qouta': 'quota', 'exboyfriend': 'ex boyfriend', 'airhostess': 'air hostess', "whst": 'what', 'watsapp': 'whatsapp', 'demonitisation': 'demonetization', 'demonitization': 'demonetization', 'demonetisation': 'demonetization'}
def _get_mispell(mispell_dict):
mispell_re = re.compile('(%s)' % '|'.join(mispell_dict.keys()))
return mispell_dict, mispell_re
mispellings, mispellings_re = _get_mispell(mispell_dict)
 def replace_typical_misspell(text):
def replace(match):
return mispellings[match.group(0)]
return mispellings_re.sub(replace, text)
<load_from_csv>
check_max_min_img_height_width(df_train, TRAIN_DIR )
def load_and_prec() :
train_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 print("Train shape : ",train_df.shape)
 print("Test shape : ",test_df.shape)
 train_df["question_text"] = train_df["question_text"].apply(lambda x: x.lower())
 test_df["question_text"] = test_df["question_text"].apply(lambda x: x.lower())
 train_df["question_text"] = train_df["question_text"].progress_apply(lambda x: clean_text(x))
test_df["question_text"] = test_df["question_text"].apply(lambda x: clean_text(x))
train_df["question_text"] = train_df["question_text"].progress_apply(lambda x: clean_numbers(x))
test_df["question_text"] = test_df["question_text"].apply(lambda x: clean_numbers(x))
train_df["question_text"] = train_df["question_text"].progress_apply(lambda x: replace_typical_misspell(x))
test_df["question_text"] = test_df["question_text"].apply(lambda x: replace_typical_misspell(x))
train_X = train_df["question_text"].fillna("_
test_X = test_df["question_text"].fillna("_
tokenizer = Tokenizer(num_words=max_features)
 tokenizer.fit_on_texts(list(train_X))
train_X = tokenizer.texts_to_sequences(train_X)
 test_X = tokenizer.texts_to_sequences(test_X)
 train_X = pad_sequences(train_X, maxlen=maxlen)
 test_X = pad_sequences(test_X, maxlen=maxlen)
 train_y = train_df['target'].values
return train_X, test_X, train_y,tokenizer.word_index<statistical_test>
check_max_min_img_height_width(df_test, TEST_DIR )
def load_glove(word_index):
EMBEDDING_FILE = '.. /input/embeddings/glove.840B.300d/glove.840B.300d.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')[:300]
embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE))
all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = -0.005838499,0.48782197
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words+1, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
return embedding_matrix
def load_fasttext(word_index):
EMBEDDING_FILE = '.. /input/embeddings/wiki-news-300d-1M/wiki-news-300d-1M.vec'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE)if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = all_embs.mean() , all_embs.std()
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words+1, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
return embedding_matrix
def load_para(word_index):
EMBEDDING_FILE = '.. /input/embeddings/paragram_300_sl999/paragram_300_sl999.txt'
def get_coefs(word,*arr): return word, np.asarray(arr, dtype='float32')
 embeddings_index = dict(get_coefs(*o.split(" ")) for o in open(EMBEDDING_FILE, encoding="utf8", errors='ignore')if len(o)>100)
 all_embs = np.stack(embeddings_index.values())
 emb_mean,emb_std = -0.0053247833,0.49346462
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std,(nb_words+1, embed_size))
for word, i in word_index.items() :
if i >= max_features: continue
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None: embedding_matrix[i] = embedding_vector
return embedding_matrix<randomize_order>
def crop_image1(img,tol=7):
mask = img>tol
return img[np.ix_(mask.any(1),mask.any(0)) ]
class QuraData(data.Dataset):
def __init__(self,questions,labels,augument=False,training=True):
super(QuraData, self ).__init__()
self.augument=augument
self.questions=questions
self.labels= labels
self.len_ = len(self.questions)
 self.training=training
def shuffle(self,d):
return np.random.permutation(d.tolist())
 def dropout(self,d,p=0.5):
len_ = len(d)
 index = np.random.choice(len_,int(len_*p))
d[index]=0
return d
def __getitem__(self,index):
question,label = self.questions[index],self.labels[index,np.newaxis]
if self.training and self.augument :
question= self.dropout(question,p=0.05)
 question=torch.from_numpy(question ).long()
label=torch.LongTensor(label ).long()
return question,label
def __len__(self):
return self.len_ 
<normalization>
def crop_image(img,tol=7):
w, h = img.shape[1],img.shape[0]
gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
 gray_img = cv2.blur(gray_img,(5,5))
shape = gray_img.shape
gray_img = gray_img.reshape(-1,1)
 quant = quantile_transform(gray_img, n_quantiles=256, random_state=0, copy=True)
 quant =(quant*256 ).astype(int)
 gray_img = quant.reshape(shape)
 xp =(gray_img.mean(axis=0)>tol)
 yp =(gray_img.mean(axis=1)>tol)
 x1, x2 = np.argmax(xp), w-np.argmax(np.flip(xp))
y1, y2 = np.argmax(yp), h-np.argmax(np.flip(yp))
if x1 >= x2 or y1 >= y2 :
return img
else:
img1=img[y1:y2,x1:x2,0]
img2=img[y1:y2,x1:x2,1]
img3=img[y1:y2,x1:x2,2]
img = np.stack([img1,img2,img3],axis=-1)
 return img
def process_image(image, size=512):
image = cv2.resize(image,(size,int(size*image.shape[0]/image.shape[1])))
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 try:
image = crop_image(image, tol=15)
 except Exception as e:
image = image
print(str(e))
return image
class Attention(nn.Module):
def __init__(self, feature_dim, step_dim, bias=True, **kwargs):
super(Attention, self ).__init__(**kwargs)
 self.supports_masking = True
self.bias = bias
self.feature_dim = feature_dim
self.step_dim = step_dim
self.features_dim = 0
weight = torch.zeros(feature_dim, 1)
 nn.init.xavier_uniform_(weight)
 self.weight = nn.Parameter(weight)
 if bias:
self.b = nn.Parameter(torch.zeros(step_dim))
def forward(self, x, mask=None):
feature_dim = self.feature_dim
step_dim = self.step_dim
eij = torch.mm(
 x.contiguous().view(-1, feature_dim),
self.weight
).view(-1, step_dim)
 if self.bias:
eij = eij + self.b
eij = torch.tanh(eij)
 a = torch.exp(eij)
 if mask is not None:
a = a * mask
a = a / torch.sum(a, 1, keepdim=True)+ 1e-10
weighted_input = x * torch.unsqueeze(a, -1)
 return torch.sum(weighted_input, 1 )<choose_model_class>
def random_crop(img, random_crop_size):
assert img.shape[2] == 3
height, width = img.shape[0], img.shape[1]
dy, dx = random_crop_size
x = np.random.randint(0, width - dx + 1)
 y = np.random.randint(0, height - dy + 1)
 img = img[y:(y + dy), x:(x + dx), :]
return img

 def crop_generator(batches, crop_length):
while True:
batch_x, batch_y = next(batches)
 batch_crops = np.zeros(( batch_x.shape[0], crop_length, 3))
for i in range(batch_x.shape[0]):
batch_crops[0] = random_crop(batch_x[i],(crop_length, crop_length))
yield(batch_crops, batch_y)
class CyclicLR(object):
def __init__(self, optimizer, base_lr=1e-3, max_lr=6e-3,
step_size=2000, mode='triangular', gamma=1.,
scale_fn=None, scale_mode='cycle', last_batch_iteration=-1):
if not isinstance(optimizer, Optimizer):
raise TypeError('{} is not an Optimizer'.format(
 type(optimizer ).__name__))
self.optimizer = optimizer
if isinstance(base_lr, list)or isinstance(base_lr, tuple):
if len(base_lr)!= len(optimizer.param_groups):
raise ValueError("expected {} base_lr, got {}".format(
 len(optimizer.param_groups), len(base_lr)))
 self.base_lrs = list(base_lr)
 else:
self.base_lrs = [base_lr] * len(optimizer.param_groups)
 if isinstance(max_lr, list)or isinstance(max_lr, tuple):
if len(max_lr)!= len(optimizer.param_groups):
raise ValueError("expected {} max_lr, got {}".format(
 len(optimizer.param_groups), len(max_lr)))
 self.max_lrs = list(max_lr)
 else:
self.max_lrs = [max_lr] * len(optimizer.param_groups)
 self.step_size = step_size
if mode not in ['triangular', 'triangular2', 'exp_range'] \
and scale_fn is None:
raise ValueError('mode is invalid and scale_fn is None')
 self.mode = mode
self.gamma = gamma
if scale_fn is None:
if self.mode == 'triangular':
self.scale_fn = self._triangular_scale_fn
self.scale_mode = 'cycle'
elif self.mode == 'triangular2':
self.scale_fn = self._triangular2_scale_fn
self.scale_mode = 'cycle'
elif self.mode == 'exp_range':
self.scale_fn = self._exp_range_scale_fn
self.scale_mode = 'iterations'
else:
self.scale_fn = scale_fn
self.scale_mode = scale_mode
self.batch_step(last_batch_iteration + 1)
 self.last_batch_iteration = last_batch_iteration
def batch_step(self, batch_iteration=None):
if batch_iteration is None:
batch_iteration = self.last_batch_iteration + 1
self.last_batch_iteration = batch_iteration
for param_group, lr in zip(self.optimizer.param_groups, self.get_lr()):
param_group['lr'] = lr
def _triangular_scale_fn(self, x):
return 1.
 def _triangular2_scale_fn(self, x):
return 1 /(2.**(x - 1))
def _exp_range_scale_fn(self, x):
return self.gamma**(x)
 def get_lr(self):
step_size = float(self.step_size)
 cycle = np.floor(1 + self.last_batch_iteration /(2 * step_size))
x = np.abs(self.last_batch_iteration / step_size - 2 * cycle + 1)
 lrs = []
param_lrs = zip(self.optimizer.param_groups, self.base_lrs, self.max_lrs)
 for param_group, base_lr, max_lr in param_lrs:
base_height =(max_lr - base_lr)* np.maximum(0,(1 - x))
if self.scale_mode == 'cycle':
lr = base_lr + base_height * self.scale_fn(cycle)
 else:
lr = base_lr + base_height * self.scale_fn(self.last_batch_iteration)
 lrs.append(lr)
 return lrs 
<statistical_test>
gc.collect()
df_train.id_code = df_train.id_code.apply(lambda x: x + ".png")
 df_test.id_code = df_test.id_code.apply(lambda x: x + ".png")
 df_train['diagnosis'] = df_train['diagnosis'].astype('str')
def kmax_pooling(x, dim, k):
index = x.topk(k, dim = dim)[1].sort(dim = dim)[0]
return x.gather(dim, index )<normalization>
datagenerator=ImageDataGenerator(
 horizontal_flip=True,
vertical_flip=True,
rotation_range=40,
zoom_range=0.2,
shear_range=0.1,
fill_mode='nearest' )
class Gate(nn.Module):
def __init__(self, input_size):
super(Gate, self ).__init__()
self.linear = nn.Linear(input_size, input_size)
 def forward(self, x):
x_proj = self.linear(x)
 gate = torch.sigmoid(x_proj)
 return x * gate<normalization>
train_datagen = ImageDataGenerator(rescale=1./ 255,
validation_split=0.15,
horizontal_flip=True,
vertical_flip=True,
rotation_range=40,
zoom_range=0.2,
shear_range=0.1,
fill_mode='nearest')
class SFU(nn.Module):

 def __init__(self, input_size, fusion_size):
super(SFU, self ).__init__()
self.linear_r = nn.Linear(input_size + fusion_size, input_size)
 self.linear_g = nn.Linear(input_size + fusion_size, input_size)
 def forward(self, x, fusions):
r_f = torch.cat([x, fusions], 2)
 r = F.tanh(self.linear_r(r_f))
g = F.sigmoid(self.linear_g(r_f))
o = g * r +(1-g)* x
return o<load_pretrained>
train_generator = train_datagen.flow_from_dataframe(dataframe=df_train,
directory=".. /input/aptos2019-blindness-detection/train_images/",
x_col="id_code",
y_col="diagnosis",
batch_size=BATCH_SIZE,
class_mode="categorical",
target_size=(IMG_DIM, IMG_DIM),
subset='training',
shaffle=True,
seed=SEED, 
)
 valid_generator = train_datagen.flow_from_dataframe(dataframe=df_train,
directory=".. /input/aptos2019-blindness-detection/train_images/",
x_col="id_code",
y_col="diagnosis",
batch_size=BATCH_SIZE,
class_mode="categorical",
target_size=(IMG_DIM, IMG_DIM),
subset='validation',
shaffle=True,
seed=SEED 
)
 del x_train
del y_train
gc.collect()
kernel_sizes = [2,3,5]
class mergeNN(nn.Module):
def __init__(self,embedding_matrix):
super(mergeNN, self ).__init__()
self.model_name = 'LSTMText'
self.encoder = nn.Embedding.from_pretrained(torch.FloatTensor(embedding_matrix))
self.title_lstm = nn.LSTM(input_size = embed_size*2,\
hidden_size = 128,
num_layers =1,
bias = True,
batch_first = True,
bidirectional = True 
)
 self.title_gru = nn.GRU(input_size = 256,\
hidden_size = 128,
num_layers =1,
bias = True,
batch_first = True,
bidirectional = True 
)
 question_convs = [nn.Sequential(nn.Conv1d(in_channels = embed_size*2,out_channels = 48,kernel_size = kernel_size),
nn.BatchNorm1d(48),
nn.ReLU(inplace=True),
nn.MaxPool1d(kernel_size =(maxlen - kernel_size + 1)) 
)for kernel_size in kernel_sizes]
self.question_convs = nn.ModuleList(question_convs)
 self.lstm_attention = Attention(256, maxlen)
 self.fc = nn.Sequential(
 nn.Linear(912,256),
nn.BatchNorm1d(256),
nn.ReLU(inplace=True),
nn.Dropout(0.3),
nn.Linear(256,1)
)
 self.dropout=nn.Dropout2d(0.1)
 self.linear1=nn.Linear(600,256)
 for name, param in self.title_lstm.named_parameters() :
if 'bias' in name:
nn.init.constant_(param, 0.0)
 elif 'weight_ih' in name:
nn.init.kaiming_normal_(param)
 elif 'weight_hh' in name:
nn.init.orthogonal_(param)
 for name, param in self.title_gru.named_parameters() :
if 'bias' in name:
nn.init.constant_(param, 0.0)
 elif 'weight_ih' in name:
nn.init.kaiming_normal_(param)
 elif 'weight_hh' in name:
nn.init.orthogonal_(param)
 def forward(self, question):
question = self.encoder(question)
 question = torch.squeeze(self.dropout(torch.unsqueeze(question, 0)))
 question_lstm_out = self.title_lstm(question)[0]
question_res_out=self.linear1(question)+question_lstm_out
question_gru_out = self.title_gru(question_res_out)[0]
question_res_out2=question_res_out+question_gru_out
question_conv_out = kmax_pooling(( question_res_out2.permute(0,2,1)) ,2,2)
 question_conv_out = question_conv_out.view(question_conv_out.size(0), -1)
 question_attention_out=self.lstm_attention(question_res_out2)
 cnn_question_out = [question_conv(question.permute(0, 2, 1)) for question_conv in self.question_convs]
conv_out = torch.cat(cnn_question_out,dim=2)
 conv_out = conv_out.view(conv_out.size(0), -1)
 reshaped=torch.cat(( question_conv_out,question_attention_out,conv_out),1)
 logits = self.fc(( reshaped))
return logits<split>
def design_model() :
model = Sequential()
model.add(Conv2D(filters=16, kernel_size=(2, 2), input_shape=[IMG_DIM, IMG_DIM, CHANNEL_SIZE], activation=relu))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(filters=32, kernel_size=(2, 2), activation=relu))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(filters=64, kernel_size=(2, 2), activation=relu))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(GlobalAveragePooling2D())
 model.add(Dense(units=1000, activation=relu))
model.add(Dropout(rate=0.2))
model.add(Dense(units=1000, activation=relu))
model.add(Dropout(rate=0.2))
model.add(Dense(5, activation='softmax'))
return model
model = design_model()
splits = list(StratifiedKFold(n_splits=5, shuffle=True, random_state=2018 ).split(train_X, train_y))<normalization>
model.compile(optimizer=RMSprop() , loss='categorical_crossentropy', metrics=['accuracy'] )
glove_embeddings = load_glove(word_index)
 paragram_embeddings = load_para(word_index)
 embedding_matrix = np.concatenate(( glove_embeddings, paragram_embeddings), axis=1)
 np.shape(embedding_matrix )<data_type_conversions>
eraly_stop = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=3, verbose=1, mode='auto')
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', min_delta=0.0004, patience=2, factor=0.1, min_lr=1e-6, mode='auto',
verbose=1 )
def train(n_epochs):
train_preds = np.zeros(( len(train_X)))
 test_preds = np.zeros(( len(test_X)))
 for i,(train_idx, valid_idx)in enumerate(splits):
train_dataset= QuraData(train_X[train_idx.astype(int)],train_y[train_idx.astype(int)],augument=False,training=True)
 valid_dataset= QuraData(train_X[valid_idx.astype(int)],train_y[valid_idx.astype(int)],augument=False,training=False)
 train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
 valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=batch_size, shuffle=False)
 model=mergeNN(embedding_matrix=embedding_matrix ).cuda()
loss_fn = torch.nn.BCEWithLogitsLoss(reduction='mean')
 step_size = 500
base_lr, max_lr = 0.001, 0.003
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()),
lr=max_lr)
 scheduler = CyclicLR(optimizer, base_lr=base_lr, max_lr=max_lr,
step_size=step_size, mode='exp_range',
gamma=0.99994)
 valid_preds_fold= np.zeros(( len(valid_idx)))
 test_preds_fold= np.zeros(( len(test_X)))
 print(f'Fold {i + 1}')
 for epoch in range(n_epochs):
start_time = time.time()
model.train(True)
 avg_loss = 0
for ii,(x_batch,y_batch)in enumerate(train_loader):
x_batch=Variable(x_batch ).cuda()
y_batch=Variable(y_batch ).cuda()
y_pred = model(x_batch)
 scheduler.batch_step()
loss = loss_fn(y_pred, y_batch.float())
 optimizer.zero_grad()
loss.backward()
optimizer.step()
avg_loss += loss.item() / len(train_loader)
 model.train(False)
 model.eval()
avg_val_loss = 0.
 with torch.no_grad() :
for i,(x_batch, y_batch)in enumerate(valid_loader):
x_batch=Variable(x_batch ).cuda()
y_batch=Variable(y_batch ).cuda()
y_pred = model(x_batch)
 avg_val_loss += loss_fn(y_pred, y_batch.float() ).item() / len(valid_loader)
 valid_preds_fold[i * batch_size:(i+1)* batch_size] = sigmoid(y_pred.cpu().numpy())[:, 0]
train_preds[valid_idx] = valid_preds_fold
elapsed_time = time.time() - start_time
print('Epoch {}/{} \t loss={:.4f} \t val_loss={:.4f} \t time={:.2f}s'.format(epoch + 1, n_epochs, avg_loss, avg_val_loss,elapsed_time))
model.eval()
with torch.no_grad() :

 for i,(x_batch,)in enumerate(test_loader):
x_batch=Variable(x_batch ).cuda()
y_pred = model(x_batch)
 test_preds_fold[i * batch_size:(i+1)* batch_size] = sigmoid(y_pred.cpu().numpy())[:, 0]
test_preds+=test_preds_fold/len(splits)
 return train_preds,test_preds<train_model>
NUB_TRAIN_STEPS = train_generator.n // train_generator.batch_size
NUB_VALID_STEPS = valid_generator.n // valid_generator.batch_size
NUB_TRAIN_STEPS, NUB_VALID_STEPS
train_preds,test_preds=train(n_epochs=3)
<find_best_params>
def create_resnet(img_dim, CHANNEL, n_class):
input_tensor = Input(shape=(img_dim, img_dim, CHANNEL))
base_model = ResNet50(weights=None, include_top=False, input_tensor=input_tensor)
 base_model.load_weights('.. /input/resnet50weightsfile/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5')
 x = GlobalAveragePooling2D()(base_model.output)
 x = BatchNormalization()(x)
 x = Dropout(0.4 )(x)
 x = Dense(2048, activation=elu )(x)
 x = BatchNormalization()(x)
 x = Dropout(0.4 )(x)
 x = Dense(1024, activation=elu )(x)
 x = BatchNormalization()(x)
 x = Dropout(0.3 )(x)
 x = Dense(512, activation=elu )(x)
 x = BatchNormalization()(x)
 x = Dropout(0.5 )(x)
 output_layer = Dense(n_class, activation='softmax', name="Output_Layer" )(x)
 model_resnet = Model(input_tensor, output_layer)
 return model_resnet
model_resnet = create_resnet(IMG_DIM, CHANNEL_SIZE, NUM_CLASSES )
thresholds = []
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 res = metrics.f1_score(train_y,(train_preds > thresh ).astype(int))
thresholds.append([thresh, res])
 print("F1 score at threshold {0} is {1}".format(thresh, res))
thresholds.sort(key=lambda x: x[1], reverse=True)
 best_thresh = thresholds[0][0]<train_model>
for layers in model_resnet.layers:
layers.trainable = True
<save_to_csv>
lr = 1e-3
optimizer = SGD(lr=lr, decay=1e-6, momentum=0.9, nesterov=True)
 model_resnet.compile(optimizer=optimizer, loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])
 gc.collect()
pred_test_y =(test_preds > best_thresh ).astype(int)
 test_df = pd.read_csv(".. /input/test.csv", usecols=["qid"])
 out_df = pd.DataFrame({"qid":test_df["qid"].values})
 out_df['prediction'] = pred_test_y
out_df.to_csv("submission.csv", index=False)
<import_modules>
history = model_resnet.fit_generator(generator=train_generator,
steps_per_epoch=NUB_TRAIN_STEPS,
validation_data=valid_generator,
validation_steps=NUB_VALID_STEPS,
epochs=NUM_EPOCHS,
callbacks=[eraly_stop, reduce_lr],
verbose=2)
 gc.collect()
tqdm.pandas()<set_options>
test_datagen = ImageDataGenerator(rescale=1./ 255, validation_split=0.2, horizontal_flip=True)
 test_generator = test_datagen.flow_from_dataframe(dataframe=df_test,
directory=".. /input/aptos2019-blindness-detection/test_images/",
x_col="id_code",
target_size=(IMG_DIM, IMG_DIM),
batch_size=1,
shuffle=False,
class_mode=None,
seed=SEED)
 print(df_test.shape[0])
 gc.collect()
def seed_torch(seed=43):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True<define_variables>
tta_steps = 5
preds_tta = []
for i in tqdm(range(tta_steps)) :
test_generator.reset()
preds = model_resnet.predict_generator(generator=test_generator, steps=ceil(df_test.shape[0]))
preds_tta.append(preds)
embed_size = 300
max_features = 100000
maxlen = 70
batch_size = 512
train_epochs = 4
SEED = 43<define_variables>
final_pred = np.mean(preds_tta, axis=0)
 predicted_class_indices = np.argmax(final_pred, axis=1)
 len(predicted_class_indices)
<load_from_csv><EOS> 
results = pd.DataFrame({"id_code": test_generator.filenames, "diagnosis": predicted_class_indices})
 results.id_code = results.id_code.apply(lambda x: x[:-4])
 results.to_csv("submission.csv", index=False)