Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<train_model>
!pip install.. /input/weights/timm-0.3.1-py3-none-any.whl
lgbm_params = {
'task': 'train',
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse'
}<predict_on_test>
device = "cuda:0"
pred_test_y = np.zeros(x_test.shape[0])
 train_set = lgbm.Dataset(x_train, y_train, silent=True)
 model = lgbm.train(lgbm_params, train_set = train_set, num_boost_round=300)
 pred_test_y = model.predict(x_test, num_iteration = model.best_iteration )<save_to_csv>
FeaturePyramidNetwork,
LastLevelMaxPool, 
)
 def gem(x, p=3, eps=1e-6):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./p)
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-6, flatten=False):
super(GeM,self ).__init__()
self.p = Parameter(torch.ones(1)*p)
 self.eps = eps
self.flatten = flatten
def forward(self, x):
x = gem(x, p=self.p, eps=self.eps)
 if self.flatten:
x = x.flatten(1)
 return x
def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(self.eps)+ ')'
class FrozenBatchNorm2d(torch.nn.Module):

 def __init__(
 self,
num_features: int,
eps: float = 1e-5,
n: Optional[int] = None, 
):
if n is not None:
warnings.warn("`n` argument is deprecated and has been renamed `num_features`",
DeprecationWarning)
 num_features = n
super(FrozenBatchNorm2d, self ).__init__()
self.eps = eps
self.register_buffer("weight", torch.ones(num_features))
self.register_buffer("bias", torch.zeros(num_features))
self.register_buffer("running_mean", torch.zeros(num_features))
self.register_buffer("running_var", torch.ones(num_features))
def _load_from_state_dict(
 self,
state_dict: dict,
prefix: str,
local_metadata: dict,
strict: bool,
missing_keys: List[str],
unexpected_keys: List[str],
error_msgs: List[str], 
):
num_batches_tracked_key = prefix + 'num_batches_tracked'
if num_batches_tracked_key in state_dict:
del state_dict[num_batches_tracked_key]
super(FrozenBatchNorm2d, self )._load_from_state_dict(
 state_dict, prefix, local_metadata, strict,
missing_keys, unexpected_keys, error_msgs)
 def forward(self, x: Tensor)-> Tensor:
w = self.weight.reshape(1, -1, 1, 1)
 b = self.bias.reshape(1, -1, 1, 1)
 rv = self.running_var.reshape(1, -1, 1, 1)
 rm = self.running_mean.reshape(1, -1, 1, 1)
 scale = w *(rv + self.eps ).rsqrt()
bias = b - rm * scale
return x * scale + bias
def __repr__(self)-> str:
return f"{self.__class__.__name__}({self.weight.shape[0]}, eps={self.eps})"
class Linear(nn.Linear):
def forward(self, input: torch.Tensor)-> torch.Tensor:
if torch.jit.is_scripting() :
bias = self.bias.to(dtype=input.dtype)if self.bias is not None else None
return F.linear(input, self.weight.to(dtype=input.dtype), bias=bias)
 else:
return F.linear(input, self.weight, self.bias)
 class backboneNet_efficient(nn.Module):
def __init__(self):
super(backboneNet_efficient, self ).__init__()
net = timm.create_model('tf_efficientnet_b4_ns', pretrained=False)
 layers_to_train = ['blocks']
for name, parameter in net.named_parameters() :
if all([not name.startswith(layer)for layer in layers_to_train]):
parameter.requires_grad_(False)
 self.num_features = 1792
self.conv_stem = net.conv_stem
self.bn1 = net.bn1
self.act1 = net.act1
self.block0 = net.blocks[0]
self.block1 = net.blocks[1]
self.block2 = net.blocks[2]
self.block3 = net.blocks[3]
self.block4 = net.blocks[4]
self.block5 = net.blocks[5]
self.block6 = net.blocks[6]
self.conv_head = net.conv_head
self.bn2 = net.bn2
self.act2 = net.act2
self.global_pool = net.global_pool
self.drop_rate = 0.4
self.rg_cls = Linear(self.num_features, 1, bias=True)
 self.cls_cls = Linear(self.num_features, 5, bias=True)
 self.ord_cls = Linear(self.num_features, 4, bias=True)
 def forward(self, x):
x1 = self.conv_stem(x)
 x2 = self.bn1(x1)
 x3 = self.act1(x2)
 x4 = self.block0(x1)
 x5 = self.block1(x4)
 x6 = self.block2(x5)
 x7 = self.block3(x6)
 x8 = self.block4(x7)
 x9 = self.block5(x8)
 x10 = self.block6(x9)
 x11 = self.conv_head(x10)
 x12 = self.bn2(x11)
 x13 = self.act2(x12)
 x14 = self.global_pool(x13)
 if self.drop_rate > 0.:
x14 = F.dropout(x14, p=self.drop_rate, training=self.training)
 x15 = self.rg_cls(x14)
 x16 = self.cls_cls(x14)
 x17 = self.ord_cls(x14)
 return x15, x16, x17
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['fare_amount'] = pred_test_y
submission.to_csv('lgbm_submission.csv', index=False)
 submission.head(20 )<import_modules>
threshold = [0.75, 1.5, 2.5, 3.5]
def regress2class(out):
prediction = 0
for i in range(4):
prediction +=(out.data >= threshold[i] ).squeeze().cpu().item()
return prediction
def ordinal2class_prob(out):
pred_prob = torch.zeros(out.size(0), 5 ).cuda()
pred_prob[:, 0] =(1 - out[:, 0] ).squeeze()
pred_prob[:, 1] =(out[:, 0] *(1 - out[:, 1])).squeeze()
pred_prob[:, 2] =(out[:, 1] *(1 - out[:, 2])).squeeze()
pred_prob[:, 3] =(out[:, 2] *(1 - out[:, 3])).squeeze()
pred_prob[:, 4] = out[:, 3].squeeze().cpu()
return F.softmax(pred_prob, dim=1)
 def regress2class_prob(out):
pred_prob = torch.zeros(( out.size(0), 5)).cuda()
for i in range(out.size(0)) :
if out[i] < 4.:
l1 = int(math.floor(out[i]))
l2 = int(math.ceil(out[i]))
pred_prob[i][l1] = 1 -(out[i] - l1)
 pred_prob[i][l2] = 1 -(l2 - out[i])
 else:
pred_prob[i][4] = 1.
 return pred_prob
def combine3output(r_out, c_out, o_out):
R = regress2class(r_out.data)
 _, C = torch.max(c_out.data, 1)
 C = C.squeeze().item()
_, O = torch.max(o_out.data, 1)
 O = O.squeeze().item()
P =(R + C + O)/ 3.
 P = int(round(P))
return P
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt<load_from_csv>
def gem(x, p=3, eps=1e-6):
return F.avg_pool2d(x.clamp(min=eps ).pow(p),(x.size(-2), x.size(-1)) ).pow(1./p)
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-6, flatten=False):
super(GeM,self ).__init__()
self.p = Parameter(torch.ones(1)*p)
 self.eps = eps
self.flatten = flatten
def forward(self, x):
x = gem(x, p=self.p, eps=self.eps)
 if self.flatten:
x = x.flatten(1)
 return x
def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(self.eps)+ ')'
class Regressor(nn.Module):
def __init__(self):
super(Regressor, self ).__init__()
self.backbone = timm.models.tf_efficientnet_b5_ns(pretrained=False)
 self.backbone.global_pool = GeM(flatten=True)
 self.regressor = nn.Linear(1000, 1)
 def forward(self, x):
x = self.backbone(x)
 out = self.regressor(x)
 out = torch.sigmoid(out)* 4.5
return out
class ThreeStage_Model(nn.Module):
def __init__(self, backbone=None):
super(ThreeStage_Model, self ).__init__()
self.backbone = timm.models.tf_efficientnet_b4_ns(pretrained=False)
 self.backbone.global_pool = GeM(flatten=True)
 self.classifier = nn.Sequential(
 nn.SiLU() ,
nn.Linear(1000, 500),
nn.SiLU() ,
nn.Linear(500, 5), 
)
 self.regressor = nn.Sequential(
 nn.SiLU() ,
nn.Linear(1000, 500),
nn.SiLU() ,
nn.Linear(500, 1), 
)
 self.ordinal = nn.Sequential(
 nn.SiLU() ,
nn.Linear(1000, 500),
nn.SiLU() ,
nn.Linear(500, 4), 
)
 self.final_regressor = nn.Sequential(
 nn.SiLU() ,
nn.Linear(10, 1), 
)
 def forward(self, x, final=False):
x = self.backbone(x)
 c_out = self.classifier(x)
 r_out = self.regressor(x)
 o_out = self.ordinal(x)
 if final:
out = torch.cat(( c_out, r_out, o_out), 1)
 out = self.final_regressor(out)
 out = torch.sigmoid(out)* 4.5
return out
else:
r_out = torch.sigmoid(r_out)* 4.5
o_out = torch.sigmoid(o_out)
 return c_out, r_out, o_out
df = pd.read_csv('.. /input/train.csv',nrows=2_000_000, usecols=[1,2,3,4,5,6,7] )<data_type_conversions>
test_ids = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 test_ids = np.squeeze(test_ids.values)
 transform1 = transforms.Compose([
trim() ,
cropTo4_3() ,
transforms.Resize(( 288, 384)) ,
transforms.ToTensor() ,
transforms.Normalize(mean=[0.384, 0.258, 0.174], std=[0.124, 0.089, 0.094]),
])
 transform2 = transforms.Compose([
transforms.Resize(( 280,280)) ,
transforms.CenterCrop(256),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 net1 = ThreeStage_Model()
net1.load_state_dict(torch.load(".. /input/weights/0.926_B4_3stage_5epoch_320finetune.pkl"))
net1 = net1.to(device)
 net1.eval()
net2 = backboneNet_efficient()
net2.load_state_dict(torch.load(".. /input/weights/1.pth", map_location='cpu'))
net2 = net2.to(device)
 net2.eval()
net3 = backboneNet_efficient()
net3.load_state_dict(torch.load(".. /input/weights/2.pth", map_location='cpu'))
net3 = net3.to(device)
 net3.eval()
net5 = backboneNet_efficient()
net5.load_state_dict(torch.load(".. /input/weights/0.929292_4", map_location='cpu'))
net5 = net5.to(device)
 net5.eval()
df['pickup_datetime'] = df['pickup_datetime'].str.slice(0, 16)
 df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime'], utc=True, format='%Y-%m-%d %H:%M' )<define_search_space>
df = pd.DataFrame(submission, columns=["id_code", "diagnosis"])
 df.to_csv("submission.csv", index=False )