Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<feature_engineering>
pd.set_option("display.float_format", lambda x: "%.2f" % x)
 pd.set_option("display.max_columns", 500)
 pd.set_option("display.max_rows", 1500)
 %matplotlib inline
sys.path.append('.. /input/tabnetdevelop/tabnet-develop(1)/tabnet-develop')
 os.environ["CUDA_LAUNCH_BLOCKING"] = "1" 
<load_from_csv>
%%javascript
IPython.OutputArea.auto_scroll_threshold = 9999;
data_path = ".. /input/lish-moa/"
test = pd.read_csv(data_path+'test_features.csv')
 test.drop(columns=["sig_id"], inplace=True)
 test["cp_type"] =(test["cp_type"]=="trt_cp")+ 0
test["cp_dose"] =(test["cp_dose"]=="D1")+ 0
X_test = test.values
submission = pd.read_csv(data_path+'sample_submission.csv' )<choose_model_class>
random.seed(35)
 train = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/sales_train.csv')
 test = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/test.csv' )
mask_type = 'entmax'
n_da = 60
n_steps = 1
gamma = 1.0
lambda_sparse = 2.6743669818463933e-05
n_shared = 1
tabnet_params = dict(n_d=n_da, n_a=n_da, n_steps=n_steps, gamma=gamma,
lambda_sparse=lambda_sparse, optimizer_fn=torch.optim.Adam,
optimizer_params=dict(lr=2e-2, weight_decay=1e-5),
mask_type=mask_type, n_shared=n_shared,
scheduler_params=dict(mode="min",
patience=5,
min_lr=1e-5,
factor=0.9,),
scheduler_fn=torch.optim.lr_scheduler.ReduceLROnPlateau,
verbose=1,
)<choose_model_class>
def downcast_df(df):
before = df.memory_usage()
cols_to_skip = df.isna().sum() [df.isna().sum() > 0].index
numcols = [
col for col in df.select_dtypes("number" ).columns if col not in cols_to_skip
]
ints = df.select_dtypes(int ).columns
floats = df.select_dtypes(float ).columns
for col in ints:
df[col] = pd.to_numeric(df[col].values, downcast="integer")
 for col in floats:
df[col] = pd.to_numeric(df[col].values, downcast="float")
 after = df.memory_usage()
reduction =(before - after)/ before * 100
comparison = pd.concat([before, after, reduction], axis=1)
 comparison.columns = [
"before",
"after",
"reduction(%)",
]
print(f"Downcasting:
{comparison}
")
 return df
df = downcast_df(train)
 df = downcast_df(test)
test_cv_preds = []
model = TabNetRegressor(**tabnet_params )<drop_column>
test = test.drop('ID', axis=1)
 test['date'] = '01.11.2015'
test['date_block_num'] = 34
test['item_cnt_day'] = np.nan
test['item_price'] = np.nan
dirs = os.listdir('.. /input/tabnetregressor-2-0-3-seed-7-fold')
 dirs.remove('__results__.html')
 dirs.remove('__notebook__.ipynb')
 dirs.remove('custom.css')
 dirs.remove('__output__.json')
 dirs.remove('submission.csv' )<predict_on_test>
train[train['item_price']<0]
for x in dirs:
model.load_model(f".. /input/tabnetregressor-2-0-3-seed-7-fold/{x}")
 preds_test = model.predict(X_test)
 test_cv_preds.append(1 /(1 + np.exp(-preds_test)) )<compute_test_metric>
train[(train['item_id']==2973)&(train['shop_id']==32)]
mean = np.mean(X_test, axis=0)
 std = np.std(X_test, axis=0 )<save_to_csv>
train.loc[train['item_price']<0,'item_price'] = 2499
test_preds_all = np.stack(test_cv_preds)
 all_feat = [col for col in submission.columns if col not in ["sig_id"]]
submission[all_feat] = test_preds_all.mean(axis=0)
 submission.loc[test['cp_type']==0, submission.columns[1:]] = 0
submission.to_csv('submission.csv', index=None )<import_modules>
train = train.groupby(by=["date_block_num", "shop_id", "item_id"], as_index=False ).agg(
 {"item_cnt_day": "sum", "item_price": "mean", "date": "first"})
 train = train.rename(columns={"item_cnt_day": "item_cnt_month"})
 test = test.rename(columns={"item_cnt_day": "item_cnt_month"})
 train["item_cnt_month"] = train["item_cnt_month"].clip(0, 20 )
import abc
import yaml
import torchvision.transforms as T
import torchvision.models as models
import torch.nn.functional as F
import torch.nn as nn
import torch
import random
import pandas as pd
import os
import numpy as np
import re
import math
import collections
from functools import partial
from copy import deepcopy
import logging.config
import datetime
import cv2
import warnings
from albumentations import HorizontalFlip, Normalize, Compose, ImageOnlyTransform
from albumentations.pytorch import ToTensor
from collections import Sequence
from sklearn.model_selection import train_test_split
from tqdm import tqdm
from torch.jit import load
from torchvision.utils import make_grid
from torch.utils.data import DataLoader, Dataset
from torch.utils import model_zoo
from pathlib import Path
from os.path import join
from collections import OrderedDict
from scipy import ndimage<define_search_model>
train[train["item_price"] > 100000]
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=False)
 def forward(self, x):
x = self.conv(x)
 x = self.bn(x)
 x = self.relu(x)
 return x
class Mixed_5b(nn.Module):
def __init__(self):
super(Mixed_5b, self ).__init__()
self.branch0 = BasicConv2d(192, 96, kernel_size=1, stride=1)
 self.branch1 = nn.Sequential(
 BasicConv2d(192, 48, kernel_size=1, stride=1),
BasicConv2d(48, 64, kernel_size=5, stride=1, padding=2)
)
 self.branch2 = nn.Sequential(
 BasicConv2d(192, 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(192, 64, 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 Block35(nn.Module):
def __init__(self, scale=1.0):
super(Block35, self ).__init__()
self.scale = scale
self.branch0 = BasicConv2d(320, 32, kernel_size=1, stride=1)
 self.branch1 = nn.Sequential(
 BasicConv2d(320, 32, kernel_size=1, stride=1),
BasicConv2d(32, 32, kernel_size=3, stride=1, padding=1)
)
 self.branch2 = nn.Sequential(
 BasicConv2d(320, 32, kernel_size=1, stride=1),
BasicConv2d(32, 48, kernel_size=3, stride=1, padding=1),
BasicConv2d(48, 64, kernel_size=3, stride=1, padding=1)
)
 self.conv2d = nn.Conv2d(128, 320, kernel_size=1, stride=1)
 self.relu = nn.ReLU(inplace=False)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 x2 = self.branch2(x)
 out = torch.cat(( x0, x1, x2), 1)
 out = self.conv2d(out)
 out = out * self.scale + x
out = self.relu(out)
 return out
class Mixed_6a(nn.Module):
def __init__(self):
super(Mixed_6a, self ).__init__()
self.branch0 = BasicConv2d(320, 384, kernel_size=3, stride=2)
 self.branch1 = nn.Sequential(
 BasicConv2d(320, 256, kernel_size=1, stride=1),
BasicConv2d(256, 256, kernel_size=3, stride=1, padding=1),
BasicConv2d(256, 384, 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 Block17(nn.Module):
def __init__(self, scale=1.0):
super(Block17, self ).__init__()
self.scale = scale
self.branch0 = BasicConv2d(1088, 192, kernel_size=1, stride=1)
 self.branch1 = nn.Sequential(
 BasicConv2d(1088, 128, kernel_size=1, stride=1),
BasicConv2d(128, 160, kernel_size=(1,7), stride=1, padding=(0,3)) ,
BasicConv2d(160, 192, kernel_size=(7,1), stride=1, padding=(3,0)) 
)
 self.conv2d = nn.Conv2d(384, 1088, kernel_size=1, stride=1)
 self.relu = nn.ReLU(inplace=False)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 out = torch.cat(( x0, x1), 1)
 out = self.conv2d(out)
 out = out * self.scale + x
out = self.relu(out)
 return out
class Mixed_7a(nn.Module):
def __init__(self):
super(Mixed_7a, self ).__init__()
self.branch0 = nn.Sequential(
 BasicConv2d(1088, 256, kernel_size=1, stride=1),
BasicConv2d(256, 384, kernel_size=3, stride=2)
)
 self.branch1 = nn.Sequential(
 BasicConv2d(1088, 256, kernel_size=1, stride=1),
BasicConv2d(256, 288, kernel_size=3, stride=2)
)
 self.branch2 = nn.Sequential(
 BasicConv2d(1088, 256, kernel_size=1, stride=1),
BasicConv2d(256, 288, kernel_size=3, stride=1, padding=1),
BasicConv2d(288, 320, kernel_size=3, stride=2)
)
 self.branch3 = nn.MaxPool2d(3, stride=2)
 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 Block8(nn.Module):
def __init__(self, scale=1.0, noReLU=False):
super(Block8, self ).__init__()
self.scale = scale
self.noReLU = noReLU
self.branch0 = BasicConv2d(2080, 192, kernel_size=1, stride=1)
 self.branch1 = nn.Sequential(
 BasicConv2d(2080, 192, kernel_size=1, stride=1),
BasicConv2d(192, 224, kernel_size=(1,3), stride=1, padding=(0,1)) ,
BasicConv2d(224, 256, kernel_size=(3,1), stride=1, padding=(1,0)) 
)
 self.conv2d = nn.Conv2d(448, 2080, kernel_size=1, stride=1)
 if not self.noReLU:
self.relu = nn.ReLU(inplace=False)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 out = torch.cat(( x0, x1), 1)
 out = self.conv2d(out)
 out = out * self.scale + x
if not self.noReLU:
out = self.relu(out)
 return out
class InceptionResNetV2(nn.Module):
def __init__(self, num_classes=1001):
super(InceptionResNetV2, self ).__init__()
self.input_space = None
self.input_size =(299, 299, 3)
 self.mean = None
self.std = None
self.conv2d_1a = BasicConv2d(3, 32, kernel_size=3, stride=2)
 self.conv2d_2a = BasicConv2d(32, 32, kernel_size=3, stride=1)
 self.conv2d_2b = BasicConv2d(32, 64, kernel_size=3, stride=1, padding=1)
 self.maxpool_3a = nn.MaxPool2d(3, stride=2)
 self.conv2d_3b = BasicConv2d(64, 80, kernel_size=1, stride=1)
 self.conv2d_4a = BasicConv2d(80, 192, kernel_size=3, stride=1)
 self.maxpool_5a = nn.MaxPool2d(3, stride=2)
 self.mixed_5b = Mixed_5b()
self.repeat = nn.Sequential(
 Block35(scale=0.17),
Block35(scale=0.17),
Block35(scale=0.17),
Block35(scale=0.17),
Block35(scale=0.17),
Block35(scale=0.17),
Block35(scale=0.17),
Block35(scale=0.17),
Block35(scale=0.17),
Block35(scale=0.17)
)
 self.mixed_6a = Mixed_6a()
self.repeat_1 = nn.Sequential(
 Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10),
Block17(scale=0.10)
)
 self.mixed_7a = Mixed_7a()
self.repeat_2 = nn.Sequential(
 Block8(scale=0.20),
Block8(scale=0.20),
Block8(scale=0.20),
Block8(scale=0.20),
Block8(scale=0.20),
Block8(scale=0.20),
Block8(scale=0.20),
Block8(scale=0.20),
Block8(scale=0.20)
)
 self.block8 = Block8(noReLU=True)
 self.conv2d_7b = BasicConv2d(2080, 1536, kernel_size=1, stride=1)
 self.avgpool_1a = nn.AvgPool2d(8, count_include_pad=False)
 self.last_linear = nn.Linear(1536, num_classes)
 def features(self, input):
x = self.conv2d_1a(input)
 x = self.conv2d_2a(x)
 x = self.conv2d_2b(x)
 x = self.maxpool_3a(x)
 x = self.conv2d_3b(x)
 x = self.conv2d_4a(x)
 x = self.maxpool_5a(x)
 x = self.mixed_5b(x)
 x = self.repeat(x)
 x = self.mixed_6a(x)
 x = self.repeat_1(x)
 x = self.mixed_7a(x)
 x = self.repeat_2(x)
 x = self.block8(x)
 x = self.conv2d_7b(x)
 return x
def logits(self, features):
x = self.avgpool_1a(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
class InceptionResNetV2Encoder(InceptionResNetV2):
def __init__(self, in_channels=3, *args, **kwargs):
super().__init__(*args, **kwargs)
 self.in_channels = in_channels
self.pretrained = False
for m in self.modules() :
if isinstance(m, nn.Conv2d):
if m.kernel_size ==(3, 3):
m.padding =(1, 1)
 if isinstance(m, nn.MaxPool2d):
m.padding =(1, 1)
 del self.avgpool_1a
del self.last_linear
def forward(self, x):
x = self.conv2d_1a(x)
 x = self.conv2d_2a(x)
 x = self.conv2d_2b(x)
 x0 = x
x = self.maxpool_3a(x)
 x = self.conv2d_3b(x)
 x = self.conv2d_4a(x)
 x1 = x
x = self.maxpool_5a(x)
 x = self.mixed_5b(x)
 x = self.repeat(x)
 x2 = x
x = self.mixed_6a(x)
 x = self.repeat_1(x)
 x3 = x
x = self.mixed_7a(x)
 x = self.repeat_2(x)
 x = self.block8(x)
 x = self.conv2d_7b(x)
 x4 = x
features = [x4, x3, x2, x1, x0]
return features
inception_encoders = {
'inceptionresnetv2': {
'encoder': InceptionResNetV2Encoder,
'pretrained_settings': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionresnetv2-520b38e4.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
},
},
'out_shapes':(1536, 1088, 320, 192, 64),
'params': {
'num_classes': 1000,
}
}
} 
<define_search_model>
train = train[train["item_price"] < 100000]
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
class InceptionV4Encoder(InceptionV4):
def __init__(self, in_channels=3, *args, **kwargs):
super().__init__(*args, **kwargs)
 self.in_channels = in_channels
self.pretrained = False
self.features[0] = BasicConv2d(self.in_channels, 32, kernel_size=3, stride=2, padding=1)
 self.features[1] = BasicConv2d(32, 32, kernel_size=3, stride=1, padding=1)
 self.chunks = [3, 5, 9, 15]
for m in self.modules() :
if isinstance(m, nn.Conv2d):
if m.kernel_size ==(3, 3):
m.padding =(1, 1)
 if isinstance(m, nn.MaxPool2d):
m.padding =(1, 1)
 del self.last_linear
def forward(self, x):
x0 = self.features[:self.chunks[0]](x)
 x1 = self.features[self.chunks[0]:self.chunks[1]](x0)
 x2 = self.features[self.chunks[1]:self.chunks[2]](x1)
 x3 = self.features[self.chunks[2]:self.chunks[3]](x2)
 x4 = self.features[self.chunks[3]:](x3)
 features = [x4, x3, x2, x1, x0]
return features
inceptionv4_encoders = {
'inceptionv4': {
'encoder': InceptionV4Encoder,
'pretrained_settings': {
'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
}
},
'out_shapes':(1536, 1024, 384, 192, 64),
'params': {
'num_classes': 1001,
}
}
}<categorify>
%%time
def extend_train() :

 dblocklist = train["date_block_num"].unique()
dblock_month =(
 train[["date_block_num", "date"]] 
.drop_duplicates() 
.set_index("date_block_num")
.to_dict() ["date"] 
)
 item_price = train.groupby(by=["item_id", "date_block_num"])["item_price"].mean()
train_extended_list = []
for dblock in dblocklist:
shops_dblock = train.loc[train["date_block_num"] == dblock, "shop_id"].unique()
items_dblock = train.loc[train["date_block_num"] == dblock, "item_id"].unique()
cartesian_prod = list(product(*[items_dblock, shops_dblock, [dblock]]))
train_extended_list.append(np.array(cartesian_prod))
train_extended_list = np.vstack(train_extended_list)
 train_extended = pd.DataFrame(
 train_extended_list, columns=["item_id", "shop_id", "date_block_num"] 
)
 train_extended = train_extended.merge(
 train, on=["item_id", "shop_id", "date_block_num"], how="left" 
)
 train_extended["item_cnt_month"].fillna(0, inplace=True)
 train_extended["date"] = train_extended["date_block_num"].map(dblock_month)
 train_extended["item_price"] = train_extended.set_index(
 ["item_id", "date_block_num"]
).index.map(item_price)
 train_extended["train"] = 1
test["train"] = 0
return train_extended.append(test ).reset_index(drop=True)
 df = extend_train()
del train, test
def cycle_rgb_weights(weights, n):

 slices = [(c % 3, c % 3 + 1)for c in range(n)]
new_weights = torch.cat([
weights[:, a:b, :, :] for a, b in slices
], dim=1)
 return new_weights
def avg_rgb_weights(weights):
new_weights = weights.mean(dim=1, keepdim=True)
 return new_weights
def transfer_weights(pretrained, replacement, method='cycle'):

 if method == 'cycle':
n = replacement.in_channels
weights = cycle_rgb_weights(pretrained.weight, n)
 elif method == 'avg':
weights = avg_rgb_weights(pretrained.weight)
 else:
raise NotImplementedError('`method` must be "cycle" or "avg", received {}'.format(method))
replacement.weight = nn.Parameter(weights)
 return replacement
class Model(nn.Module):
def __init__(self):
super().__init__()
def initialize(self):
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.init.constant_(m.weight, 1)
 nn.init.constant_(m.bias, 0)
 class EncoderDecoder(Model):
def __init__(self, encoder, decoder, activation):
super().__init__()
self.encoder = encoder
self.decoder = decoder
if callable(activation)or activation is None:
self.activation = activation
elif activation == 'softmax':
self.activation = nn.Softmax(dim=1)
 elif activation == 'sigmoid':
self.activation = nn.Sigmoid()
else:
raise ValueError('Activation should be "sigmoid"/"softmax"/callable/None')
 def forward(self, x):

 x = self.encoder(x)
 x = self.decoder(x)
 return x
def predict(self, x):

 if self.training:
self.eval()
with torch.no_grad() :
x = self.forward(x)
 if self.activation:
x = self.activation(x)
 return x
class EfficientNetEncoder(nn.Module):

 def __init__(self,
width_coeff,
depth_coeff,
image_size,
dropout_rate,
drop_connect_rate,
block_chunks,
in_channels = 3 
):
super().__init__()
self._blocks_args = self.get_block_args()
self._global_params = get_global_params(width_coeff, depth_coeff, image_size,
dropout_rate, drop_connect_rate)
 self.block_chunks = block_chunks
self.in_channels = in_channels
Conv2d = get_same_padding_conv2d(image_size=self._global_params.image_size)
 bn_mom = 1 - self._global_params.batch_norm_momentum
bn_eps = self._global_params.batch_norm_epsilon
out_channels = round_filters(32, self._global_params)
 self._conv_stem = Conv2d(self.in_channels, out_channels, kernel_size=3, stride=2, bias=False)
 self._bn0 = nn.BatchNorm2d(num_features=out_channels, momentum=bn_mom, eps=bn_eps)
 self._blocks = nn.ModuleList([])
 for block_args in self._blocks_args:
block_args = block_args._replace(
 input_filters=round_filters(block_args.input_filters, self._global_params),
output_filters=round_filters(block_args.output_filters, self._global_params),
num_repeat=round_repeats(block_args.num_repeat, self._global_params)
)
 self._blocks.append(MBConvBlock(block_args, self._global_params))
if block_args.num_repeat > 1:
block_args = block_args._replace(input_filters=block_args.output_filters, stride=1)
 for _ in range(block_args.num_repeat - 1):
self._blocks.append(MBConvBlock(block_args, self._global_params))
n_blocks = len(self._blocks)
 assert self.block_chunks[-1] == n_blocks, f'{self.block_chunks[-1]}, {n_blocks}'
self._out_shapes = [self._bn0.num_features]
self._out_shapes += [self._blocks[i - 1]._bn2.num_features for i in self.block_chunks[1:]]
self._out_shapes = list(reversed(self._out_shapes))
self.drop_connect_rates = np.arange(0, n_blocks, dtype=np.float)/ n_blocks
self.drop_connect_rates *= self._global_params.drop_connect_rate
def forward_blocks(self, x, start_idx, end_idx):
for idx in range(start_idx, end_idx):
x = self._blocks[idx](x, self.drop_connect_rates[idx])
 return x
def forward(self, x):
x0 = relu_fn(self._bn0(self._conv_stem(x)))
 x1 = self.forward_blocks(x0, self.block_chunks[0], self.block_chunks[1])
 x2 = self.forward_blocks(x1, self.block_chunks[1], self.block_chunks[2])
 x3 = self.forward_blocks(x2, self.block_chunks[2], self.block_chunks[3])
 x4 = self.forward_blocks(x3, self.block_chunks[3], self.block_chunks[4])
 return [x4, x3, x2, x1, x0]
def load_state_dict(self, state_dict, **kwargs):
state_dict.pop('_conv_head.weight')
 state_dict.pop('_bn1.weight')
 state_dict.pop('_bn1.bias')
 state_dict.pop('_bn1.running_mean')
 state_dict.pop('_bn1.running_var')
 state_dict.pop('_bn1.num_batches_tracked')
 state_dict.pop('_fc.bias')
 state_dict.pop('_fc.weight')
 if self.in_channels != 3:
state_dict = self.modify_in_channel_weights(state_dict, self.in_channels)
 super().load_state_dict(state_dict, **kwargs)
 def modify_in_channel_weights(self, state_dict, in_channels):
pretrained = state_dict['_conv_stem.weight']
cycled_weights = cycle_rgb_weights(pretrained, in_channels)
 state_dict['_conv_stem.weight'] = cycled_weights
return state_dict
def get_block_args(self):
blocks_args = [
'r1_k3_s11_e1_i32_o16_se0.25',
'r2_k3_s22_e6_i16_o24_se0.25',
'r2_k5_s22_e6_i24_o40_se0.25',
'r3_k3_s22_e6_i40_o80_se0.25',
'r3_k5_s11_e6_i80_o112_se0.25',
'r4_k5_s22_e6_i112_o192_se0.25',
'r1_k3_s11_e6_i192_o320_se0.25',
]
blocks_args = BlockDecoder.decode(blocks_args)
 return blocks_args
def info(self):
msg = '== EfficientNetEncoder ==
'
msg += 'x0: conv_stem
'
chunks = zip(self.block_chunks[:-1], self.block_chunks[1:])
 for i,(start, end)in enumerate(chunks):
msg += f'x{i+1}: blocks[{start}:{end}]
'
msg += '
'
msg += f'Out shapes(x4, x3, x2, x1, x0): {self._out_shapes}
'
msg += str(self._global_params)
 print(msg)
 url_map = {
'efficientnet-b0': '.. /input/efficientnet-pytorch/efficientnet-b0-355c32eb.pth',
'efficientnet-b1': '.. /input/efficientnet-pytorch/efficientnet-b1-f1951068.pth',
'efficientnet-b2': '.. /input/efficientnet-pytorch/efficientnet-b2-8bb594d6.pth',
'efficientnet-b3': '.. /input/efficientnet-pytorch/efficientnet-b3-5fb5a3c3.pth',
'efficientnet-b4': '.. /input/efficientnet-pytorch/efficientnet-b4-6ed6700e.pth',
'efficientnet-b5': '.. /input/efficientnet-pytorch/efficientnet-b5-b6417697.pth',
'efficientnet-b6': '.. /input/efficientnet-pytorch/efficientnet-b6-c76e70fd.pth',
'efficientnet-b7': '.. /input/efficientnet-pytorch/efficientnet-b7-dcc49843.pth',
}
efficientnet_encoders = {
'efficientnet-b0': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b0'],
'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
}
},
'out_shapes':(320, 112, 40, 24, 32),
'params': {
'width_coeff': 1.0,
'depth_coeff': 1.0,
'image_size': 224,
'dropout_rate': 0.2,
'drop_connect_rate': 0.2,
'block_chunks': [0, 2, 5, 11, 16]
},
},
'efficientnet-b1': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b1'],
'input_space': 'RGB',
'input_size': [3, 240, 240],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(320, 112, 40, 24, 32),
'params': {
'width_coeff': 1.0,
'depth_coeff': 1.1,
'image_size': 240,
'dropout_rate': 0.2,
'drop_connect_rate': 0.2,
'block_chunks': [0, 3, 8, 16, 23]
},
},
'efficientnet-b2': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b2'],
'input_space': 'RGB',
'input_size': [3, 260, 260],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(352, 120, 48, 24, 32),
'params': {
'width_coeff': 1.1,
'depth_coeff': 1.2,
'image_size': 260,
'dropout_rate': 0.3,
'drop_connect_rate': 0.2,
'block_chunks': [0, 3, 8, 16, 23]
},
},
'efficientnet-b3': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b3'],
'input_space': 'RGB',
'input_size': [3, 300, 300],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(384, 136, 48, 32, 40),
'params': {
'width_coeff': 1.2,
'depth_coeff': 1.4,
'image_size': 300,
'dropout_rate': 0.3,
'drop_connect_rate': 0.2,
'block_chunks': [0, 3, 8, 18, 26]
},
},
'efficientnet-b4': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b4'],
'input_space': 'RGB',
'input_size': [3, 380, 380],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(448, 160, 56, 32, 48),
'params': {
'width_coeff': 1.4,
'depth_coeff': 1.8,
'image_size': 380,
'dropout_rate': 0.4,
'drop_connect_rate': 0.2,
'block_chunks': [0, 5, 10, 22, 32]
},
},
'efficientnet-b5': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b5'],
'input_space': 'RGB',
'input_size': [3, 456, 456],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(512, 176, 64, 40, 48),
'params': {
'width_coeff': 1.6,
'depth_coeff': 2.2,
'image_size': 456,
'dropout_rate': 0.4,
'drop_connect_rate': 0.2,
'block_chunks': [0, 6, 13, 27, 39]
},
},
'efficientnet-b6': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b6'],
'input_space': 'RGB',
'input_size': [3, 528, 528],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(576, 200, 72, 40, 56),
'params': {
'width_coeff': 1.8,
'depth_coeff': 2.6,
'image_size': 528,
'dropout_rate': 0.5,
'drop_connect_rate': 0.2,
'block_chunks': [0, 7, 15, 31, 45]
},
},
'efficientnet-b7': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b7'],
'input_space': 'RGB',
'input_size': [3, 600, 600],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(640, 224, 80, 48, 64),
'params': {
'width_coeff': 2.0,
'depth_coeff': 3.1,
'image_size': 600,
'dropout_rate': 0.5,
'drop_connect_rate': 0.2,
'block_chunks': [0, 9, 18, 38, 55]
},
},
}
class MBConvBlock(nn.Module):

 def __init__(self, block_args, global_params):
super().__init__()
self._block_args = block_args
self._bn_mom = 1 - global_params.batch_norm_momentum
self._bn_eps = global_params.batch_norm_epsilon
self.has_se =(self._block_args.se_ratio is not None)and(0 < self._block_args.se_ratio <= 1)
 self.id_skip = block_args.id_skip
Conv2d = get_same_padding_conv2d(image_size=global_params.image_size)
 inp = self._block_args.input_filters
oup = self._block_args.input_filters * self._block_args.expand_ratio
if self._block_args.expand_ratio != 1:
self._expand_conv = Conv2d(in_channels=inp, out_channels=oup, kernel_size=1, bias=False)
 self._bn0 = nn.BatchNorm2d(num_features=oup, momentum=self._bn_mom, eps=self._bn_eps)
 k = self._block_args.kernel_size
s = self._block_args.stride
self._depthwise_conv = Conv2d(
 in_channels=oup, out_channels=oup, groups=oup,
kernel_size=k, stride=s, bias=False)
 self._bn1 = nn.BatchNorm2d(num_features=oup, momentum=self._bn_mom, eps=self._bn_eps)
 if self.has_se:
num_squeezed_channels = max(1, int(self._block_args.input_filters * self._block_args.se_ratio))
self._se_reduce = Conv2d(in_channels=oup, out_channels=num_squeezed_channels, kernel_size=1)
 self._se_expand = Conv2d(in_channels=num_squeezed_channels, out_channels=oup, kernel_size=1)
 final_oup = self._block_args.output_filters
self._project_conv = Conv2d(in_channels=oup, out_channels=final_oup, kernel_size=1, bias=False)
 self._bn2 = nn.BatchNorm2d(num_features=final_oup, momentum=self._bn_mom, eps=self._bn_eps)
 def forward(self, inputs, drop_connect_rate=None):

 x = inputs
if self._block_args.expand_ratio != 1:
x = relu_fn(self._bn0(self._expand_conv(inputs)))
 x = relu_fn(self._bn1(self._depthwise_conv(x)))
 if self.has_se:
x_squeezed = F.adaptive_avg_pool2d(x, 1)
 x_squeezed = self._se_expand(relu_fn(self._se_reduce(x_squeezed)))
 x = torch.sigmoid(x_squeezed)* x
x = self._bn2(self._project_conv(x))
input_filters, output_filters = self._block_args.input_filters, self._block_args.output_filters
if self.id_skip and self._block_args.stride == 1 and input_filters == output_filters:
if drop_connect_rate:
x = drop_connect(x, p=drop_connect_rate, training=self.training)
 x = x + inputs
return x
GlobalParams = collections.namedtuple('GlobalParams', [
'batch_norm_momentum', 'batch_norm_epsilon', 'dropout_rate',
'num_classes', 'width_coefficient', 'depth_coefficient',
'depth_divisor', 'min_depth', 'drop_connect_rate', 'image_size'])
 BlockArgs = collections.namedtuple('BlockArgs', [
'kernel_size', 'num_repeat', 'input_filters', 'output_filters',
'expand_ratio', 'id_skip', 'stride', 'se_ratio'])
 GlobalParams.__new__.__defaults__ =(None,)* len(GlobalParams._fields)
 BlockArgs.__new__.__defaults__ =(None,)* len(BlockArgs._fields)
 def relu_fn(x):

 return x * torch.sigmoid(x)
 def round_filters(filters, global_params):

 multiplier = global_params.width_coefficient
if not multiplier:
return filters
divisor = global_params.depth_divisor
min_depth = global_params.min_depth
filters *= multiplier
min_depth = min_depth or divisor
new_filters = max(min_depth, int(filters + divisor / 2)// divisor * divisor)
 if new_filters < 0.9 * filters:
new_filters += divisor
return int(new_filters)
 def round_repeats(repeats, global_params):

 multiplier = global_params.depth_coefficient
if not multiplier:
return repeats
return int(math.ceil(multiplier * repeats))
def drop_connect(inputs, p, training):

 if not training: return inputs
batch_size = inputs.shape[0]
keep_prob = 1 - p
random_tensor = keep_prob
random_tensor += torch.rand([batch_size, 1, 1, 1], dtype=inputs.dtype, device=inputs.device)
 binary_tensor = torch.floor(random_tensor)
 output = inputs / keep_prob * binary_tensor
return output
def get_same_padding_conv2d(image_size=None):

 if image_size is None:
return Conv2dDynamicSamePadding
else:
return partial(Conv2dStaticSamePadding, image_size=image_size)
 class Conv2dDynamicSamePadding(nn.Conv2d):

 def __init__(self, in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1, bias=True):
super().__init__(in_channels, out_channels, kernel_size, stride, 0, dilation, groups, bias)
 self.stride = self.stride if len(self.stride)== 2 else [self.stride[0]]*2
def forward(self, x):
ih, iw = x.size() [-2:]
kh, kw = self.weight.size() [-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
 pad_h = max(( oh - 1)* self.stride[0] +(kh - 1)* self.dilation[0] + 1 - ih, 0)
 pad_w = max(( ow - 1)* self.stride[1] +(kw - 1)* self.dilation[1] + 1 - iw, 0)
 if pad_h > 0 or pad_w > 0:
x = F.pad(x, [pad_w//2, pad_w - pad_w//2, pad_h//2, pad_h - pad_h//2])
 return F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
 class Conv2dStaticSamePadding(nn.Conv2d):

 def __init__(self, in_channels, out_channels, kernel_size, image_size=None, **kwargs):
super().__init__(in_channels, out_channels, kernel_size, **kwargs)
 self.stride = self.stride if len(self.stride)== 2 else [self.stride[0]] * 2
assert image_size is not None
ih, iw = image_size if type(image_size)== list else [image_size, image_size]
kh, kw = self.weight.size() [-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
 pad_h = max(( oh - 1)* self.stride[0] +(kh - 1)* self.dilation[0] + 1 - ih, 0)
 pad_w = max(( ow - 1)* self.stride[1] +(kw - 1)* self.dilation[1] + 1 - iw, 0)
 if pad_h > 0 or pad_w > 0:
self.static_padding = nn.ZeroPad2d(( pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2))
else:
self.static_padding = Identity()
def forward(self, x):
x = self.static_padding(x)
 x = F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
 return x
class Identity(nn.Module):
def __init__(self,):
super(Identity, self ).__init__()
def forward(self, input):
return input
class BlockDecoder(object):

 @staticmethod
def _decode_block_string(block_string):

 assert isinstance(block_string, str)
 ops = block_string.split('_')
 options = {}
for op in ops:
splits = re.split(r'(\d.*)', op)
 if len(splits)>= 2:
key, value = splits[:2]
options[key] = value
assert(( 's' in options and len(options['s'])== 1)or 
(len(options['s'])== 2 and options['s'][0] == options['s'][1]))
return BlockArgs(
 kernel_size=int(options['k']),
num_repeat=int(options['r']),
input_filters=int(options['i']),
output_filters=int(options['o']),
expand_ratio=int(options['e']),
id_skip=('noskip' not in block_string),
se_ratio=float(options['se'])if 'se' in options else None,
stride=[int(options['s'][0])])
 @staticmethod
def _encode_block_string(block):

 args = [
'r%d' % block.num_repeat,
'k%d' % block.kernel_size,
's%d%d' %(block.strides[0], block.strides[1]),
'e%s' % block.expand_ratio,
'i%d' % block.input_filters,
'o%d' % block.output_filters
]
if 0 < block.se_ratio <= 1:
args.append('se%s' % block.se_ratio)
 if block.id_skip is False:
args.append('noskip')
 return '_'.join(args)
 @staticmethod
def decode(string_list):

 assert isinstance(string_list, list)
 blocks_args = []
for block_string in string_list:
blocks_args.append(BlockDecoder._decode_block_string(block_string))
return blocks_args
@staticmethod
def encode(blocks_args):

 block_strings = []
for block in blocks_args:
block_strings.append(BlockDecoder._encode_block_string(block))
return block_strings
def get_global_params(width_coeff, depth_coeff, image_size, dropout_rate, drop_connect_rate):

 global_params = GlobalParams(
 batch_norm_momentum=0.99,
batch_norm_epsilon=1e-3,
dropout_rate=dropout_rate,
drop_connect_rate=drop_connect_rate,
num_classes=1000,
width_coefficient=width_coeff,
depth_coefficient=depth_coeff,
depth_divisor=8,
min_depth=None,
image_size=image_size, 
)
 return global_params
class swish(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
result = i * torch.sigmoid(i)
 ctx.save_for_backward(i)
 return result
@staticmethod
def backward(ctx, grad_output):
i = ctx.saved_variables[0]
sigmoid_i = torch.sigmoid(i)
 return grad_output *(sigmoid_i *(1 + i *(1 - sigmoid_i)))
 class Swish(torch.nn.Module):

 def forward(self, x):
return swish.apply(x)
 class Conv2dReLU(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, padding=0,
stride=1, use_batchnorm=True, **batchnorm_params):
super().__init__()
layers = [
nn.Conv2d(in_channels, out_channels, kernel_size,
stride=stride, padding=padding, bias=not(use_batchnorm)) ,
nn.ReLU(inplace=True),
]
if use_batchnorm:
layers.insert(1, nn.BatchNorm2d(out_channels, **batchnorm_params))
self.block = nn.Sequential(*layers)
 def forward(self, x):
return self.block(x)
 class SCSEModule(nn.Module):
def __init__(self, ch, re=16):
super().__init__()
self.cSE = nn.Sequential(nn.AdaptiveAvgPool2d(1),
nn.Conv2d(ch, ch//re, 1),
nn.ReLU(inplace=True),
nn.Conv2d(ch//re, ch, 1),
nn.Sigmoid() 
)
 self.sSE = nn.Sequential(nn.Conv2d(ch, ch, 1),
nn.Sigmoid())
 def forward(self, x):
return x * self.cSE(x)+ x * self.sSE(x)
 class SENetEncoder(SENet):
def __init__(self, in_channels=3, *args, **kwargs):
super().__init__(*args, **kwargs)
 self.pretrained = False
self.in_channels = in_channels
del self.last_linear
del self.avg_pool
self.layer0[0] = nn.Conv2d(in_channels, 64,(7, 7),(2, 2),(3, 3), bias=False)
 def forward(self, x):
for module in self.layer0[:-1]:
x = module(x)
 x0 = x
x = self.layer0[-1](x)
 x1 = self.layer1(x)
 x2 = self.layer2(x1)
 x3 = self.layer3(x2)
 x4 = self.layer4(x3)
 features = [x4, x3, x2, x1, x0]
return features
def load_state_dict(self, state_dict, **kwargs):
state_dict.pop('last_linear.bias')
 state_dict.pop('last_linear.weight')
 if self.in_channels != 3:
state_dict = self.modify_in_channel_weights(state_dict, self.in_channels)
 super().load_state_dict(state_dict, **kwargs)
 def modify_in_channel_weights(self, state_dict, in_channels):
self.layer0[0] = nn.Conv2d(in_channels, 64,(7, 7),(2, 2),(3, 3), bias=False)
 pretrained = state_dict['layer0.conv1.weight']
cycled_weights = cycle_rgb_weights(pretrained, in_channels)
 state_dict['layer0.conv1.weight'] = cycled_weights
return state_dict
senet_encoders = {
'senet154': {
'encoder': SENetEncoder,
'pretrained_settings': pretrained_settings['senet154'],
'out_shapes':(2048, 1024, 512, 256, 128),
'params': {
'block': SEBottleneck,
'dropout_p': 0.2,
'groups': 64,
'layers': [3, 8, 36, 3],
'num_classes': 1000,
'reduction': 16
},
},
'se_resnet50': {
'encoder': SENetEncoder,
'pretrained_settings': pretrained_settings['se_resnet50'],
'out_shapes':(2048, 1024, 512, 256, 64),
'params': {
'block': SEResNetBottleneck,
'layers': [3, 4, 6, 3],
'downsample_kernel_size': 1,
'downsample_padding': 0,
'dropout_p': None,
'groups': 1,
'inplanes': 64,
'input_3x3': False,
'num_classes': 1000,
'reduction': 16
},
},
'se_resnet101': {
'encoder': SENetEncoder,
'pretrained_settings': pretrained_settings['se_resnet101'],
'out_shapes':(2048, 1024, 512, 256, 64),
'params': {
'block': SEResNetBottleneck,
'layers': [3, 4, 23, 3],
'downsample_kernel_size': 1,
'downsample_padding': 0,
'dropout_p': None,
'groups': 1,
'inplanes': 64,
'input_3x3': False,
'num_classes': 1000,
'reduction': 16
},
},
'se_resnet152': {
'encoder': SENetEncoder,
'pretrained_settings': pretrained_settings['se_resnet152'],
'out_shapes':(2048, 1024, 512, 256, 64),
'params': {
'block': SEResNetBottleneck,
'layers': [3, 8, 36, 3],
'downsample_kernel_size': 1,
'downsample_padding': 0,
'dropout_p': None,
'groups': 1,
'inplanes': 64,
'input_3x3': False,
'num_classes': 1000,
'reduction': 16
},
},
'se_resnext50_32x4d': {
'encoder': SENetEncoder,
'pretrained_settings': pretrained_settings['se_resnext50_32x4d'],
'out_shapes':(2048, 1024, 512, 256, 64),
'params': {
'block': SEResNeXtBottleneck,
'layers': [3, 4, 6, 3],
'downsample_kernel_size': 1,
'downsample_padding': 0,
'dropout_p': None,
'groups': 32,
'inplanes': 64,
'input_3x3': False,
'num_classes': 1000,
'reduction': 16
},
},
'se_resnext101_32x4d': {
'encoder': SENetEncoder,
'pretrained_settings': pretrained_settings['se_resnext101_32x4d'],
'out_shapes':(2048, 1024, 512, 256, 64),
'params': {
'block': SEResNeXtBottleneck,
'layers': [3, 4, 23, 3],
'downsample_kernel_size': 1,
'downsample_padding': 0,
'dropout_p': None,
'groups': 32,
'inplanes': 64,
'input_3x3': False,
'num_classes': 1000,
'reduction': 16
},
},
}
encoders = {}
encoders.update(efficientnet_encoders)
 encoders.update(senet_encoders)
 encoders.update(inceptionv4_encoders)
 encoders.update(inception_encoders)
 def get_encoder(name, encoder_weights=None, in_channels=3):
Encoder = encoders[name]['encoder']
encoder = Encoder(in_channels=in_channels, **encoders[name]['params'])
 encoder.out_shapes = encoders[name]['out_shapes']
if encoder_weights is not None:
settings = encoders[name]['pretrained_settings'][encoder_weights]
r = encoder.load_state_dict(torch.load(settings['url']))
print(f'Load result: {r}')
 return encoder
def get_encoder_names() :
return list(encoders.keys())
 def get_preprocessing_params(encoder_name, pretrained='imagenet'):
settings = encoders[encoder_name]['pretrained_settings']
if pretrained not in settings.keys() :
raise ValueError('Avaliable pretrained options {}'.format(settings.keys()))
formatted_settings = {}
formatted_settings['input_space'] = settings[pretrained].get('input_space')
 formatted_settings['input_range'] = settings[pretrained].get('input_range')
 formatted_settings['mean'] = settings[pretrained].get('mean')
 formatted_settings['std'] = settings[pretrained].get('std')
 return formatted_settings
def get_preprocessing_fn(encoder_name, pretrained='imagenet'):
params = get_preprocessing_params(encoder_name, pretrained=pretrained)
 return functools.partial(preprocess_input, **params)
 class DecoderBlock(nn.Module):
def __init__(self, in_channels, out_channels, use_batchnorm=True, attention_type=None):
super().__init__()
if attention_type is None:
self.attention1 = nn.Identity()
self.attention2 = nn.Identity()
elif attention_type == 'scse':
self.attention1 = SCSEModule(in_channels)
 self.attention2 = SCSEModule(out_channels)
 self.block = nn.Sequential(
 Conv2dReLU(in_channels, out_channels, kernel_size=3, padding=1, use_batchnorm=use_batchnorm),
Conv2dReLU(out_channels, out_channels, kernel_size=3, padding=1, use_batchnorm=use_batchnorm), 
)
 def forward(self, x):
x, skip = x
if skip is not None:
skipsize =(skip.shape[-2], skip.shape[-1])
 x = F.interpolate(x, size=skipsize, mode='nearest')
 x = torch.cat([x, skip], dim=1)
 x = self.attention1(x)
 else:
x = F.interpolate(x, scale_factor=2, mode='nearest')
 x = self.block(x)
 x = self.attention2(x)
 return x
class CenterBlock(DecoderBlock):
def forward(self, x):
return self.block(x)
 class UnetDecoder(Model):
def __init__(
 self,
encoder_channels,
decoder_channels=(256, 128, 64, 32, 16),
final_channels=1,
use_batchnorm=True,
center=False,
attention_type=None 
):
super().__init__()
if center:
channels = encoder_channels[0]
self.center = CenterBlock(channels, channels, use_batchnorm=use_batchnorm)
 else:
self.center = None
in_channels = self.compute_channels(encoder_channels, decoder_channels)
 out_channels = decoder_channels
self.layer1 = DecoderBlock(in_channels[0], out_channels[0],
use_batchnorm=use_batchnorm, attention_type=attention_type)
 self.layer2 = DecoderBlock(in_channels[1], out_channels[1],
use_batchnorm=use_batchnorm, attention_type=attention_type)
 self.layer3 = DecoderBlock(in_channels[2], out_channels[2],
use_batchnorm=use_batchnorm, attention_type=attention_type)
 self.layer4 = DecoderBlock(in_channels[3], out_channels[3],
use_batchnorm=use_batchnorm, attention_type=attention_type)
 self.layer5 = DecoderBlock(in_channels[4], out_channels[4],
use_batchnorm=use_batchnorm, attention_type=attention_type)
 self.final_conv = nn.Conv2d(out_channels[4], final_channels, kernel_size=(1, 1))
self.initialize()
def compute_channels(self, encoder_channels, decoder_channels):
channels = [
encoder_channels[0] + encoder_channels[1],
encoder_channels[2] + decoder_channels[0],
encoder_channels[3] + decoder_channels[1],
encoder_channels[4] + decoder_channels[2],
0 + decoder_channels[3],
]
return channels
def forward(self, x):
encoder_head = x[0]
skips = x[1:]
if self.center:
encoder_head = self.center(encoder_head)
 x = self.layer1([encoder_head, skips[0]])
 x = self.layer2([x, skips[1]])
 x = self.layer3([x, skips[2]])
 x = self.layer4([x, skips[3]])
 x = self.layer5([x, None])
 x = self.final_conv(x)
 return x
class Conv2dWS(nn.Conv2d):
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True):
super(Conv2dWS, self ).__init__(in_channels, out_channels, kernel_size, stride,
padding, dilation, groups, bias)
 def forward(self, x):
weight = self.weight
weight_mean = weight.mean(dim=1, keepdim=True ).mean(dim=2,
keepdim=True ).mean(dim=3, keepdim=True)
 weight = weight - weight_mean
std = weight.view(weight.size(0), -1 ).std(dim=1 ).view(-1, 1, 1, 1)+ 1e-5
weight = weight / std.expand_as(weight)
 return F.conv2d(x, weight, self.bias, self.stride,
self.padding, self.dilation, self.groups)
 class Conv3x3GNReLU(nn.Module):
def __init__(self, in_channels, out_channels, weight_std=False, upsample=False, activation='relu'):
if weight_std:
Conv2d = Conv2dWS
else:
Conv2d = nn.Conv2d
if activation == 'relu':
relu_fn = nn.ReLU(inplace=True)
 elif activation == 'swish':
relu_fn = Swish()
else:
raise ValueError(f'`activation` must be "relu" or "swish"')
 super().__init__()
self.upsample = upsample
self.block = nn.Sequential(
 Conv2d(in_channels, out_channels,(3, 3),
stride=1, padding=1, bias=False),
nn.GroupNorm(32, out_channels),
relu_fn, 
)
 def forward(self, x):
x = self.block(x)
 if self.upsample:
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
 return x
class FPNBlock(nn.Module):
def __init__(self, pyramid_channels, skip_channels):
super().__init__()
self.skip_conv = nn.Conv2d(skip_channels, pyramid_channels, kernel_size=1)
 def forward(self, x):
x, skip = x
x = F.interpolate(x, scale_factor=2, mode='nearest')
 skip = self.skip_conv(skip)
 x = x + skip
return x
class SegmentationBlock(nn.Module):
def __init__(self, in_channels, out_channels, weight_std, n_upsamples=0, activation='relu'):
super().__init__()
blocks = [
Conv3x3GNReLU(in_channels, out_channels, upsample=bool(n_upsamples), activation=activation)
 ]
if n_upsamples > 1:
for _ in range(1, n_upsamples):
blocks.append(Conv3x3GNReLU(
 out_channels, out_channels, weight_std, upsample=True, activation=activation))
self.block = nn.Sequential(*blocks)
 def forward(self, x):
return self.block(x)
 class FPNDecoder(Model):
def __init__(
 self,
encoder_channels,
pyramid_channels=256,
segmentation_channels=128,
final_upsampling=4,
final_channels=1,
dropout=0.2,
weight_std=False,
merge_policy='add',
activation='relu' 
):
super().__init__()
if merge_policy not in ['add', 'cat']:
raise ValueError("`merge_policy` must be one of: ['add', 'cat'], got {}".format(merge_policy))
self.merge_policy = merge_policy
self.final_upsampling = final_upsampling
self.conv1 = nn.Conv2d(encoder_channels[0], pyramid_channels, kernel_size=(1, 1))
self.p4 = FPNBlock(pyramid_channels, encoder_channels[1])
 self.p3 = FPNBlock(pyramid_channels, encoder_channels[2])
 self.p2 = FPNBlock(pyramid_channels, encoder_channels[3])
 self.s5 = SegmentationBlock(pyramid_channels, segmentation_channels, weight_std, 3, activation)
 self.s4 = SegmentationBlock(pyramid_channels, segmentation_channels, weight_std, 2, activation)
 self.s3 = SegmentationBlock(pyramid_channels, segmentation_channels, weight_std, 1, activation)
 self.s2 = SegmentationBlock(pyramid_channels, segmentation_channels, weight_std, 0, activation)
 self.dropout = nn.Dropout2d(p=dropout, inplace=True)
 if self.merge_policy == 'cat':
segmentation_channels *= 4
self.final_conv = nn.Conv2d(segmentation_channels, final_channels, kernel_size=1, padding=0)
 self.initialize()
def forward(self, x):
c5, c4, c3, c2, _ = x
p5 = self.conv1(c5)
 p4 = self.p4([p5, c4])
 p3 = self.p3([p4, c3])
 p2 = self.p2([p3, c2])
 s5 = self.s5(p5)
 s4 = self.s4(p4)
 s3 = self.s3(p3)
 s2 = self.s2(p2)
 if self.merge_policy == 'add':
x = s5 + s4 + s3 + s2
elif self.merge_policy == 'cat':
x = torch.cat([s5, s4, s3, s2], dim=1)
 x = self.dropout(x)
 x = self.final_conv(x)
 if self.final_upsampling is not None and self.final_upsampling > 1:
x = F.interpolate(x, scale_factor=self.final_upsampling, mode='bilinear', align_corners=True)
 return x
class Unet(EncoderDecoder):

 def __init__(
 self,
encoder_name='resnet34',
encoder_weights='imagenet',
decoder_use_batchnorm=True,
decoder_channels=(256, 128, 64, 32, 16),
classes=1,
activation='sigmoid',
center=False,
attention_type=None,
in_channels=3 
):
encoder = get_encoder(
 encoder_name,
encoder_weights=encoder_weights,
in_channels=in_channels 
)
 decoder = UnetDecoder(
 encoder_channels=encoder.out_shapes,
decoder_channels=decoder_channels,
final_channels=classes,
use_batchnorm=decoder_use_batchnorm,
center=center,
attention_type=attention_type 
)
 super().__init__(encoder, decoder, activation)
 self.name = 'u-{}'.format(encoder_name)
 class FPN(EncoderDecoder):

 def __init__(
 self,
encoder_name='resnet34',
encoder_weights='imagenet',
decoder_pyramid_channels=256,
decoder_segmentation_channels=128,
decoder_merge_policy='add',
decoder_activation='relu',
classes=1,
dropout=0.2,
activation='sigmoid',
in_channels=3,
weight_std=False,
final_upsampling=4, 
):
encoder = get_encoder(
 encoder_name,
encoder_weights=encoder_weights,
in_channels=in_channels 
)
 decoder = FPNDecoder(
 encoder_channels=encoder.out_shapes,
pyramid_channels=decoder_pyramid_channels,
segmentation_channels=decoder_segmentation_channels,
final_channels=classes,
dropout=dropout,
weight_std=weight_std,
final_upsampling=4,
merge_policy=decoder_merge_policy,
activation=decoder_activation 
)
 super().__init__(encoder, decoder, activation)
 self.name = 'fpn-{}'.format(encoder_name )<choose_model_class>
%%time
def get_cat_shop_info(df):

 itemsdf = pd.read_csv(
 ".. /input/competitive-data-science-predict-future-sales/items.csv")
 shopsdf = pd.read_csv(".. /input/filestranslated/shops-translated.csv")
 itemcatsdf = pd.read_csv(".. /input/filestranslated/categories_translated.csv")
 item_cat = dict(zip(itemsdf["item_id"], itemsdf["item_category_id"]))
item_cat_name = dict(
 zip(itemcatsdf["item_category_id"], itemcatsdf["item_category_name"]))
shop_name = dict(zip(shopsdf["shop_id"], shopsdf["shop_name_translated"]))
df["item_cat_id"] = df["item_id"].map(item_cat)
 df["item_cat_name"] = df["item_cat_id"].map(item_cat_name)
 df["shop_name"] = df["shop_id"].map(shop_name)
 return df
def extract_month_year(df):

 df["month"] = df["date"].apply(lambda x: x.split(".")[1] ).astype(int)
 df["year"] = df["date"].apply(lambda x: x.split(".")[2] ).astype(int)
 df.drop(columns="date", inplace=True)
 return df
df = get_cat_shop_info(df)
 df = extract_month_year(df )
class BaseTrainer:

 def __init__(self, model, loss, metrics, optimizer, resume, config, device):
self.logger = setup_logger(self, verbose=config['training']['verbose'])
 self.model = model
self.device = device
self.loss = loss
self.metrics = metrics
self.optimizer = optimizer
self.config = config
cfg_trainer = config['training']
self.epochs = cfg_trainer['epochs']
self.save_period = cfg_trainer['save_period']
self.monitor = cfg_trainer.get('monitor', 'off')
 if self.monitor == 'off':
self.mnt_mode = 'off'
self.mnt_best = 0
else:
self.mnt_mode, self.mnt_metric = self.monitor.split()
assert self.mnt_mode in ['min', 'max']
self.mnt_best = math.inf if self.mnt_mode == 'min' else -math.inf
self.early_stop = cfg_trainer.get('early_stop', math.inf)
 self.start_epoch = 1
self.checkpoint_dir, writer_dir = trainer_paths(config)
 self.writer = TensorboardWriter(writer_dir, self.logger, cfg_trainer['tensorboard'])
 config_save_path = os.path.join(self.checkpoint_dir, 'config.yaml')
 with open(config_save_path, 'w')as handle:
yaml.dump(config, handle, default_flow_style=False)
 if resume:
self._resume_checkpoint(resume)
 def train(self):

 self.logger.info('Starting training...')
 for epoch in range(self.start_epoch, self.epochs + 1):
result = self._train_epoch(epoch)
 log = {'epoch': epoch}
for key, value in result.items() :
if key == 'metrics':
log.update({
mtr.__name__: value[i] for i, mtr in enumerate(self.metrics)})
 elif key == 'val_metrics':
log.update({
'val_' + mtr.__name__: value[i] for i, mtr in enumerate(self.metrics)})
 else:
log[key] = value
for key, value in log.items() :
self.logger.info(f'{str(key):15s}: {value}')
 best = False
if self.mnt_mode != 'off':
try:
improved =(self.mnt_mode == 'min' and log[self.mnt_metric] < self.mnt_best)or\ 
(self.mnt_mode == 'max' and log[self.mnt_metric] > self.mnt_best)
 except KeyError:
self.logger.warning(f"Warning: Metric '{self.mnt_metric}' is not found.Model "
"performance monitoring is disabled.")
 self.mnt_mode = 'off'
improved = False
not_improved_count = 0
if improved:
self.mnt_best = log[self.mnt_metric]
not_improved_count = 0
best = True
else:
not_improved_count += 1
if not_improved_count > self.early_stop:
self.logger.info("Validation performance didn't improve for "
f"{self.early_stop} epochs.Training stops.")
 break
if epoch % self.save_period == 0:
self._save_checkpoint(epoch, save_best=best)
 def _train_epoch(self, epoch):

 raise NotImplementedError
def _save_checkpoint(self, epoch, save_best=False):

 arch = type(self.model ).__name__
state = {
'arch': arch,
'epoch': epoch,
'state_dict': self.model.state_dict() ,
'optimizer': self.optimizer.state_dict() ,
'monitor_best': self.mnt_best,
'config': self.config,
}
filename = os.path.join(self.checkpoint_dir, f'checkpoint-epoch{epoch}.pth')
 torch.save(state, filename)
 self.logger.info(f"Saving checkpoint: {filename}...")
 if save_best:
best_path = os.path.join(self.checkpoint_dir, 'model_best.pth')
 torch.save(state, best_path)
 self.logger.info(f'Saving current best: {best_path}')
 def _resume_checkpoint(self, resume_path):

 self.logger.info(f'Loading checkpoint: {resume_path}')
 checkpoint = torch.load(resume_path)
 self.start_epoch = checkpoint['epoch'] + 1
self.mnt_best = checkpoint['monitor_best']
if checkpoint['config']['arch'] != self.config['arch']:
self.logger.warning("Warning: Architecture configuration given in config file is "
"different from that of checkpoint.This may yield an "
"exception while state_dict is being loaded.")
 self.model.load_state_dict(checkpoint['state_dict'])
 if checkpoint['config']['optimizer']['type'] != self.config['optimizer']['type']:
self.logger.warning("Warning: Optimizer type given in config file is different from "
"that of checkpoint.Optimizer parameters not being resumed.")
 else:
self.optimizer.load_state_dict(checkpoint['optimizer'])
 self.logger.info(f'Checkpoint "{resume_path}"(epoch {self.start_epoch})loaded')
 class ChannelTranspose(ImageOnlyTransform):
def get_transform_init_args_names(self):
return()
def get_params_dependent_on_targets(self, params):
pass
def __init__(self, axes=(2, 0, 1)) :
super().__init__(always_apply=True)
 self.axes = axes
def apply(self, img, **params):
return np.transpose(img, self.axes)
 class AugmentationBase(abc.ABC):
MEAN = [0.3439]
STD = [0.0383]
H = 256
W = 1600
def __init__(self):
self.transform = self.notimplemented
def build_transforms(self, train):
if train:
self.transform = self.build_train()
else:
self.transform = self.build_test()
@abc.abstractmethod
def build_train(self):
pass
def build_test(self):
return Compose([
Normalize(mean=self.MEAN, std=self.STD),
ToTensor() ,
])
 def notimplemented(self, *args, **kwargs):
raise Exception('You must call `build_transforms() ` before using me!')
 def __call__(self, *args, **kwargs):
return self.transform(*args, **kwargs)
 def copy(self):
return deepcopy(self)
 class LightTransforms(AugmentationBase):
def __init__(self):
super().__init__()
def build_train(self):
return Compose([
HorizontalFlip(p=0.5),
Normalize(mean=self.MEAN, std=self.STD),
ToTensor() ,
])
 class CompatabilityTransforms(AugmentationBase):
def __init__(self):
super().__init__()
def build_train(self):
return Compose([
HorizontalFlip(p=0.5),
Normalize(mean=self.MEAN, std=self.STD),
ToTensor() ,
])
 def build_test(self):
return Compose([
Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)) ,
ChannelTranspose()
])
 class SteelDataset(Dataset):
img_folder = 'implement me!'
N_CLASSES = 4
rle_cols = [f'rle{i}' for i in range(N_CLASSES)]
def __init__(self, df, data_dir, transforms):
self.df = df
self.data_dir = data_dir / self.img_folder
self.transforms = transforms
self.fnames = self.df.index.tolist()
def read_greyscale(self, idx):
f = self.fnames[idx]
return f, cv2.imread(str(self.data_dir / f)) [:, :, 0:1]
def rle(self, idx):
return self.df.iloc[idx][self.rle_cols]
def __len__(self):
return len(self.fnames)
 class SteelDatasetTest(SteelDataset):
img_folder = 'test_images'
def __init__(self, df, data_dir, transforms):
super().__init__(df, data_dir, transforms)
 self.transforms.build_transforms(train=False)
 def __getitem__(self, idx):
f, image = self.read_greyscale(idx)
 images = self.transforms(image=image)["image"]
return f, images
class SteelDataLoader(DataLoader):
train_csv = 'train.csv'
test_csv = 'sample_submission.csv'
def __init__(self, transforms, data_dir, batch_size, shuffle,
validation_split, nworkers, pin_memory=True, train=True, alpha=None, balance=None 
):
self.transforms, self.shuffle = transforms, shuffle
self.bs, self.nworkers, self.pin_memory = batch_size, nworkers, pin_memory
self.data_dir = Path(data_dir)
 self.train_df, self.val_df = self.load_df(train, validation_split)
 if train:
dataset = SteelDatasetTrainVal(self.train_df, self.data_dir, transforms.copy() , True)
 else:
dataset = SteelDatasetTest(self.train_df, self.data_dir, transforms.copy())
 if train and balance is not None and alpha is not None:
class_idxs = self.sort_classes(self.train_df)
 n_batches = self.train_df.shape[0] // batch_size
sampler = SamplerFactory(2 ).get(class_idxs, batch_size, n_batches, alpha, balance)
 super().__init__(dataset, batch_sampler=sampler,
num_workers=nworkers, pin_memory=pin_memory)
 else:
super().__init__(dataset, batch_size, shuffle=shuffle,
num_workers=nworkers, pin_memory=pin_memory)
 def load_df(self, train, validation_split):
csv_filename = self.train_csv if train else self.test_csv
df = pd.read_csv(self.data_dir / csv_filename)
 df['ImageId'], df['ClassId'] = zip(*df['ImageId_ClassId'].str.split('_'))
df['ClassId'] = df['ClassId'].astype(int)
 df = df.pivot(index='ImageId', columns='ClassId', values='EncodedPixels')
 df.columns = [f'rle{c}' for c in range(4)]
df['defects'] = df.count(axis=1)
 for c in range(4):
df[f'c{c}'] = df[f'rle{c}'].apply(lambda rle: not pd.isnull(rle))
if train and validation_split > 0:
return train_test_split(df, test_size=validation_split, stratify=df["defects"])
 return df, pd.DataFrame({})
 def sort_classes(self, df):
counts = {c: df[f'c{c}'].sum() for c in range(4)}
sorted_classes = sorted(counts.items() , key=lambda kv: kv[1])
 def assign_min_sample_class(row, sorted_classes):
for c, _ in sorted_classes:
if row[f'c{c}']:
return c
return -1
df['sample_class'] = df.apply(
 lambda row: assign_min_sample_class(row, sorted_classes), axis=1)
 class_idxs = [list(np.where(df['sample_class'] == c)[0])for c in range(-1, 4)]
return class_idxs
def split_validation(self):
if self.val_df.empty:
return None
else:
dataset = SteelDatasetTrainVal(
 self.val_df, self.data_dir, self.transforms.copy() , False)
 return DataLoader(dataset, self.bs // 2,
num_workers=self.nworkers, pin_memory=self.pin_memory)
 class RLE:

 MASK_H = 256
MASK_W = 1600
@classmethod
def from_str(cls, s):
if s != s:
return cls()
list_ = [int(n)for n in s.split(' ')]
return cls.from_list(list_)
 @classmethod
def from_mask(cls, mask):
pixels = mask.T.flatten()
pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return cls.from_list(runs)
 @classmethod
def from_list(cls, list_):
n_items = int(len(list_)/ 2)
 items = np.zeros(( n_items, 2), dtype=np.uint64)
 for i in range(n_items):
items[i, 0] = list_[i * 2]
items[i, 1] = list_[i * 2 + 1]
return cls(items)
 def __init__(self, items=np.zeros(( 0, 0))):
self._items = items
@property
def items(self):
return self._items
def __iter__(self):
for idx, item in enumerate(self.items):
yield(item[0], item[1])
 def __len__(self):
return self.items.shape[0]
def to_mask(self):
mask = np.zeros(self.MASK_H * self.MASK_W, dtype=np.uint8)
 for run, length in self:
run = int(run - 1)
 end = int(run + length)
 mask[run:end] = 1
return mask.reshape(self.MASK_H, self.MASK_W, order='F')
 def to_str_list(self):
list_ = []
for run, length in self:
list_.append(str(run))
list_.append(str(length))
return list_
def __str__(self):
if len(self)== 0:
return ''
return ' '.join(self.to_str_list())
 def __repr__(self):
return self.__str__()
class PostProcessor:
N_CLASSES = 4
def __init__(self, p_thresh=None, min_class_sizes=None):
self.p_thresh = np.array(p_thresh)
 self.min_class_sizes = np.array(min_class_sizes)
 print(self.p_thresh)
 print(self.min_class_sizes)
 def process(self, probabilities):
mask = probabilities > self.p_thresh[:, np.newaxis, np.newaxis]
for c in range(self.N_CLASSES):
if mask[c, :, :].sum() < self.min_class_sizes[c]:
mask[c, :, :] = 0
return mask
def ensure_dir(path):
if not os.path.exists(path):
os.makedirs(path)
 return path
def ensure_parent(path):
parent = os.path.sep.join(path.split(os.path.sep)[:-1])
 ensure_dir(parent)
 return path
def arch_path(config):
return ensure_dir(join(config['save_dir'], config['name']))
def arch_datetime_path(config):
start_time = datetime.datetime.now().strftime('%m%d_%H%M%S')
 return ensure_dir(join(arch_path(config), start_time))
def log_path(config):
return ensure_dir(join(arch_path(config), 'logs'))
def trainer_paths(config):

 arch_datetime = arch_datetime_path(config)
 return(
 ensure_dir(join(arch_datetime, 'checkpoints')) ,
ensure_dir(join(arch_datetime, 'runs')) 
)
 LOG_CONFIG =
def setup_logging(run_config, log_config=LOG_CONFIG, default_level=logging.INFO):

 config = yaml.safe_load(log_config)
 logging.config.dictConfig(config)
 logging_level_dict = {
0: logging.WARNING,
1: logging.INFO,
2: logging.DEBUG
}
def setup_logger(cls, verbose=0):
logger = logging.getLogger(cls.__class__.__name__)
 if verbose not in logging_level_dict:
raise KeyError(f'verbose option {verbose} for {cls} not valid.'
f'Valid options are {logging_level_dict.keys() }.')
 logger.setLevel(logging_level_dict[verbose])
 return logger
def build_instance(ctor, name, config, *args):
return ctor(*args, **config[name]['args'])
<categorify>
df.loc[df["shop_id"] == 11, "shop_id"] = 10
df.loc[df["shop_id"] == 10, "shop_name"] = "Zhukovsky st.Chkalov 39m?"
df.loc[df["shop_id"] == 0, "shop_id"] = 57
df.loc[df["shop_id"] == 57, "shop_name"] = "Yakutsk Ordzhonikidze, 56"
df.loc[df["shop_id"] == 1, "shop_id"] = 58
df.loc[df["shop_id"] == 58, "shop_name"] = 'Yakutsk TC "Central"'
df.loc[df["shop_id"] == 40, "shop_id"] = 39
df.loc[df["shop_id"] == 39, "shop_name"] = "Rostov-on-Don Megacenter Horizon"
class MultiModel(nn.Module):
flips = [None, 3]
def __init__(self, models):
super().__init__()
self.models = models
for name, model in self.models.items() :
print(f'Loaded: {name}')
 def forward(self, x):
res = []
for name, cfg in self.models.items() :
m = cfg['model']
for flip in self.flips:
if flip is None:
output = m(x)
 else:
output = m(torch.flip(x, dims=[flip]))
output = torch.flip(output, dims=[flip])
 res.append(torch.sigmoid(output))
res = torch.stack(res, dim=0)
 res = self.reduce_mean(res)
 res = self.zero_nonmax(res)
 return res
def reduce_mean(self, t):
return torch.mean(t, dim=0)
 def reduce_rms(self, t):
tsq = torch.mul(t, t)
 return torch.sqrt(tsq.mean(dim=0))
def reduce_rmc(self, t):
tcb = torch.mul(t, torch.mul(t, t))
cube_root = torch.tensor(1/3 ).cuda()
return torch.pow(tcb.mean(dim=0), cube_root)
 def zero_nonmax(self, t):
m = torch.max(t, dim=1, keepdim=True)[0]
t[t < m] = 0
return t 
<choose_model_class>
def map_cat_shop(df):

 category_mapping = {
1: "Console Accessories",2: "Console Accessories",
3: "Console Accessories",4: "Console Accessories",
5: "Console Accessories",6: "Console Accessories",
7: "Console Accessories",25: "Game Accessories",
8: "Tickets",
42: "Books",43: "Books",44: "Books",45: "Books",
47: "Books",49: "Books",54: "Books",46: "Books",
48: "Books",50: "Books",51: "Books",52: "Books",
53: "Books",
11: "Game Consoles",12: "Game Consoles",15: "Game Consoles",
16: "Game Consoles",10: "Game Consoles",13: "Game Consoles",
14: "Game Consoles",17: "Game Consoles",
18: "Games",24: "Games",19: "Games",20: "Games",
21: "Games",22: "Games",23: "Games",28: "Games",
29: "Games",30: "Games",31: "Games",
26: "Mobile Games",27: "Mobile Games",
61: "Gifts",71: "Gifts",64: "Gifts",65: "Gifts",
67: "Gifts",62: "Gifts",72: "Gifts",69: "Gifts",
70: "Gifts",63: "Gifts",66: "Gifts",68: "Gifts",
37: "Movies",40: "Movies",38: "Movies",41: "Movies",
39: "Movies",
60: "Music",57: "Music",58: "Music",56: "Music",
55: "Music",59: "Music",
32: "Payment Cards",33: "Payment Cards",
34: "Payment Cards",35: "Payment Cards",
36: "Payment Cards",
73: "Programs",74: "Programs",
75: "Programs",76: "Programs",
77: "Programs",78: "Programs",
83: "Misc",9: "Misc",0: "Misc",80: "Misc",
79: "Office",
81: "Blank Media",82: "Blank Media",
}
shop_mapping = {
0: "Yakutsk",1: "Yakutsk",
2: "Maykop",
3: "Balashikha",
56: "Chekhov",
55: "Online",12: "Online",
15: "Kaluga",
13: "Kazan",14: "Kazan",
54: "Khimki",
16: "Kolomna",
18: "Krasnoyarsk",17: "Krasnoyarsk",
19: "Kursk",
20: "Moscow",21: "Moscow",
30: "Moscow",22: "Moscow",
26: "Moscow",24: "Moscow",
27: "Moscow",28: "Moscow",
31: "Moscow",23: "Moscow",
25: "Moscow",29: "Moscow",
32: "Moscow",
33: "Mytishchi",
34: "Nizhny Novgorod",35: "Nizhny Novgorod",
36: "Novosibirsk",37: "Novosibirsk",
9: "Other",
38: "Omsk",
40: "Rostov-on-Don",39: "Rostov-on-Don",
41: "Rostov-on-Don",
42: "Saint Petersburg",43: "Saint Petersburg",
44: "Samara",45: "Samara",
46: "Sergiyev Posad",
47: "Surgut",
48: "Tomsk",
49: "Tyumen",50: "Tyumen",51: "Tyumen",
52: "Ufa",53: "Ufa",
5: "Vologda",
4: "Volzhsky",
6: "Voronezh",7: "Voronezh",8: "Voronezh",
57: "Yakutsk",58: "Yakutsk",
59: "Yaroslavl",
10: "Zhukovsky",11: "Zhukovsky",
}
df["item_cat_group"] = df["item_cat_id"].map(category_mapping)
 df["city"] = df["shop_id"].astype(int ).map(shop_mapping)
 return df
df = map_cat_shop(df )
class Runner:
def predict(self, config):
setup_logging(config)
 self.logger = setup_logger(self, config['testing']['verbose'])
 self._seed_everything(config['seed'])
 self.logger.info(f'Using config:
{config}')
 pp = build_instance(PostProcessor, 'postprocess', config)
 tsfm = LightTransforms()
self.logger.debug('Getting data_loader instance')
 data_loader = SteelDataLoader(
 tsfm,
config['testing']['data_dir'],
batch_size=config['testing']['batch_size'],
validation_split=0.0,
train=False,
shuffle=False,
nworkers=config['testing']['num_workers'] 
)
 self.logger.debug('Building model architecture')
 models = {}
for arch, cfg in config['models'].items() :
self.logger.debug(f'Building {arch}')
 if cfg['type'] == 'Unet':
model = build_instance(Unet, arch, config['models'])
 elif cfg['type'] == 'FPN':
model = build_instance(FPN, arch, config['models'])
 model, device = self._prepare_device(model, config['n_gpu'])
 model_checkpoint = config['models'][arch]['checkpoint']
self.logger.debug(f'Loading checkpoint {model_checkpoint}')
 checkpoint = torch.load(model_checkpoint, map_location=device)
 model.load_state_dict(checkpoint['state_dict'])
 model.eval()
models[arch] = {
'model': model,
}
model = MultiModel(models)
 model.eval()
for name, cfg in model.models.items() :
assert not cfg['model'].training, 'model is still in training mode!'
predictions = []
preds = torch.zeros(len(data_loader.dataset))
self.logger.debug('Starting...')
 with torch.no_grad() :
for i,(fs, data)in enumerate(tqdm(data_loader)) :
data = data.to(device)
 output = model(data)
 batch_preds = output.detach().cpu().numpy()
for(f, preds)in zip(fs, batch_preds):
masks = pp.process(preds)
 for class_, mask in enumerate(masks):
rle = str(RLE.from_mask(mask))
name = f + f"_{class_+1}"
predictions.append([name, rle])
 df = pd.DataFrame(predictions, columns=['ImageId_ClassId', 'EncodedPixels'])
 return df
def _prepare_device(self, model, n_gpu_use):
device, device_ids = self._get_device(n_gpu_use)
 model = model.to(device)
 return model, device
def _get_device(self, n_gpu_use):

 n_gpu = torch.cuda.device_count()
if n_gpu_use > 0 and n_gpu == 0:
self.logger.warning("Warning: There's no GPU available on this machine,"
"training will be performed on CPU.")
 n_gpu_use = 0
if n_gpu_use > n_gpu:
self.logger.warning(f"Warning: The number of GPU's configured to use is {n_gpu_use}, "
f"but only {n_gpu} are available on this machine.")
 n_gpu_use = n_gpu
device = torch.device('cuda:0' if n_gpu_use > 0 else 'cpu')
 list_ids = list(range(n_gpu_use))
self.logger.info(f'Using device: {device}, {list_ids}')
 return device, list_ids
def _seed_everything(self, seed):
self.logger.info(f'Using random seed: {seed}')
 random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed )<categorify>
def bin_prices(df):
kbd = KBinsDiscretizer(n_bins=10, encode="ordinal", strategy="quantile")
 df.loc[df["train"] == 1, "binned_item_price"] = kbd.fit_transform(
 df.loc[df["train"] == 1, "item_price"].values.reshape(-1, 1)
)
 df.loc[df["train"] == 0, "binned_item_price"] = kbd.transform(
 df.loc[df["train"] == 0, "item_price"].values.reshape(-1, 1)
)
 item_lvl_bin_edges = dict(zip(range(kbd.n_bins), kbd.bin_edges_[0].astype(int)))
 return df, item_lvl_bin_edges
df, bin_edges = bin_prices(df )
config_str =
config = yaml.safe_load(config_str)
 try:
df = Runner().predict(config)
 except Exception as ex:
print(f'Caught exception: {ex}')
 print(config)
 print(df.head(20))
df.to_csv("submission.csv", index=False)
 print(f'Finished saving predictions to "submission.csv"')
 df_orig = df.copy()<feature_engineering>
def determine_first_sale(df):
item_firstsale =(
 df[df["item_cnt_month"] > 0].groupby(by=["item_id", "shop_id"])["date_block_num"].min())
 df["item_firstsale_month"] = df.set_index(["item_id", "shop_id"] ).index.map(item_firstsale)
 df["item_first_sale"] = 0
df.loc[df["item_firstsale_month"] == df["date_block_num"], "item_first_sale"] = 1
df.drop(columns=["item_firstsale_month"], inplace=True)
 return df
df = determine_first_sale(df )
<import_modules>
def set_similar_items(df):

 itemstranslated = pd.read_csv(".. /input/filestranslated/items-translated.csv")
 itemdf = pd.DataFrame(
 columns=["item_id", "item_description"], data=itemstranslated.values 
)
 corpus = list(itemdf["item_description"].values)
 vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
 cosine_similarities = pd.DataFrame(
 cosine_similarity(X), index=itemdf["item_id"], columns=itemdf["item_id"] 
)
 similar_items = []
for i in cosine_similarities.columns:
similar = np.append(cosine_similarities[i].nlargest(4)[1:].index.values, i)
 similar_items.append(similar)
 similar_items = pd.DataFrame(
 np.vstack(similar_items),
columns=["similar_item1", "similar_item2", "similar_item3", "item_id"], 
)
 sim1 = dict(zip(similar_items["item_id"], similar_items["similar_item1"]))
sim2 = dict(zip(similar_items["item_id"], similar_items["similar_item2"]))
sim3 = dict(zip(similar_items["item_id"], similar_items["similar_item3"]))
df["similar_item1"] = df["item_id"].map(sim1)
 df["similar_item2"] = df["item_id"].map(sim2)
 df["similar_item3"] = df["item_id"].map(sim3)
 return df
df = set_similar_items(df)
import abc
import yaml
import torchvision.transforms as T
import torchvision.models as models
import torch.nn.functional as F
import torch.nn as nn
import torch
import random
import pandas as pd
import os
import numpy as np
import re
import math
import collections
from functools import partial
from copy import deepcopy
import logging.config
import datetime
import cv2
import warnings
from albumentations import HorizontalFlip, Normalize, Compose, ImageOnlyTransform
from albumentations.pytorch import ToTensor
from collections import Sequence
from sklearn.model_selection import train_test_split
from tqdm import tqdm
from torch.jit import load
from torchvision.utils import make_grid
from torch.utils.data import DataLoader, Dataset
from torch.utils import model_zoo
from pathlib import Path
from os.path import join
from collections import OrderedDict<categorify>
df = downcast_df(df )
def cycle_rgb_weights(weights, n):

 slices = [(c % 3, c % 3 + 1)for c in range(n)]
new_weights = torch.cat([
weights[:, a:b, :, :] for a, b in slices
], dim=1)
 return new_weights
def select_rgb_weights(weights, rgb_str):

 rgb_str = rgb_str.lower()
rgb_map = {'r': 0, 'g': 1, 'b': 2}
slices = [(rgb_map[c] % 3, rgb_map[c] % 3 + 1)for c in rgb_str]
new_weights = torch.cat([
weights[:, a:b, :, :] for a, b in slices
], dim=1)
 return new_weights
def avg_rgb_weights(weights):
new_weights = weights.mean(dim=1, keepdim=True)
 return new_weights
def transfer_weights(pretrained, replacement, method='cycle'):

 if method == 'cycle':
n = replacement.in_channels
weights = cycle_rgb_weights(pretrained.weight, n)
 elif method == 'avg':
weights = avg_rgb_weights(pretrained.weight)
 else:
raise NotImplementedError('`method` must be "cycle" or "avg", received {}'.format(method))
replacement.weight = nn.Parameter(weights)
 return replacement
class Model(nn.Module):
def __init__(self):
super().__init__()
def initialize(self):
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.init.constant_(m.weight, 1)
 nn.init.constant_(m.bias, 0)
 class EncoderDecoder(Model):
def __init__(self, encoder, decoder, activation):
super().__init__()
self.encoder = encoder
self.decoder = decoder
if callable(activation)or activation is None:
self.activation = activation
elif activation == 'softmax':
self.activation = nn.Softmax(dim=1)
 elif activation == 'sigmoid':
self.activation = nn.Sigmoid()
else:
raise ValueError('Activation should be "sigmoid"/"softmax"/callable/None')
 def forward(self, x):

 x = self.encoder(x)
 x = self.decoder(x)
 return x
def predict(self, x):

 if self.training:
self.eval()
with torch.no_grad() :
x = self.forward(x)
 if self.activation:
x = self.activation(x)
 return x
class EfficientNetEncoder(nn.Module):

 def __init__(self,
width_coeff,
depth_coeff,
image_size,
dropout_rate,
drop_connect_rate,
block_chunks,
in_channels = 3 
):
super().__init__()
self._blocks_args = self.get_block_args()
self._global_params = get_global_params(width_coeff, depth_coeff, image_size,
dropout_rate, drop_connect_rate)
 self.block_chunks = block_chunks
self.in_channels = in_channels
Conv2d = get_same_padding_conv2d(image_size=self._global_params.image_size)
 bn_mom = 1 - self._global_params.batch_norm_momentum
bn_eps = self._global_params.batch_norm_epsilon
out_channels = round_filters(32, self._global_params)
 self._conv_stem = Conv2d(self.in_channels, out_channels, kernel_size=3, stride=2, bias=False)
 self._bn0 = nn.BatchNorm2d(num_features=out_channels, momentum=bn_mom, eps=bn_eps)
 self._blocks = nn.ModuleList([])
 for block_args in self._blocks_args:
block_args = block_args._replace(
 input_filters=round_filters(block_args.input_filters, self._global_params),
output_filters=round_filters(block_args.output_filters, self._global_params),
num_repeat=round_repeats(block_args.num_repeat, self._global_params)
)
 self._blocks.append(MBConvBlock(block_args, self._global_params))
if block_args.num_repeat > 1:
block_args = block_args._replace(input_filters=block_args.output_filters, stride=1)
 for _ in range(block_args.num_repeat - 1):
self._blocks.append(MBConvBlock(block_args, self._global_params))
n_blocks = len(self._blocks)
 assert self.block_chunks[-1] == n_blocks, f'{self.block_chunks[-1]}, {n_blocks}'
self._out_shapes = [self._bn0.num_features]
self._out_shapes += [self._blocks[i - 1]._bn2.num_features for i in self.block_chunks[1:]]
self._out_shapes = list(reversed(self._out_shapes))
self.drop_connect_rates = np.arange(0, n_blocks, dtype=np.float)/ n_blocks
self.drop_connect_rates *= self._global_params.drop_connect_rate
def forward_blocks(self, x, start_idx, end_idx):
for idx in range(start_idx, end_idx):
x = self._blocks[idx](x, self.drop_connect_rates[idx])
 return x
def forward(self, x):
x0 = relu_fn(self._bn0(self._conv_stem(x)))
 x1 = self.forward_blocks(x0, self.block_chunks[0], self.block_chunks[1])
 x2 = self.forward_blocks(x1, self.block_chunks[1], self.block_chunks[2])
 x3 = self.forward_blocks(x2, self.block_chunks[2], self.block_chunks[3])
 x4 = self.forward_blocks(x3, self.block_chunks[3], self.block_chunks[4])
 return [x4, x3, x2, x1, x0]
def load_state_dict(self, state_dict, **kwargs):
state_dict.pop('_conv_head.weight')
 state_dict.pop('_bn1.weight')
 state_dict.pop('_bn1.bias')
 state_dict.pop('_bn1.running_mean')
 state_dict.pop('_bn1.running_var')
 state_dict.pop('_bn1.num_batches_tracked')
 state_dict.pop('_fc.bias')
 state_dict.pop('_fc.weight')
 if self.in_channels != 3:
state_dict = self.modify_in_channel_weights(state_dict, self.in_channels)
 super().load_state_dict(state_dict, **kwargs)
 def modify_in_channel_weights(self, state_dict, in_channels):
pretrained = state_dict['_conv_stem.weight']
cycled_weights = cycle_rgb_weights(pretrained, in_channels)
 state_dict['_conv_stem.weight'] = cycled_weights
return state_dict
def get_block_args(self):
blocks_args = [
'r1_k3_s11_e1_i32_o16_se0.25',
'r2_k3_s22_e6_i16_o24_se0.25',
'r2_k5_s22_e6_i24_o40_se0.25',
'r3_k3_s22_e6_i40_o80_se0.25',
'r3_k5_s11_e6_i80_o112_se0.25',
'r4_k5_s22_e6_i112_o192_se0.25',
'r1_k3_s11_e6_i192_o320_se0.25',
]
blocks_args = BlockDecoder.decode(blocks_args)
 return blocks_args
def info(self):
msg = '== EfficientNetEncoder ==
'
msg += 'x0: conv_stem
'
chunks = zip(self.block_chunks[:-1], self.block_chunks[1:])
 for i,(start, end)in enumerate(chunks):
msg += f'x{i+1}: blocks[{start}:{end}]
'
msg += '
'
msg += f'Out shapes(x4, x3, x2, x1, x0): {self._out_shapes}
'
msg += str(self._global_params)
 print(msg)
 url_map = {
'efficientnet-b0': '.. /input/efficientnet-pytorch/efficientnet-b0-355c32eb.pth',
'efficientnet-b1': '.. /input/efficientnet-pytorch/efficientnet-b1-f1951068.pth',
'efficientnet-b2': '.. /input/efficientnet-pytorch/efficientnet-b2-8bb594d6.pth',
'efficientnet-b3': '.. /input/efficientnet-pytorch/efficientnet-b3-5fb5a3c3.pth',
'efficientnet-b4': '.. /input/efficientnet-pytorch/efficientnet-b4-6ed6700e.pth',
'efficientnet-b5': '.. /input/efficientnet-pytorch/efficientnet-b5-b6417697.pth',
'efficientnet-b6': '.. /input/efficientnet-pytorch/efficientnet-b6-c76e70fd.pth',
'efficientnet-b7': '.. /input/efficientnet-pytorch/efficientnet-b7-dcc49843.pth',
}
efficientnet_encoders = {
'efficientnet-b0': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b0'],
'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
}
},
'out_shapes':(320, 112, 40, 24, 32),
'params': {
'width_coeff': 1.0,
'depth_coeff': 1.0,
'image_size': 224,
'dropout_rate': 0.2,
'drop_connect_rate': 0.2,
'block_chunks': [0, 2, 5, 11, 16]
},
},
'efficientnet-b1': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b1'],
'input_space': 'RGB',
'input_size': [3, 240, 240],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(320, 112, 40, 24, 32),
'params': {
'width_coeff': 1.0,
'depth_coeff': 1.1,
'image_size': 240,
'dropout_rate': 0.2,
'drop_connect_rate': 0.2,
'block_chunks': [0, 3, 8, 16, 23]
},
},
'efficientnet-b2': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b2'],
'input_space': 'RGB',
'input_size': [3, 260, 260],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(352, 120, 48, 24, 32),
'params': {
'width_coeff': 1.1,
'depth_coeff': 1.2,
'image_size': 260,
'dropout_rate': 0.3,
'drop_connect_rate': 0.2,
'block_chunks': [0, 3, 8, 16, 23]
},
},
'efficientnet-b3': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b3'],
'input_space': 'RGB',
'input_size': [3, 300, 300],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(384, 136, 48, 32, 40),
'params': {
'width_coeff': 1.2,
'depth_coeff': 1.4,
'image_size': 300,
'dropout_rate': 0.3,
'drop_connect_rate': 0.2,
'block_chunks': [0, 3, 8, 18, 26]
},
},
'efficientnet-b4': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b4'],
'input_space': 'RGB',
'input_size': [3, 380, 380],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(448, 160, 56, 32, 48),
'params': {
'width_coeff': 1.4,
'depth_coeff': 1.8,
'image_size': 380,
'dropout_rate': 0.4,
'drop_connect_rate': 0.2,
'block_chunks': [0, 5, 10, 22, 32]
},
},
'efficientnet-b5': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b5'],
'input_space': 'RGB',
'input_size': [3, 456, 456],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(512, 176, 64, 40, 48),
'params': {
'width_coeff': 1.6,
'depth_coeff': 2.2,
'image_size': 456,
'dropout_rate': 0.4,
'drop_connect_rate': 0.2,
'block_chunks': [0, 6, 13, 27, 39]
},
},
'efficientnet-b6': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b6'],
'input_space': 'RGB',
'input_size': [3, 528, 528],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(576, 200, 72, 40, 56),
'params': {
'width_coeff': 1.8,
'depth_coeff': 2.6,
'image_size': 528,
'dropout_rate': 0.5,
'drop_connect_rate': 0.2,
'block_chunks': [0, 7, 15, 31, 45]
},
},
'efficientnet-b7': {
'encoder': EfficientNetEncoder,
'pretrained_settings': {
'imagenet': {
'url': url_map['efficientnet-b7'],
'input_space': 'RGB',
'input_size': [3, 600, 600],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'out_shapes':(640, 224, 80, 48, 64),
'params': {
'width_coeff': 2.0,
'depth_coeff': 3.1,
'image_size': 600,
'dropout_rate': 0.5,
'drop_connect_rate': 0.2,
'block_chunks': [0, 9, 18, 38, 55]
},
},
}
class MBConvBlock(nn.Module):

 def __init__(self, block_args, global_params):
super().__init__()
self._block_args = block_args
self._bn_mom = 1 - global_params.batch_norm_momentum
self._bn_eps = global_params.batch_norm_epsilon
self.has_se =(self._block_args.se_ratio is not None)and(0 < self._block_args.se_ratio <= 1)
 self.id_skip = block_args.id_skip
Conv2d = get_same_padding_conv2d(image_size=global_params.image_size)
 inp = self._block_args.input_filters
oup = self._block_args.input_filters * self._block_args.expand_ratio
if self._block_args.expand_ratio != 1:
self._expand_conv = Conv2d(in_channels=inp, out_channels=oup, kernel_size=1, bias=False)
 self._bn0 = nn.BatchNorm2d(num_features=oup, momentum=self._bn_mom, eps=self._bn_eps)
 k = self._block_args.kernel_size
s = self._block_args.stride
self._depthwise_conv = Conv2d(
 in_channels=oup, out_channels=oup, groups=oup,
kernel_size=k, stride=s, bias=False)
 self._bn1 = nn.BatchNorm2d(num_features=oup, momentum=self._bn_mom, eps=self._bn_eps)
 if self.has_se:
num_squeezed_channels = max(1, int(self._block_args.input_filters * self._block_args.se_ratio))
self._se_reduce = Conv2d(in_channels=oup, out_channels=num_squeezed_channels, kernel_size=1)
 self._se_expand = Conv2d(in_channels=num_squeezed_channels, out_channels=oup, kernel_size=1)
 final_oup = self._block_args.output_filters
self._project_conv = Conv2d(in_channels=oup, out_channels=final_oup, kernel_size=1, bias=False)
 self._bn2 = nn.BatchNorm2d(num_features=final_oup, momentum=self._bn_mom, eps=self._bn_eps)
 def forward(self, inputs, drop_connect_rate=None):

 x = inputs
if self._block_args.expand_ratio != 1:
x = relu_fn(self._bn0(self._expand_conv(inputs)))
 x = relu_fn(self._bn1(self._depthwise_conv(x)))
 if self.has_se:
x_squeezed = F.adaptive_avg_pool2d(x, 1)
 x_squeezed = self._se_expand(relu_fn(self._se_reduce(x_squeezed)))
 x = torch.sigmoid(x_squeezed)* x
x = self._bn2(self._project_conv(x))
input_filters, output_filters = self._block_args.input_filters, self._block_args.output_filters
if self.id_skip and self._block_args.stride == 1 and input_filters == output_filters:
if drop_connect_rate:
x = drop_connect(x, p=drop_connect_rate, training=self.training)
 x = x + inputs
return x
GlobalParams = collections.namedtuple('GlobalParams', [
'batch_norm_momentum', 'batch_norm_epsilon', 'dropout_rate',
'num_classes', 'width_coefficient', 'depth_coefficient',
'depth_divisor', 'min_depth', 'drop_connect_rate', 'image_size'])
 BlockArgs = collections.namedtuple('BlockArgs', [
'kernel_size', 'num_repeat', 'input_filters', 'output_filters',
'expand_ratio', 'id_skip', 'stride', 'se_ratio'])
 GlobalParams.__new__.__defaults__ =(None,)* len(GlobalParams._fields)
 BlockArgs.__new__.__defaults__ =(None,)* len(BlockArgs._fields)
 def relu_fn(x):

 return x * torch.sigmoid(x)
 def round_filters(filters, global_params):

 multiplier = global_params.width_coefficient
if not multiplier:
return filters
divisor = global_params.depth_divisor
min_depth = global_params.min_depth
filters *= multiplier
min_depth = min_depth or divisor
new_filters = max(min_depth, int(filters + divisor / 2)// divisor * divisor)
 if new_filters < 0.9 * filters:
new_filters += divisor
return int(new_filters)
 def round_repeats(repeats, global_params):

 multiplier = global_params.depth_coefficient
if not multiplier:
return repeats
return int(math.ceil(multiplier * repeats))
def drop_connect(inputs, p, training):

 if not training: return inputs
batch_size = inputs.shape[0]
keep_prob = 1 - p
random_tensor = keep_prob
random_tensor += torch.rand([batch_size, 1, 1, 1], dtype=inputs.dtype, device=inputs.device)
 binary_tensor = torch.floor(random_tensor)
 output = inputs / keep_prob * binary_tensor
return output
def get_same_padding_conv2d(image_size=None):

 if image_size is None:
return Conv2dDynamicSamePadding
else:
return partial(Conv2dStaticSamePadding, image_size=image_size)
 class Conv2dDynamicSamePadding(nn.Conv2d):

 def __init__(self, in_channels, out_channels, kernel_size, stride=1, dilation=1, groups=1, bias=True):
super().__init__(in_channels, out_channels, kernel_size, stride, 0, dilation, groups, bias)
 self.stride = self.stride if len(self.stride)== 2 else [self.stride[0]]*2
def forward(self, x):
ih, iw = x.size() [-2:]
kh, kw = self.weight.size() [-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
 pad_h = max(( oh - 1)* self.stride[0] +(kh - 1)* self.dilation[0] + 1 - ih, 0)
 pad_w = max(( ow - 1)* self.stride[1] +(kw - 1)* self.dilation[1] + 1 - iw, 0)
 if pad_h > 0 or pad_w > 0:
x = F.pad(x, [pad_w//2, pad_w - pad_w//2, pad_h//2, pad_h - pad_h//2])
 return F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
 class Conv2dStaticSamePadding(nn.Conv2d):

 def __init__(self, in_channels, out_channels, kernel_size, image_size=None, **kwargs):
super().__init__(in_channels, out_channels, kernel_size, **kwargs)
 self.stride = self.stride if len(self.stride)== 2 else [self.stride[0]] * 2
assert image_size is not None
ih, iw = image_size if type(image_size)== list else [image_size, image_size]
kh, kw = self.weight.size() [-2:]
sh, sw = self.stride
oh, ow = math.ceil(ih / sh), math.ceil(iw / sw)
 pad_h = max(( oh - 1)* self.stride[0] +(kh - 1)* self.dilation[0] + 1 - ih, 0)
 pad_w = max(( ow - 1)* self.stride[1] +(kw - 1)* self.dilation[1] + 1 - iw, 0)
 if pad_h > 0 or pad_w > 0:
self.static_padding = nn.ZeroPad2d(( pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2))
else:
self.static_padding = Identity()
def forward(self, x):
x = self.static_padding(x)
 x = F.conv2d(x, self.weight, self.bias, self.stride, self.padding, self.dilation, self.groups)
 return x
class Identity(nn.Module):
def __init__(self,):
super(Identity, self ).__init__()
def forward(self, input):
return input
class BlockDecoder(object):

 @staticmethod
def _decode_block_string(block_string):

 assert isinstance(block_string, str)
 ops = block_string.split('_')
 options = {}
for op in ops:
splits = re.split(r'(\d.*)', op)
 if len(splits)>= 2:
key, value = splits[:2]
options[key] = value
assert(( 's' in options and len(options['s'])== 1)or 
(len(options['s'])== 2 and options['s'][0] == options['s'][1]))
return BlockArgs(
 kernel_size=int(options['k']),
num_repeat=int(options['r']),
input_filters=int(options['i']),
output_filters=int(options['o']),
expand_ratio=int(options['e']),
id_skip=('noskip' not in block_string),
se_ratio=float(options['se'])if 'se' in options else None,
stride=[int(options['s'][0])])
 @staticmethod
def _encode_block_string(block):

 args = [
'r%d' % block.num_repeat,
'k%d' % block.kernel_size,
's%d%d' %(block.strides[0], block.strides[1]),
'e%s' % block.expand_ratio,
'i%d' % block.input_filters,
'o%d' % block.output_filters
]
if 0 < block.se_ratio <= 1:
args.append('se%s' % block.se_ratio)
 if block.id_skip is False:
args.append('noskip')
 return '_'.join(args)
 @staticmethod
def decode(string_list):

 assert isinstance(string_list, list)
 blocks_args = []
for block_string in string_list:
blocks_args.append(BlockDecoder._decode_block_string(block_string))
return blocks_args
@staticmethod
def encode(blocks_args):

 block_strings = []
for block in blocks_args:
block_strings.append(BlockDecoder._encode_block_string(block))
return block_strings
def get_global_params(width_coeff, depth_coeff, image_size, dropout_rate, drop_connect_rate):

 global_params = GlobalParams(
 batch_norm_momentum=0.99,
batch_norm_epsilon=1e-3,
dropout_rate=dropout_rate,
drop_connect_rate=drop_connect_rate,
num_classes=1000,
width_coefficient=width_coeff,
depth_coefficient=depth_coeff,
depth_divisor=8,
min_depth=None,
image_size=image_size, 
)
 return global_params
class swish(torch.autograd.Function):
@staticmethod
def forward(ctx, i):
result = i * torch.sigmoid(i)
 ctx.save_for_backward(i)
 return result
@staticmethod
def backward(ctx, grad_output):
i = ctx.saved_variables[0]
sigmoid_i = torch.sigmoid(i)
 return grad_output *(sigmoid_i *(1 + i *(1 - sigmoid_i)))
 class Swish(torch.nn.Module):

 def forward(self, x):
return swish.apply(x)
 class Conv2dReLU(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, padding=0,
stride=1, use_batchnorm=True, **batchnorm_params):
super().__init__()
layers = [
nn.Conv2d(in_channels, out_channels, kernel_size,
stride=stride, padding=padding, bias=not(use_batchnorm)) ,
nn.ReLU(inplace=True),
]
if use_batchnorm:
layers.insert(1, nn.BatchNorm2d(out_channels, **batchnorm_params))
self.block = nn.Sequential(*layers)
 def forward(self, x):
return self.block(x)
 class SCSEModule(nn.Module):
def __init__(self, ch, re=16):
super().__init__()
self.cSE = nn.Sequential(nn.AdaptiveAvgPool2d(1),
nn.Conv2d(ch, ch//re, 1),
nn.ReLU(inplace=True),
nn.Conv2d(ch//re, ch, 1),
nn.Sigmoid() 
)
 self.sSE = nn.Sequential(nn.Conv2d(ch, ch, 1),
nn.Sigmoid())
 def forward(self, x):
return x * self.cSE(x)+ x * self.sSE(x)
 class SENetEncoder(SENet):
def __init__(self, in_channels=3, *args, **kwargs):
super().__init__(*args, **kwargs)
 self.pretrained = False
self.in_channels = in_channels
del self.last_linear
del self.avg_pool
self.layer0[0] = nn.Conv2d(in_channels, 64,(7, 7),(2, 2),(3, 3), bias=False)
 def forward(self, x):
for module in self.layer0[:-1]:
x = module(x)
 x0 = x
x = self.layer0[-1](x)
 x1 = self.layer1(x)
 x2 = self.layer2(x1)
 x3 = self.layer3(x2)
 x4 = self.layer4(x3)
 features = [x4, x3, x2, x1, x0]
return features
def load_state_dict(self, state_dict, **kwargs):
state_dict.pop('last_linear.bias')
 state_dict.pop('last_linear.weight')
 if self.in_channels != 3:
state_dict = self.modify_in_channel_weights(state_dict, self.in_channels)
 super().load_state_dict(state_dict, **kwargs)
 def modify_in_channel_weights(self, state_dict, in_channels):
self.layer0[0] = nn.Conv2d(in_channels, 64,(7, 7),(2, 2),(3, 3), bias=False)
 pretrained = state_dict['layer0.conv1.weight']
cycled_weights = cycle_rgb_weights(pretrained, in_channels)
 state_dict['layer0.conv1.weight'] = cycled_weights
return state_dict
senet_encoders = {
'senet154': {
'encoder': SENetEncoder,
'pretrained_settings': pretrained_settings['senet154'],
'out_shapes':(2048, 1024, 512, 256, 128),
'params': {
'block': SEBottleneck,
'dropout_p': 0.2,
'groups': 64,
'layers': [3, 8, 36, 3],
'num_classes': 1000,
'reduction': 16
},
},
'se_resnet50': {
'encoder': SENetEncoder,
'pretrained_settings': pretrained_settings['se_resnet50'],
'out_shapes':(2048, 1024, 512, 256, 64),
'params': {
'block': SEResNetBottleneck,
'layers': [3, 4, 6, 3],
'downsample_kernel_size': 1,
'downsample_padding': 0,
'dropout_p': None,
'groups': 1,
'inplanes': 64,
'input_3x3': False,
'num_classes': 1000,
'reduction': 16
},
},
'se_resnet101': {
'encoder': SENetEncoder,
'pretrained_settings': pretrained_settings['se_resnet101'],
'out_shapes':(2048, 1024, 512, 256, 64),
'params': {
'block': SEResNetBottleneck,
'layers': [3, 4, 23, 3],
'downsample_kernel_size': 1,
'downsample_padding': 0,
'dropout_p': None,
'groups': 1,
'inplanes': 64,
'input_3x3': False,
'num_classes': 1000,
'reduction': 16
},
},
'se_resnet152': {
'encoder': SENetEncoder,
'pretrained_settings': pretrained_settings['se_resnet152'],
'out_shapes':(2048, 1024, 512, 256, 64),
'params': {
'block': SEResNetBottleneck,
'layers': [3, 8, 36, 3],
'downsample_kernel_size': 1,
'downsample_padding': 0,
'dropout_p': None,
'groups': 1,
'inplanes': 64,
'input_3x3': False,
'num_classes': 1000,
'reduction': 16
},
},
'se_resnext50_32x4d': {
'encoder': SENetEncoder,
'pretrained_settings': pretrained_settings['se_resnext50_32x4d'],
'out_shapes':(2048, 1024, 512, 256, 64),
'params': {
'block': SEResNeXtBottleneck,
'layers': [3, 4, 6, 3],
'downsample_kernel_size': 1,
'downsample_padding': 0,
'dropout_p': None,
'groups': 32,
'inplanes': 64,
'input_3x3': False,
'num_classes': 1000,
'reduction': 16
},
},
'se_resnext101_32x4d': {
'encoder': SENetEncoder,
'pretrained_settings': pretrained_settings['se_resnext101_32x4d'],
'out_shapes':(2048, 1024, 512, 256, 64),
'params': {
'block': SEResNeXtBottleneck,
'layers': [3, 4, 23, 3],
'downsample_kernel_size': 1,
'downsample_padding': 0,
'dropout_p': None,
'groups': 32,
'inplanes': 64,
'input_3x3': False,
'num_classes': 1000,
'reduction': 16
},
},
}
encoders = {}
encoders.update(efficientnet_encoders)
 encoders.update(senet_encoders)
 def get_encoder(name, encoder_weights=None, in_channels=3):
Encoder = encoders[name]['encoder']
encoder = Encoder(in_channels=in_channels, **encoders[name]['params'])
 encoder.out_shapes = encoders[name]['out_shapes']
if encoder_weights is not None:
settings = encoders[name]['pretrained_settings'][encoder_weights]
r = encoder.load_state_dict(torch.load(settings['url']))
print(f'Load result: {r}')
 return encoder
def get_encoder_names() :
return list(encoders.keys())
 def get_preprocessing_params(encoder_name, pretrained='imagenet'):
settings = encoders[encoder_name]['pretrained_settings']
if pretrained not in settings.keys() :
raise ValueError('Avaliable pretrained options {}'.format(settings.keys()))
formatted_settings = {}
formatted_settings['input_space'] = settings[pretrained].get('input_space')
 formatted_settings['input_range'] = settings[pretrained].get('input_range')
 formatted_settings['mean'] = settings[pretrained].get('mean')
 formatted_settings['std'] = settings[pretrained].get('std')
 return formatted_settings
def get_preprocessing_fn(encoder_name, pretrained='imagenet'):
params = get_preprocessing_params(encoder_name, pretrained=pretrained)
 return functools.partial(preprocess_input, **params)
 class DecoderBlock(nn.Module):
def __init__(self, in_channels, out_channels, use_batchnorm=True, attention_type=None):
super().__init__()
if attention_type is None:
self.attention1 = nn.Identity()
self.attention2 = nn.Identity()
elif attention_type == 'scse':
self.attention1 = SCSEModule(in_channels)
 self.attention2 = SCSEModule(out_channels)
 self.block = nn.Sequential(
 Conv2dReLU(in_channels, out_channels, kernel_size=3, padding=1, use_batchnorm=use_batchnorm),
Conv2dReLU(out_channels, out_channels, kernel_size=3, padding=1, use_batchnorm=use_batchnorm), 
)
 def forward(self, x):
x, skip = x
if skip is not None:
skipsize =(skip.shape[-2], skip.shape[-1])
 x = F.interpolate(x, size=skipsize, mode='nearest')
 x = torch.cat([x, skip], dim=1)
 x = self.attention1(x)
 else:
x = F.interpolate(x, scale_factor=2, mode='nearest')
 x = self.block(x)
 x = self.attention2(x)
 return x
class CenterBlock(DecoderBlock):
def forward(self, x):
return self.block(x)
 class UnetDecoder(Model):
def __init__(
 self,
encoder_channels,
decoder_channels=(256, 128, 64, 32, 16),
final_channels=1,
use_batchnorm=True,
center=False,
attention_type=None 
):
super().__init__()
if center:
channels = encoder_channels[0]
self.center = CenterBlock(channels, channels, use_batchnorm=use_batchnorm)
 else:
self.center = None
in_channels = self.compute_channels(encoder_channels, decoder_channels)
 out_channels = decoder_channels
self.layer1 = DecoderBlock(in_channels[0], out_channels[0],
use_batchnorm=use_batchnorm, attention_type=attention_type)
 self.layer2 = DecoderBlock(in_channels[1], out_channels[1],
use_batchnorm=use_batchnorm, attention_type=attention_type)
 self.layer3 = DecoderBlock(in_channels[2], out_channels[2],
use_batchnorm=use_batchnorm, attention_type=attention_type)
 self.layer4 = DecoderBlock(in_channels[3], out_channels[3],
use_batchnorm=use_batchnorm, attention_type=attention_type)
 self.layer5 = DecoderBlock(in_channels[4], out_channels[4],
use_batchnorm=use_batchnorm, attention_type=attention_type)
 self.final_conv = nn.Conv2d(out_channels[4], final_channels, kernel_size=(1, 1))
self.initialize()
def compute_channels(self, encoder_channels, decoder_channels):
channels = [
encoder_channels[0] + encoder_channels[1],
encoder_channels[2] + decoder_channels[0],
encoder_channels[3] + decoder_channels[1],
encoder_channels[4] + decoder_channels[2],
0 + decoder_channels[3],
]
return channels
def forward(self, x):
encoder_head = x[0]
skips = x[1:]
if self.center:
encoder_head = self.center(encoder_head)
 x = self.layer1([encoder_head, skips[0]])
 x = self.layer2([x, skips[1]])
 x = self.layer3([x, skips[2]])
 x = self.layer4([x, skips[3]])
 x = self.layer5([x, None])
 x = self.final_conv(x)
 return x
class Conv2dWS(nn.Conv2d):
def __init__(self, in_channels, out_channels, kernel_size, stride=1,
padding=0, dilation=1, groups=1, bias=True):
super(Conv2dWS, self ).__init__(in_channels, out_channels, kernel_size, stride,
padding, dilation, groups, bias)
 def forward(self, x):
weight = self.weight
weight_mean = weight.mean(dim=1, keepdim=True ).mean(dim=2,
keepdim=True ).mean(dim=3, keepdim=True)
 weight = weight - weight_mean
std = weight.view(weight.size(0), -1 ).std(dim=1 ).view(-1, 1, 1, 1)+ 1e-5
weight = weight / std.expand_as(weight)
 return F.conv2d(x, weight, self.bias, self.stride,
self.padding, self.dilation, self.groups)
 class Conv3x3GNReLU(nn.Module):
def __init__(self, in_channels, out_channels, weight_std=False, upsample=False, activation='relu'):
if weight_std:
Conv2d = Conv2dWS
else:
Conv2d = nn.Conv2d
if activation == 'relu':
relu_fn = nn.ReLU(inplace=True)
 elif activation == 'swish':
relu_fn = Swish()
else:
raise ValueError(f'`activation` must be "relu" or "swish"')
 super().__init__()
self.upsample = upsample
self.block = nn.Sequential(
 Conv2d(in_channels, out_channels,(3, 3),
stride=1, padding=1, bias=False),
nn.GroupNorm(32, out_channels),
relu_fn, 
)
 def forward(self, x):
x = self.block(x)
 if self.upsample:
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=True)
 return x
class FPNBlock(nn.Module):
def __init__(self, pyramid_channels, skip_channels):
super().__init__()
self.skip_conv = nn.Conv2d(skip_channels, pyramid_channels, kernel_size=1)
 def forward(self, x):
x, skip = x
x = F.interpolate(x, scale_factor=2, mode='nearest')
 skip = self.skip_conv(skip)
 x = x + skip
return x
class SegmentationBlock(nn.Module):
def __init__(self, in_channels, out_channels, weight_std, n_upsamples=0, activation='relu'):
super().__init__()
blocks = [
Conv3x3GNReLU(in_channels, out_channels, upsample=bool(n_upsamples), activation=activation)
 ]
if n_upsamples > 1:
for _ in range(1, n_upsamples):
blocks.append(Conv3x3GNReLU(
 out_channels, out_channels, weight_std, upsample=True, activation=activation))
self.block = nn.Sequential(*blocks)
 def forward(self, x):
return self.block(x)
 class FPNDecoder(Model):
def __init__(
 self,
encoder_channels,
pyramid_channels=256,
segmentation_channels=128,
final_upsampling=4,
final_channels=1,
dropout=0.2,
weight_std=False,
merge_policy='add',
activation='relu' 
):
super().__init__()
if merge_policy not in ['add', 'cat']:
raise ValueError("`merge_policy` must be one of: ['add', 'cat'], got {}".format(merge_policy))
self.merge_policy = merge_policy
self.final_upsampling = final_upsampling
self.conv1 = nn.Conv2d(encoder_channels[0], pyramid_channels, kernel_size=(1, 1))
self.p4 = FPNBlock(pyramid_channels, encoder_channels[1])
 self.p3 = FPNBlock(pyramid_channels, encoder_channels[2])
 self.p2 = FPNBlock(pyramid_channels, encoder_channels[3])
 self.s5 = SegmentationBlock(pyramid_channels, segmentation_channels, weight_std, 3, activation)
 self.s4 = SegmentationBlock(pyramid_channels, segmentation_channels, weight_std, 2, activation)
 self.s3 = SegmentationBlock(pyramid_channels, segmentation_channels, weight_std, 1, activation)
 self.s2 = SegmentationBlock(pyramid_channels, segmentation_channels, weight_std, 0, activation)
 self.dropout = nn.Dropout2d(p=dropout, inplace=True)
 if self.merge_policy == 'cat':
segmentation_channels *= 4
self.final_conv = nn.Conv2d(segmentation_channels, final_channels, kernel_size=1, padding=0)
 self.initialize()
def forward(self, x):
c5, c4, c3, c2, _ = x
p5 = self.conv1(c5)
 p4 = self.p4([p5, c4])
 p3 = self.p3([p4, c3])
 p2 = self.p2([p3, c2])
 s5 = self.s5(p5)
 s4 = self.s4(p4)
 s3 = self.s3(p3)
 s2 = self.s2(p2)
 if self.merge_policy == 'add':
x = s5 + s4 + s3 + s2
elif self.merge_policy == 'cat':
x = torch.cat([s5, s4, s3, s2], dim=1)
 x = self.dropout(x)
 x = self.final_conv(x)
 if self.final_upsampling is not None and self.final_upsampling > 1:
x = F.interpolate(x, scale_factor=self.final_upsampling, mode='bilinear', align_corners=True)
 return x
class Unet(EncoderDecoder):

 def __init__(
 self,
encoder_name='resnet34',
encoder_weights='imagenet',
decoder_use_batchnorm=True,
decoder_channels=(256, 128, 64, 32, 16),
classes=1,
activation='sigmoid',
center=False,
attention_type=None,
in_channels=3 
):
encoder = get_encoder(
 encoder_name,
encoder_weights=encoder_weights,
in_channels=in_channels 
)
 decoder = UnetDecoder(
 encoder_channels=encoder.out_shapes,
decoder_channels=decoder_channels,
final_channels=classes,
use_batchnorm=decoder_use_batchnorm,
center=center,
attention_type=attention_type 
)
 super().__init__(encoder, decoder, activation)
 self.name = 'u-{}'.format(encoder_name)
 class FPN(EncoderDecoder):

 def __init__(
 self,
encoder_name='resnet34',
encoder_weights='imagenet',
decoder_pyramid_channels=256,
decoder_segmentation_channels=128,
decoder_merge_policy='add',
decoder_activation='relu',
classes=1,
dropout=0.2,
activation='sigmoid',
in_channels=3,
weight_std=False,
final_upsampling=4, 
):
encoder = get_encoder(
 encoder_name,
encoder_weights=encoder_weights,
in_channels=in_channels 
)
 decoder = FPNDecoder(
 encoder_channels=encoder.out_shapes,
pyramid_channels=decoder_pyramid_channels,
segmentation_channels=decoder_segmentation_channels,
final_channels=classes,
dropout=dropout,
weight_std=weight_std,
final_upsampling=4,
merge_policy=decoder_merge_policy,
activation=decoder_activation 
)
 super().__init__(encoder, decoder, activation)
 self.name = 'fpn-{}'.format(encoder_name )<define_search_space>

 oe = OrdinalEncoder()
df[['item_id','similar_item1','item_cat_group',
'similar_item2','similar_item3','city',
'item_cat_id','shop_id','month','year']] = oe.fit_transform(df[['item_id','similar_item1','item_cat_group',
'similar_item2','similar_item3','city',
'item_cat_id','shop_id','month','year']])
 X_cols_to_drop = [
"train",
"item_cnt_month",
"item_cat_name",
"shop_name",
'binned_item_price'
]
def efficientnet_params(model_name):

 params_dict = {
'efficientnet-b0':(1.0, 1.0, 224, 0.2),
'efficientnet-b1':(1.0, 1.1, 240, 0.2),
'efficientnet-b2':(1.1, 1.2, 260, 0.3),
'efficientnet-b3':(1.2, 1.4, 300, 0.3),
'efficientnet-b4':(1.4, 1.8, 380, 0.4),
'efficientnet-b5':(1.6, 2.2, 456, 0.4),
'efficientnet-b6':(1.8, 2.6, 528, 0.5),
'efficientnet-b7':(2.0, 3.1, 600, 0.5),
}
return params_dict[model_name]
def efficientnet(width_coefficient=None, depth_coefficient=None, dropout_rate=0.2,
drop_connect_rate=0.2, image_size=None, num_classes=1000):

 blocks_args = [
'r1_k3_s11_e1_i32_o16_se0.25', 'r2_k3_s22_e6_i16_o24_se0.25',
'r2_k5_s22_e6_i24_o40_se0.25', 'r3_k3_s22_e6_i40_o80_se0.25',
'r3_k5_s11_e6_i80_o112_se0.25', 'r4_k5_s22_e6_i112_o192_se0.25',
'r1_k3_s11_e6_i192_o320_se0.25',
]
blocks_args = BlockDecoder.decode(blocks_args)
 global_params = GlobalParams(
 batch_norm_momentum=0.99,
batch_norm_epsilon=1e-3,
dropout_rate=dropout_rate,
drop_connect_rate=drop_connect_rate,
num_classes=num_classes,
width_coefficient=width_coefficient,
depth_coefficient=depth_coefficient,
depth_divisor=8,
min_depth=None,
image_size=image_size, 
)
 return blocks_args, global_params
def get_model_params(model_name, override_params):

 if model_name.startswith('efficientnet'):
w, d, s, p = efficientnet_params(model_name)
 blocks_args, global_params = efficientnet(
 width_coefficient=w, depth_coefficient=d, dropout_rate=p, image_size=s)
 else:
raise NotImplementedError('model name is not pre-defined: %s' % model_name)
 if override_params:
global_params = global_params._replace(**override_params)
 return blocks_args, global_params
def load_pretrained_weights(model, model_name, load_fc=True):

 state_dict = torch.load(url_map[model_name])
 if load_fc:
model.load_state_dict(state_dict)
 else:
state_dict.pop('_fc.weight')
 state_dict.pop('_fc.bias')
 res = model.load_state_dict(state_dict, strict=False)
 assert str(res.missing_keys)== str(['_fc.weight', '_fc.bias']), 'issue loading pretrained weights'
print('Loaded pretrained weights for {}'.format(model_name))
class EfficientNet(nn.Module):

 def __init__(self, blocks_args=None, global_params=None):
super().__init__()
assert isinstance(blocks_args, list), 'blocks_args should be a list'
assert len(blocks_args)> 0, 'block args must be greater than 0'
self._global_params = global_params
self._blocks_args = blocks_args
Conv2d = get_same_padding_conv2d(image_size=global_params.image_size)
 bn_mom = 1 - self._global_params.batch_norm_momentum
bn_eps = self._global_params.batch_norm_epsilon
in_channels = 3
out_channels = round_filters(32, self._global_params)
 self._conv_stem = Conv2d(in_channels, out_channels, kernel_size=3, stride=2, bias=False)
 self._bn0 = nn.BatchNorm2d(num_features=out_channels, momentum=bn_mom, eps=bn_eps)
 self._blocks = nn.ModuleList([])
 for block_args in self._blocks_args:
block_args = block_args._replace(
 input_filters=round_filters(block_args.input_filters, self._global_params),
output_filters=round_filters(block_args.output_filters, self._global_params),
num_repeat=round_repeats(block_args.num_repeat, self._global_params)
)
 self._blocks.append(MBConvBlock(block_args, self._global_params))
if block_args.num_repeat > 1:
block_args = block_args._replace(input_filters=block_args.output_filters, stride=1)
 for _ in range(block_args.num_repeat - 1):
self._blocks.append(MBConvBlock(block_args, self._global_params))
in_channels = block_args.output_filters
out_channels = round_filters(1280, self._global_params)
 self._conv_head = Conv2d(in_channels, out_channels, kernel_size=1, bias=False)
 self._bn1 = nn.BatchNorm2d(num_features=out_channels, momentum=bn_mom, eps=bn_eps)
 self._dropout = self._global_params.dropout_rate
self._fc = nn.Linear(out_channels, self._global_params.num_classes)
 def extract_features(self, inputs):

 x = relu_fn(self._bn0(self._conv_stem(inputs)))
 for idx, block in enumerate(self._blocks):
drop_connect_rate = self._global_params.drop_connect_rate
if drop_connect_rate:
drop_connect_rate *= float(idx)/ len(self._blocks)
 x = block(x, drop_connect_rate=drop_connect_rate)
 x = relu_fn(self._bn1(self._conv_head(x)))
 return x
def forward(self, inputs):

 x = self.extract_features(inputs)
 x = F.adaptive_avg_pool2d(x, 1 ).squeeze(-1 ).squeeze(-1)
 if self._dropout:
x = F.dropout(x, p=self._dropout, training=self.training)
 x = self._fc(x)
 return x
@classmethod
def from_name(cls, model_name, override_params=None):
cls._check_model_name_is_valid(model_name)
 blocks_args, global_params = get_model_params(model_name, override_params)
 return EfficientNet(blocks_args, global_params)
 @classmethod
def from_pretrained(cls, model_name, num_classes=1000):
model = EfficientNet.from_name(model_name, override_params={'num_classes': num_classes})
 load_pretrained_weights(model, model_name, load_fc=(num_classes == 1000))
return model
@classmethod
def get_image_size(cls, model_name):
cls._check_model_name_is_valid(model_name)
 _, _, res, _ = efficientnet_params(model_name)
 return res
@classmethod
def _check_model_name_is_valid(cls, model_name, also_need_pretrained_weights=False):

 num_models = 4 if also_need_pretrained_weights else 8
valid_models = ['efficientnet_b'+str(i)for i in range(num_models)]
if model_name.replace('-','_')not in valid_models:
raise ValueError('model_name should be one of: ' + ', '.join(valid_models))
class EffNet(nn.Module):

 def __init__(self, encoder_name, classes, pretrained, in_channels='rgb', verbose=0):
super().__init__()
if pretrained:
self.encoder = EfficientNet.from_pretrained(encoder_name, num_classes=classes)
 else:
self.encoder = EfficientNet.from_name(
 encoder_name,
override_params={'num_classes': classes})
 Conv2d = get_same_padding_conv2d(image_size=self.encoder._global_params.image_size)
 conv_stem = Conv2d(
 len(in_channels),
self.encoder._conv_stem.out_channels,
kernel_size=3,
stride=2,
bias=False 
)
 self.transfer_weights(self.encoder._conv_stem, conv_stem, in_channels)
 self.encoder._conv_stem = conv_stem
self.decoder = Noop()
def forward(self, x):
return self.encoder(x)
 def __str__(self):
return str(self.encoder)
 def __repr__(self):
return self.__str__()
def transfer_weights(self, pretrained_layer, replacement_layer, rgb_str='rgb'):

 weights = select_rgb_weights(pretrained_layer.weight, rgb_str)
 replacement_layer.weight = nn.Parameter(weights)
 return replacement_layer
class Noop(nn.Module):
def forward(self, x):
return x<normalization>
def df_to_xy(df):
X_train = df[df["date_block_num"] < 33].drop(columns=X_cols_to_drop)
 X_val = df[df["date_block_num"] == 33].drop(columns=X_cols_to_drop)
 y_train = df.loc[df["date_block_num"] < 33, "item_cnt_month"]
y_val = df.loc[df["date_block_num"] == 33, "item_cnt_month"]
X_test = df[df["date_block_num"] == 34].drop(columns=X_cols_to_drop)
 return X_test, X_train, X_val, y_train, y_val
X_test, X_train, X_val, y_train, y_val = df_to_xy(df)
 del df
capture = gc.collect()
class ChannelTranspose(ImageOnlyTransform):
def get_transform_init_args_names(self):
return()
def get_params_dependent_on_targets(self, params):
pass
def __init__(self, axes=(2, 0, 1)) :
super().__init__(always_apply=True)
 self.axes = axes
def apply(self, img, **params):
return np.transpose(img, self.axes)
 class AugmentationBase(abc.ABC):
MEAN = [0.3439]
STD = [0.0383]
H = 256
W = 1600
def __init__(self):
self.transform = self.notimplemented
def build_transforms(self, train):
if train:
self.transform = self.build_train()
else:
self.transform = self.build_test()
@abc.abstractmethod
def build_train(self):
pass
def build_test(self):
return Compose([
Normalize(mean=self.MEAN, std=self.STD),
ToTensor() ,
])
 def notimplemented(self, *args, **kwargs):
raise Exception('You must call `build_transforms() ` before using me!')
 def __call__(self, *args, **kwargs):
return self.transform(*args, **kwargs)
 def copy(self):
return deepcopy(self)
 class LightTransforms(AugmentationBase):
def __init__(self):
super().__init__()
def build_train(self):
return Compose([
HorizontalFlip(p=0.5),
Normalize(mean=self.MEAN, std=self.STD),
ToTensor() ,
])
 class CompatabilityTransforms(AugmentationBase):
def __init__(self):
super().__init__()
def build_train(self):
return Compose([
HorizontalFlip(p=0.5),
Normalize(mean=self.MEAN, std=self.STD),
ToTensor() ,
])
 def build_test(self):
return Compose([
Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)) ,
ChannelTranspose()
])
 class SteelDataset(Dataset):
img_folder = 'implement me!'
N_CLASSES = 4
rle_cols = [f'rle{i}' for i in range(N_CLASSES)]
def __init__(self, df, data_dir, transforms):
self.df = df
self.data_dir = data_dir / self.img_folder
self.transforms = transforms
self.fnames = self.df.index.tolist()
def read_greyscale(self, idx):
f = self.fnames[idx]
return f, cv2.imread(str(self.data_dir / f)) [:, :, 0:1]
def rle(self, idx):
return self.df.iloc[idx][self.rle_cols]
def __len__(self):
return len(self.fnames)
 class SteelDatasetTest(SteelDataset):
img_folder = 'test_images'
def __init__(self, df, data_dir, transforms):
super().__init__(df, data_dir, transforms)
 self.transforms.build_transforms(train=False)
 def __getitem__(self, idx):
f, image = self.read_greyscale(idx)
 images = self.transforms(image=image)["image"]
return f, images
class SteelClasTestDataLoader(DataLoader):
test_csv = 'sample_submission.csv'
def __init__(self, transforms, data_dir, batch_size, nworkers, pin_memory=True):
self.transforms = transforms
self.bs, self.nworkers, self.pin_memory = batch_size, nworkers, pin_memory
self.data_dir = Path(data_dir)
 self.test_df = self.load_df()
dataset = SteelDatasetTest(self.test_df, self.data_dir, transforms.copy())
 super().__init__(dataset, batch_size=self.bs, num_workers=nworkers, pin_memory=pin_memory)
 def load_df(self):
df = pd.read_csv(self.data_dir / self.test_csv)
 df['ImageId'], df['ClassId'] = zip(*df['ImageId_ClassId'].str.split('_'))
df['ClassId'] = df['ClassId'].astype(int)
 df = df.pivot(index='ImageId', columns='ClassId', values='EncodedPixels')
 df.columns = [f'rle{c}' for c in range(4)]
return df
def split_validation(self):
raise Exception('Attempt to split a validation data_loader from a test-only data_loader')
 class SteelDataLoader(DataLoader):
train_csv = 'train.csv'
test_csv = 'sample_submission.csv'
def __init__(self, transforms, data_dir, batch_size, shuffle,
validation_split, nworkers, pin_memory=True, train=True, alpha=None, balance=None 
):
self.transforms, self.shuffle = transforms, shuffle
self.bs, self.nworkers, self.pin_memory = batch_size, nworkers, pin_memory
self.data_dir = Path(data_dir)
 self.train_df, self.val_df = self.load_df(train, validation_split)
 if train:
dataset = SteelDatasetTrainVal(self.train_df, self.data_dir, transforms.copy() , True)
 else:
dataset = SteelDatasetTest(self.train_df, self.data_dir, transforms.copy())
 if train and balance is not None and alpha is not None:
class_idxs = self.sort_classes(self.train_df)
 n_batches = self.train_df.shape[0] // batch_size
sampler = SamplerFactory(2 ).get(class_idxs, batch_size, n_batches, alpha, balance)
 super().__init__(dataset, batch_sampler=sampler,
num_workers=nworkers, pin_memory=pin_memory)
 else:
super().__init__(dataset, batch_size, shuffle=shuffle,
num_workers=nworkers, pin_memory=pin_memory)
 def load_df(self, train, validation_split):
csv_filename = self.train_csv if train else self.test_csv
df = pd.read_csv(self.data_dir / csv_filename)
 df['ImageId'], df['ClassId'] = zip(*df['ImageId_ClassId'].str.split('_'))
df['ClassId'] = df['ClassId'].astype(int)
 df = df.pivot(index='ImageId', columns='ClassId', values='EncodedPixels')
 df.columns = [f'rle{c}' for c in range(4)]
df['defects'] = df.count(axis=1)
 for c in range(4):
df[f'c{c}'] = df[f'rle{c}'].apply(lambda rle: not pd.isnull(rle))
if train and validation_split > 0:
return train_test_split(df, test_size=validation_split, stratify=df["defects"])
 return df, pd.DataFrame({})
 def sort_classes(self, df):
counts = {c: df[f'c{c}'].sum() for c in range(4)}
sorted_classes = sorted(counts.items() , key=lambda kv: kv[1])
 def assign_min_sample_class(row, sorted_classes):
for c, _ in sorted_classes:
if row[f'c{c}']:
return c
return -1
df['sample_class'] = df.apply(
 lambda row: assign_min_sample_class(row, sorted_classes), axis=1)
 class_idxs = [list(np.where(df['sample_class'] == c)[0])for c in range(-1, 4)]
return class_idxs
def split_validation(self):
if self.val_df.empty:
return None
else:
dataset = SteelDatasetTrainVal(
 self.val_df, self.data_dir, self.transforms.copy() , False)
 return DataLoader(dataset, self.bs // 2,
num_workers=self.nworkers, pin_memory=self.pin_memory)
 class RLE:

 MASK_H = 256
MASK_W = 1600
@classmethod
def from_str(cls, s):
if s != s:
return cls()
list_ = [int(n)for n in s.split(' ')]
return cls.from_list(list_)
 @classmethod
def from_mask(cls, mask):
pixels = mask.T.flatten()
pixels = np.concatenate([[0], pixels, [0]])
 runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
runs[1::2] -= runs[::2]
return cls.from_list(runs)
 @classmethod
def from_list(cls, list_):
n_items = int(len(list_)/ 2)
 items = np.zeros(( n_items, 2), dtype=np.uint64)
 for i in range(n_items):
items[i, 0] = list_[i * 2]
items[i, 1] = list_[i * 2 + 1]
return cls(items)
 def __init__(self, items=np.zeros(( 0, 0))):
self._items = items
@property
def items(self):
return self._items
def __iter__(self):
for idx, item in enumerate(self.items):
yield(item[0], item[1])
 def __len__(self):
return self.items.shape[0]
def to_mask(self):
mask = np.zeros(self.MASK_H * self.MASK_W, dtype=np.uint8)
 for run, length in self:
run = int(run - 1)
 end = int(run + length)
 mask[run:end] = 1
return mask.reshape(self.MASK_H, self.MASK_W, order='F')
 def to_str_list(self):
list_ = []
for run, length in self:
list_.append(str(run))
list_.append(str(length))
return list_
def __str__(self):
if len(self)== 0:
return ''
return ' '.join(self.to_str_list())
 def __repr__(self):
return self.__str__()
class PostProcessor:
N_CLASSES = 4
MIN_COMPONENT_SIZE = 200
def __init__(
 self,
classification_factor=5,
domination=False,
dilation=0,
p_thresh=None,
min_class_sizes=None 
):
self.clas_factor = classification_factor
self.domination = domination
self.dilation = dilation
self.p_thresh = np.array(p_thresh)
 self.min_class_sizes = np.array(min_class_sizes)
 print(self.clas_factor)
 print(self.domination)
 print(self.dilation)
 print(self.p_thresh)
 print(self.min_class_sizes)
 self.n_wipes = 0
def _component_domination(self, preds):

 components, component_sizes = self._find_components(preds)
 mask = self._write_preds(components, component_sizes)
 return mask
def _find_components(self, preds):
C, H, W = preds.shape
total_components = 0
channel_components = []
for c in range(C):
max_label, labelled_components = cv2.connectedComponents(preds[c].astype(np.uint8))
n_components = max_label - 1
labelled_components[labelled_components > 0] += total_components
total_components += n_components
channel_components.append(labelled_components)
 components = np.stack(channel_components, axis=0)
 component_sizes = [(label,(components == label ).sum())
 for label in range(1, total_components + 1)]
component_sizes = sorted(component_sizes, key=lambda item: item[1])
 return components, component_sizes
def _write_preds(self, components, component_sizes):
C, H, W = components.shape
mask = np.zeros(( C, H, W), dtype=np.uint8)
 for label, size in component_sizes:
component_mask_3d = components == label
component_mask_flatten = component_mask_3d.any(axis=0)
 component_mask_expand = np.repeat(component_mask_flatten[np.newaxis, :, :], C, axis=0)
 if self.domination:
mask[component_mask_expand] = 0
mask[component_mask_3d] = 1
return mask
def process(self, prob_seg, prob_clas):
mask = prob_seg > self.p_thresh[:, np.newaxis, np.newaxis]
for c in range(self.N_CLASSES):
t0 = self.min_class_sizes[c]
t1 =(t0 * self.clas_factor)-(t0 *(self.clas_factor - 1)* prob_clas[c])
 if mask[c, :, :].sum() < t1:
mask[c, :, :] = 0
self.n_wipes += 1
elif self.dilation > 0:
mask[c, :, :] = ndimage.binary_dilation(
 mask[c, :, :],
iterations=self.dilation 
)
 return mask
def ensure_dir(path):
if not os.path.exists(path):
os.makedirs(path)
 return path
def ensure_parent(path):
parent = os.path.sep.join(path.split(os.path.sep)[:-1])
 ensure_dir(parent)
 return path
def arch_path(config):
return ensure_dir(join(config['save_dir'], config['name']))
def arch_datetime_path(config):
start_time = datetime.datetime.now().strftime('%m%d_%H%M%S')
 return ensure_dir(join(arch_path(config), start_time))
def log_path(config):
return ensure_dir(join(arch_path(config), 'logs'))
def trainer_paths(config):

 arch_datetime = arch_datetime_path(config)
 return(
 ensure_dir(join(arch_datetime, 'checkpoints')) ,
ensure_dir(join(arch_datetime, 'runs')) 
)
 LOG_CONFIG =
def setup_logging(run_config, log_config=LOG_CONFIG, default_level=logging.INFO):

 config = yaml.safe_load(log_config)
 logging.config.dictConfig(config)
 logging_level_dict = {
0: logging.WARNING,
1: logging.INFO,
2: logging.DEBUG
}
def setup_logger(cls, verbose=0):
logger = logging.getLogger(cls.__class__.__name__)
 if verbose not in logging_level_dict:
raise KeyError(f'verbose option {verbose} for {cls} not valid.'
f'Valid options are {logging_level_dict.keys() }.')
 logger.setLevel(logging_level_dict[verbose])
 return logger 
<categorify>
model_params = {
"n_estimators": 200,
"num_leaves": 40,
"learning_rate": 0.15,
"min_child_weight": 0.35,
"colsample_bytree": 0.27,
"reg_alpha": 0.83,
"reg_lambda": 0.56,
}
model = LGBMRegressor(**model_params)
 model.fit(
 X_train,
y_train,
eval_metric="rmse",
eval_set=(X_val, y_val),
early_stopping_rounds=10,
categorical_feature=[
"item_id","shop_id",'city',
"date_block_num","item_cat_id",'item_cat_group',
"similar_item1","similar_item2","similar_item3"
] 
)
 pred = model.predict(X_test ).clip(0, 20)
 sub_ID = pd.read_csv(".. /input/competitive-data-science-predict-future-sales/test.csv")[
"ID"
].values
submissionfile = pd.DataFrame(list(zip(sub_ID, pred)) , columns=["ID", "item_cnt_month"] )
<choose_model_class><EOS> 
submissionfile.to_csv('submission.csv', index=False )