Kaggle competition: LANL Earthquake Prediction
<feature_engineering><EOS> 
import pandas as pd
<SOS> metric: MAE Kaggle data source: lanl-earthquake-prediction<categorify>
import pandas as pd
class CarDataset(Dataset):

 def __init__(self, dataframe, root_dir, training=True, transform=None):
self.df = dataframe
self.root_dir = root_dir
self.transform = transform
self.training = training
def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
if torch.is_tensor(idx):
idx = idx.tolist()
idx, labels = self.df.values[idx]
img_name = self.root_dir.format(idx)
 img0 = imread(img_name, True)
 img = preprocess_image(img0)
 img = np.rollaxis(img, 2, 0)
 if self.training:
mask, regr = get_mask_and_regr(img0, labels)
 regr = np.rollaxis(regr, 2, 0)
 else:
mask, regr = 0, 0
return [img, mask, regr]<split>
df1 = pd.read_csv('.. /input/lanl-blend-csv/blend_ver1.csv')
 df2 = pd.read_csv('.. /input/lanl-blend-csv/blend_ver2.csv')
 df3 = pd.read_csv('.. /input/lanl-blend-csv/blend_ver3.csv' )
train_images_dir = PATH + 'train_images/{}.jpg'
test_images_dir = PATH + 'test_images/{}.jpg'
df_train, df_dev = train_test_split(train, test_size=0.08, random_state=63)
 df_test = test
train_dataset = CarDataset(df_train, train_images_dir)
 dev_dataset = CarDataset(df_dev, train_images_dir)
 test_dataset = CarDataset(df_test, test_images_dir )<load_pretrained>
blend = df1['time_to_failure'] *0.33333333334 + df2['time_to_failure'] * 0.33333333333 + df3['time_to_failure'] * 0.33333333333
sample = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv' )
BATCH_SIZE = 2
train_loader = DataLoader(dataset=train_dataset, batch_size=BATCH_SIZE, shuffle=True, num_workers=2)
 dev_loader = DataLoader(dataset=dev_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=2)
 test_loader = DataLoader(dataset=test_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=2 )<choose_model_class>
sample['time_to_failure'] = blend
sample.to_csv('blend_ver4.csv',index=False )
class double_conv(nn.Module):

 def __init__(self, in_ch, out_ch):
super(double_conv, self ).__init__()
self.conv = nn.Sequential(
 nn.Conv2d(in_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True),
nn.Conv2d(out_ch, out_ch, 3, padding=1),
nn.BatchNorm2d(out_ch),
nn.ReLU(inplace=True)
)
 def forward(self, x):
x = self.conv(x)
 return x
class up(nn.Module):
def __init__(self, in_ch, out_ch, bilinear=True):
super(up, self ).__init__()
if bilinear:
self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
 else:
self.up = nn.ConvTranspose2d(in_ch//2, in_ch//2, 2, stride=2)
 self.conv = double_conv(in_ch, out_ch)
 def forward(self, x1, x2=None):
x1 = self.up(x1)
 diffY = x2.size() [2] - x1.size() [2]
diffX = x2.size() [3] - x1.size() [3]
x1 = F.pad(x1,(diffX // 2, diffX - diffX//2,
diffY // 2, diffY - diffY//2))
if x2 is not None:
x = torch.cat([x2, x1], dim=1)
 else:
x = x1
x = self.conv(x)
 return x
def get_mesh(batch_size, shape_x, shape_y):
mg_x, mg_y = np.meshgrid(np.linspace(0, 1, shape_y), np.linspace(0, 1, shape_x))
mg_x = np.tile(mg_x[None, None, :, :], [batch_size, 1, 1, 1] ).astype('float32')
 mg_y = np.tile(mg_y[None, None, :, :], [batch_size, 1, 1, 1] ).astype('float32')
 mesh = torch.cat([torch.tensor(mg_x ).to(device), torch.tensor(mg_y ).to(device)], 1)
 return mesh<define_variables>
df4 = sample
__all__ = ['ResNet', 'resnet18', 'resnet34', 'resnet50', 'resnet101',
'resnet152', 'resnext50_32x4d', 'resnext101_32x8d',
'wide_resnet50_2', 'wide_resnet101_2']
model_urls = {
'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',
'resnext50_32x4d': 'https://download.pytorch.org/models/resnext50_32x4d-7cdf4587.pth',
'resnext101_32x8d': 'https://download.pytorch.org/models/resnext101_32x8d-8ba56ff5.pth',
'wide_resnet50_2': 'https://download.pytorch.org/models/wide_resnet50_2-95faca4d.pth',
'wide_resnet101_2': 'https://download.pytorch.org/models/wide_resnet101_2-32ee1156.pth',
}
def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):

 return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=dilation, groups=groups, bias=False, dilation=dilation)
 def conv1x1(in_planes, out_planes, stride=1):

 return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
 class BasicBlock(nn.Module):
expansion = 1
__constants__ = ['downsample']
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1, norm_layer=None):
super(BasicBlock, self ).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
if groups != 1 or base_width != 64:
raise ValueError('BasicBlock only supports groups=1 and base_width=64')
 if dilation > 1:
raise NotImplementedError("Dilation > 1 not supported in BasicBlock")
 self.conv1 = conv3x3(inplanes, planes, stride)
 self.bn1 = norm_layer(planes)
 self.relu = nn.ReLU(inplace=True)
 self.conv2 = conv3x3(planes, planes)
 self.bn2 = norm_layer(planes)
 self.downsample = downsample
self.stride = stride
def forward(self, x):
identity = x
out = self.conv1(x)
 out = self.bn1(out)
 out = self.relu(out)
 out = self.conv2(out)
 out = self.bn2(out)
 if self.downsample is not None:
identity = self.downsample(x)
 out += identity
out = self.relu(out)
 return out
class Bottleneck(nn.Module):
expansion = 4
__constants__ = ['downsample']
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
base_width=64, dilation=1, norm_layer=None):
super(Bottleneck, self ).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
width = int(planes *(base_width / 64.))* groups
self.conv1 = conv1x1(inplanes, width)
 self.bn1 = norm_layer(width)
 self.conv2 = conv3x3(width, width, stride, groups, dilation)
 self.bn2 = norm_layer(width)
 self.conv3 = conv1x1(width, planes * self.expansion)
 self.bn3 = norm_layer(planes * self.expansion)
 self.relu = nn.ReLU(inplace=True)
 self.downsample = downsample
self.stride = stride
def forward(self, x):
identity = 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:
identity = self.downsample(x)
 out += identity
out = self.relu(out)
 return out
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes=1000, zero_init_residual=False,
groups=1, width_per_group=64, replace_stride_with_dilation=None,
norm_layer=None):
super(ResNet, self ).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self._norm_layer = norm_layer
self.inplanes = 64
self.dilation = 1
if replace_stride_with_dilation is None:
replace_stride_with_dilation = [False, False, False]
if len(replace_stride_with_dilation)!= 3:
raise ValueError("replace_stride_with_dilation should be None "
"or a 3-element tuple, got {}".format(replace_stride_with_dilation))
self.groups = groups
self.base_width = width_per_group
self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3,
bias=False)
 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])
 self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
dilate=replace_stride_with_dilation[0])
 self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
dilate=replace_stride_with_dilation[1])
 self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
dilate=replace_stride_with_dilation[2])
 self.avgpool = nn.AdaptiveAvgPool2d(( 1, 1))
self.fc = nn.Linear(512 * block.expansion, num_classes)
 for m in self.modules() :
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
 elif isinstance(m,(nn.BatchNorm2d, nn.GroupNorm)) :
nn.init.constant_(m.weight, 1)
 nn.init.constant_(m.bias, 0)
 if zero_init_residual:
for m in self.modules() :
if isinstance(m, Bottleneck):
nn.init.constant_(m.bn3.weight, 0)
 elif isinstance(m, BasicBlock):
nn.init.constant_(m.bn2.weight, 0)
 def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
norm_layer = self._norm_layer
downsample = None
previous_dilation = self.dilation
if dilate:
self.dilation *= stride
stride = 1
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
 conv1x1(self.inplanes, planes * block.expansion, stride),
norm_layer(planes * block.expansion), 
)
 layers = []
layers.append(block(self.inplanes, planes, stride, downsample, self.groups,
self.base_width, previous_dilation, norm_layer))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, groups=self.groups,
base_width=self.base_width, dilation=self.dilation,
norm_layer=norm_layer))
return nn.Sequential(*layers)

def forward(self, x):
conv1 = F.relu(self.bn1(self.conv1(x)) , inplace=True)
 conv1 = F.max_pool2d(conv1, 3, stride=2, padding=1)
 feats4 = self.layer1(conv1)
 feats8 = self.layer2(feats4)
 feats16 = self.layer3(feats8)
 feats32 = self.layer4(feats16)
 return feats8, feats16, feats32
def _resnet(arch, block, layers, pretrained, progress, **kwargs):
model = ResNet(block, layers, **kwargs)
 if pretrained:
state_dict = load_state_dict_from_url(model_urls[arch],
progress=progress)
 model.load_state_dict(state_dict)
 return model
def resnet18(pretrained=False, progress=True, **kwargs):
r
return _resnet('resnet18', BasicBlock, [2, 2, 2, 2], pretrained, progress,
**kwargs)
 def resnet34(pretrained=False, progress=True, **kwargs):
r
return _resnet('resnet34', BasicBlock, [3, 4, 6, 3], pretrained, progress,
**kwargs)
 def resnet50(pretrained=False, progress=True, **kwargs):
r
return _resnet('resnet50', Bottleneck, [3, 4, 6, 3], pretrained, progress,
**kwargs)
 def resnet101(pretrained=False, progress=True, **kwargs):
r
return _resnet('resnet101', Bottleneck, [3, 4, 23, 3], pretrained, progress,
**kwargs)
 def resnet152(pretrained=False, progress=True, **kwargs):
r
return _resnet('resnet152', Bottleneck, [3, 8, 36, 3], pretrained, progress,
**kwargs)
 def resnext50_32x4d(pretrained=False, progress=True, **kwargs):
r
kwargs['groups'] = 32
kwargs['width_per_group'] = 4
return _resnet('resnext50_32x4d', Bottleneck, [3, 4, 6, 3],
pretrained, progress, **kwargs)
 def resnext101_32x8d(pretrained=False, progress=True, **kwargs):
r
kwargs['groups'] = 32
kwargs['width_per_group'] = 8
return _resnet('resnext101_32x8d', Bottleneck, [3, 4, 23, 3],
pretrained, progress, **kwargs)
 def wide_resnet50_2(pretrained=False, progress=True, **kwargs):
r
kwargs['width_per_group'] = 64 * 2
return _resnet('wide_resnet50_2', Bottleneck, [3, 4, 6, 3],
pretrained, progress, **kwargs)
 def wide_resnet101_2(pretrained=False, progress=True, **kwargs):
r
kwargs['width_per_group'] = 64 * 2
return _resnet('wide_resnet101_2', Bottleneck, [3, 4, 23, 3],
pretrained, progress, **kwargs )<choose_model_class>
blend1 = df1['time_to_failure'] *0.33333333333 + df2['time_to_failure'] * 0.33333333333 + df4['time_to_failure'] * 0.33333333334
sample1 = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv' )
base_model = resnext50_32x4d(pretrained=True)
 base_model<train_on_grid>
sample1['time_to_failure'] = blend1
sample1.to_csv('blend_ver5.csv',index=False )
class CentResnet(nn.Module):

 def __init__(self, n_classes):
super(CentResnet, self ).__init__()
self.base_model = base_model
self.lat8 = nn.Conv2d(512, 256, 1)
 self.lat16 = nn.Conv2d(1024, 256, 1)
 self.lat32 = nn.Conv2d(2048, 256, 1)
 self.bn8 = nn.GroupNorm(16, 256)
 self.bn16 = nn.GroupNorm(16, 256)
 self.bn32 = nn.GroupNorm(16, 256)
 self.conv0 = double_conv(5, 64)
 self.conv1 = double_conv(64, 128)
 self.conv2 = double_conv(128, 512)
 self.conv3 = double_conv(512, 1024)
 self.mp = nn.MaxPool2d(2)
 self.up1 = up(1282 , 512)
 self.up2 = up(512 + 512, 256)
 self.outc = nn.Conv2d(256, n_classes, 1)
 def forward(self, x):
batch_size = x.shape[0]
mesh1 = get_mesh(batch_size, x.shape[2], x.shape[3])
 x0 = torch.cat([x, mesh1], 1)
 x1 = self.mp(self.conv0(x0))
x2 = self.mp(self.conv1(x1))
x3 = self.mp(self.conv2(x2))
x4 = self.mp(self.conv3(x3))
feats8, feats16, feats32 = self.base_model(x)
 lat8 = F.relu(self.bn8(self.lat8(feats8)))
 lat16 = F.relu(self.bn16(self.lat16(feats16)))
 lat32 = F.relu(self.bn32(self.lat32(feats32)))
 mesh2 = get_mesh(batch_size, lat32.shape[2], lat32.shape[3])
 feats = torch.cat([lat32, mesh2], 1)
 x = self.up1(feats, x4)
 x = self.up2(x, x3)
 x = self.outc(x)
 return x<choose_model_class>
df5 = sample1
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 print(device)
 n_epochs = 3
model = CentResnet(8 ).to(device)
 optimizer = optim.AdamW(model.parameters() , lr=0.001)
 exp_lr_scheduler = optim.lr_scheduler.CyclicLR(optimizer,base_lr=0.001,cycle_momentum=False,max_lr=0.0013,step_size_up=2000 )<compute_test_metric>
blend2 = df1['time_to_failure'] *0.2 + df2['time_to_failure'] * 0.2 + df3['time_to_failure'] * 0.2 + df4['time_to_failure'] * 0.2 + df5['time_to_failure'] * 0.2
sample2 = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv' )
def criterion(prediction, mask, regr,weight=0.4, size_average=True):
pred_mask = torch.sigmoid(prediction[:, 0])
 mask_loss = mask * torch.log(pred_mask + 1e-12)+(1 - mask)* torch.log(1 - pred_mask + 1e-12)
 mask_loss = -mask_loss.mean(0 ).sum()
pred_regr = prediction[:, 1:]
regr_loss =(torch.abs(pred_regr - regr ).sum(1)* mask ).sum(1 ).sum(1)/ mask.sum(1 ).sum(1)
 regr_loss = regr_loss.mean(0)
 loss = mask_loss +regr_loss
if not size_average:
loss *= prediction.shape[0]
return loss ,mask_loss , regr_loss<train_model>
sample2['time_to_failure'] = blend2
sample2.to_csv('blend_ver6.csv',index=False )
def train(epoch, history=None):
model.train()
t = tqdm(train_loader)
 for batch_idx,(img_batch, mask_batch, regr_batch)in enumerate(t):
img_batch = img_batch.to(device)
 mask_batch = mask_batch.to(device)
 regr_batch = regr_batch.to(device)
 optimizer.zero_grad()
output = model(img_batch)
 if epoch < SWITCH_LOSS_EPOCH :
loss,mask_loss, regr_loss = criterion(output, mask_batch, regr_batch,1)
 else:
loss,mask_loss, regr_loss = criterion(output, mask_batch, regr_batch,0.5)
 t.set_description(f'train_loss(l={loss:.3f} )(m={mask_loss:.2f} )(r={regr_loss:.4f}')
 if history is not None:
history.loc[epoch + batch_idx / len(train_loader), 'train_loss'] = loss.data.cpu().numpy()
loss.backward()
optimizer.step()
exp_lr_scheduler.step()
print('Train Epoch: {} \tLR: {:.6f}\tLoss: {:.6f}\tMaskLoss: {:.6f}\tRegLoss: {:.6f}'.format(
 epoch,
optimizer.state_dict() ['param_groups'][0]['lr'],
loss.data,
mask_loss.data,
regr_loss.data))
def evaluate(epoch, history=None):
model.eval()
loss = 0
valid_loss = 0
valid_mask_loss = 0
valid_regr_loss = 0
with torch.no_grad() :
for img_batch, mask_batch, regr_batch in dev_loader:
img_batch = img_batch.to(device)
 mask_batch = mask_batch.to(device)
 regr_batch = regr_batch.to(device)
 output = model(img_batch)
 if epoch < SWITCH_LOSS_EPOCH :
loss,mask_loss, regr_loss= criterion(output, mask_batch, regr_batch,1, size_average=False)
 valid_loss += loss.data
valid_mask_loss += mask_loss.data
valid_regr_loss += regr_loss.data
else :
loss,mask_loss, regr_loss = criterion(output, mask_batch, regr_batch,0.5, size_average=False)
 valid_loss += loss.data
valid_mask_loss += mask_loss.data
valid_regr_loss += regr_loss.data
valid_loss /= len(dev_loader.dataset)
 valid_mask_loss /= len(dev_loader.dataset)
 valid_regr_loss /= len(dev_loader.dataset)
 if history is not None:
history.loc[epoch, 'dev_loss'] = valid_loss.cpu().numpy()
history.loc[epoch, 'mask_loss'] = valid_mask_loss.cpu().numpy()
history.loc[epoch, 'regr_loss'] = valid_regr_loss.cpu().numpy()
print('Dev loss: {:.4f}'.format(valid_loss)) 
<save_model>
df6 = sample2
torch.save(model.state_dict() , './resnext50.pth' )<statistical_test>
blend3 = df1['time_to_failure'] *0.16666666666 + df2['time_to_failure'] * 0.16666666666 + df3['time_to_failure'] * 0.16666666666 + df4['time_to_failure'] * 0.16666666666 + df5['time_to_failure'] * 0.16666666666 + df6['time_to_failure'] * 0.16666666666
sample3 = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv' )
<train_model><EOS> 
sample3['time_to_failure'] = blend3
sample3.to_csv('blend_ver7.csv',index=False )