Kaggle competition: COVID19 Global Forecasting (Week 4)
def squash(x, axis=-1):
s_squared_norm = K.sum(K.square(x), axis, keepdims=True)
 scale = K.sqrt(s_squared_norm + K.epsilon())
 return x / scale
class Capsule(Layer):
def __init__(self, num_capsule, dim_capsule, routings=3, kernel_size=(9, 1), share_weights=True,
activation='default', **kwargs):
super(Capsule, self ).__init__(**kwargs)
 self.num_capsule = num_capsule
self.dim_capsule = dim_capsule
self.routings = routings
self.kernel_size = kernel_size
self.share_weights = share_weights
if activation == 'default':
self.activation = squash
else:
self.activation = Activation(activation)
 def build(self, input_shape):
super(Capsule, self ).build(input_shape)
 input_dim_capsule = input_shape[-1]
if self.share_weights:
self.W = self.add_weight(name='capsule_kernel',
shape=(1, input_dim_capsule,
self.num_capsule * self.dim_capsule),
initializer='glorot_uniform',
trainable=True)
 else:
input_num_capsule = input_shape[-2]
self.W = self.add_weight(name='capsule_kernel',
shape=(input_num_capsule,
input_dim_capsule,
self.num_capsule * self.dim_capsule),
initializer='glorot_uniform',
trainable=True)
 def call(self, u_vecs):
if self.share_weights:
u_hat_vecs = K.conv1d(u_vecs, self.W)
 else:
u_hat_vecs = K.local_conv1d(u_vecs, self.W, [1], [1])
 batch_size = K.shape(u_vecs)[0]
input_num_capsule = K.shape(u_vecs)[1]
u_hat_vecs = K.reshape(u_hat_vecs,(batch_size, input_num_capsule,
self.num_capsule, self.dim_capsule))
u_hat_vecs = K.permute_dimensions(u_hat_vecs,(0, 2, 1, 3))
b = K.zeros_like(u_hat_vecs[:, :, :, 0])
 for i in range(self.routings):
b = K.permute_dimensions(b,(0, 2, 1))
c = K.softmax(b)
 c = K.permute_dimensions(c,(0, 2, 1))
b = K.permute_dimensions(b,(0, 2, 1))
outputs = self.activation(tf.keras.backend.batch_dot(c, u_hat_vecs, [2, 2]))
if i < self.routings - 1:
b = tf.keras.backend.batch_dot(outputs, u_hat_vecs, [2, 3])
 return outputs
def compute_output_shape(self, input_shape):
return(None, self.num_capsule, self.dim_capsule)
 class Attention(Layer):
def __init__(self, step_dim,
W_regularizer=None, b_regularizer=None,
W_constraint=None, b_constraint=None,
bias=True, **kwargs):
self.supports_masking = True
self.init = initializers.get('glorot_uniform')
 self.W_regularizer = regularizers.get(W_regularizer)
 self.b_regularizer = regularizers.get(b_regularizer)
 self.W_constraint = constraints.get(W_constraint)
 self.b_constraint = constraints.get(b_constraint)
 self.bias = bias
self.step_dim = step_dim
self.features_dim = 0
super(Attention, self ).__init__(**kwargs)
 def build(self, input_shape):
assert len(input_shape)== 3
self.W = self.add_weight(( input_shape[-1],),
initializer=self.init,
name='{}_W'.format(self.name),
regularizer=self.W_regularizer,
constraint=self.W_constraint)
 self.features_dim = input_shape[-1]
if self.bias:
self.b = self.add_weight(( input_shape[1],),
initializer='zero',
name='{}_b'.format(self.name),
regularizer=self.b_regularizer,
constraint=self.b_constraint)
 else:
self.b = None
self.built = True
def compute_mask(self, input, input_mask=None):
return None
def call(self, x, mask=None):
features_dim = self.features_dim
step_dim = self.step_dim
eij = K.reshape(K.dot(K.reshape(x,(-1, features_dim)) ,
K.reshape(self.W,(features_dim, 1))),(-1, step_dim))
if self.bias:
eij += self.b
eij = K.tanh(eij)
 a = K.exp(eij)
 if mask is not None:
a *= K.cast(mask, K.floatx())
 a /= K.cast(K.sum(a, axis=1, keepdims=True)+ K.epsilon() , K.floatx())
 a = K.expand_dims(a)
 weighted_input = x * a
return K.sum(weighted_input, axis=1)
 def compute_output_shape(self, input_shape):
return input_shape[0], self.features_dim
class CyclicLR(Callback):

 def __init__(self, base_lr=0.001, max_lr=0.006, step_size=2000., mode='triangular',
gamma=1., scale_fn=None, scale_mode='cycle'):
super(CyclicLR, self ).__init__()
self.base_lr = base_lr
self.max_lr = max_lr
self.step_size = step_size
self.mode = mode
self.gamma = gamma
if scale_fn == None:
if self.mode == 'triangular':
self.scale_fn = lambda x: 1.
 self.scale_mode = 'cycle'
elif self.mode == 'triangular2':
self.scale_fn = lambda x: 1/(2.**(x-1))
self.scale_mode = 'cycle'
elif self.mode == 'exp_range':
self.scale_fn = lambda x: gamma**(x)
 self.scale_mode = 'iterations'
else:
self.scale_fn = scale_fn
self.scale_mode = scale_mode
self.clr_iterations = 0.
 self.trn_iterations = 0.
 self.history = {}
self._reset()
def _reset(self, new_base_lr=None, new_max_lr=None,
new_step_size=None):

 if new_base_lr != None:
self.base_lr = new_base_lr
if new_max_lr != None:
self.max_lr = new_max_lr
if new_step_size != None:
self.step_size = new_step_size
self.clr_iterations = 0.
 def clr(self):
cycle = np.floor(1+self.clr_iterations/(2*self.step_size))
x = np.abs(self.clr_iterations/self.step_size - 2*cycle + 1)
 if self.scale_mode == 'cycle':
return self.base_lr +(self.max_lr-self.base_lr)*np.maximum(0,(1-x)) *self.scale_fn(cycle)
 else:
return self.base_lr +(self.max_lr-self.base_lr)*np.maximum(0,(1-x)) *self.scale_fn(self.clr_iterations)
 def on_train_begin(self, logs={}):
logs = logs or {}
if self.clr_iterations == 0:
K.set_value(self.model.optimizer.lr, self.base_lr)
 else:
K.set_value(self.model.optimizer.lr, self.clr())
 def on_batch_end(self, epoch, logs=None):
logs = logs or {}
self.trn_iterations += 1
self.clr_iterations += 1
self.history.setdefault('lr', [] ).append(K.get_value(self.model.optimizer.lr))
self.history.setdefault('iterations', [] ).append(self.trn_iterations)
 for k, v in logs.items() :
self.history.setdefault(k, [] ).append(v)
 K.set_value(self.model.optimizer.lr, self.clr())
 def f1(y_true, y_pred):

 def recall(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
def precision(y_true, y_pred):

 true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 return precision
precision = precision(y_true, y_pred)
 recall = recall(y_true, y_pred)
 return 2*(( precision*recall)/(precision+recall+K.epsilon())) 
<split>
PATH ='/kaggle/input/covid19-global-forecasting-week-4'
trainingdata = pd.read_csv(f'{PATH}/train.csv')
 testdata = pd.read_csv(f'{PATH}/test.csv' )
DATA_SPLIT_SEED = 2018
NUM_SPLITS = 4
clr = CyclicLR(base_lr=0.001, max_lr=0.002,
step_size=300., mode='exp_range',
gamma=0.99994)
 train_meta = np.zeros(train_y.shape)
 test_meta = np.zeros(test_X.shape[0])
 splits = list(StratifiedKFold(n_splits=NUM_SPLITS, shuffle=True, random_state=DATA_SPLIT_SEED ).split(train_X, train_y))
features_split = list(StratifiedKFold(n_splits=NUM_SPLITS, shuffle=True, random_state=DATA_SPLIT_SEED ).split(x_features_train, train_y))<prepare_x_and_y>
train = trainingdata
test = testdata
for(( train_idx, valid_idx),(train_f_idx, valid_f_idx)) in zip(splits, features_split):
print(train_idx)
 print(valid_idx)
 print(train_f_idx)
 print(valid_f_idx)
 X_train = train_X[train_idx]
y_train = train_y[train_idx]
X_val = train_X[valid_idx]
y_val = train_y[valid_idx]
Xfeaturestrain = np.array(x_features_train)[train_f_idx]
Xfeaturesval = np.array(x_features_train)[valid_f_idx]
model = model_gru_cap(embedding_matrix)
 pred_val_y, pred_test_y, best_score = train_pred(model, X_train, y_train, X_val, y_val, Xfeaturestrain, Xfeaturesval, epochs = 6, callback = [clr,])
 print(pred_val_y.shape)
 print(pred_test_y.shape)
 train_meta[valid_idx] = pred_val_y.reshape(-1)
 test_meta += pred_test_y.reshape(-1)/ len(splits )<compute_test_metric>
trainingdate = pd.to_datetime(trainingdata["Date"])
 testdate = pd.to_datetime(testdata["Date"])
 print(trainingdate )
threshold = threshold_search(train_y, train_meta)
 threshold<compute_test_metric>
m = []
d = []
for i in range(0,ltrainingdate):
dx =(trainingdate[i].strftime("%d"))
mx =(trainingdate[i].strftime("%m"))
m.append(int(mx))
d.append(int(dx))
mt = []
dt = []
for i in range(0,ltestdate):
dtx =(testdate[i].strftime("%d"))
mtx =(testdate[i].strftime("%m"))
mt.append(int(mtx))
dt.append(int(dtx))
f1_score(y_true=train_y, y_pred=train_meta > threshold["threshold"] )<save_to_csv>
m = []
d = []
for i in range(0,ltrainingdate):
dx =(trainingdate[i].strftime("%d"))
mx =(trainingdate[i].strftime("%m"))
m.append(int(mx))
d.append(int(dx))
mt = []
dt = []
for i in range(0,ltestdate):
dtx =(testdate[i].strftime("%d"))
mtx =(testdate[i].strftime("%m"))
mt.append(int(mtx))
dt.append(int(dtx))
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub.prediction =(test_meta > threshold["threshold"] ).astype(int)
 sub.to_csv("submission.csv", index=False )<set_options>
train.insert(6,"Month",m,False)
 train.insert(7,"Day",d,False)
 test.insert(4,"Month",mt,False)
 test.insert(5,"Day",dt,False )
%matplotlib inline
warnings.filterwarnings("ignore")
<define_variables>
print("Training Data")
 traindays = trainingdata['Date'].nunique()
print("Number of Country_Region: ", trainingdata['Country_Region'].nunique())
 print("Number of Province_State: ", trainingdata['Province_State'].nunique())
 print("Number of Days: ", traindays)
 notrain = trainingdata['Id'].nunique()
print("Number of datapoints in train:", notrain)
 lotrain = int(notrain/traindays)
 print("L Trains:", lotrain)
fold = 1; head_num = 4; head_run = 3
target = 'sirna_ct'
resolution = 256
dropout_rate = 0.4
weight_decay = 2e-4
channel_size = 200
margin = 0.45
learning_rate = 0.035
warmup_learning_rate = learning_rate/100
focal_gamma = 3.5
remove_head = False
train_to = -18
freeze_at_1st_n_last = False
device = 'cuda' if torch.cuda.is_available() else 'cpu'
use_amp = True if device == 'cuda' else False
training = True if device == 'cuda' else False
train_batch_size = 96 if device == 'cuda' else 16
eval_batch_size = 16 if device == 'cuda' else 8
debug = False
create_sub =(device == 'cuda' or debug)
 print(f'Interactive:{is_interactive() } Debug:{debug} Device:{device} Training:{training}' )<load_from_csv>
print("Test Data")
 testdays = testdata['Date'].nunique()
print("Number of Days: ", testdays)
 notest = testdata['ForecastId'].nunique()
print("Number of datapoints in test:", notest)
 lotest = int(notest/testdays)
 print("L Test:", lotest)
path_data = '/kaggle/input/recursion-cellular-image-classification'
model_path = glob.glob(f'/kaggle/input/*/fold{fold}*.pth')[0]
best_sub = glob.glob(f'/kaggle/input/*/submission.csv')[0]
print(f'Training from model {model_path}')
 print(f'with pseudo labels from {best_sub}' )<define_variables>
zt = testdate[0]
daycount = []
for i in range(0,lotrain):
for j in range(1,traindays+1):
daycount.append(j)
seed_everything(42000 + head_num*100 + head_run*10 + fold )<count_values>
for i in range(traindays):
if(zt == trainingdate[i]):
zx = i
print(zx)
 daytest = []
for i in range(0,lotest):
for j in range(1,testdays+1):
jr = zx + j
daytest.append(jr)
print(torch.__version__, ignite.__version__, os.cpu_count())
 if device == 'cuda': print(torch.cuda.get_device_name() )<install_modules>
train.insert(8,"DayCount",daycount,False)
 test.insert(6,"DayCount",daytest,False )
if use_amp and training:
try:
except ImportError:
!pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext".. /input/*/*/NVIDIA-apex* 
<normalization>
traincount = int(len(train["Date"]))
testcount = int(len(test["Date"]))
class Swish(nn.Module):
def forward(self, x):
return x * torch.sigmoid(x)
 class Flatten(nn.Module):
def forward(self, x):
return x.reshape(x.shape[0], -1 )<define_search_model>
train.Province_State = train.Province_State.fillna(0)
 empty = 0
for i in range(0,traincount):
if(train.Province_State[i] == empty):
train.Province_State[i] = train.Country_Region[i]
class SqueezeExcitation(nn.Module):
def __init__(self, inplanes, se_planes):
super(SqueezeExcitation, self ).__init__()
self.reduce_expand = nn.Sequential(
 nn.Conv2d(inplanes, se_planes,
kernel_size=1, stride=1, padding=0, bias=True),
Swish() ,
nn.Conv2d(se_planes, inplanes,
kernel_size=1, stride=1, padding=0, bias=True),
nn.Sigmoid() 
)
 def forward(self, x):
x_se = torch.mean(x, dim=(-2, -1), keepdim=True)
 x_se = self.reduce_expand(x_se)
 return x_se * x 
<choose_model_class>
test.Province_State = test.Province_State.fillna(0)
 empty = 0
for i in range(0,testcount):
if(test.Province_State[i] == empty):
test.Province_State[i] = test.Country_Region[i]
class MBConv(nn.Module):
def __init__(self, inplanes, planes, kernel_size, stride,
expand_rate=1.0, se_rate=0.25,
drop_connect_rate=0.2):
super(MBConv, self ).__init__()
expand_planes = int(inplanes * expand_rate)
 se_planes = max(1, int(inplanes * se_rate))
self.expansion_conv = None
if expand_rate > 1.0:
self.expansion_conv = nn.Sequential(
 nn.Conv2d(inplanes, expand_planes,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(expand_planes, momentum=0.01, eps=1e-3),
Swish() 
)
 inplanes = expand_planes
self.depthwise_conv = nn.Sequential(
 nn.Conv2d(inplanes, expand_planes,
kernel_size=kernel_size, stride=stride,
padding=kernel_size // 2, groups=expand_planes,
bias=False),
nn.BatchNorm2d(expand_planes, momentum=0.01, eps=1e-3),
Swish() 
)
 self.squeeze_excitation = SqueezeExcitation(expand_planes, se_planes)
 self.project_conv = nn.Sequential(
 nn.Conv2d(expand_planes, planes,
kernel_size=1, stride=1, padding=0, bias=False),
nn.BatchNorm2d(planes, momentum=0.01, eps=1e-3), 
)
 self.with_skip = stride == 1
self.drop_connect_rate = torch.tensor(drop_connect_rate, requires_grad=False)
 def _drop_connect(self, x):
keep_prob = 1.0 - self.drop_connect_rate
drop_mask = torch.rand(x.shape[0], 1, 1, 1)+ keep_prob
drop_mask = drop_mask.type_as(x)
 drop_mask.floor_()
return drop_mask * x / keep_prob
def forward(self, x):
z = x
if self.expansion_conv is not None:
x = self.expansion_conv(x)
 x = self.depthwise_conv(x)
 x = self.squeeze_excitation(x)
 x = self.project_conv(x)
 if x.shape == z.shape and self.with_skip:
if self.training and self.drop_connect_rate is not None:
self._drop_connect(x)
 x += z
return x<normalization>
label = preprocessing.LabelEncoder()
train.Country_Region = label.fit_transform(train.Country_Region)
 train.Province_State = label.fit_transform(train.Province_State )
def init_weights(module):
if isinstance(module, nn.Conv2d):
nn.init.kaiming_normal_(module.weight, a=0, mode='fan_out')
 elif isinstance(module, nn.Linear):
init_range = 1.0 / math.sqrt(module.weight.shape[1])
 nn.init.uniform_(module.weight, a=-init_range, b=init_range)
 class EfficientNet(nn.Module):
def _setup_repeats(self, num_repeats):
return int(math.ceil(self.depth_coefficient * num_repeats))
def _setup_channels(self, num_channels):
num_channels *= self.width_coefficient
new_num_channels = math.floor(num_channels / self.divisor + 0.5)* self.divisor
new_num_channels = max(self.divisor, new_num_channels)
 if new_num_channels < 0.9 * num_channels:
new_num_channels += self.divisor
return new_num_channels
def __init__(self, num_classes,
width_coefficient=1.0,
depth_coefficient=1.0,
se_rate=0.25,
dropout_rate=0.2,
drop_connect_rate=0.2):
super(EfficientNet, self ).__init__()
self.width_coefficient = width_coefficient
self.depth_coefficient = depth_coefficient
self.divisor = 8
list_channels = [32, 16, 24, 40, 80, 112, 192, 320, 1280]
list_channels = [self._setup_channels(c)for c in list_channels]
list_num_repeats = [1, 2, 2, 3, 3, 4, 1]
list_num_repeats = [self._setup_repeats(r)for r in list_num_repeats]
expand_rates = [1, 6, 6, 6, 6, 6, 6]
strides = [1, 2, 2, 2, 1, 2, 1]
kernel_sizes = [3, 3, 5, 3, 5, 5, 3]
self.stem = nn.Sequential(
 nn.Conv2d(3, list_channels[0], kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(list_channels[0], momentum=0.01, eps=1e-3),
Swish() 
)
 blocks = []
counter = 0
num_blocks = sum(list_num_repeats)
 for idx in range(7):
num_channels = list_channels[idx]
next_num_channels = list_channels[idx + 1]
num_repeats = list_num_repeats[idx]
expand_rate = expand_rates[idx]
kernel_size = kernel_sizes[idx]
stride = strides[idx]
drop_rate = drop_connect_rate * counter / num_blocks
name = "MBConv{}_{}".format(expand_rate, counter)
 blocks.append((
name,
MBConv(num_channels, next_num_channels,
kernel_size=kernel_size, stride=stride, expand_rate=expand_rate,
se_rate=se_rate, drop_connect_rate=drop_rate)
))
counter += 1
for i in range(1, num_repeats):
name = "MBConv{}_{}".format(expand_rate, counter)
 drop_rate = drop_connect_rate * counter / num_blocks
blocks.append((
name,
MBConv(next_num_channels, next_num_channels,
kernel_size=kernel_size, stride=1, expand_rate=expand_rate,
se_rate=se_rate, drop_connect_rate=drop_rate)
))
counter += 1
self.blocks = nn.Sequential(OrderedDict(blocks))
self.head = nn.Sequential(
 nn.Conv2d(list_channels[-2], list_channels[-1],
kernel_size=1, bias=False),
nn.BatchNorm2d(list_channels[-1], momentum=0.01, eps=1e-3),
Swish() ,
nn.AdaptiveAvgPool2d(1),
Flatten() ,
nn.Dropout(p=dropout_rate),
nn.Linear(list_channels[-1], num_classes)
)
 self.apply(init_weights)
 def forward(self, x):
f = self.stem(x)
 f = self.blocks(f)
 y = self.head(f)
 return y<import_modules>
test.Country_Region = label.fit_transform(test.Country_Region)
 test.Province_State = label.fit_transform(test.Province_State)
import torchvision.utils as vutils
import pandas as pd
from sklearn import preprocessing
from sklearn.utils import shuffle<categorify>
X = np.c_[train["Province_State"], train["Country_Region"], train["DayCount"], train["Month"], train["Day"]]
Xt = np.c_[test["Province_State"], test["Country_Region"], test["DayCount"], test["Month"], test["Day"]]
le = preprocessing.LabelEncoder()
le.fit_transform(['HEPG2', 'HUVEC', 'RPE', 'U2OS'] )<load_from_csv>
Y1 = train["ConfirmedCases"]
Y2 = train["Fatalities"]
df = pd.read_csv(path_data+'/train.csv')
 df['set'] = 'train'
df_test = pd.read_csv(path_data+'/test.csv')
 df_test['set'] = 'test'
best = pd.read_csv(best_sub, index_col=0)
 df_test_pseudo = df_test.set_index('id_code' ).join(best, how='inner' ).reset_index()
df_ctrl = pd.read_csv(path_data+'/train_controls.csv')
 df_ctrl['set'] = 'train'
df_ctrl_test = pd.read_csv(path_data+'/test_controls.csv')
 df_ctrl_test['set'] = 'test'
df_ctrl = pd.concat([df_ctrl, df_ctrl_test], ignore_index=False, sort=False)
 del df_ctrl_test
n_targets = df.sirna.nunique() + df_ctrl.sirna.nunique()
for d in [df, df_test, df_test_pseudo, df_ctrl]:
d['cell_type'] = d.experiment.apply(lambda s: s[:-3])
 d['cell_type_n'] = le.transform(d.cell_type)
 if d is not df_test:
d['sirna_ct'] = d.sirna +(d.cell_type_n * n_targets)
 n_classes = df.sirna_ct.nunique() + df_ctrl.sirna_ct.nunique()
print(n_targets, n_classes )<concatenate>
regr = XGBRegressor(n_estimators = 1500, gamma = 0, learning_rate = 0.8, random_state = 42, max_depth = 50, subsample = 1, reg_lambda = 0, reg_alpha = 0.5)
 regr1 = XGBRegressor(n_estimators = 1500, gamma = 0, learning_rate = 0.8, random_state = 42, max_depth = 50, subsample = 1, reg_lambda = 0, reg_alpha = 0.5 )
if training or not debug:
for label,n in target_dist.items() :
copies = min(int(round(max_n/n)- 1), 4)
 if copies != 0:
addon = [df_train[df_train[target] == label]] * copies
df_train = pd.concat([df_train] + addon, ignore_index=False, sort=False )<concatenate>
def rmsle(y_true, y_pred):
return mean_squared_log_error(y_true, y_pred)**(1/2);
df_train = pd.concat([df_train, df_ctrl[(df_ctrl['cell_type_n'] == 4)]],
ignore_index=False, sort=False)
 df_train = pd.concat([df_train, df_ctrl[(
 df_ctrl['cell_type_n'].isin([0,2])) &((
df_ctrl['plate'] != fold)|(
 df_ctrl.experiment.isin(df_test.experiment.unique())| df_ctrl.experiment.isin(valid_experiments)))]],
ignore_index=False, sort=False)
 df_train = pd.concat([df_train, df_ctrl[(
 df_ctrl['cell_type_n'] == 1)&(
 df_ctrl['plate'] == fold)&(
 df_ctrl.experiment.isin(df_test.experiment.unique())| df_ctrl.experiment.isin(valid_experiments)) ]],
ignore_index=False, sort=False )<count_unique_values>
regr.fit(X,Y1.ravel())
 yscore = regr.predict(X)
 a = abs(yscore)
 b=abs(Y1)
 ascore =(rmsle(a,b))
print("RMSLE of Confirmed Cases is ",ascore )
if debug:
df_train = df_train[df_train[target].isin(df_train[target].sample(50)) ]
df_valid = df_valid[df_valid[target].isin(df_train[target].unique())]
print(df_train.shape, df_valid.shape, df_test.shape )<find_best_params>
ypred = regr.predict(Xt)
 ypred = pd.DataFrame({'ConfirmedCases' : ypred})
 ypred = round(ypred)
 ypred.head(20 )
class_weights = class_weights.values.ravel()<drop_column>
regr1.fit(X,Y2.ravel())
 ypred2 = regr1.predict(Xt)
 yptest = regr1.predict(X)
 yptest = np.round(yptest)
 c = abs(yptest)
 d = abs(Y2)
 ascore = rmsle(c,d)
 print("RMSLE of Fatalities is", ascore)
if not is_interactive() :
del df; del df_ctrl; del target_dist
gc.collect()<categorify>
ypred2 = pd.DataFrame({'Fatalities' : ypred2})
 ypred2 = round(ypred2)
 ypred2.head(20 )
class ExpNormTwinDataset(Dataset):

 def __init__(self, df, img_dir, target, mode='train', sites=[1,2], channels=[1,2,3,4,5,6],
img_stats=None, transform=None):
self.df = df
self.channels = channels
self.sites = sites
self.target = target
self.mode = mode
self.img_dir = img_dir
self.stats = img_stats
self.transform = transform
@staticmethod
def _load_channel(file_name):
img = cv2.imread(file_name, cv2.IMREAD_UNCHANGED)
 return np.float32(img)
 def _get_img(self, rec, site):
dset, experiment, well, plate = rec.set, rec.experiment, rec.well, rec.plate
paths = [os.path.join(self.img_dir, dset, experiment, f'Plate{plate}', f'{well}_s{site}_w{channel}.png')
 for channel in self.channels]
img = [self._load_channel(img_path)for img_path in paths]
if self.stats is not None:
stats = self.stats.loc[experiment, ['mean', 'std']]
img = [i-m for i,m in zip(img, stats['mean'].values)]
img = [i/s for i,s in zip(img, stats['std'].values)]
img = np.stack(img, axis=-1)
 if self.transform:
img = self.transform(image=img)['image']
return img
def _get_twin(self, rec, site):
experiment, target = rec.experiment, rec[self.target]
twin = self.df[(self.df[self.target] == target)&(self.df.experiment != experiment)].sample(1 ).iloc[0]
return self._get_img(twin, site)
 def __getitem__(self, index):
rec = self.df.iloc[index]
if self.mode == 'train':
img = self._get_img(rec, random.choice(self.sites))
twin = self._get_twin(rec, random.choice(self.sites))
return img, twin, rec[self.target]
elif self.mode == 'eval':
img = self._get_img(rec, random.choice(self.sites))
return img, rec[self.target]
elif self.mode == 'test':
return [self._get_img(rec, site)for site in self.sites]
def __len__(self):

 return len(self.df)
<import_modules>
ypc = pd.DataFrame()
forecast = test["ForecastId"]
from albumentations import *
from albumentations.pytorch import ToTensor<load_pretrained>
ypc.insert(0,"ForecastId",forecast,False)
 ypc.insert(1,"ConfirmedCases",ypred,False)
 ypc.insert(2, "Fatalities",ypred2,False )
<set_options><EOS> 
ypc.to_csv('submission.csv', index=False )