Kaggle competition: Ultrasound Nerve Segmentation
<SOS> metric: Dice Kaggle data source: ultrasound-nerve-segmentation<split>
!pip install git+https://github.com/qubvel/segmentation_models.pytorch
X_train, X_valid, Y_train, Y_valid = train_test_split(train, y_train, test_size=0.05, random_state=0 )<choose_model_class>
!git clone https://github.com/Bjarten/early-stopping-pytorch.git esp
RF_clf=RandomForestRegressor(random_state=42,n_jobs=-1)
<train_model>
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.utils.data import Dataset, DataLoader
import torchvision
from torchvision import transforms
import pandas as pd
import numpy as np
import os
from torchvision import transforms
from PIL import Image
import segmentation_models_pytorch as smp
from esp.pytorchtools import EarlyStopping
import matplotlib.pyplot as plt
RF_clf.fit(X_train,Y_train )<compute_test_metric>
def save_checkpoint(self, val_loss, model):
if self.verbose:
self.trace_func(f'Validation loss decreased({self.val_loss_min:.6f} --> {val_loss:.6f} ).Saving model...')
 with open(self.path, 'wb')as f:
pickle.dump(model, f)
 self.val_loss_min = val_loss
def evaluate(model, features, labels):
predictions = model.predict(features)
 errors = abs(predictions - labels)
 m = 100 * np.mean(errors / labels)
 accuracy = 100 - m
print('Model Performance')
 print('Average Error: {:0.4f} degrees.'.format(np.mean(errors)))
 print('Accuracy = {:0.2f}%.'.format(accuracy))
return accuracy<compute_test_metric>
EarlyStopping.save_checkpoint = save_checkpoint
print('valid Accuracy')
 valid_accuracy = evaluate(RF_clf, X_valid, Y_valid )<predict_on_test>
input_ = '.. /input/ultrasound-nerve-segmentation'
train_path = f'{input_}/train'
test_path = f'{input_}/test'
train_csv_path = 'train_annotation.csv'
test_path = '.. /input/ultrasound-nerve-segmentation/test'
RF_target=np.expm1(RF_clf.predict(test)) 
<create_dataframe>
def create_csv(data_path, out_csv_path, key_word='mask'):
to_delete = f'_{key_word}'
for file_name in os.listdir(data_path):
if key_word in file_name:
img = file_name.replace(to_delete, '')
 data = pd.DataFrame([img], index=['img'] ).transpose()
data.insert(0, 'mask', file_name)
 else:
if not os.path.exists(out_csv_path):
data.to_csv(out_csv_path, header=True, index=False)
 else:
data.to_csv(out_csv_path, mode='a', header=False, index=False )
NO_OF_FEATURES=1000
col = pd.DataFrame({'importance': RF_clf.feature_importances_, 'feature': train.columns} ).sort_values(
 by=['importance'], ascending=[False])[:NO_OF_FEATURES]['feature'].values<drop_column>
create_csv(data_path=train_path, out_csv_path=train_csv_path )
train=train[col]
test=test[col] 
<feature_engineering>
class ImageDataset(Dataset):
def __init__(self, df, root_dir, transform=None):
self.df = df
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
mask = Image.open(os.path.join(self.root_dir, self.df.iloc[idx, 0]))
image = Image.open(os.path.join(self.root_dir, self.df.iloc[idx, 1]))
if self.transform:
return self.transform(image), self.transform(mask)
 return image, mask
train["sum"] = train.sum(axis=1)
 test["sum"] = test.sum(axis=1)
 train["var"] = train.var(axis=1)
 test["var"] = test.var(axis=1)
 train["median"] = train.median(axis=1)
 test["median"] = test.median(axis=1)
 train["mean"] = train.mean(axis=1)
 test["mean"] = test.mean(axis=1)
 train["std"] = train.std(axis=1)
 test["std"] = test.std(axis=1)
 train["max"] = train.max(axis=1)
 test["max"] = test.max(axis=1)
 train["min"] =train.min(axis=1)
 test["min"] = test.min(axis=1)
 train["skew"] = train.skew(axis=1)
 test["skew"] = test.skew(axis=1 )<split>
train_df = pd.read_csv(train_csv_path )
X_train, X_valid, Y_train, Y_valid = train_test_split(train, y_train, test_size=0.1, random_state=0 )<train_model>
train_samples = ImageDataset(df=train_df, root_dir=train_path )
def run_lgb(X_train, Y_train, X_valid, Y_valid, test):
params = {
"objective" : "regression",
"metric" : "rmse",
"task": "train",
"boosting type":'dart',
"num_leaves" :100,
"learning_rate" : 0.01,
"bagging_fraction" : 0.8,
"feature_fraction" : 0.8,
"bagging_frequency" : 6,
"bagging_seed" : 42,
"verbosity" : -1,
"seed": 42
}
lgtrain = lgb.Dataset(X_train, label=Y_train)
 lgval = lgb.Dataset(X_valid, label=Y_valid)
 evals_result = {}
model = lgb.train(params, lgtrain, 5000,
valid_sets=[lgtrain, lgval],
early_stopping_rounds=300,
verbose_eval=100,
evals_result=evals_result)
 lgb_prediction = np.expm1(model.predict(test, num_iteration=model.best_iteration))
return lgb_prediction, model, evals_result<train_model>
ENCODER = 'vgg11_bn'
ENCODER_WEIGHTS = 'imagenet'
ACTIVATION = 'sigmoid'
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )
lgb_pred, model, evals_result = run_lgb(X_train, Y_train, X_valid, Y_valid, test)
 print("LightGBM Training Completed..." )<save_to_csv>
model = smp.Unet(
 encoder_name=ENCODER,
encoder_weights=ENCODER_WEIGHTS,
in_channels=1,
classes=1,
activation=ACTIVATION
)
sub=pd.read_csv('.. /input/sample_submission.csv')
 sub_rf = pd.DataFrame()
sub_rf["target"] = RF_target
sub_rf["ID"] = sub["ID"]
sub_rf.to_csv("sub_rf.csv", index=False)
 sub_lgb = pd.DataFrame()
sub_lgb["target"] = lgb_pred
sub_lgb["ID"] = sub["ID"]
sub_lgb.to_csv("sub_lgb.csv", index=False)
 sub["target"] =(sub_lgb["target"] + sub_rf['target'])/2
print(sub.head())
 sub.to_csv('sub_lgb_xgb.csv', index=False)
 sub.head()<import_modules>
loss = smp.utils.losses.DiceLoss()
metrics = [smp.utils.metrics.IoU() ]
optimizer = torch.optim.Adam
scheduler = lr_scheduler.StepLR
import gc
import numpy as np
import pandas as pd
import lightgbm as lgbm
from sklearn.model_selection import KFold
from sklearn.metrics import mean_squared_error<load_from_csv>
my_transforms = transforms.Compose([
transforms.Resize(size=(224, 224)) ,
transforms.ToTensor()
] )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 train['target'] = np.log1p(train['target'])
 test.insert(1,'target',-1.)<define_variables>
def split_df(df, fraction=0.8):
df_1 = df.sample(frac=fraction)
 return df_1, df.drop(df_1.index )
floatcolumns = []
intcolumns = []
for c in train.columns[2:]:
s = train[c].dtype
if(s=='float64'):
floatcolumns.append(c)
 else:
intcolumns.append(c )<concatenate>
def train(model, train_df, train_dir, optimizer, loss, metrics,
learning_rate=0.01, batch_size=20, epochs=10, patience=3,
scheduler=None, step_size=5, gamma=0.1, device='cpu', transform=None):
early_stopping = EarlyStopping(patience, path='best_model.pkl', verbose=True)
 optimizer = optimizer(model.parameters() , learning_rate)
 if scheduler:
scheduler = scheduler(optimizer, step_size, gamma)
 train_epoch = smp.utils.train.TrainEpoch(
 model, loss, metrics, optimizer, device, verbose=True 
)
 valid_epoch = smp.utils.train.ValidEpoch(
 model, loss, metrics, device, verbose=True 
)
 train_logs, valid_logs = [], []
for epoch in range(epochs):
train_dataframe, val_dataframe = split_df(train_df)
 train_dataset = ImageDataset(train_dataframe, train_dir, transform=transform)
 valid_dataset = ImageDataset(val_dataframe, train_dir, transform=transform)
 train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
 valid_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=False)
 print(f'
Epoch: {epoch+1}/{epochs}')
 train_log = train_epoch.run(train_loader)
 valid_log = valid_epoch.run(valid_loader)
 train_logs.append(train_log)
 valid_logs.append(valid_log)
 early_stopping(valid_log[loss.__name__], model)
 if early_stopping.early_stop:
print("Early stopping")
 break
if scheduler:
scheduler.step()
return train_logs, valid_logs
alldata = pd.concat([train,test])
 del train,test
gc.collect()<feature_engineering>
res = train(model=model,
train_df=train_df,
train_dir=train_path,
optimizer=optimizer,
loss=loss,
learning_rate=0.01,
metrics=metrics,
batch_size=20,
epochs=20,
scheduler=scheduler,
step_size=10,
patience=3,
device=DEVICE,
transform=my_transforms )
alldata['sumofzeros'] =(alldata[intcolumns]==0 ).sum(axis=1)
 train = alldata[alldata.target!=-1]
test = alldata[alldata.target==-1]
del alldata
gc.collect()<init_hyperparams>
import pickle
lgbm_params = {
'task': 'train',
'boosting_type': 'gbdt',
'objective': 'binary',
'metric': 'binary_logloss',
"learning_rate": 0.01,
"num_leaves": 100,
"feature_fraction":.5,
"bagging_fraction":.5,
"max_depth": -1,
"reg_alpha": 0.3,
"reg_lambda": 0.1,
"min_child_weight":10,
'zero_as_missing':True
}<define_search_space>
with open('results.pkl', 'wb')as f:
pickle.dump(res, f )
bestparams = {0:(387, 0.32549223081879874),
1:(363, 0.48964347828583543),
2:(389, 0.5646998803516646),
3:(412, 0.5738770523457457),
4:(371, 0.5016286734255448),
5:(433, 0.3353076747094824)}<define_variables>
with open('best_model.pkl', 'rb')as f:
best_model = pickle.load(f )
folds = KFold(n_splits=5, shuffle=True, random_state=42)
 oof_preds = np.zeros(( train.shape[0],6))
sub_preds = np.zeros(( test.shape[0],6))<split>
train_logs_df = pd.DataFrame(res[0])
 valid_logs_df = pd.DataFrame(res[1])
 res_dict = {'train': train_logs_df, 'valid': valid_logs_df}
for i in [0, 1, 2, 3, 4, 5]:
optimal_rounds, best_cv_score = bestparams[i]
print(i, optimal_rounds, best_cv_score)
 for n_fold,(trn_idx, val_idx)in enumerate(folds.split(train)) :
print(n_fold)
 trn_x, trn_y = train[feats].iloc[trn_idx], x[trn_idx]>i
val_x, val_y = train[feats].iloc[val_idx], x[val_idx]>i
clf = lgbm.train(lgbm_params,
lgbm.Dataset(trn_x,trn_y),
num_boost_round = optimal_rounds + 1,
verbose_eval=200)
 oof_preds[val_idx,i] = clf.predict(val_x, num_iteration=optimal_rounds + 1)
 sub_preds[:,i] += clf.predict(test[feats], num_iteration=optimal_rounds + 1)/ folds.n_splits
del clf
del trn_x, trn_y, val_x, val_y
gc.collect()<concatenate>
def rle_encoding(x):
dots = np.where(x.T.flatten() ==1)[0]
run_lengths = []
prev = -2
for b in dots:
if(b>prev+1): run_lengths.extend(( b+1, 0))
run_lengths[-1] += 1
prev = b
return run_lengths
off_preds_withbias = np.hstack([oof_preds,np.ones(shape=(oof_preds.shape[0],1)) ])
 sub_preds_withbias = np.hstack([sub_preds,np.ones(shape=(sub_preds.shape[0],1)) ] )<compute_test_metric>
from tqdm.notebook import tqdm
params = np.linalg.lstsq(off_preds_withbias, train.target)[0]<compute_test_metric>
imgs = [f for f in os.listdir(test_path)]
imgs = sorted(imgs, key=lambda s: int(s.split('.')[0]))
trainpreds = np.dot(off_preds_withbias,params)
 print(np.sqrt(mean_squared_error(train.target,trainpreds)) )<save_to_csv>
def create_csv_submission(model, data_path, img_list, out_path):
submission_df = pd.DataFrame(columns=['img', 'pixels'])
 model.to(DEVICE)
 model.eval()
for i, img in enumerate(tqdm(img_list)) :
x = Image.open(os.path.join(data_path, img))
x = my_transforms(x)
 x = x.unsqueeze(0 ).to(DEVICE)
 pred_mask = model.predict(x)
 pred_mask = pred_mask.cpu()
pred_mask = transforms.Resize(size=(420, 580))(pred_mask)
 encoding = rle_encoding(pred_mask)
 pixels = ' '.join(map(str, encoding))
submission_df.loc[i] = [str(i+1), pixels]
submission_df.to_csv(out_path, index=False )
<set_options><EOS> 
create_csv_submission(model=model,
data_path=test_path,
img_list=imgs,
out_path='submission.csv' )