Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<train_model>
import numpy as np
import pandas as pd
print('Old size: %d' % len(train_df))
train_df = train_df.dropna(how = 'any', axis = 'rows')
 print('New size: %d' % len(train_df))<count_values>
__all__ = [
'alexnet',
'densenet121', 'densenet169', 'densenet201', 'densenet161',
'resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152',
'inceptionv3',
'squeezenet1_0', 'squeezenet1_1',
'vgg11', 'vgg11_bn', 'vgg13', 'vgg13_bn', 'vgg16', 'vgg16_bn',
'vgg19_bn', 'vgg19'
]
model_urls = {
'alexnet': 'https://download.pytorch.org/models/alexnet-owt-4df8aa71.pth',
'densenet121': 'http://data.lip6.fr/cadene/pretrainedmodels/densenet121-fbdb23505.pth',
'densenet169': 'http://data.lip6.fr/cadene/pretrainedmodels/densenet169-f470b90a4.pth',
'densenet201': 'http://data.lip6.fr/cadene/pretrainedmodels/densenet201-5750cbb1e.pth',
'densenet161': 'http://data.lip6.fr/cadene/pretrainedmodels/densenet161-347e6b360.pth',
'inceptionv3': 'https://download.pytorch.org/models/inception_v3_google-1a9a5a14.pth',
'resnet18': 'https://download.pytorch.org/models/resnet18-5c106cde.pth',
'resnet34': 'https://download.pytorch.org/models/resnet34-333f7ec4.pth',
'resnet50': 'https://download.pytorch.org/models/resnet50-19c8e357.pth',
'resnet101': 'https://download.pytorch.org/models/resnet101-5d3b4d8f.pth',
'resnet152': 'https://download.pytorch.org/models/resnet152-b121ed2d.pth',
'squeezenet1_0': 'https://download.pytorch.org/models/squeezenet1_0-a815701f.pth',
'squeezenet1_1': 'https://download.pytorch.org/models/squeezenet1_1-f364aa15.pth',
'vgg11': 'https://download.pytorch.org/models/vgg11-bbd30ac9.pth',
'vgg13': 'https://download.pytorch.org/models/vgg13-c768596a.pth',
'vgg16': 'https://download.pytorch.org/models/vgg16-397923af.pth',
'vgg19': 'https://download.pytorch.org/models/vgg19-dcbb9e9d.pth',
'vgg11_bn': 'https://download.pytorch.org/models/vgg11_bn-6002323d.pth',
'vgg13_bn': 'https://download.pytorch.org/models/vgg13_bn-abd245e5.pth',
'vgg16_bn': 'https://download.pytorch.org/models/vgg16_bn-6c64b313.pth',
'vgg19_bn': 'https://download.pytorch.org/models/vgg19_bn-c79401a0.pth',
}
input_sizes = {}
means = {}
stds = {}
for model_name in __all__:
input_sizes[model_name] = [3, 224, 224]
means[model_name] = [0.485, 0.456, 0.406]
stds[model_name] = [0.229, 0.224, 0.225]
for model_name in ['inceptionv3']:
input_sizes[model_name] = [3, 299, 299]
means[model_name] = [0.5, 0.5, 0.5]
stds[model_name] = [0.5, 0.5, 0.5]
pretrained_settings = {}
for model_name in __all__:
pretrained_settings[model_name] = {
'imagenet': {
'url': model_urls[model_name],
'input_space': 'RGB',
'input_size': input_sizes[model_name],
'input_range': [0, 1],
'mean': means[model_name],
'std': stds[model_name],
'num_classes': 1000
}
}
def update_state_dict(state_dict):
pattern = re.compile(
 r'^ (.*denselayer\d+\.( ?:norm|relu|conv)) \.((?:[12])\.( ?:weight|bias|running_mean|running_var)) $')
 for key in list(state_dict.keys()):
res = pattern.match(key)
 if res:
new_key = res.group(1)+ res.group(2)
 state_dict[new_key] = state_dict[key]
del state_dict[key]
return state_dict
def load_pretrained(model, num_classes, settings):
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
 state_dict = model_zoo.load_url(settings['url'])
 state_dict = update_state_dict(state_dict)
 model.load_state_dict(state_dict)
 model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
return model
def modify_alexnet(model):
model._features = model.features
del model.features
model.dropout0 = model.classifier[0]
model.linear0 = model.classifier[1]
model.relu0 = model.classifier[2]
model.dropout1 = model.classifier[3]
model.linear1 = model.classifier[4]
model.relu1 = model.classifier[5]
model.last_linear = model.classifier[6]
del model.classifier
def features(self, input):
x = self._features(input)
 x = x.view(x.size(0), 256 * 6 * 6)
 x = self.dropout0(x)
 x = self.linear0(x)
 x = self.relu0(x)
 x = self.dropout1(x)
 x = self.linear1(x)
 return x
def logits(self, features):
x = self.relu1(features)
 x = self.last_linear(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
model.features = types.MethodType(features, model)
 model.logits = types.MethodType(logits, model)
 model.forward = types.MethodType(forward, model)
 return model
def alexnet(num_classes=1000, pretrained='imagenet'):
r
model = models.alexnet(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['alexnet'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_alexnet(model)
 return model
def modify_densenets(model):
model.last_linear = model.classifier
del model.classifier
def logits(self, features):
x = F.relu(features, inplace=True)
 x = F.avg_pool2d(x, kernel_size=7, stride=1)
 x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
model.logits = types.MethodType(logits, model)
 model.forward = types.MethodType(forward, model)
 return model
def densenet121(num_classes=1000, pretrained='imagenet'):
r
model = models.densenet121(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['densenet121'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_densenets(model)
 return model
def densenet169(num_classes=1000, pretrained='imagenet'):
r
model = models.densenet169(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['densenet169'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_densenets(model)
 return model
def densenet201(num_classes=1000, pretrained='imagenet'):
r
model = models.densenet201(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['densenet201'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_densenets(model)
 return model
def densenet161(num_classes=1000, pretrained='imagenet'):
r
model = models.densenet161(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['densenet161'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_densenets(model)
 return model
def inceptionv3(num_classes=1000, pretrained='imagenet'):
r
model = models.inception_v3(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['inceptionv3'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model.last_linear = model.fc
del model.fc
def features(self, input):
x = self.Conv2d_1a_3x3(input)
 x = self.Conv2d_2a_3x3(x)
 x = self.Conv2d_2b_3x3(x)
 x = F.max_pool2d(x, kernel_size=3, stride=2)
 x = self.Conv2d_3b_1x1(x)
 x = self.Conv2d_4a_3x3(x)
 x = F.max_pool2d(x, kernel_size=3, stride=2)
 x = self.Mixed_5b(x)
 x = self.Mixed_5c(x)
 x = self.Mixed_5d(x)
 x = self.Mixed_6a(x)
 x = self.Mixed_6b(x)
 x = self.Mixed_6c(x)
 x = self.Mixed_6d(x)
 x = self.Mixed_6e(x)
 if self.training and self.aux_logits:
self._out_aux = self.AuxLogits(x)
 x = self.Mixed_7a(x)
 x = self.Mixed_7b(x)
 x = self.Mixed_7c(x)
 return x
def logits(self, features):
x = F.avg_pool2d(features, kernel_size=8)
 x = F.dropout(x, training=self.training)
 x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 if self.training and self.aux_logits:
aux = self._out_aux
self._out_aux = None
return x, aux
return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
model.features = types.MethodType(features, model)
 model.logits = types.MethodType(logits, model)
 model.forward = types.MethodType(forward, model)
 return model
def modify_resnets(model):
model.last_linear = model.fc
model.fc = None
def features(self, input):
x = self.conv1(input)
 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)
 return x
def logits(self, features):
x = self.avgpool(features)
 x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
model.features = types.MethodType(features, model)
 model.logits = types.MethodType(logits, model)
 model.forward = types.MethodType(forward, model)
 return model
def resnet18(num_classes=1000, pretrained='imagenet'):

 model = models.resnet18(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['resnet18'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_resnets(model)
 return model
def resnet34(num_classes=1000, pretrained='imagenet'):

 model = models.resnet34(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['resnet34'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_resnets(model)
 return model
def resnet50(num_classes=1000, pretrained='imagenet'):

 model = models.resnet50(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['resnet50'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_resnets(model)
 return model
def resnet101(num_classes=1000, pretrained='imagenet'):

 model = models.resnet101(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['resnet101'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_resnets(model)
 return model
def resnet152(num_classes=1000, pretrained='imagenet'):

 model = models.resnet152(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['resnet152'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_resnets(model)
 return model
def modify_squeezenets(model):
model.dropout = model.classifier[0]
model.last_conv = model.classifier[1]
model.relu = model.classifier[2]
model.avgpool = model.classifier[3]
del model.classifier
def logits(self, features):
x = self.dropout(features)
 x = self.last_conv(x)
 x = self.relu(x)
 x = self.avgpool(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
model.logits = types.MethodType(logits, model)
 model.forward = types.MethodType(forward, model)
 return model
def squeezenet1_0(num_classes=1000, pretrained='imagenet'):
r
model = models.squeezenet1_0(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['squeezenet1_0'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_squeezenets(model)
 return model
def squeezenet1_1(num_classes=1000, pretrained='imagenet'):
r
model = models.squeezenet1_1(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['squeezenet1_1'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_squeezenets(model)
 return model
def modify_vggs(model):
model._features = model.features
del model.features
model.linear0 = model.classifier[0]
model.relu0 = model.classifier[1]
model.dropout0 = model.classifier[2]
model.linear1 = model.classifier[3]
model.relu1 = model.classifier[4]
model.dropout1 = model.classifier[5]
model.last_linear = model.classifier[6]
del model.classifier
def features(self, input):
x = self._features(input)
 x = x.view(x.size(0), -1)
 x = self.linear0(x)
 x = self.relu0(x)
 x = self.dropout0(x)
 x = self.linear1(x)
 return x
def logits(self, features):
x = self.relu1(features)
 x = self.dropout1(x)
 x = self.last_linear(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
model.features = types.MethodType(features, model)
 model.logits = types.MethodType(logits, model)
 model.forward = types.MethodType(forward, model)
 return model
def vgg11(num_classes=1000, pretrained='imagenet'):

 model = models.vgg11(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg11'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg11_bn(num_classes=1000, pretrained='imagenet'):

 model = models.vgg11_bn(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg11_bn'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg13(num_classes=1000, pretrained='imagenet'):

 model = models.vgg13(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg13'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg13_bn(num_classes=1000, pretrained='imagenet'):

 model = models.vgg13_bn(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg13_bn'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg16(num_classes=1000, pretrained='imagenet'):

 model = models.vgg16(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg16'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg16_bn(num_classes=1000, pretrained='imagenet'):

 model = models.vgg16_bn(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg16_bn'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg19(num_classes=1000, pretrained='imagenet'):

 model = models.vgg19(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg19'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
def vgg19_bn(num_classes=1000, pretrained='imagenet'):

 model = models.vgg19_bn(pretrained=False)
 if pretrained is not None:
settings = pretrained_settings['vgg19_bn'][pretrained]
model = load_pretrained(model, num_classes, settings)
 model = modify_vggs(model)
 return model
train_df['fare_amount'].value_counts()<drop_column>

 __all__ = ['SENet', 'senet154', 'se_resnet50', 'se_resnet101', 'se_resnet152',
'se_resnext50_32x4d', 'se_resnext101_32x4d']
pretrained_settings = {
'senet154': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/senet154-c7b49a05.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnet50': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnet50-ce0d4300.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnet101': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnet101-7e38fcc6.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnet152': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnet152-d17c99b7.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnext50_32x4d': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnext50_32x4d-a260b3a4.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnext101_32x4d': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnext101_32x4d-3b2fe3d8.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
}
class SEModule(nn.Module):
def __init__(self, channels, reduction):
super(SEModule, self ).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
 self.fc1 = nn.Conv2d(channels, channels // reduction, kernel_size=1,
padding=0)
 self.relu = nn.ReLU(inplace=True)
 self.fc2 = nn.Conv2d(channels // reduction, channels, kernel_size=1,
padding=0)
 self.sigmoid = nn.Sigmoid()
def forward(self, x):
module_input = x
x = self.avg_pool(x)
 x = self.fc1(x)
 x = self.relu(x)
 x = self.fc2(x)
 x = self.sigmoid(x)
 return module_input * x
class Bottleneck(nn.Module):

 def forward(self, x):
residual = x
out = self.conv1(x)
 out = self.bn1(out)
 out = self.relu(out)
 out = self.conv2(out)
 out = self.bn2(out)
 out = self.relu(out)
 out = self.conv3(out)
 out = self.bn3(out)
 if self.downsample is not None:
residual = self.downsample(x)
 out = self.se_module(out)+ residual
out = self.relu(out)
 return out
class SEBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None):
super(SEBottleneck, self ).__init__()
self.conv1 = nn.Conv2d(inplanes, planes * 2, kernel_size=1, bias=False)
 self.bn1 = nn.BatchNorm2d(planes * 2)
 self.conv2 = nn.Conv2d(planes * 2, planes * 4, kernel_size=3,
stride=stride, padding=1, groups=groups,
bias=False)
 self.bn2 = nn.BatchNorm2d(planes * 4)
 self.conv3 = nn.Conv2d(planes * 4, planes * 4, kernel_size=1,
bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SEResNetBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None):
super(SEResNetBottleneck, self ).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False,
stride=stride)
 self.bn1 = nn.BatchNorm2d(planes)
 self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1,
groups=groups, bias=False)
 self.bn2 = nn.BatchNorm2d(planes)
 self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SEResNeXtBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None, base_width=4):
super(SEResNeXtBottleneck, self ).__init__()
width = math.floor(planes *(base_width / 64)) * groups
self.conv1 = nn.Conv2d(inplanes, width, kernel_size=1, bias=False,
stride=1)
 self.bn1 = nn.BatchNorm2d(width)
 self.conv2 = nn.Conv2d(width, width, kernel_size=3, stride=stride,
padding=1, groups=groups, bias=False)
 self.bn2 = nn.BatchNorm2d(width)
 self.conv3 = nn.Conv2d(width, planes * 4, kernel_size=1, bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SENet(nn.Module):
def __init__(self, block, layers, groups, reduction, dropout_p=0.2,
inplanes=128, input_3x3=True, downsample_kernel_size=3,
downsample_padding=1, num_classes=1000):

 super(SENet, self ).__init__()
self.inplanes = inplanes
if input_3x3:
layer0_modules = [ 
('conv1', nn.Conv2d(3, 64, 3, stride=2, padding=1,
bias=False)) , 
('bn1', nn.BatchNorm2d(64)) , 
('relu1', nn.ReLU(inplace=True)) , 
('conv2', nn.Conv2d(64, 64, 3, stride=1, padding=1,
bias=False)) , 
('bn2', nn.BatchNorm2d(64)) , 
('relu2', nn.ReLU(inplace=True)) , 
('conv3', nn.Conv2d(64, inplanes, 3, stride=1, padding=1,
bias=False)) , 
('bn3', nn.BatchNorm2d(inplanes)) , 
('relu3', nn.ReLU(inplace=True)) ,
]
else:
layer0_modules = [ 
('conv1', nn.Conv2d(3, inplanes, kernel_size=7, stride=2,
padding=3, bias=False)) , 
('bn1', nn.BatchNorm2d(inplanes)) , 
('relu1', nn.ReLU(inplace=True)) ,
]
layer0_modules.append(( 'pool', nn.MaxPool2d(3, stride=2,
ceil_mode=True)))
 self.layer0 = nn.Sequential(OrderedDict(layer0_modules))
self.layer1 = self._make_layer(
 block,
planes=64,
blocks=layers[0],
groups=groups,
reduction=reduction,
downsample_kernel_size=1,
downsample_padding=0 
)
 self.layer2 = self._make_layer(
 block,
planes=128,
blocks=layers[1],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.layer3 = self._make_layer(
 block,
planes=256,
blocks=layers[2],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.layer4 = self._make_layer(
 block,
planes=512,
blocks=layers[3],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.avg_pool = nn.AvgPool2d(7, stride=1)
 self.dropout = nn.Dropout(dropout_p)if dropout_p is not None else None
self.last_linear = nn.Linear(512 * block.expansion, num_classes)
 def _make_layer(self, block, planes, blocks, groups, reduction, stride=1,
downsample_kernel_size=1, downsample_padding=0):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
 nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=downsample_kernel_size, stride=stride,
padding=downsample_padding, bias=False),
nn.BatchNorm2d(planes * block.expansion), 
)
 layers = []
layers.append(block(self.inplanes, planes, groups, reduction, stride,
downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes, groups, reduction))
return nn.Sequential(*layers)
 def features(self, x):
x = self.layer0(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 return x
def logits(self, x):
x = self.avg_pool(x)
 if self.dropout is not None:
x = self.dropout(x)
 x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 return x
def forward(self, x):
x = self.features(x)
 x = self.logits(x)
 return x
def initialize_pretrained_model(model, num_classes, settings):
assert num_classes == settings['num_classes'], \
'num_classes should be {}, but is {}'.format(
 settings['num_classes'], num_classes)
 model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
def senet154(num_classes=1000, pretrained='imagenet'):
model = SENet(SEBottleneck, [3, 8, 36, 3], groups=64, reduction=16,
dropout_p=0.2, num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['senet154'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnet50(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNetBottleneck, [3, 4, 6, 3], groups=1, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnet50'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnet101(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNetBottleneck, [3, 4, 23, 3], groups=1, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnet101'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnet152(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNetBottleneck, [3, 8, 36, 3], groups=1, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnet152'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnext50_32x4d(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNeXtBottleneck, [3, 4, 6, 3], groups=32, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnext50_32x4d'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnext101_32x4d(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNeXtBottleneck, [3, 4, 23, 3], groups=32, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnext101_32x4d'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
train_df = train_df.drop(train_df[train_df['fare_amount'] < 0].index, axis=0)
 train_df.shape<drop_column>
sys.path.append('/kaggle/working/')
 class GeM(nn.Module):
def __init__(self, p=3, eps=1e-6):
super(GeM,self ).__init__()
self.p = Parameter(torch.ones(1)*p)
 self.eps = eps
def forward(self, x):
return gem(x, p=self.p, eps=self.eps)
 def __repr__(self):
return self.__class__.__name__ + '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist() [0])+ ', ' + 'eps=' + str(self.eps)+ ')'
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)
 def get_se_resnet50_gem(pretrain):
if pretrain == 'imagenet':
model = se_resnet50(num_classes=1000, pretrained='imagenet')
 else:
model = se_resnet50(num_classes=1000, pretrained=None)
 model.avg_pool = GeM()
model.last_linear = nn.Linear(2048, 1)
 return model
def get_densenet121_gem(pretrain):
if pretrain == 'imagenet':
model = densenet121(num_classes=1000, pretrained='imagenet')
 else:
model = densenet121(num_classes=1000, pretrained=None)
 model.avg_pool = GeM()
model.last_linear = nn.Linear(1024, 1)
 return model
train_df = train_df.drop(train_df[train_df['passenger_count'] == 208].index, axis=0 )<data_type_conversions>
TEST_IMAGE_PATH = '/kaggle/input/aptos2019-blindness-detection/test_images'
device = torch.device("cuda")
 test_images = glob(os.path.join(TEST_IMAGE_PATH, '*.png'))
train_df['key'] = pd.to_datetime(train_df['key'])
 train_df['pickup_datetime'] = pd.to_datetime(train_df['pickup_datetime'] )<data_type_conversions>
def make_predictions(model, test_images, transforms, size=256, device=torch.device("cuda")) :
predictions = []
for i, im_path in enumerate(test_images):
image = Image.open(im_path)
 image = image.resize(( size, size), resample=Image.BILINEAR)
 image = transforms(image ).to(device)
 output = model(image.unsqueeze(0))
output_flip = model(torch.flip(image.unsqueeze(0), dims=(3,)))
 final_prediction =(output.item() +output_flip.item())/2
predictions.append(( os.path.splitext(im_path.split('/')[-1])[0], final_prediction))
return predictions
test_df['pickup_datetime'] = pd.to_datetime(test_df['pickup_datetime'] )<feature_engineering>
MODEL_PATH = '.. /input/densenet121/model_densenet121_bs64_30.pth'
model = get_densenet121_gem(pretrain=False)
 model.to(device)
 model.load_state_dict(torch.load(MODEL_PATH, map_location='cuda:0'))
model.eval()
norm = transforms.Compose([transforms.ToTensor() ])
data = [train_df, test_df]
for i in data:
i['Year'] = i['pickup_datetime'].dt.year
i['Month'] = i['pickup_datetime'].dt.month
i['Date'] = i['pickup_datetime'].dt.day
i['Day of Week'] = i['pickup_datetime'].dt.dayofweek
i['Hour'] = i['pickup_datetime'].dt.hour<drop_column>
MODEL_PATH = '.. /input/seresnet50testpseudo/model10.pth'
model = get_se_resnet50_gem(pretrain=False)
 model.to(device)
 model.load_state_dict(torch.load(MODEL_PATH, map_location='cuda:0'))
model.eval()
norm = transforms.Compose([transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
train_df = train_df.drop(['key','pickup_datetime'], axis = 1)
 test_df = test_df.drop(['key', 'pickup_datetime'], axis = 1 )<prepare_x_and_y>
MODEL_PATH = '.. /input/seresnet50pseudo-512/model30.pth'
model = get_se_resnet50_gem(pretrain=False)
 model.to(device)
 model.load_state_dict(torch.load(MODEL_PATH, map_location='cuda:0'))
model.eval()
norm = transforms.Compose([transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 predictions_seresnet_512 = make_predictions(model, test_images, norm, size=512, device=torch.device("cuda"))
x_train = train_df.iloc[:, train_df.columns != 'fare_amount']
y_train = train_df['fare_amount'].values
x_test = test_df<save_to_csv>
final_predictions = predictions_seresnet_512
<import_modules><EOS> 
submission = pd.DataFrame(final_predictions)
 submission.columns = ['id_code','diagnosis']
submission.loc[submission.diagnosis < 0.75, 'diagnosis'] = 0
submission.loc[(0.75 <= submission.diagnosis)&(submission.diagnosis < 1.5), 'diagnosis'] = 1
submission.loc[(1.5 <= submission.diagnosis)&(submission.diagnosis < 2.5), 'diagnosis'] = 2
submission.loc[(2.5 <= submission.diagnosis)&(submission.diagnosis < 3.5), 'diagnosis'] = 3
submission.loc[3.5 <= submission.diagnosis, 'diagnosis'] = 4
submission['diagnosis'] = submission['diagnosis'].astype(int)
 submission.to_csv('submission.csv', index=False)
 submission.head()