Kaggle competition: Cornell Birdcall Identification
<SOS> metric: MeanFScoreBeta Kaggle data source: cornell-birdcall-identification<load_from_csv>
pd.options.display.max_rows = 500
pd.options.display.max_columns = 500
df = pd.read_csv(sample_submission_path)
 test_dataset = SteelDataset(df)
 test_loader = DataLoader(
 test_dataset,
batch_size = 2,
drop_last = False,
num_workers = 0,
pin_memory = True,
collate_fn = null_collate
)<normalization>
def load_audio(path, sr):
clip, _ = librosa.load(path, sr=sr, mono=True, res_type="kaiser_fast")
 return clip
def null_augment(input): return input
def flip_lr_augment(input): return torch.flip(input, dims=[2])
 def flip_ud_augment(input): return torch.flip(input, dims=[3])
 def null_inverse_augment(logit): return logit
def flip_lr_inverse_augment(logit): return torch.flip(logit, dims=[2])
 def flip_ud_inverse_augment(logit): return torch.flip(logit, dims=[3])
 augment =(
(null_augment, null_inverse_augment), 
(flip_lr_augment,flip_lr_inverse_augment), 
(flip_ud_augment,flip_ud_inverse_augment),
)<categorify>
def load_model_weights(model, weights):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 state_dict = torch.load(weights, map_location=device)
 model.load_state_dict(state_dict )
TEMPERATE=0.5
def probability_mask_to_probability_label(probability):
batch_size,num_class,H,W = probability.shape
probability = probability.permute(0, 2, 3, 1 ).contiguous().view(batch_size,-1, 5)
 value, index = probability.max(1)
 probability = value[:,1:]
return probability
def remove_small_one(predict, min_size):
H,W = predict.shape
num_component, component = cv2.connectedComponents(predict.astype(np.uint8))
predict = np.zeros(( H,W), np.bool)
 for c in range(1,num_component):
p =(component==c)
 if p.sum() >min_size:
predict[p] = True
return predict
def remove_small(predict, min_size):
for b in range(len(predict)) :
for c in range(4):
predict[b,c] = remove_small_one(predict[b,c], min_size[c])
 return predict<normalization>
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
torch.backends.cudnn.benchmark = True
def do_evaluate_segmentation(net, test_loader, augment=[]):
def sharpen(p,t=TEMPERATE):
if t!=0:
return p**t
else:
return p
test_num = 0
test_id = []
test_probability_label = []
test_probability_mask = []
test_truth_label = []
test_truth_mask = []
for t,(fnames, input)in enumerate(tqdm(test_loader)) :
batch_size,C,H,W = input.shape
input = input.cuda()
with torch.no_grad() :
net.eval()
num_augment = 0
if 1:
logit = net(input)
 probability = torch.softmax(logit,1)
 probability_mask = sharpen(probability,0)
 num_augment+=1
if 'flip_lr' in augment:
logit = net(torch.flip(input,dims=[3]))
probability = torch.softmax(torch.flip(logit,dims=[3]),1)
 probability_mask += sharpen(probability)
 num_augment+=1
if 'flip_ud' in augment:
logit = net(torch.flip(input,dims=[2]))
probability = torch.softmax(torch.flip(logit,dims=[2]),1)
 probability_mask += sharpen(probability)
 num_augment+=1
probability_mask = probability_mask/num_augment
probability_label = probability_mask_to_probability_label(probability_mask)
 probability_mask =(probability_mask.data.cpu().numpy() *255 ).astype(np.uint8)
 probability_label =(probability_label.data.cpu().numpy() *255 ).astype(np.uint8)
 test_id.extend([i for i in fnames])
 test_probability_mask.append(probability_mask)
 test_probability_label.append(probability_label)
 test_probability_mask = np.concatenate(test_probability_mask)
 test_probability_label = np.concatenate(test_probability_label)
 return test_probability_label, test_probability_mask, test_id 
<set_options>
SEED = 1213
seed_everything(SEED )
ckpt_file = '.. /input/henge5/trace_model_swa.pth'
net = torch.jit.load(ckpt_file ).cuda()<split>
ROOT = Path.cwd().parent
INPUT_ROOT = ROOT / "input"
RAW_DATA = INPUT_ROOT / "birdsong-recognition"
TRAIN_AUDIO_DIR = RAW_DATA / "train_audio"
probability_label, probability_mask, image_id = do_evaluate_segmentation(net, test_loader, augment=['null'] )<set_options>
train = pd.read_csv(RAW_DATA / "train.csv" )
del net
gc.collect()<define_variables>
TEST_AUDIO_DIR = RAW_DATA / "test_audio"
if not TEST_AUDIO_DIR.exists() :
TEST_AUDIO_DIR = INPUT_ROOT / "birdcall-check" / "test_audio"
test = pd.read_csv(INPUT_ROOT / "birdcall-check" / "test.csv")
 else:
test = pd.read_csv(RAW_DATA / "test.csv" )
threshold_label = [ 0.70, 0.8, 0.50, 0.70,]
threshold_mask_pixel = [ 0.6, 0.8, 0.5, 0.6,]
threshold_mask_size = [ 1, 1, 1, 1,]<data_type_conversions>
CLASSES = sorted(os.listdir(TRAIN_AUDIO_DIR))
NUM_CLASSES = len(CLASSES)
 NUM_WORKERS = 4
predict_label = probability_label>(np.array(threshold_label)*255 ).astype(np.uint8 ).reshape(1,4)
 predict_mask = probability_mask>(np.array(threshold_mask_pixel)*255 ).astype(np.uint8 ).reshape(1,4,1,1 )<categorify>
class AudioParams:
sr = 32000
stride = 5
true_kernel_size = 5
img_size = None
n_mels = 128
fmin = 20
fmax = 16000
def mask2rle(img):

 pixels= img.T.flatten()
pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return ' '.join(str(x)for x in runs )<categorify>
def convert_site_3(df, clip_length, params):
n_samples = clip_length //(params.sr * params.true_kernel_size)
 audio_id = [df['audio_id'].values[0]] * n_samples
site = ['site_3'] * n_samples
seconds = [i * params.true_kernel_size for i in range(1, n_samples + 1)]
row_id = [f'site_3_{audio_id[0]}_{int(s)}' for s in seconds]
new_df = pd.DataFrame(data={'site': site,
'row_id': row_id,
'seconds': seconds,
'audio_id': audio_id
})
 return new_df
image_id_class_id = []
encoded_pixel = []
for b in range(len(image_id)) :
for c in range(4):
image_id_class_id.append(image_id[b]+'_%d'%(c+1))
if predict_label[b,c]==0:
rle=''
else:
rle = mask2rle(predict_mask[b,c])
 encoded_pixel.append(rle )<save_to_csv>
def compute_melspec(y, params):
melspec = librosa.feature.melspectrogram(
 y,
sr=params.sr,
n_mels=params.n_mels,
fmin=params.fmin,
fmax=params.fmax 
)
 melspec = librosa.power_to_db(melspec ).astype(np.float32)
 return melspec
df = pd.DataFrame(zip(image_id_class_id, encoded_pixel), columns=['ImageId_ClassId', 'EncodedPixels'])
 df.to_csv('submission.csv', index=False )<data_type_conversions>
def mono_to_color(X, eps=1e-6, mean=None, std=None):
X = np.stack([X, X, X], axis=-1)
 mean = mean or X.mean()
std = std or X.std()
X =(X - mean)/(std + eps)
 _min, _max = X.min() , X.max()
if(_max - _min)> eps:
V = np.clip(X, _min, _max)
 V = 255 *(V - _min)/(_max - _min)
 V = V.astype(np.uint8)
 else:
V = np.zeros_like(X, dtype=np.uint8)
 return V
def resize(image, size=None):
if size is not None:
h, w, _ = image.shape
new_w, new_h = int(w * size / h), size
image = cv2.resize(image,(new_w, new_h))
return image
def normalize(image, mean=None, std=None):
image = image / 255.0
if mean is not None and std is not None:
image =(image - mean)/ std
return np.moveaxis(image, 2, 0 ).astype(np.float32 )
def summarise_submission_csv(df):
text = ''
df['Class'] = df['ImageId_ClassId'].str[-1].astype(np.int32)
 df['Label'] =(df['EncodedPixels']!='' ).astype(np.int32)
 num_image = len(df)//4
num = len(df)
 pos =(df['Label']==1 ).sum()
neg = num-pos
pos1 =(( df['Class']==1)&(df['Label']==1)).sum()
pos2 =(( df['Class']==2)&(df['Label']==1)).sum()
pos3 =(( df['Class']==3)&(df['Label']==1)).sum()
pos4 =(( df['Class']==4)&(df['Label']==1)).sum()
neg1 = num_image-pos1
neg2 = num_image-pos2
neg3 = num_image-pos3
neg4 = num_image-pos4
text += 'compare with LB probing...
 '
text += '\t\tnum_image = %5d(1801)
 '%num_image
text += '\t\tnum = %5d(7204)
 '%num
text += '
'
text += '\t\tpos1 = %5d(128)%0.3f
'%(pos1,pos1/128)
 text += '\t\tpos2 = %5d(43)%0.3f
'%(pos2,pos2/43)
 text += '\t\tpos3 = %5d(741)%0.3f
'%(pos3,pos3/741)
 text += '\t\tpos4 = %5d(120)%0.3f
'%(pos4,pos4/120)
 text += '
'
text += '\t\tneg1 = %5d(1673)%0.3f %3d
'%(neg1,neg1/1673, neg1-1673)
 text += '\t\tneg2 = %5d(1758)%0.3f %3d
'%(neg2,neg2/1758, neg2-1758)
 text += '\t\tneg3 = %5d(1060)%0.3f %3d
'%(neg3,neg3/1060, neg3-1060)
 text += '\t\tneg4 = %5d(1681)%0.3f %3d
'%(neg4,neg4/1681, neg4-1681)
 text += '--------------------------------------------------
'
text += '\t\tneg = %5d(6172)%0.3f %3d
'%(neg,neg/6172, neg-6172)
 text += '
'
if 1:
pass
return text<categorify>
class TestDataset(data.Dataset):
def __init__(self, df, clip, params):
self.df = df
self.clip = clip
self.params = params
if df['site'].values[0] == 'site_3':
self.df = convert_site_3(df, len(clip), params)
 def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
end_seconds = int(self.df['seconds'][idx])
 start_seconds = int(end_seconds - 5)
 start_index = self.params.sr * start_seconds
end_index = self.params.sr * end_seconds
y = self.clip[start_index:end_index].astype(np.float32)
 melspec = compute_melspec(y, self.params)
 image = mono_to_color(melspec)
 image = resize(image, self.params.img_size)
 image = normalize(image, mean=None, std=None)
 return image
def rle2mask(mask_rle, shape=(1600,256)) :

 s = mask_rle.split()
starts, lengths = [np.asarray(x, dtype=int)for x in(s[0:][::2], s[1:][::2])]
starts -= 1
ends = starts + lengths
img = np.zeros(shape[0]*shape[1], dtype=np.uint8)
 for lo, hi in zip(starts, ends):
img[lo:hi] = 1
return img.reshape(shape ).T 
<feature_engineering>
class SplAtConv2d(Module):

 def __init__(self, in_channels, channels, kernel_size, stride=(1, 1), padding=(0, 0),
dilation=(1, 1), groups=1, bias=True,
radix=2, reduction_factor=4,
rectify=False, rectify_avg=False, norm_layer=None,
dropblock_prob=0.0, **kwargs):
super(SplAtConv2d, self ).__init__()
padding = _pair(padding)
 self.rectify = rectify and(padding[0] > 0 or padding[1] > 0)
 self.rectify_avg = rectify_avg
inter_channels = max(in_channels*radix//reduction_factor, 32)
 self.radix = radix
self.cardinality = groups
self.channels = channels
self.dropblock_prob = dropblock_prob
if self.rectify:
self.conv = RFConv2d(in_channels, channels*radix, kernel_size, stride, padding, dilation,
groups=groups*radix, bias=bias, average_mode=rectify_avg, **kwargs)
 else:
self.conv = Conv2d(in_channels, channels*radix, kernel_size, stride, padding, dilation,
groups=groups*radix, bias=bias, **kwargs)
 self.use_bn = norm_layer is not None
if self.use_bn:
self.bn0 = norm_layer(channels*radix)
 self.relu = ReLU(inplace=True)
 self.fc1 = Conv2d(channels, inter_channels, 1, groups=self.cardinality)
 if self.use_bn:
self.bn1 = norm_layer(inter_channels)
 self.fc2 = Conv2d(inter_channels, channels*radix, 1, groups=self.cardinality)
 if dropblock_prob > 0.0:
self.dropblock = DropBlock2D(dropblock_prob, 3)
 self.rsoftmax = rSoftMax(radix, groups)
 def forward(self, x):
x = self.conv(x)
 if self.use_bn:
x = self.bn0(x)
 if self.dropblock_prob > 0.0:
x = self.dropblock(x)
 x = self.relu(x)
 batch, rchannel = x.shape[:2]
if self.radix > 1:
if torch.__version__ < '1.5':
splited = torch.split(x, int(rchannel//self.radix), dim=1)
 else:
splited = torch.split(x, rchannel//self.radix, dim=1)
 gap = sum(splited)
 else:
gap = x
gap = F.adaptive_avg_pool2d(gap, 1)
 gap = self.fc1(gap)
 if self.use_bn:
gap = self.bn1(gap)
 gap = self.relu(gap)
 atten = self.fc2(gap)
 atten = self.rsoftmax(atten ).view(batch, -1, 1, 1)
 if self.radix > 1:
if torch.__version__ < '1.5':
attens = torch.split(atten, int(rchannel//self.radix), dim=1)
 else:
attens = torch.split(atten, rchannel//self.radix, dim=1)
 out = sum([att*split for(att, split)in zip(attens, splited)])
 else:
out = atten * x
return out.contiguous()
class rSoftMax(nn.Module):
def __init__(self, radix, cardinality):
super().__init__()
self.radix = radix
self.cardinality = cardinality
def forward(self, x):
batch = x.size(0)
 if self.radix > 1:
x = x.view(batch, self.cardinality, self.radix, -1 ).transpose(1, 2)
 x = F.softmax(x, dim=1)
 x = x.reshape(batch, -1)
 else:
x = torch.sigmoid(x)
 return x
%matplotlib inline
df = pd.read_csv('submission.csv')[:60]
df['Image'] = df['ImageId_ClassId'].map(lambda x: x.split('_')[0])
 df['Class'] = df['ImageId_ClassId'].map(lambda x: x.split('_')[1])
 for row in df.itertuples() :
img_path = os.path.join(test_data_folder, row.Image)
 img = cv2.imread(img_path)
 mask = rle2mask(row.EncodedPixels,(1600, 256)) \
if isinstance(row.EncodedPixels, str)else np.zeros(( 256, 1600))
if mask.sum() == 0:
continue
fig, axes = plt.subplots(1, 2, figsize=(20, 60))
axes[0].imshow(img/255)
 axes[1].imshow(mask*60)
 axes[0].set_title(row.Image)
 axes[1].set_title(row.Class)
 plt.show()<import_modules>
class DropBlock2D(object):
def __init__(self, *args, **kwargs):
raise NotImplementedError
class GlobalAvgPool2d(nn.Module):
def __init__(self):

 super(GlobalAvgPool2d, self ).__init__()
def forward(self, inputs):
return nn.functional.adaptive_avg_pool2d(inputs, 1 ).view(inputs.size(0), -1)
 class Bottleneck(nn.Module):

 expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None,
radix=1, cardinality=1, bottleneck_width=64,
avd=False, avd_first=False, dilation=1, is_first=False,
rectified_conv=False, rectify_avg=False,
norm_layer=None, dropblock_prob=0.0, last_gamma=False):
super(Bottleneck, self ).__init__()
group_width = int(planes *(bottleneck_width / 64.))* cardinality
self.conv1 = nn.Conv2d(inplanes, group_width, kernel_size=1, bias=False)
 self.bn1 = norm_layer(group_width)
 self.dropblock_prob = dropblock_prob
self.radix = radix
self.avd = avd and(stride > 1 or is_first)
 self.avd_first = avd_first
if self.avd:
self.avd_layer = nn.AvgPool2d(3, stride, padding=1)
 stride = 1
if dropblock_prob > 0.0:
self.dropblock1 = DropBlock2D(dropblock_prob, 3)
 if radix == 1:
self.dropblock2 = DropBlock2D(dropblock_prob, 3)
 self.dropblock3 = DropBlock2D(dropblock_prob, 3)
 if radix >= 1:
self.conv2 = SplAtConv2d(
 group_width, group_width, kernel_size=3,
stride=stride, padding=dilation,
dilation=dilation, groups=cardinality, bias=False,
radix=radix, rectify=rectified_conv,
rectify_avg=rectify_avg,
norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 elif rectified_conv:
self.conv2 = RFConv2d(
 group_width, group_width, kernel_size=3, stride=stride,
padding=dilation, dilation=dilation,
groups=cardinality, bias=False,
average_mode=rectify_avg)
 self.bn2 = norm_layer(group_width)
 else:
self.conv2 = nn.Conv2d(
 group_width, group_width, kernel_size=3, stride=stride,
padding=dilation, dilation=dilation,
groups=cardinality, bias=False)
 self.bn2 = norm_layer(group_width)
 self.conv3 = nn.Conv2d(
 group_width, planes * 4, kernel_size=1, bias=False)
 self.bn3 = norm_layer(planes*4)
 if last_gamma:
zeros_(self.bn3.weight)
 self.relu = nn.ReLU(inplace=True)
 self.downsample = downsample
self.dilation = dilation
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
 out = self.bn1(out)
 if self.dropblock_prob > 0.0:
out = self.dropblock1(out)
 out = self.relu(out)
 if self.avd and self.avd_first:
out = self.avd_layer(out)
 out = self.conv2(out)
 if self.radix == 0:
out = self.bn2(out)
 if self.dropblock_prob > 0.0:
out = self.dropblock2(out)
 out = self.relu(out)
 if self.avd and not self.avd_first:
out = self.avd_layer(out)
 out = self.conv3(out)
 out = self.bn3(out)
 if self.dropblock_prob > 0.0:
out = self.dropblock3(out)
 if self.downsample is not None:
residual = self.downsample(x)
 out += residual
out = self.relu(out)
 return out
class ResNet(nn.Module):

 def __init__(self, block, layers, radix=1, groups=1, bottleneck_width=64,
num_classes=1000, dilated=False, dilation=1,
deep_stem=False, stem_width=64, avg_down=False,
rectified_conv=False, rectify_avg=False,
avd=False, avd_first=False,
final_drop=0.0, dropblock_prob=0,
last_gamma=False, norm_layer=nn.BatchNorm2d):
self.cardinality = groups
self.bottleneck_width = bottleneck_width
self.inplanes = stem_width*2 if deep_stem else 64
self.avg_down = avg_down
self.last_gamma = last_gamma
self.radix = radix
self.avd = avd
self.avd_first = avd_first
super(ResNet, self ).__init__()
self.rectified_conv = rectified_conv
self.rectify_avg = rectify_avg
if rectified_conv:
conv_layer = RFConv2d
else:
conv_layer = nn.Conv2d
conv_kwargs = {'average_mode': rectify_avg} if rectified_conv else {}
if deep_stem:
self.conv1 = nn.Sequential(
 conv_layer(3, stem_width, kernel_size=3, stride=2, padding=1, bias=False, **conv_kwargs),
norm_layer(stem_width),
nn.ReLU(inplace=True),
conv_layer(stem_width, stem_width, kernel_size=3, stride=1, padding=1, bias=False, **conv_kwargs),
norm_layer(stem_width),
nn.ReLU(inplace=True),
conv_layer(stem_width, stem_width*2, kernel_size=3, stride=1, padding=1, bias=False, **conv_kwargs), 
)
 else:
self.conv1 = conv_layer(3, 64, kernel_size=7, stride=2, padding=3,
bias=False, **conv_kwargs)
 self.bn1 = norm_layer(self.inplanes)
 self.relu = nn.ReLU(inplace=True)
 self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
 self.layer1 = self._make_layer(block, 64, layers[0], norm_layer=norm_layer, is_first=False)
 self.layer2 = self._make_layer(block, 128, layers[1], stride=2, norm_layer=norm_layer)
 if dilated or dilation == 4:
self.layer3 = self._make_layer(block, 256, layers[2], stride=1,
dilation=2, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.layer4 = self._make_layer(block, 512, layers[3], stride=1,
dilation=4, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 elif dilation==2:
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
dilation=1, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.layer4 = self._make_layer(block, 512, layers[3], stride=1,
dilation=2, norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 else:
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
norm_layer=norm_layer,
dropblock_prob=dropblock_prob)
 self.avgpool = GlobalAvgPool2d()
self.drop = nn.Dropout(final_drop)if final_drop > 0.0 else None
self.fc = nn.Linear(512 * block.expansion, num_classes)
 for m in self.modules() :
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2./ n))
elif isinstance(m, norm_layer):
m.weight.data.fill_(1)
 m.bias.data.zero_()
def _make_layer(self, block, planes, blocks, stride=1, dilation=1, norm_layer=None,
dropblock_prob=0.0, is_first=True):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
down_layers = []
if self.avg_down:
if dilation == 1:
down_layers.append(nn.AvgPool2d(kernel_size=stride, stride=stride,
ceil_mode=True, count_include_pad=False))
else:
down_layers.append(nn.AvgPool2d(kernel_size=1, stride=1,
ceil_mode=True, count_include_pad=False))
down_layers.append(nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=1, bias=False))
else:
down_layers.append(nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False))
down_layers.append(norm_layer(planes * block.expansion))
downsample = nn.Sequential(*down_layers)
 layers = []
if dilation == 1 or dilation == 2:
layers.append(block(self.inplanes, planes, stride, downsample=downsample,
radix=self.radix, cardinality=self.cardinality,
bottleneck_width=self.bottleneck_width,
avd=self.avd, avd_first=self.avd_first,
dilation=1, is_first=is_first, rectified_conv=self.rectified_conv,
rectify_avg=self.rectify_avg,
norm_layer=norm_layer, dropblock_prob=dropblock_prob,
last_gamma=self.last_gamma))
elif dilation == 4:
layers.append(block(self.inplanes, planes, stride, downsample=downsample,
radix=self.radix, cardinality=self.cardinality,
bottleneck_width=self.bottleneck_width,
avd=self.avd, avd_first=self.avd_first,
dilation=2, is_first=is_first, rectified_conv=self.rectified_conv,
rectify_avg=self.rectify_avg,
norm_layer=norm_layer, dropblock_prob=dropblock_prob,
last_gamma=self.last_gamma))
else:
raise RuntimeError("=> unknown dilation size: {}".format(dilation))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes,
radix=self.radix, cardinality=self.cardinality,
bottleneck_width=self.bottleneck_width,
avd=self.avd, avd_first=self.avd_first,
dilation=dilation, rectified_conv=self.rectified_conv,
rectify_avg=self.rectify_avg,
norm_layer=norm_layer, dropblock_prob=dropblock_prob,
last_gamma=self.last_gamma))
return nn.Sequential(*layers)
 def forward(self, x):
x = self.conv1(x)
 x = self.bn1(x)
 x = self.relu(x)
 x = self.maxpool(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 x = self.avgpool(x)
 x = torch.flatten(x, 1)
 if self.drop:
x = self.drop(x)
 x = self.fc(x)
 return x
import mxnet as mx
from mxnet.gluon import data, HybridBlock, nn
import pandas as pd
import cv2
import os
import numpy as np
from mxnet.gluon.data.vision import transforms
from mxnet.gluon.model_zoo import vision
import glob
from mxnet import nd as F, gluon
from gluoncv import model_zoo as gm<set_options>
MODEL_CONFIGS = {
"resnest50_fast_1s1x64d":
{
"num_classes": 264,
"block": Bottleneck,
"layers": [3, 4, 6, 3],
"radix": 1,
"groups": 1,
"bottleneck_width": 64,
"deep_stem": True,
"stem_width": 32,
"avg_down": True,
"avd" : True,
"avd_first" : True
},
"resnest101":
{
"num_classes": 264,
"block": Bottleneck,
"layers": [3, 4, 23, 3],
"radix": 2,
"groups": 1,
"bottleneck_width": 64,
"deep_stem": True,
"stem_width": 64,
"avg_down": True,
"avd" : True,
"avd_first" : False
},
}
%pylab inline<define_variables>
def get_model(name):
if "resnest" in name:
model = ResNet(**MODEL_CONFIGS[name])
 elif "resnext101" in name:
model = torchvision.models.resnext101_32x8d(pretrained=False)
 elif "resnext50" in name:
model = torchvision.models.resnext50_32x4d(pretrained=False)
 else:
raise NotImplementedError
nb_ft = model.fc.in_features
del model.fc
model.fc = nn.Linear(nb_ft, NUM_CLASSES)
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 model.to(device)
 model.eval()
return model
ls.. /input/mxnet-gluon-baseline/model/<import_modules>
def predict(model, dataset, batch_size=16):
model.eval()
preds = np.empty(( 0, NUM_CLASSES))
loader = DataLoader(
 dataset, batch_size=batch_size, shuffle=False, num_workers=NUM_WORKERS, pin_memory=True 
)
 with torch.no_grad() :
for x in loader:
y_pred = model(x.cuda() ).detach()
preds = np.concatenate([preds, torch.sigmoid(y_pred ).cpu().numpy() ])
 return preds
class ResNetBackbone(mx.gluon.HybridBlock):
def __init__(self, backbone='resnet50', pretrained_base=True,dilated=True, **kwargs):
super(ResNetBackbone, self ).__init__()
with self.name_scope() :
if backbone == 'resnet50':
pretrained = resnet50_v1s(pretrained=pretrained_base, dilated=dilated, **kwargs)
 elif backbone == 'resnet101':
pretrained = resnet101_v1s(pretrained=pretrained_base, dilated=dilated, **kwargs)
 elif backbone == 'resnet152':
pretrained = resnet152_v1s(pretrained=pretrained_base, dilated=dilated, **kwargs)
 else:
raise RuntimeError(f'unknown backbone: {backbone}')
 self.conv1 = pretrained.conv1
self.bn1 = pretrained.bn1
self.relu = pretrained.relu
self.maxpool = pretrained.maxpool
self.layer1 = pretrained.layer1
self.layer2 = pretrained.layer2
self.layer3 = pretrained.layer3
self.layer4 = pretrained.layer4
def hybrid_forward(self, F, x):
x = self.conv1(x)
 x = self.bn1(x)
 x = self.relu(x)
 x = self.maxpool(x)
 c1 = self.layer1(x)
 c2 = self.layer2(c1)
 c3 = self.layer3(c2)
 c4 = self.layer4(c3)
 return c1, c2, c3, c4<choose_model_class>
def post_process_site_12(preds, threshold=0.5, maxpreds=3):
preds = preds *(preds >= threshold)
 next_preds = np.concatenate([preds[1:], np.zeros(( 1, preds.shape[-1])) ])
 prev_preds = np.concatenate([np.zeros(( 1, preds.shape[-1])) , preds[:-1]])
 score = preds + 0.5 * next_preds + 0.5 * prev_preds
n_birds =(score >= threshold ).sum(-1)
 n_birds = np.clip(n_birds, 0, maxpreds)
 labels = [np.argsort(- score[i])[:n_birds[i]] for i in range(len(preds)) ]
class_labels = [" ".join([CLASSES[l] for l in label])for label in labels]
return class_labels
class ResNetFPN(mx.gluon.HybridBlock):
def __init__(self, backbone= 'resnet50', backbone_lr_mult=0.1, **kwargs):
super(ResNetFPN, self ).__init__()
self.backbone_name = backbone
self.backbone_lr_mult = backbone_lr_mult
self._kwargs = kwargs
with self.name_scope() :
self.backbone = ResNetBackbone(backbone=self.backbone_name, pretrained_base=False, dilated=False, **kwargs)
 self.head = _FPNHead(output_channels=256, **kwargs)
 def load_pretrained_weights(self):
pretrained = ResNetBackbone(backbone=self.backbone_name, pretrained_base=True, dilated=False, **self._kwargs)
 backbone_params = self.backbone.collect_params()
pretrained_weights = pretrained.collect_params()
for k, v in pretrained_weights.items() :
param_name = backbone_params.prefix + k[len(pretrained_weights.prefix):]
backbone_params[param_name].set_data(v.data())
 self.backbone.collect_params().setattr('lr_mult', self.backbone_lr_mult)
 def hybrid_forward(self,F, x):
c1, c2, c3, c4 = self.backbone(x)
 p1, p2, p3, p4 = self.head(c1, c2, c3, c4)
 return p1, p2, p3, p4
class ResNetUnet(mx.gluon.HybridBlock):
def __init__(self, backbone= 'resnet101', backbone_lr_mult=0.1, cls_branch=False, **kwargs):
super(ResNetUnet, self ).__init__()
self.backbone_name = backbone
self.backbone_lr_mult = backbone_lr_mult
self.cls_branch = cls_branch
self._kwargs = kwargs
with self.name_scope() :
self.backbone = ResNetBackbone(backbone=self.backbone_name, pretrained_base=False, dilated=False, **kwargs)
 self.head = _UnetHead(**kwargs)
 def load_pretrained_weights(self):
pretrained = ResNetBackbone(backbone=self.backbone_name, pretrained_base=True, dilated=False, **self._kwargs)
 backbone_params = self.backbone.collect_params()
pretrained_weights = pretrained.collect_params()
for k, v in pretrained_weights.items() :
param_name = backbone_params.prefix + k[len(pretrained_weights.prefix):]
backbone_params[param_name].set_data(v.data())
 self.backbone.collect_params().setattr('lr_mult', self.backbone_lr_mult)
 def hybrid_forward(self,F, x):
c1, c2, c3, c4 = self.backbone(x)
 out = self.head(c1, c2, c3, c4)
 if self.cls_branch:
logits = F.max(F.mean(out, axis=1), axis=(1, 2))
return out, logits
return out
class _DecoderBlock(HybridBlock):
def __init__(self, output_channels, norm_layer=nn.BatchNorm):
super(_DecoderBlock, self ).__init__()
with self.name_scope() :
self.block = nn.HybridSequential()
self.block.add(ConvBlock(output_channels, kernel_size=3, padding=1, norm_layer=norm_layer))
self.block.add(ConvBlock(output_channels, kernel_size=3, padding=1, norm_layer=norm_layer))
def hybrid_forward(self, F, x, y=None):
if y is not None:
x = F.contrib.BilinearResize2D(x, scale_height=2, scale_width=2)
 x = F.concat(x, y, dim=1)
 out = self.block(x)
 return out
class _UnetHead(HybridBlock):
def __init__(self, num_classes, output_channels=[256, 128, 64, 32], scale=4, norm_layer=nn.BatchNorm):
super(_UnetHead, self ).__init__()
self.scale = scale
with self.name_scope() :
self.block4 = _DecoderBlock(output_channels[0], norm_layer=norm_layer)
 self.block3 = _DecoderBlock(output_channels[1], norm_layer=norm_layer)
 self.block2 = _DecoderBlock(output_channels[2], norm_layer=norm_layer)
 self.block1 = _DecoderBlock(output_channels[3], norm_layer=norm_layer)
 self.postprocess_block = nn.Conv2D(num_classes, kernel_size=1)
 def hybrid_forward(self, F, c1, c2, c3, c4):
p4 = self.block4(c4)
 p3 = self.block3(p4, c3)
 p2 = self.block2(p3, c2)
 p1 = self.block1(p2, c1)
 if self.scale > 1:
p1 = F.contrib.BilinearResize2D(p1, scale_height=self.scale, scale_width=self.scale)
 out = self.postprocess_block(p1)
 return out
class _FPNHead(HybridBlock):
def __init__(self, output_channels=256, norm_layer=nn.BatchNorm):
super(_FPNHead, self ).__init__()
self._hdsize = {}
with self.name_scope() :
self.block4 = ConvBlock(output_channels, kernel_size=1, norm_layer=norm_layer)
 self.block3 = ConvBlock(output_channels, kernel_size=1, norm_layer=norm_layer)
 self.block2 = ConvBlock(output_channels, kernel_size=1, norm_layer=norm_layer)
 self.block1 = ConvBlock(output_channels, kernel_size=1, norm_layer=norm_layer)
 def hybrid_forward(self, F, c1, c2, c3, c4):
p4 = self.block4(c4)
 p3 = self._resize_as(F, 'id_1', p4, c3)+ self.block3(c3)
 p2 = self._resize_as(F, 'id_2', p3, c2)+ self.block2(c2)
 p1 = self._resize_as(F, 'id_3', p2, c1)+ self.block1(c1)
 return p1, p2, p3, p4
def _resize_as(self, F, name, x, y):
h_key = name + '_h'
w_key = name + '_w'
if hasattr(y, 'shape'):
_, _, h, w = y.shape
_, _, h2, w2 = x.shape
if h == h2 and w == w2:
h = 0
w = 0
self._hdsize[h_key] = h
self._hdsize[w_key] = w
else:
h, w = self._hdsize[h_key], self._hdsize[w_key]
if h == 0 and w == 0:
return x
else:
return F.contrib.BilinearResize2D(x, height=h, width=w)
 class SemanticFPNHead(HybridBlock):
def __init__(self, num_classes, output_channels=128, norm_layer=nn.BatchNorm):
super(SemanticFPNHead, self ).__init__()
self._hdsize = {}
with self.name_scope() :
self.block4_1 = ConvBlock(output_channels, kernel_size=3, padding=1, norm_layer=norm_layer)
 self.block4_2 = ConvBlock(output_channels, kernel_size=3, padding=1, norm_layer=norm_layer)
 self.block4_3 = ConvBlock(output_channels, kernel_size=3, padding=1, norm_layer=norm_layer)
 self.block3_1 = ConvBlock(output_channels, kernel_size=3, padding=1, norm_layer=norm_layer)
 self.block3_2 = ConvBlock(output_channels, kernel_size=3, padding=1, norm_layer=norm_layer)
 self.block2 = ConvBlock(output_channels, kernel_size=3, padding=1, norm_layer=norm_layer)
 self.block1 = ConvBlock(output_channels, kernel_size=1, norm_layer=norm_layer)
 self.postprocess_block = nn.Conv2D(num_classes, kernel_size=1)
 def hybrid_forward(self, F, c1, c2, c3, c4):
out4 = self._resize_as(F, 'id_1', self.block4_1(c4), c3)
 out4 = self._resize_as(F, 'id_2', self.block4_2(out4), c2)
 out4 = self._resize_as(F, 'id_3', self.block4_3(out4), c1)
 out3 = self._resize_as(F, 'id_4', self.block3_1(c3), c2)
 out3 = self._resize_as(F, 'id_5', self.block3_2(out3), c1)
 out2 = self._resize_as(F, 'id_6', self.block2(c2), c1)
 out1 = self.block1(c1)
 out = out1 + out2 + out3 + out4
out = self.postprocess_block(out)
 out = F.contrib.BilinearResize2D(out,scale_height=4,scale_width=4)
 return out
def _resize_as(self, F,name, x, y):
h_key = name + '_h'
w_key = name + '_w'
if hasattr(y, 'shape'):
_, _, h, w = y.shape
_, _, h2, w2 = x.shape
if h == h2 and w == w2:
h = 0
w = 0
self._hdsize[h_key]=h
self._hdsize[w_key]=w
else:
h, w = self._hdsize[h_key], self._hdsize[w_key]
if h == 0 and w == 0:
return x
else:
return F.contrib.BilinearResize2D(x,height=h,width=w)
 class ConvBlock(HybridBlock):
def __init__(self, output_channels, kernel_size, padding=0, activation='relu', norm_layer=nn.BatchNorm):
super().__init__()
self.body = nn.HybridSequential()
self.body.add(
 nn.Conv2D(output_channels, kernel_size=kernel_size, padding=padding, activation=activation),
norm_layer(in_channels=output_channels)
)
 def hybrid_forward(self, F, x):
return self.body(x)
<feature_engineering>
def post_process_site_3(preds, threshold=0.5, maxpreds=3):
preds = preds *(preds >= threshold)
 score = np.sum(preds, 0)
 n_birds =(score >= threshold ).sum(-1)
 n_birds = np.clip(n_birds, 0, maxpreds)
 label = np.argsort(- score)[:n_birds]
class_labels = " ".join([CLASSES[l] for l in label])
 return class_labels
class SteelUnet(HybridBlock):
def __init__(self, n_classes=5, ctx=mx.cpu()):
super().__init__()
with self.name_scope() :
self.feature_extractor = ResNetFPN()
self.segment_head = SemanticFPNHead(num_classes=n_classes)
 def hybrid_forward(self, F, x):
fpn_feature = self.feature_extractor(x)
 segment_out = self.segment_head(*fpn_feature)
 return segment_out<train_on_grid>
def max_pred_gen(site, duration):
if site != "site_3":
return 3
else:
rets = [(7,2),(15, 3),(30, 5),(60, 7)]
for ref_duration,thresh in rets:
if ref_duration >= duration:
return thresh
return 10
ctx = mx.gpu()
unet = ResNetUnet(output_channels=[256, 128, 64, 32], num_classes=5)
 unet.load_parameters('.. /input/mxnet-gluon-baseline/unet_14_-1.params')
 unet.collect_params().reset_ctx(ctx )<categorify>
def reformat_preds(preds, df, site):
prediction_df = pd.DataFrame({
"row_id": df['row_id'].values,
"birds": preds
})
 prediction_df['birds'] = prediction_df['birds'].replace([''],'nocall')
 return prediction_df
def mask2rle(mask):
if np.sum(mask)== 0: return ''
ar = mask.flatten(order='F')
 EncodedPixel = ''
l = 0
for i in range(len(ar)) :
if ar[i] == 0:
if l > 0:
if EncodedPixel != '': EncodedPixel += ' '
EncodedPixel += str(st+1)+' '+str(l)
 l = 0
else:
if l == 0: st = i
l += 1
return EncodedPixel<categorify>
def inference(test_df, test_audio, configs, params, threshold=0.5):
unique_audio_id = test_df.audio_id.unique()
models = []
for config in configs:
models_ = []
for weights in config["weights"]:
model = get_model(config['name'])
 load_model_weights(model, weights)
 models_.append(model)
 models.append(models_)
 print(f'\t -> Using {len(models)} models, with {len(models[0])} weights per model.')
 pred_dfs = []
for audio_id in unique_audio_id :
audio_df = test_df[test_df['audio_id'] == audio_id].reset_index(drop=True)
 site = audio_df["site"].values[0]
print(f'
Making predictions for audio {audio_id} in {site} ')
 clip = load_audio(test_audio /(audio_id + ".mp3"), params.sr)
 clip_duration = len(clip)// params.sr
dataset = TestDataset(audio_df, clip, params)
 preds = []
for i, config in enumerate(configs):
for j, weights in enumerate(config["weights"]):
pred = predict(models[i][j], dataset, batch_size=16)
 preds.append(pred)
 preds = np.mean(preds, 0)
 maxpreds = max_pred_gen(site, clip_duration)
 print(f'Limiting the number of birds to {maxpreds}')
 if site == 'site_3':
preds_pp = post_process_site_3(preds, threshold=threshold, maxpreds=maxpreds)
 else:
preds_pp = post_process_site_12(preds, threshold=threshold, maxpreds=maxpreds)
 print("Predicted classes :", preds_pp)
 pred_df = reformat_preds(preds_pp, audio_df, site)
 pred_dfs.append(pred_df)
 sub = pd.concat(pred_dfs, axis=0, sort=False ).reset_index(drop=True)
 return sub
def remove_small_one(predict, min_size):
H,W = predict.shape
num_component, component = cv2.connectedComponents(predict.astype(np.uint8))
predict = np.zeros(( H,W), np.bool)
 for c in range(1,num_component):
p =(component==c)
 if p.sum() >min_size:
predict[p] = True
return predict 
<save_to_csv>
def vote(preds, min_votes=3):
votes = Counter(preds)
 return [c for c, count in votes.items() if count >= min_votes]
submission = pd.read_csv(".. /input/severstal-steel-defect-detection/sample_submission.csv")
 print(len(ImageId),len(submission['ImageId_ClassId']))
for i, encoded in zip(ImageId_ClassIds,EncodedPixels):
submission.loc[submission['ImageId_ClassId']==i,["EncodedPixels"]] = encoded
submission.to_csv('submission.csv',index=False )<set_options>
def inference_voting(test_df, test_audio, configs, params, threshold=0.5, min_votes=3):
unique_audio_id = test_df.audio_id.unique()
models = []
for config in configs:
models_ = []
for weights in config["weights"]:
model = get_model(config['name'])
 load_model_weights(model, weights)
 models_.append(model)
 models.append(models_)
 print(f'\t -> Using {len(models)} models, with {len(models[0])} weights per model.')
 pred_dfs = []
for audio_id in unique_audio_id :
audio_df = test_df[test_df['audio_id'] == audio_id].reset_index(drop=True)
 site = audio_df["site"].values[0]
print(f'
Making predictions for audio {audio_id} in {site} ')
 clip = load_audio(test_audio /(audio_id + ".mp3"), params.sr)
 clip_duration = len(clip)// params.sr
dataset = TestDataset(audio_df, clip, params)
 all_preds = []
for i, config in enumerate(configs):
preds = []
for j, weights in enumerate(config["weights"]):
pred = predict(models[i][j], dataset, batch_size=16)
 preds.append(pred)
 preds = np.mean(preds, 0)
 maxpreds = max_pred_gen(site, clip_duration)
 print(f'Limiting the number of birds to {maxpreds}')
 if site == 'site_3':
preds_pp = post_process_site_3(preds, threshold=threshold, maxpreds=maxpreds)
 preds_pp = [preds_pp]
else:
preds_pp = post_process_site_12(preds, threshold=threshold, maxpreds=maxpreds)
 all_preds.append(preds_pp)
 print("Predicted classes :", preds_pp)
 final_preds = []
for i in range(len(all_preds[0])) :
preds = []
for m in range(len(all_preds)) :
preds += all_preds[m][i].split(' ')
 final_pred = vote(preds, min_votes=min_votes)
 final_preds.append(' '.join(final_pred))
print("
-> Voted classes :", final_preds)
 pred_df = reformat_preds(final_preds, audio_df, site)
 pred_dfs.append(pred_df)
 sub = pd.concat(pred_dfs, axis=0, sort=False ).reset_index(drop=True)
 return sub
warnings.filterwarnings('ignore')
<load_pretrained>
configs = []
unet_se_resnext50_32x4d = \
load('/kaggle/input/severstalmodels/unet_se_resnext50_32x4d.pth' ).cuda()
unet_mobilenet2 = load('/kaggle/input/severstalmodels/unet_mobilenet2.pth' ).cuda()
unet_resnet34 = load('/kaggle/input/severstalmodels/unet_resnet34.pth' ).cuda()<normalization>
model_name = "resnext50_32x4d"
weights = [f".. /input/birds-cp-2/{model_name}_extra_{i}.pt" for i in range(5)]
for w in weights:
assert os.path.isfile(w), f"Weights {w} not found"
configs.append({
"name": model_name,
"weights": weights,
} )
class Model:
def __init__(self, models):
self.models = models
def __call__(self, x):
res = []
x = x.cuda()
with torch.no_grad() :
for m in self.models:
res.append(m(x))
res = torch.stack(res)
 return torch.mean(res, dim=0)
 model = Model([unet_se_resnext50_32x4d, unet_mobilenet2, unet_resnet34] )<categorify>
model_name = "resnext101_32x8d_wsl"
weights = [f".. /input/birds-cp-2/{model_name}_extra_{i}.pt" for i in range(5)]
for w in weights:
assert os.path.isfile(w), f"Weights {w} not found"
configs.append({
"name": model_name,
"weights": weights,
} )
def create_transforms(additional):
res = list(additional)
 res.extend([
A.Normalize(
 mean=(0.485, 0.456, 0.406), std=(0.230, 0.225, 0.223)
),
ChannelTranspose()
])
 res = A.Compose(res)
 return res
img_folder = '/kaggle/input/severstal-steel-defect-detection/test_images'
batch_size = 2
num_workers = 0
transforms = [
[],
[A.HorizontalFlip(p=1)]
]
transforms = [create_transforms(t)for t in transforms]
datasets = [TtaWrap(ImageDataset(img_folder=img_folder, transforms=t), tfms=t)for t in transforms]
loaders = [DataLoader(d, num_workers=num_workers, batch_size=batch_size, shuffle=False)for d in datasets]<define_search_space>
model_name = "resnest50_fast_1s1x64d"
weights = [f".. /input/birds-cp-1/{model_name}_conf_{i}.pt" for i in range(5)]
for w in weights:
assert os.path.isfile(w), f"Weights {w} not found"
configs.append({
"name": model_name,
"weights": weights,
} )
thresholds = [0.5, 0.5, 0.5, 0.5]
min_area = [600, 600, 1000, 2000]
res = []
total = len(datasets[0])//batch_size
for loaders_batch in tqdm_notebook(zip(*loaders), total=total):
preds = []
image_file = []
for i, batch in enumerate(loaders_batch):
features = batch['features'].cuda()
p = torch.sigmoid(model(features))
p = datasets[i].inverse(p)
 preds.append(p)
 image_file = batch['image_file']
preds = torch.stack(preds)
 preds = torch.mean(preds, dim=0)
 preds = preds.detach().cpu().numpy()
for p, file in zip(preds, image_file):
file = os.path.basename(file)
 for i in range(4):
p_channel = p[i]
imageid_classid = file+'_'+str(i+1)
 p_channel =(p_channel>thresholds[i] ).astype(np.uint8)
 if p_channel.sum() < min_area[i]:
p_channel = np.zeros(p_channel.shape, dtype=p_channel.dtype)
 res.append({
'ImageId_ClassId': imageid_classid,
'EncodedPixels': mask2rle(p_channel)
 })
 df = pd.DataFrame(res)
 df.to_csv('submission.csv', index=False)	<save_to_csv>
threshold = 0.5
min_votes = 2
df = pd.DataFrame(res)
 df = df.fillna('')
 df.to_csv('submission.csv', index=False )<feature_engineering>
warnings.filterwarnings("ignore")
 submission = inference_voting(test, TEST_AUDIO_DIR, configs, AudioParams, threshold=threshold, min_votes=min_votes )
<feature_engineering><EOS> 
submission.to_csv("submission.csv", index=False)