Kaggle competition: Multi-label Bird Species Classification - NIPS 2013
df_submission = pd.DataFrame(columns=["img", "pixels"])
 for i, encoding in enumerate(encodings):
pixels = ' '.join(map(str, encoding))
df_submission.loc[i] = [str(i+1), pixels]
df_submission.to_csv('./submission.csv', index=False)
 print('Done!' )<install_modules>
! tar xf.. /input/bird-songs-pad-and-resize-spectrogram/spectrograms_resized.tar.bz2
!pip install git+https://github.com/qubvel/segmentation_models.pytorch<load_from_zip>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
!git clone https://github.com/Bjarten/early-stopping-pytorch.git esp<import_modules>
data_dir = Path('.. /input')
 label_dir = data_dir/'multilabel-bird-species-classification-nips2013/nips4b_bird_challenge_train_labels/NIPS4B_BIRD_CHALLENGE_TRAIN_LABELS'
spect_dir = Path('./spectrograms_resized' )
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<train_model>
df = pd.read_csv(label_dir/'nips4b_birdchallenge_train_labels.csv', skiprows=[0, 2])
 df.tail()
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<drop_column>
df.drop(df.columns[[1, 3]],axis=1,inplace=True)
 df.rename(columns={df.columns[0]:'file', df.columns[1]:'EMPTY'}, inplace=True)
 df = df[:-1]
df.fillna(0, inplace=True)
 df = df.astype('int32', errors='ignore')
 df['file'] = df['file'].apply(lambda fn: str(Path(fn ).with_suffix('')))
 df.tail()
EarlyStopping.save_checkpoint = save_checkpoint<define_variables>
tfms = get_transforms(do_flip=False, max_rotate=None, max_warp=None)
 data =(src.transform(tfms, size=128)
.databunch(num_workers=0 ).normalize(imagenet_stats))
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'<save_to_csv>
data.show_batch(rows=3, figsize=(12,9), ds_type=DatasetType.Valid )
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 )<save_to_csv>
arch = models.resnet50
acc_02 = partial(accuracy_thresh, thresh=0.2)
 learn = cnn_learner(data, arch, metrics=acc_02, path='.' )
create_csv(data_path=train_path, out_csv_path=train_csv_path )<categorify>
lr = 2.29E-02
learn.fit_one_cycle(5, slice(lr))
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<load_from_csv>
learn.save('stage-1-rn50', return_path=True )
train_df = pd.read_csv(train_csv_path )<create_dataframe>
learn.fit_one_cycle(5, slice(3.02E-03, lr/5))
train_samples = ImageDataset(df=train_df, root_dir=train_path )<define_variables>
learn.save('stage-2-rn50' )
ENCODER = 'vgg11_bn'
ENCODER_WEIGHTS = 'imagenet'
ACTIVATION = 'sigmoid'
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu' )<categorify>
data_256 =(src.transform(tfms, size=256)
.databunch(num_workers=0 ).normalize(imagenet_stats))
learn.data = data_256
learn.data.train_ds[0][0].shape
model = smp.Unet(
 encoder_name=ENCODER,
encoder_weights=ENCODER_WEIGHTS,
in_channels=1,
classes=1,
activation=ACTIVATION
)<choose_model_class>
lr = 5E-03
learn.fit_one_cycle(5, slice(lr))
loss = smp.utils.losses.DiceLoss()
metrics = [smp.utils.metrics.IoU() ]
optimizer = torch.optim.Adam
scheduler = lr_scheduler.StepLR<categorify>
learn.save('stage-1-256-rn50' )
my_transforms = transforms.Compose([
transforms.Resize(size=(224, 224)) ,
transforms.ToTensor()
] )<split>
learn.fit_one_cycle(5, slice(1.58E-06, lr/5))
def split_df(df, fraction=0.8):
df_1 = df.sample(frac=fraction)
 return df_1, df.drop(df_1.index )<train_model>
learn.save('stage-2-256-rn50' )
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<train_model>
learn.fit_one_cycle(5, slice(1.58E-06, lr/5))
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 )<import_modules>
learn.save('stage-2-256-rn50-10e' )
import pickle<save_to_csv>
test = ImageList.from_folder(spect_dir/'test/')
 len(test )
with open('results.pkl', 'wb')as f:
pickle.dump(res, f )<load_pretrained>
predictor = load_learner('.', test=test, num_workers=0)
 preds, _ = predictor.get_preds(ds_type=DatasetType.Test)
 fpreds = preds[:, 1:].reshape(-1, )
with open('best_model.pkl', 'rb')as f:
best_model = pickle.load(f )<create_dataframe>
names = [f.stem for f in predictor.data.test_ds.items]
fnames = [x + '.wav_classnumber_' + str(i)for x in names for i in range(1, len(data.classes)) ]
<categorify><EOS> 
test_df = pd.DataFrame({'ID':fnames, 'Probability':fpreds}, columns=['ID', 'Probability'])
 test_df.to_csv('submission.csv', index=False )