Kaggle competition: Recursion Cellular Image Classification
<SOS> metric: CategorizationAccuracy Kaggle data source: recursion-cellular-image-classification<load_pretrained>
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
print("Extrayendo Paragram embedding...")
 embed_paragram = load_embed(paragram )<drop_column>
def seed_everything(seed):
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 = 0
seed_everything(SEED )
add_lower(embed_paragram, vocab)
<categorify>
train_df = pd.read_csv('.. /input/train.csv')
 train_df.head(10 )
_, _, word_index = make_data(train_df['treated_question'], test_df['treated_question'])
 embedding_matrix_paragram = make_embed_matrix(embed_paragram, word_index, max_features)
 del word_index
del embed_paragram
gc.collect()<groupby>
def generate_df(train_df,sample_num=1):
train_df['path'] = train_df['experiment'].str.cat(train_df['plate'].astype(str ).str.cat(train_df['well'],sep='/'),sep='/Plate')+ '_s'+str(sample_num)+ '_w'
train_df = train_df.drop(columns=['id_code','experiment','plate','well'] ).reindex(columns=['path','sirna'])
 return train_df
proc_train_df = generate_df(train_df )
embedding_matrix_final = np.sum([embedding_matrix_glove*0.7,embedding_matrix_paragram*0.3], axis=0 )<feature_engineering>
il = MultiChannelImageList.from_df(df=proc_train_df,path='.. /input/train/' )
train_stats = statistical_features(train_df)
 train_stats[train_df.target==1].head()<compute_test_metric>
def image2np(image:Tensor)->np.ndarray:
"Convert from torch style `image` to numpy/matplotlib style."
res = image.cpu().permute(1,2,0 ).numpy()
if res.shape[2]==1:
return res[...,0]
elif res.shape[2]>3:
return res[...,:3]
else:
return res
vision.image.image2np = image2np
test_stats = statistical_features(test_df)
 test_stats.head()<normalization>
train_df,val_df = train_test_split(proc_train_df,test_size=0.035, stratify = proc_train_df.sirna, random_state=42)
 _proc_train_df = pd.concat([train_df,val_df] )
sc = StandardScaler()
train_stats = sc.fit_transform(train_stats)
 test_stats = sc.transform(test_stats )<split>
data =(MultiChannelImageList.from_df(df=_proc_train_df,path='.. /input/train/')
.split_by_idx(list(range(len(train_df),len(_proc_train_df)))) 
.label_from_df() 
.transform(get_transforms() ,size=256)
.databunch(bs=128,num_workers=4)
.normalize()
)
y = train_df['target'].values
X_train_stats, X_val_stats, _, _ = train_test_split(train_stats, y, test_size=0.1, random_state=42 )<choose_model_class>
data.show_batch()
def model() :
optim = Adam(lr=0.0010)
 main_inp = Input(shape=(max_len,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix_final], trainable=True )(main_inp)
 x = SpatialDropout1D(0.15 )(x)
 x = Bidirectional(CuDNNGRU(128, return_sequences=True))(x)
 x = Conv1D(filters=64, kernel_size=1 )(x)
 x = GlobalMaxPool1D()(x)
 stat_inp = Input(shape=(X_train_stats.shape[1],))
x_2 = Dense(64, activation="relu" )(stat_inp)
 x_2 = Dropout(0.15 )(x_2)
 x_2 = Dense(32, activation="relu" )(x_2)
 x_f = concatenate([x, x_2])
 x_f = Dense(128, activation="relu" )(x_f)
 x_f = Dropout(0.15 )(x_f)
 x_f = BatchNormalization()(x_f)
 x_f = Dense(1, activation="sigmoid" )(x_f)
 model = Model(inputs=[main_inp, stat_inp], outputs = x_f)
 model.compile(loss='binary_crossentropy', optimizer=optim, metrics=['binary_accuracy'])
 return model<define_variables>
!pip install efficientnet_pytorch
BATCH_SIZE = 512
EPOCHS = 2<train_model>
from efficientnet_pytorch import *
model1.fit([X_train,X_train_stats], y_train, batch_size=BATCH_SIZE, epochs=EPOCHS, validation_data=([X_val,X_val_stats],y_val))<compute_test_metric>

 RESNET_MODELS = {
18: torchvision.models.resnet18,
34: torchvision.models.resnet34,
50: torchvision.models.resnet50,
101: torchvision.models.resnet101,
152: torchvision.models.resnet152,
}
def resnet_multichannel(depth=50,pretrained=True,num_classes=1108,num_channels=6):
model = RESNET_MODELS[depth](pretrained=pretrained)
 w = model.conv1.weight
model.conv1 = nn.Conv2d(num_channels, 64, kernel_size=7, stride=2, padding=3,
bias=False)
 model.conv1.weight = nn.Parameter(torch.stack([torch.mean(w, 1)]*num_channels, dim=1))
return model
DENSENET_MODELS = {
121: torchvision.models.densenet121,
161: torchvision.models.densenet161,
169: torchvision.models.densenet169,
201: torchvision.models.densenet201,
}
def densenet_multichannel(depth=121,pretrained=True,num_classes=1108,num_channels=6):
model = DENSENET_MODELS[depth](pretrained=pretrained)
 w = model.features.conv0.weight
model.features.conv0 = nn.Conv2d(num_channels, 64, kernel_size=7, stride=2, padding=3,
bias=False)
 model.features.conv0.weight = nn.Parameter(torch.stack([torch.mean(w, 1)]*num_channels, dim=1))
return model
def efficientnet_multichannel(pretrained=True,name='b0',num_classes=1108,num_channels=6,image_size=256):
model = EfficientNet.from_pretrained('efficientnet-'+name,num_classes=num_classes)
 w = model._conv_stem.weight
model._conv_stem = utils.Conv2dStaticSamePadding(num_channels,32,kernel_size=(3, 3), stride=(2, 2), bias=False, image_size = image_size)
 model._conv_stem.weight = nn.Parameter(torch.stack([torch.mean(w, 1)]*num_channels, dim=1))
return model
def f1_score(pred_model):
max_t = 0
max_f1 = 0
for thresh in np.arange(0.1, 0.701, 0.01):
thresh = np.round(thresh, 2)
 f1 = metrics.f1_score(y_val,(pred_model>thresh ).astype(int))
if(f1>max_f1):
max_f1 = f1
max_t = thresh
print(max_t, max_f1)
 return max_t<predict_on_test>
def resnet18(pretrained,num_channels=6):
return resnet_multichannel(depth=18,pretrained=pretrained,num_channels=num_channels)
 def _resnet_split(m): return(m[0][6],m[1])
 def densenet161(pretrained,num_channels=6):
return densenet_multichannel(depth=161,pretrained=pretrained,num_channels=num_channels)
 def _densenet_split(m:nn.Module): return(m[0][0][7],m[1])
 def efficientnetb0(pretrained=True,num_channels=6):
return efficientnet_multichannel(pretrained=pretrained,name='b0',num_channels=num_channels)
pred_model1 = model1.predict([X_val,X_val_stats], batch_size=1024, verbose=1)
 f1_score(pred_model1 )<train_model>
learn = Learner(data, efficientnetb0() ,metrics=[accuracy] ).to_fp16()
learn.path = Path('.. /' )
model2 = model()
model2.fit([X_train,X_train_stats], y_train, batch_size=BATCH_SIZE, epochs=EPOCHS, validation_data=([X_val,X_val_stats],y_val))<predict_on_test>
learn.unfreeze()
pred_model2 = model2.predict([X_val,X_val_stats], batch_size=1024, verbose=1)
 f1_score(pred_model2 )<compute_test_metric>
learn.fit_one_cycle(18,1e-3 )
pred_model = pred_model1*0.5 + pred_model2*0.5
max_t = f1_score(pred_model )<save_to_csv>
test_df = pd.read_csv('.. /input/test.csv')
 proc_test_df = generate_df(test_df.copy() )
pred_model1_test = model1.predict([X_test,test_stats], batch_size=1024, verbose=1)
 pred_model2_test = model2.predict([X_test,test_stats], batch_size=1024, verbose=1)
 pred_model_test = pred_model1_test*0.5 + pred_model2_test*0.5
pred_test =(pred_model_test>max_t ).astype(int)
 out_df = pd.DataFrame({"qid":test_df["qid"].values})
 out_df['prediction'] = pred_test
out_df.to_csv("submission.csv", index=False )<set_options>
data_test = MultiChannelImageList.from_df(df=proc_test_df,path='.. /input/test/')
 learn.data.add_test(data_test )
%reload_ext autoreload
%autoreload 2<import_modules>
preds, _ = learn.get_preds(DatasetType.Test )
import gc
import os
import pickle
import random
import re
import sklearn.metrics
import sklearn.model_selection
import time
import warnings
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from collections import OrderedDict
from tqdm import tqdm, tqdm_notebook
import functools
import multiprocessing
import sklearn.preprocessing
import unicodedata
import copy
import time
from sklearn.model_selection import train_test_split, StratifiedKFold
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data<define_variables>
preds_ = preds.argmax(dim=-1 )
def tf_seed_everything(seed):
os.environ['PYTHONHASHSEED'] = str(seed)
 random.seed(seed)
 np.random.seed(seed)
 tf.set_random_seed(seed )<find_best_params>
submission_df = pd.read_csv('.. /input/sample_submission.csv' )
def f1_curve(target, preds, t_min=0.01, t_max=0.99, steps=99):
curve = {}
for t in np.linspace(t_min, t_max, steps):
with warnings.catch_warnings() :
warnings.simplefilter("ignore")
 curve[t] = sklearn.metrics.f1_score(target, preds >= t)
 return pd.Series(curve ).sort_index()<choose_model_class>
submission_df.sirna = preds_.numpy().astype(int)
 submission_df.head(10 )
<init_hyperparams><EOS> 
submission_df.to_csv('submission.csv',index=False )