Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<drop_column>
%reset -f
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
print(PIL.PILLOW_VERSION)
 train_on_gpu = torch.cuda.is_available()
if not train_on_gpu:
print('CUDA is not available.Training on CPU...')
 else:
print('CUDA is available! Training on GPU...')
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device)
 ImageFile.LOAD_TRUNCATED_IMAGES = True
ImageFile.LOAD_TRUNCATED_IMAGES = True
train = train.drop(train[(train['H_Distance']==0)&(train['fare_amount']==0)].index, axis = 0 )<define_variables>
! ls -la.. /input/
data_dir = '.. /input/aptos2019-blindness-detection/'
train_dir = data_dir + '/train_images/'
test_dir= data_dir + '/test_images/'
nThreads = 4
batch_size = 32
use_gpu = torch.cuda.is_available()
rush_hour = train.loc[(((train['Hour']>=6)&(train['Hour']<=20)) &(( train['Day of Week']>=1)&(train['Day of Week']<=5)) &(train['H_Distance']==0)&(train['fare_amount'] < 2.5)) ]
rush_hour<drop_column>
class GenericDataset() :
def __init__(self, labels, root_dir, subset=False, transform=None):
self.labels = labels
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.labels)
 def __getitem__(self, idx):
img_name = self.labels.iloc[idx, 0]
fullname = join(self.root_dir, img_name)
 image = Image.open(fullname ).convert('RGB')
 labels = self.labels.iloc[idx, 2]
if self.transform:
image = self.transform(image)
 return image, labels
@staticmethod
def find_classes(fullDir):
classes = [d for d in os.listdir(fullDir)if os.path.isdir(os.path.join(fullDir, d)) ]
classes.sort()
class_to_idx = {classes[i]: i for i in range(len(classes)) }
idx_to_class = {v: k for k, v in class_to_idx.items() }
train = []
for index, label in enumerate(classes):
path = fullDir + label + '/'
for file in listdir(path):
train.append(['{}/{}'.format(label, file), label, index])
 df = pd.DataFrame(train, columns=['file', 'category', 'category_id', ])
 return classes, class_to_idx, idx_to_class, df
@staticmethod
def find_classes_retino(fullDir):
df_labels = pd.read_csv(".. /input/aptos2019-blindness-detection/train.csv", sep=',')
 class_to_idx = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4}
idx_to_class = dict(zip(class_to_idx.values() , class_to_idx.keys()))
classes = ['0', '1', '2', '3', '4']
print('Sorted Classes: {}'.format(classes))
print('class_to_idx: {}'.format(class_to_idx))
print('num_to_class: {}'.format(idx_to_class))
train = []
for index, row in(df_labels.iterrows()):
id =(row['id_code'])
 currImage_on_disk = os.path.join(fullDir, id + '.png')
 if os.path.isfile(currImage_on_disk):
if(int(row['diagnosis'])) == 0:
trait = '0'
elif(int(row['diagnosis'])) == 1:
trait = '1'
elif(int(row['diagnosis'])) == 2:
trait = '2'
elif(int(row['diagnosis'])) == 3:
trait = '3'
elif(int(row['diagnosis'])) == 4:
trait = '4'
train.append(['{}'.format(currImage_on_disk), trait, class_to_idx[trait]])
 df = pd.DataFrame(train, columns=['file', 'category', 'category_id', ])
 if os.path.isfile('full_retino_labels.csv'):
os.remove('full_retino_labels.csv')
 df.to_csv('full_retino_labels.csv', index=None)
 return classes, class_to_idx, idx_to_class, df
train=train.drop(rush_hour.index, axis=0 )<feature_engineering>
class GenericDatasetTTA() :
def __init__(self, labels, root_dir, subset=False, transform=None,TTA=8):
self.labels = labels
self.root_dir = root_dir
self.transform = transform
self.TTA = TTA
def __len__(self):
return len(self.labels)
 def __getitem__(self, idx):
img_name = self.labels.iloc[idx, 0]
fullname = join(self.root_dir, img_name)
 image = Image.open(fullname ).convert('RGB')
 labels = self.labels.iloc[idx, 2]
a = []
for _ in range(self.TTA):
image1 = self.transform(image)
 a.append(image1)
 image = torch.stack(a)
 return image, labels
non_rush_hour = train.loc[(((train['Hour']<6)|(train['Hour']>20)) &(( train['Day of Week']>=1)&(train['Day of Week']<=5)) &(train['H_Distance']==0)&(train['fare_amount'] < 3.0)) ]
non_rush_hour 
<filter>

 __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
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size(0), -1)
 def create_net(net_cls, pretrained: bool):
net = net_cls(pretrained=pretrained)
 return net
def get_head(nf: int, n_classes):
model = nn.Sequential(
 nn.AdaptiveAvgPool2d(1),
Flatten() ,
nn.Linear(nf, n_classes)
)
 return model
def init_weights(model):
for i, module in enumerate(model):
if isinstance(module,(nn.BatchNorm1d, nn.BatchNorm2d)) :
if module.weight is not None:
nn.init.uniform_(module.weight)
 if module.bias is not None:
nn.init.constant_(module.bias, 0)
 if isinstance(module,(nn.Linear, nn.Conv2d, nn.Conv1d)) :
if getattr(module, "weight_v", None)is not None:
print("Initing linear with weight normalization")
 assert model[i].weight_g is not None
else:
nn.init.kaiming_normal_(module.weight)
 print("Initing linear")
 if module.bias is not None:
nn.init.constant_(module.bias, 0)
 return model
def get_seresnet_model(arch: str = "se_resnext50_32x4d", n_classes: int = 10, pretrained: bool = False):
full = se_resnext50_32x4d(
 pretrained='imagenet' if pretrained else None)
 model = nn.Sequential(
 nn.Sequential(full.layer0, full.layer1, full.layer2, full.layer3[:3]),
nn.Sequential(full.layer3[3:], full.layer4),
get_head(2048, n_classes))
print(" | ".join([
"{:,d}".format(np.sum([p.numel() for p in x.parameters() ])) for x in model]))
if pretrained:
init_weights(model[-1])
 return model
return init_weights(model)
 class Swish(nn.Module):
def forward(self, x):

 return x * torch.sigmoid(x)
 pretrained_settings = {
'inceptionv4': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionv4-8e4777a0.pth',
'input_space': 'RGB',
'input_size': [3, 299, 299],
'input_range': [0, 1],
'mean': [0.5, 0.5, 0.5],
'std': [0.5, 0.5, 0.5],
'num_classes': 1000
},
'imagenet+background': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionv4-8e4777a0.pth',
'input_space': 'RGB',
'input_size': [3, 299, 299],
'input_range': [0, 1],
'mean': [0.5, 0.5, 0.5],
'std': [0.5, 0.5, 0.5],
'num_classes': 1001
}
}
}
class BasicConv2d(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride, padding=0):
super(BasicConv2d, self ).__init__()
self.conv = nn.Conv2d(in_planes, out_planes,
kernel_size=kernel_size, stride=stride,
padding=padding, bias=False)
 self.bn = nn.BatchNorm2d(out_planes,
eps=0.001,
momentum=0.1,
affine=True)
 self.relu = nn.ReLU(inplace=True)
 def forward(self, x):
x = self.conv(x)
 x = self.bn(x)
 x = self.relu(x)
 return x
class Mixed_3a(nn.Module):
def __init__(self):
super(Mixed_3a, self ).__init__()
self.maxpool = nn.MaxPool2d(3, stride=2)
 self.conv = BasicConv2d(64, 96, kernel_size=3, stride=2)
 def forward(self, x):
x0 = self.maxpool(x)
 x1 = self.conv(x)
 out = torch.cat(( x0, x1), 1)
 return out
class Mixed_4a(nn.Module):
def __init__(self):
super(Mixed_4a, self ).__init__()
self.branch0 = nn.Sequential(
 BasicConv2d(160, 64, kernel_size=1, stride=1),
BasicConv2d(64, 96, kernel_size=3, stride=1)
)
 self.branch1 = nn.Sequential(
 BasicConv2d(160, 64, kernel_size=1, stride=1),
BasicConv2d(64, 64, kernel_size=(1,7), stride=1, padding=(0,3)) ,
BasicConv2d(64, 64, kernel_size=(7,1), stride=1, padding=(3,0)) ,
BasicConv2d(64, 96, kernel_size=(3,3), stride=1)
)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 out = torch.cat(( x0, x1), 1)
 return out
class Mixed_5a(nn.Module):
def __init__(self):
super(Mixed_5a, self ).__init__()
self.conv = BasicConv2d(192, 192, kernel_size=3, stride=2)
 self.maxpool = nn.MaxPool2d(3, stride=2)
 def forward(self, x):
x0 = self.conv(x)
 x1 = self.maxpool(x)
 out = torch.cat(( x0, x1), 1)
 return out
class Inception_A(nn.Module):
def __init__(self):
super(Inception_A, self ).__init__()
self.branch0 = BasicConv2d(384, 96, kernel_size=1, stride=1)
 self.branch1 = nn.Sequential(
 BasicConv2d(384, 64, kernel_size=1, stride=1),
BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1)
)
 self.branch2 = nn.Sequential(
 BasicConv2d(384, 64, kernel_size=1, stride=1),
BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1),
BasicConv2d(96, 96, kernel_size=3, stride=1, padding=1)
)
 self.branch3 = nn.Sequential(
 nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False),
BasicConv2d(384, 96, kernel_size=1, stride=1)
)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 x2 = self.branch2(x)
 x3 = self.branch3(x)
 out = torch.cat(( x0, x1, x2, x3), 1)
 return out
class Reduction_A(nn.Module):
def __init__(self):
super(Reduction_A, self ).__init__()
self.branch0 = BasicConv2d(384, 384, kernel_size=3, stride=2)
 self.branch1 = nn.Sequential(
 BasicConv2d(384, 192, kernel_size=1, stride=1),
BasicConv2d(192, 224, kernel_size=3, stride=1, padding=1),
BasicConv2d(224, 256, kernel_size=3, stride=2)
)
 self.branch2 = nn.MaxPool2d(3, stride=2)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 x2 = self.branch2(x)
 out = torch.cat(( x0, x1, x2), 1)
 return out
class Inception_B(nn.Module):
def __init__(self):
super(Inception_B, self ).__init__()
self.branch0 = BasicConv2d(1024, 384, kernel_size=1, stride=1)
 self.branch1 = nn.Sequential(
 BasicConv2d(1024, 192, kernel_size=1, stride=1),
BasicConv2d(192, 224, kernel_size=(1,7), stride=1, padding=(0,3)) ,
BasicConv2d(224, 256, kernel_size=(7,1), stride=1, padding=(3,0)) 
)
 self.branch2 = nn.Sequential(
 BasicConv2d(1024, 192, kernel_size=1, stride=1),
BasicConv2d(192, 192, kernel_size=(7,1), stride=1, padding=(3,0)) ,
BasicConv2d(192, 224, kernel_size=(1,7), stride=1, padding=(0,3)) ,
BasicConv2d(224, 224, kernel_size=(7,1), stride=1, padding=(3,0)) ,
BasicConv2d(224, 256, kernel_size=(1,7), stride=1, padding=(0,3)) 
)
 self.branch3 = nn.Sequential(
 nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False),
BasicConv2d(1024, 128, kernel_size=1, stride=1)
)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 x2 = self.branch2(x)
 x3 = self.branch3(x)
 out = torch.cat(( x0, x1, x2, x3), 1)
 return out
class Reduction_B(nn.Module):
def __init__(self):
super(Reduction_B, self ).__init__()
self.branch0 = nn.Sequential(
 BasicConv2d(1024, 192, kernel_size=1, stride=1),
BasicConv2d(192, 192, kernel_size=3, stride=2)
)
 self.branch1 = nn.Sequential(
 BasicConv2d(1024, 256, kernel_size=1, stride=1),
BasicConv2d(256, 256, kernel_size=(1,7), stride=1, padding=(0,3)) ,
BasicConv2d(256, 320, kernel_size=(7,1), stride=1, padding=(3,0)) ,
BasicConv2d(320, 320, kernel_size=3, stride=2)
)
 self.branch2 = nn.MaxPool2d(3, stride=2)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 x2 = self.branch2(x)
 out = torch.cat(( x0, x1, x2), 1)
 return out
class Inception_C(nn.Module):
def __init__(self):
super(Inception_C, self ).__init__()
self.branch0 = BasicConv2d(1536, 256, kernel_size=1, stride=1)
 self.branch1_0 = BasicConv2d(1536, 384, kernel_size=1, stride=1)
 self.branch1_1a = BasicConv2d(384, 256, kernel_size=(1,3), stride=1, padding=(0,1))
self.branch1_1b = BasicConv2d(384, 256, kernel_size=(3,1), stride=1, padding=(1,0))
self.branch2_0 = BasicConv2d(1536, 384, kernel_size=1, stride=1)
 self.branch2_1 = BasicConv2d(384, 448, kernel_size=(3,1), stride=1, padding=(1,0))
self.branch2_2 = BasicConv2d(448, 512, kernel_size=(1,3), stride=1, padding=(0,1))
self.branch2_3a = BasicConv2d(512, 256, kernel_size=(1,3), stride=1, padding=(0,1))
self.branch2_3b = BasicConv2d(512, 256, kernel_size=(3,1), stride=1, padding=(1,0))
self.branch3 = nn.Sequential(
 nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False),
BasicConv2d(1536, 256, kernel_size=1, stride=1)
)
 def forward(self, x):
x0 = self.branch0(x)
 x1_0 = self.branch1_0(x)
 x1_1a = self.branch1_1a(x1_0)
 x1_1b = self.branch1_1b(x1_0)
 x1 = torch.cat(( x1_1a, x1_1b), 1)
 x2_0 = self.branch2_0(x)
 x2_1 = self.branch2_1(x2_0)
 x2_2 = self.branch2_2(x2_1)
 x2_3a = self.branch2_3a(x2_2)
 x2_3b = self.branch2_3b(x2_2)
 x2 = torch.cat(( x2_3a, x2_3b), 1)
 x3 = self.branch3(x)
 out = torch.cat(( x0, x1, x2, x3), 1)
 return out
class InceptionV4(nn.Module):
def __init__(self, num_classes=1001):
super(InceptionV4, self ).__init__()
self.input_space = None
self.input_size =(299, 299, 3)
 self.mean = None
self.std = None
self.features = nn.Sequential(
 BasicConv2d(3, 32, kernel_size=3, stride=2),
BasicConv2d(32, 32, kernel_size=3, stride=1),
BasicConv2d(32, 64, kernel_size=3, stride=1, padding=1),
Mixed_3a() ,
Mixed_4a() ,
Mixed_5a() ,
Inception_A() ,
Inception_A() ,
Inception_A() ,
Inception_A() ,
Reduction_A() ,
Inception_B() ,
Inception_B() ,
Inception_B() ,
Inception_B() ,
Inception_B() ,
Inception_B() ,
Inception_B() ,
Reduction_B() ,
Inception_C() ,
Inception_C() ,
Inception_C() 
)
 self.last_linear = nn.Linear(1536, num_classes)
 def logits(self, features):
adaptiveAvgPoolWidth = features.shape[2]
x = F.avg_pool2d(features, kernel_size=adaptiveAvgPoolWidth)
 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
def inceptionv4(num_classes=1000, pretrained='imagenet'):
if pretrained:
settings = pretrained_settings['inceptionv4'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
 model = InceptionV4(num_classes=1001)
 model.load_state_dict(model_zoo.load_url(settings['url']))
if pretrained == 'imagenet':
new_last_linear = nn.Linear(1536, 1000)
 new_last_linear.weight.data = model.last_linear.weight.data[1:]
new_last_linear.bias.data = model.last_linear.bias.data[1:]
model.last_linear = new_last_linear
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']
else:
model = InceptionV4(num_classes=num_classes)
 return model
weekends = train.loc[(( train['Day of Week']==0)|(train['Day of Week']==6)) &(train['H_Distance']==0)&(train['fare_amount'] < 3.0)]
weekends 
<filter>

 class ImageNetPolicy(object):

 def __init__(self, fillcolor=(128, 128, 128)) :
self.policies = [
SubPolicy(0.4, "posterize", 8, 0.6, "rotate", 9, fillcolor),
SubPolicy(0.6, "solarize", 5, 0.6, "autocontrast", 5, fillcolor),
SubPolicy(0.8, "equalize", 8, 0.6, "equalize", 3, fillcolor),
SubPolicy(0.6, "posterize", 7, 0.6, "posterize", 6, fillcolor),
SubPolicy(0.4, "equalize", 7, 0.2, "solarize", 4, fillcolor),
SubPolicy(0.4, "equalize", 4, 0.8, "rotate", 8, fillcolor),
SubPolicy(0.6, "solarize", 3, 0.6, "equalize", 7, fillcolor),
SubPolicy(0.8, "posterize", 5, 1.0, "equalize", 2, fillcolor),
SubPolicy(0.2, "rotate", 3, 0.6, "solarize", 8, fillcolor),
SubPolicy(0.6, "equalize", 8, 0.4, "posterize", 6, fillcolor),
SubPolicy(0.8, "rotate", 8, 0.4, "color", 0, fillcolor),
SubPolicy(0.4, "rotate", 9, 0.6, "equalize", 2, fillcolor),
SubPolicy(0.0, "equalize", 7, 0.8, "equalize", 8, fillcolor),
SubPolicy(0.6, "invert", 4, 1.0, "equalize", 8, fillcolor),
SubPolicy(0.6, "color", 4, 1.0, "contrast", 8, fillcolor),
SubPolicy(0.8, "rotate", 8, 1.0, "color", 2, fillcolor),
SubPolicy(0.8, "color", 8, 0.8, "solarize", 7, fillcolor),
SubPolicy(0.4, "sharpness", 7, 0.6, "invert", 8, fillcolor),
SubPolicy(0.6, "shearX", 5, 1.0, "equalize", 9, fillcolor),
SubPolicy(0.4, "color", 0, 0.6, "equalize", 3, fillcolor),
SubPolicy(0.4, "equalize", 7, 0.2, "solarize", 4, fillcolor),
SubPolicy(0.6, "solarize", 5, 0.6, "autocontrast", 5, fillcolor),
SubPolicy(0.6, "invert", 4, 1.0, "equalize", 8, fillcolor),
SubPolicy(0.6, "color", 4, 1.0, "contrast", 8, fillcolor)
 ]
def __call__(self, img):
policy_idx = random.randint(0, len(self.policies)- 1)
 return self.policies[policy_idx](img)
 def __repr__(self):
return "AutoAugment ImageNet Policy"
class CIFAR10Policy(object):

 def __init__(self, fillcolor=(128, 128, 128)) :
self.policies = [
SubPolicy(0.1, "invert", 7, 0.2, "contrast", 6, fillcolor),
SubPolicy(0.7, "rotate", 2, 0.3, "translateX", 9, fillcolor),
SubPolicy(0.8, "sharpness", 1, 0.9, "sharpness", 3, fillcolor),
SubPolicy(0.5, "shearY", 8, 0.7, "translateY", 9, fillcolor),
SubPolicy(0.5, "autocontrast", 8, 0.9, "equalize", 2, fillcolor),
SubPolicy(0.2, "shearY", 7, 0.3, "posterize", 7, fillcolor),
SubPolicy(0.4, "color", 3, 0.6, "brightness", 7, fillcolor),
SubPolicy(0.3, "sharpness", 9, 0.7, "brightness", 9, fillcolor),
SubPolicy(0.6, "equalize", 5, 0.5, "equalize", 1, fillcolor),
SubPolicy(0.6, "contrast", 7, 0.6, "sharpness", 5, fillcolor),
SubPolicy(0.7, "color", 7, 0.5, "translateX", 8, fillcolor),
SubPolicy(0.3, "equalize", 7, 0.4, "autocontrast", 8, fillcolor),
SubPolicy(0.4, "translateY", 3, 0.2, "sharpness", 6, fillcolor),
SubPolicy(0.9, "brightness", 6, 0.2, "color", 8, fillcolor),
SubPolicy(0.5, "solarize", 2, 0.0, "invert", 3, fillcolor),
SubPolicy(0.2, "equalize", 0, 0.6, "autocontrast", 0, fillcolor),
SubPolicy(0.2, "equalize", 8, 0.8, "equalize", 4, fillcolor),
SubPolicy(0.9, "color", 9, 0.6, "equalize", 6, fillcolor),
SubPolicy(0.8, "autocontrast", 4, 0.2, "solarize", 8, fillcolor),
SubPolicy(0.1, "brightness", 3, 0.7, "color", 0, fillcolor),
SubPolicy(0.4, "solarize", 5, 0.9, "autocontrast", 3, fillcolor),
SubPolicy(0.9, "translateY", 9, 0.7, "translateY", 9, fillcolor),
SubPolicy(0.9, "autocontrast", 2, 0.8, "solarize", 3, fillcolor),
SubPolicy(0.8, "equalize", 8, 0.1, "invert", 3, fillcolor),
SubPolicy(0.7, "translateY", 9, 0.9, "autocontrast", 1, fillcolor)
 ]
def __call__(self, img):
policy_idx = random.randint(0, len(self.policies)- 1)
 return self.policies[policy_idx](img)
 def __repr__(self):
return "AutoAugment CIFAR10 Policy"
class SVHNPolicy(object):

 def __init__(self, fillcolor=(128, 128, 128)) :
self.policies = [
SubPolicy(0.9, "shearX", 4, 0.2, "invert", 3, fillcolor),
SubPolicy(0.9, "shearY", 8, 0.7, "invert", 5, fillcolor),
SubPolicy(0.6, "equalize", 5, 0.6, "solarize", 6, fillcolor),
SubPolicy(0.9, "invert", 3, 0.6, "equalize", 3, fillcolor),
SubPolicy(0.6, "equalize", 1, 0.9, "rotate", 3, fillcolor),
SubPolicy(0.9, "shearX", 4, 0.8, "autocontrast", 3, fillcolor),
SubPolicy(0.9, "shearY", 8, 0.4, "invert", 5, fillcolor),
SubPolicy(0.9, "shearY", 5, 0.2, "solarize", 6, fillcolor),
SubPolicy(0.9, "invert", 6, 0.8, "autocontrast", 1, fillcolor),
SubPolicy(0.6, "equalize", 3, 0.9, "rotate", 3, fillcolor),
SubPolicy(0.9, "shearX", 4, 0.3, "solarize", 3, fillcolor),
SubPolicy(0.8, "shearY", 8, 0.7, "invert", 4, fillcolor),
SubPolicy(0.9, "equalize", 5, 0.6, "translateY", 6, fillcolor),
SubPolicy(0.9, "invert", 4, 0.6, "equalize", 7, fillcolor),
SubPolicy(0.3, "contrast", 3, 0.8, "rotate", 4, fillcolor),
SubPolicy(0.8, "invert", 5, 0.0, "translateY", 2, fillcolor),
SubPolicy(0.7, "shearY", 6, 0.4, "solarize", 8, fillcolor),
SubPolicy(0.6, "invert", 4, 0.8, "rotate", 4, fillcolor),
SubPolicy(0.3, "shearY", 7, 0.9, "translateX", 3, fillcolor),
SubPolicy(0.1, "shearX", 6, 0.6, "invert", 5, fillcolor),
SubPolicy(0.7, "solarize", 2, 0.6, "translateY", 7, fillcolor),
SubPolicy(0.8, "shearY", 4, 0.8, "invert", 8, fillcolor),
SubPolicy(0.7, "shearX", 9, 0.8, "translateY", 3, fillcolor),
SubPolicy(0.8, "shearY", 5, 0.7, "autocontrast", 3, fillcolor),
SubPolicy(0.7, "shearX", 2, 0.1, "invert", 5, fillcolor)
 ]
def __call__(self, img):
policy_idx = random.randint(0, len(self.policies)- 1)
 return self.policies[policy_idx](img)
 def __repr__(self):
return "AutoAugment SVHN Policy"
class SubPolicy(object):
def __init__(self, p1, operation1, magnitude_idx1, p2, operation2, magnitude_idx2, fillcolor=(128, 128, 128)) :
ranges = {
"shearX": np.linspace(0, 0.3, 10),
"shearY": np.linspace(0, 0.3, 10),
"translateX": np.linspace(0, 150 / 331, 10),
"translateY": np.linspace(0, 150 / 331, 10),
"rotate": np.linspace(0, 30, 10),
"color": np.linspace(0.0, 0.9, 10),
"posterize": np.round(np.linspace(8, 4, 10), 0 ).astype(np.int),
"solarize": np.linspace(256, 0, 10),
"contrast": np.linspace(0.0, 0.9, 10),
"sharpness": np.linspace(0.0, 0.9, 10),
"brightness": np.linspace(0.0, 0.9, 10),
"autocontrast": [0] * 10,
"equalize": [0] * 10,
"invert": [0] * 10
}
def rotate_with_fill(img, magnitude):
rot = img.convert("RGBA" ).rotate(magnitude)
 return Image.composite(rot, Image.new("RGBA", rot.size,(128,)* 4), rot ).convert(img.mode)
 func = {
"shearX": lambda img, magnitude: img.transform(
 img.size, Image.AFFINE,(1, magnitude * random.choice([-1, 1]), 0, 0, 1, 0),
Image.BICUBIC, fillcolor=fillcolor),
"shearY": lambda img, magnitude: img.transform(
 img.size, Image.AFFINE,(1, 0, 0, magnitude * random.choice([-1, 1]), 1, 0),
Image.BICUBIC, fillcolor=fillcolor),
"translateX": lambda img, magnitude: img.transform(
 img.size, Image.AFFINE,(1, 0, magnitude * img.size[0] * random.choice([-1, 1]), 0, 1, 0),
fillcolor=fillcolor),
"translateY": lambda img, magnitude: img.transform(
 img.size, Image.AFFINE,(1, 0, 0, 0, 1, magnitude * img.size[1] * random.choice([-1, 1])) ,
fillcolor=fillcolor),
"rotate": lambda img, magnitude: rotate_with_fill(img, magnitude),
"color": lambda img, magnitude: ImageEnhance.Color(img ).enhance(1 + magnitude * random.choice([-1, 1])) ,
"posterize": lambda img, magnitude: ImageOps.posterize(img, magnitude),
"solarize": lambda img, magnitude: ImageOps.solarize(img, magnitude),
"contrast": lambda img, magnitude: ImageEnhance.Contrast(img ).enhance(
 1 + magnitude * random.choice([-1, 1])) ,
"sharpness": lambda img, magnitude: ImageEnhance.Sharpness(img ).enhance(
 1 + magnitude * random.choice([-1, 1])) ,
"brightness": lambda img, magnitude: ImageEnhance.Brightness(img ).enhance(
 1 + magnitude * random.choice([-1, 1])) ,
"autocontrast": lambda img, magnitude: ImageOps.autocontrast(img),
"equalize": lambda img, magnitude: ImageOps.equalize(img),
"invert": lambda img, magnitude: ImageOps.invert(img)
 }
self.p1 = p1
self.operation1 = func[operation1]
self.magnitude1 = ranges[operation1][magnitude_idx1]
self.p2 = p2
self.operation2 = func[operation2]
self.magnitude2 = ranges[operation2][magnitude_idx2]
def __call__(self, img):
if random.random() < self.p1: img = self.operation1(img, self.magnitude1)
 if random.random() < self.p2: img = self.operation2(img, self.magnitude2)
 return img
train.loc[(train['H_Distance']!=0)&(train['fare_amount']==0)]<feature_engineering>
ImageFile.LOAD_TRUNCATED_IMAGES = True
AUG_IMG_SIZE=512
def david(image_path):
image = Image.open(image_path ).convert('RGB')
 image = cv2.cvtColor(numpy.array(image), cv2.COLOR_BGR2RGB)
 image = crop_image_from_gray(image)
 image = cv2.resize(image,(AUG_IMG_SIZE, AUG_IMG_SIZE))
image = cv2.addWeighted(image, 4, cv2.GaussianBlur(image,(0, 0), 30), -4, 128)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 image = Image.fromarray(image)
 return image
def nodavid(image_path):
image = Image.open(image_path ).convert('RGB')
 return image
tensor_transform_norm = transforms.Compose([
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
 test_transform_tsr= transforms.Compose([
transforms.Resize(( AUG_IMG_SIZE,AUG_IMG_SIZE),interpolation=Image.NEAREST),
transforms.CenterCrop(AUG_IMG_SIZE),
tensor_transform_norm
])
 train_transform_tsr = transforms.Compose([
transforms.Resize(( AUG_IMG_SIZE, AUG_IMG_SIZE), interpolation=Image.NEAREST),
transforms.CenterCrop(AUG_IMG_SIZE),
transforms.RandomApply([
ImageNetPolicy() ,
transforms.RandomHorizontalFlip() ,
transforms.RandomRotation(20, resample=PIL.Image.BICUBIC),
transforms.RandomAffine(0, translate=(
 0.2, 0.2), resample=PIL.Image.BICUBIC),
transforms.RandomAffine(0, shear=20, resample=PIL.Image.BICUBIC),
transforms.RandomAffine(0, scale=(0.8, 1.2),
resample=PIL.Image.BICUBIC)
 ],p=0.85),
tensor_transform_norm,
])
 SEED=999
def seed_everything(seed=SEED):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(int(seed))
torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 def predictOne(image_path, topk=1):
preds = []
image=david(image_path)
 image_orig=image
image = test_transform_tsr(image_orig)
 image = image.unsqueeze(0)
 image.cuda()
for m in mdls:
preds.append(m.forward(Variable(image)).data.cpu().numpy() [0])
 num_tta=10
for tta in range(num_tta):
seed_everything(SEED+102*tta)
 image = train_transform_tsr(image_orig)
 image = image.unsqueeze(0)
 image.cuda()
for m in mdls:
preds.append(m.forward(Variable(image)).data.cpu().numpy() [0])
 preds=numpy.mean(numpy.array(preds),axis=0)
 pobabilities = numpy.exp(preds)
 top_idx = np.argsort(pobabilities)[-topk:][::-1]
top_class = [idx_to_class[x] for x in top_idx]
top_probability = pobabilities[top_idx]
return top_class[0]
def testModel(test_dir):
for m in mdls:
m.cuda()
m.eval()
topk=1
columns = ['id_code', 'diagnosis']
df_pred = pd.DataFrame(data=np.zeros(( 0, len(columns))), columns=columns)
 for index, row in tqdm(sample_submission.iterrows()):
currImage=os.path.join(test_dir, row['id_code'])
 currImage=currImage + ".png"
if os.path.isfile(currImage):
with torch.no_grad() :
df_pred = df_pred.append({'id_code': row['id_code'], 'diagnosis': predictOne(currImage)}, ignore_index=True)
 return df_pred
if __name__ == '__main__':
sample_submission = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 sample_submission.columns = ['id_code', 'diagnosis']
sample_submission.head(3)
 df_pred=testModel(test_dir)
 df_pred.to_csv("submission.csv", columns=('id_code', 'diagnosis'), index=None )