Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<load_from_csv>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
%matplotlib inline
train_data = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 test_data = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 submission_csv = pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )<data_type_conversions>
package_path = '.. /input/efficientnet/efficientnet-pytorch/EfficientNet-PyTorch/'
sys.path.append(package_path)
convert_dict = {'Province_State': str,'Country_Region':str,'ConfirmedCases':int,'Fatalities':int}
convert_dict_test = {'Province_State': str,'Country_Region':str}
train_data = train_data.astype(convert_dict)
 test_data = test_data.astype(convert_dict_test )<data_type_conversions>
num_classes = 1
md_ef = EfficientNet.from_name('efficientnet-b5')
 md_ef.load_state_dict(torch.load('.. /input/efficientnet-pytorch/efficientnet-b5-586e6cc6.pth'))
in_features = md_ef._fc.in_features
md_ef._fc = nn.Linear(in_features, num_classes)
 md_ef.cuda()
train_data['Date'] = pd.to_datetime(train_data['Date'], infer_datetime_format=True)
 test_data['Date'] = pd.to_datetime(test_data['Date'], infer_datetime_format=True )<data_type_conversions>
def get_df() :
base_image_dir = os.path.join('.. ', 'input/aptos2019-blindness-detection/')
 train_dir = os.path.join(base_image_dir,'train_images/')
 df = pd.read_csv(os.path.join(base_image_dir, 'train.csv'))
df['path'] = df['id_code'].map(lambda x: os.path.join(train_dir,'{}.png'.format(x)))
 df = df.drop(columns=['id_code'])
 df = df.sample(frac=1 ).reset_index(drop=True)
 test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 return df, test_df
df, test_df = get_df()
train_data.loc[:, 'Date'] = train_data.Date.dt.strftime('%m%d')
 train_data.loc[:, 'Date'] = train_data['Date'].astype(int)
 test_data.loc[:, 'Date'] = test_data.Date.dt.strftime('%m%d')
 test_data.loc[:, 'Date'] = test_data['Date'].astype(int )<feature_engineering>
bs = 64
sz = 224
tfms = get_transforms(do_flip=True,flip_vert=True )
train_data['Country_Region'] = np.where(train_data['Province_State'] == 'nan',train_data['Country_Region'],train_data['Province_State']+' '+train_data['Country_Region'])
 test_data['Country_Region'] = np.where(test_data['Province_State'] == 'nan',test_data['Country_Region'],test_data['Province_State']+' '+test_data['Country_Region'])
<drop_column>
def qk(y_pred, y):
return torch.tensor(cohen_kappa_score(torch.round(y_pred), y, weights='quadratic'), device='cuda:0' )
train_data = train_data.drop(columns=['Province_State'])
 test_data = test_data.drop(columns=['Province_State'] )<define_variables>
learn = Learner(data,
md_ef,
metrics = [qk],
model_dir="models" ).to_fp16()
learn.data.add_test(ImageList.from_df(test_df,
'.. /input/aptos2019-blindness-detection',
folder='test_images',
suffix='.png'))
s =(train_data.dtypes == 'object')
 object_cols = list(s[s].index )<import_modules>
learn.load('abcdef');
from sklearn.preprocessing import LabelEncoder<categorify>
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = metrics.cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 print(-loss_partial(self.coef_['x']))
def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']
label_encoder1 = LabelEncoder()
label_encoder2 = LabelEncoder()
train_data['Country_Region'] = label_encoder2.fit_transform(train_data['Country_Region'])
 test_data['Country_Region'] = label_encoder2.transform(test_data['Country_Region'])
<define_variables>
def run_subm(learn=learn, coefficients=[0.5, 1.5, 2.5, 3.5]):
opt = OptimizedRounder()
preds,y = learn.get_preds(DatasetType.Test)
 tst_pred = opt.predict(preds, coefficients)
 tst_pred = tst_pred.astype(int)
 return tst_pred
Test_id = test_data.ForecastId<drop_column>
preds1 = run_subm()
train_data.drop(['Id'], axis=1, inplace=True)
 test_data.drop('ForecastId', axis=1, inplace=True )<count_missing_values>
package_dir = ".. /input/pretrained-models/pretrained-models/pretrained-models.pytorch-master/"
sys.path.insert(0, package_dir)
 device = torch.device("cuda:0")
 ImageFile.LOAD_TRUNCATED_IMAGES = True
missing_val_count_by_column =(train_data.isnull().sum())
 print(missing_val_count_by_column[missing_val_count_by_column>0] )<import_modules>
class RetinopathyDatasetTest(Dataset):
def __init__(self, csv_file, transform):
self.data = pd.read_csv(csv_file)
 self.transform = transform
def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
img_name = os.path.join('.. /input/aptos2019-blindness-detection/test_images', self.data.loc[idx, 'id_code'] + '.png')
 image = Image.open(img_name)
 image = self.transform(image)
 return {'image': image}
from xgboost import XGBRegressor<prepare_x_and_y>
model = pretrainedmodels.__dict__['resnet101'](pretrained=None)
 model.avg_pool = nn.AdaptiveAvgPool2d(1)
 model.last_linear = nn.Sequential(
 nn.BatchNorm1d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True),
nn.Dropout(p=0.25),
nn.Linear(in_features=2048, out_features=2048, bias=True),
nn.ReLU() ,
nn.BatchNorm1d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True),
nn.Dropout(p=0.5),
nn.Linear(in_features=2048, out_features=1, bias=True), 
)
 model.load_state_dict(torch.load(".. /input/mmmodel/model.bin"))
model = model.to(device )
X_train = train_data[['Country_Region','Date']]
y_train = train_data[['ConfirmedCases', 'Fatalities']]<split>
for param in model.parameters() :
param.requires_grad = False
model.eval()
x_train = X_train.iloc[:,:].values
x_test = test_data.iloc[:,:].values<split>
test_transform = transforms.Compose([
transforms.Resize(( 224, 224)) ,
transforms.RandomHorizontalFlip() ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 test_dataset = RetinopathyDatasetTest(csv_file='.. /input/aptos2019-blindness-detection/sample_submission.csv',
transform=test_transform )
<train_on_grid>
def tta(test_dataset, batch_size=32, shuffle=False, num_workers=4):
test_data_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers)
 test_preds = np.zeros(( len(test_dataset), 1))
tk = tqdm(test_data_loader)
 for i, x_batch in enumerate(tk):
x_batch = x_batch['image']
pred = model(x_batch.to(device))
test_preds[i*32:(i+1)*32] = pred.detach().cpu().squeeze().numpy().ravel().reshape(-1, 1)
 return test_preds
error_list = []
def return_error(estimator, x_train,x_test,y_train):
model = MultiOutputRegressor(XGBRegressor(n_estimators=estimator, random_state=0, max_depth=20))
model.fit(x_train, y_train)
 predict = MultiOutputRegressor(model.predict(x_test))
return predict<compute_test_metric>
for i in range(1, 6):
locals() ['test_preds{}'.format(i)] = tta(test_dataset )
predict = return_error(1500,x_train,x_test,y_train )<save_to_csv>
preds2 =(test_preds1 + test_preds2 + test_preds3 + test_preds4 + test_preds5)/ 5.0
df_sub = pd.DataFrame()
df_sub['ForecastId'] = Test_id
df_sub['ConfirmedCases'] =(predict.estimator[:,0] ).astype(int)
 df_sub['Fatalities'] =(predict.estimator[:,1] ).astype(int)
 df_sub.to_csv('submission.csv', index=False )<import_modules>
coef = [0.5, 1.5, 2.5, 3.5]
for i, pred in enumerate(preds2):
if pred < coef[0]:
preds2[i] = 0
elif pred >= coef[0] and pred < coef[1]:
preds2[i] = 1
elif pred >= coef[1] and pred < coef[2]:
preds2[i] = 2
elif pred >= coef[2] and pred < coef[3]:
preds2[i] = 3
else:
preds2[i] = 4
import copy
import os
from collections import OrderedDict
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
from matplotlib import pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from torch.optim import AdamW
from torch.optim.lr_scheduler import ExponentialLR, ReduceLROnPlateau
from torch.utils.data import DataLoader, Dataset
from tqdm.notebook import tqdm<define_variables>
import cv2
import matplotlib.pyplot as plt
from os.path import isfile
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
import os
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms
import time
from tqdm import tqdm
import random
import sys
main_data_dir = ".. /input/covid19-global-forecasting-week-4"
metadata_dir = ".. /input/covid19-countrywise-metadata"<load_from_csv>
package_path = '.. /input/efficientnet/efficientnet-pytorch/EfficientNet-PyTorch/'
sys.path.append(package_path )
data_train = pd.read_csv(os.path.join(main_data_dir, "train.csv"))
data_test = pd.read_csv(os.path.join(main_data_dir, "test.csv"))
sample_sub = pd.read_csv(os.path.join(main_data_dir, "submission.csv"))
metadata = pd.read_csv(os.path.join(metadata_dir, "covid19_countrywise_metadata.csv"))<feature_engineering>
from efficientnet_pytorch import EfficientNet
def generate_loc(x):
if isinstance(x["Province_State"], float):
return x["Country_Region"]
else:
return "_".join([x["Country_Region"], x["Province_State"]] )<define_variables>
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
def generate_loc_date(x):
return "_".join([x["location"], x["Date"]] )<feature_engineering>
seed_everything(1234)
 TTA = 5
num_classes = 1
IMG_SIZE = 384
data_train["location"] = data_train.apply(generate_loc, axis=1)
 data_test["location"] = data_test.apply(generate_loc, axis=1 )<feature_engineering>
test = '.. /input/aptos2019-blindness-detection/test_images/'
data_train["log_cfm"] = data_train["ConfirmedCases"].map(np.log1p)
 data_train["log_ftl"] = data_train["Fatalities"].map(np.log1p )<feature_engineering>
def expand_path(p):
p = str(p)
 if isfile(test + p + ".png"):
return test +(p + ".png")
 return p
data_train["loc_date"] = data_train.apply(generate_loc_date, axis=1)
 data_test["loc_date"] = data_test.apply(generate_loc_date, axis=1 )<groupby>
test_transform = transforms.Compose([
transforms.RandomHorizontalFlip() ,
transforms.RandomRotation(( -120, 120)) ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
 testset = MyDataset(pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv'),
transform=test_transform)
 test_loader = torch.utils.data.DataLoader(testset, batch_size=16, shuffle=False )
data_train_by_loc = data_train.groupby("location" )<data_type_conversions>
model = EfficientNet.from_name('efficientnet-b0')
 in_features = model._fc.in_features
model._fc = nn.Linear(in_features, num_classes)
 model.load_state_dict(torch.load('.. /input/enet0802/weight_best_0802.pt'))
model.cuda()
data_train["Date"] = pd.to_datetime(data_train["Date"])
 data_test["Date"] = pd.to_datetime(data_test["Date"] )<feature_engineering>
for param in model.parameters() :
param.requires_grad = False
data_train["rel_date"] = data_train.apply(lambda x:(x["Date"] - data_train["Date"].min()), axis=1 ).dt.days
data_test["rel_date"] = data_test.apply(lambda x:(x["Date"] - data_train["Date"].min()), axis=1 ).dt.days<feature_engineering>
sample = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv' )
data_train["rel_date_pct"] = data_train.rel_date / data_test.rel_date.max()
data_test["rel_date_pct"] = data_test.rel_date / data_test.rel_date.max()<define_variables>
%%time
test_pred = np.zeros(( len(sample), 1))
model.eval()
for _ in range(TTA):
with torch.no_grad() :
for i, data in tqdm(enumerate(test_loader)) :
images, _ = data
images = images.cuda()
pred = model(images)
 test_pred[i * 16:(i + 1)* 16] += pred.detach().cpu().squeeze().numpy().reshape(-1, 1)
 output = test_pred / TTA
def get_since_day_zero(row, day_zero):
cur_date = row["rel_date"]
cur_loc = row["location"]
cur_day_zero = day_zero[cur_loc]
return max(cur_date - cur_day_zero, -1 )<feature_engineering>
coef = [0.57, 1.37, 2.57, 3.57]
for i, pred in enumerate(output):
if pred < coef[0]:
output[i] = 0
elif pred >= coef[0] and pred < coef[1]:
output[i] = 1
elif pred >= coef[1] and pred < coef[2]:
output[i] = 2
elif pred >= coef[2] and pred < coef[3]:
output[i] = 3
else:
output[i] = 4
preds3 = output
day_zero_location = get_day_zero(data_train)
 data_train["since_day_zero"] = data_train.apply(get_since_day_zero, axis=1, day_zero=day_zero_location)
 data_test["since_day_zero"] = data_test.apply(get_since_day_zero, axis=1, day_zero=day_zero_location )<feature_engineering>
BatchNormalization, Input, Conv2D, GlobalAveragePooling2D,concatenate,Concatenate)
 print(os.listdir(".. /input"))
WORKERS = 2
CHANNEL = 3
warnings.filterwarnings("ignore")
 SIZE = 300
NUM_CLASSES = 5
data_train["since_day_zero"] = data_train["since_day_zero"].map(lambda x: -0.1 if x < 0 else x / data_test.since_day_zero.max())
 data_test["since_day_zero"] = data_test["since_day_zero"].map(lambda x: -0.1 if x < 0 else x / data_test.since_day_zero.max() )<data_type_conversions>
df_test = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv' )
lockdown_cols = ["quarantine", "close_school", "close_public_place", "limit_gathering", "stay_home"]
metadata[lockdown_cols] = metadata[lockdown_cols].fillna("2099-12-31")
 for col in lockdown_cols:
metadata[col] = pd.to_datetime(metadata[col], format="%Y-%m-%d" )<data_type_conversions>
def create_model(input_shape, n_out):
input_tensor = Input(shape=input_shape)
 base_model = DenseNet121(include_top=False,
weights=None,
input_tensor=input_tensor)
 base_model.load_weights(".. /input/densenet-keras/DenseNet-BC-121-32-no-top.h5")
 x = GlobalAveragePooling2D()(base_model.output)
 x = Dropout(0.5 )(x)
 x = Dense(1024, activation='relu' )(x)
 x = Dropout(0.5 )(x)
 final_output = Dense(n_out, activation='softmax', name='final_output' )(x)
 model = Model(input_tensor, final_output)
 return model
def add_lockdown_data(df, metadata):
lockdown_cols = ["quarantine", "close_school", "close_public_place", "limit_gathering", "stay_home"]
lockdown_df = pd.DataFrame(np.zeros(( len(df), len(lockdown_cols))), columns=lockdown_cols)
 for i in range(len(df)) :
cur_date = df.Date[i]
cur_loc = df.location[i]
idx = metadata.loc[metadata.location == cur_loc, :].index.values[0]
lockdown_df.loc[i, :] =(metadata.loc[idx, lockdown_cols] <= cur_date ).astype(int)
 return pd.concat([df, lockdown_df], axis=1 )<load_pretrained>
submit = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 model = load_model('.. /input/densenet_v2_0817_2/densenet_v2_0817_2.h5')
 preds4 = []
data_train = add_lockdown_data(data_train, metadata)
 data_test = add_lockdown_data(data_test, metadata )<drop_column>
%reload_ext autoreload
%autoreload 2
%matplotlib inline
metadata.drop(lockdown_cols, axis=1, inplace=True )<normalization>
from fastai import *
from fastai.vision import *
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import os
import scipy as sp
from functools import partial
from sklearn import metrics
from collections import Counter
from fastai.callbacks import *
import PIL
import cv2
def min_max_normalize(data):
scaler = MinMaxScaler()
return scaler.fit_transform(data )<normalization>
def seed_everything(seed=1358):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()
num_cols = metadata.columns[3:]
metadata[num_cols] = min_max_normalize(metadata[num_cols] )<drop_column>
PATH = Path('.. /input/aptos2019-blindness-detection')
 df = pd.read_csv(PATH/'train.csv' )
metadata.drop(["region", "country"], axis=1, inplace=True )<rename_columns>
tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=0.10, max_zoom=1.3, max_warp=0.0, max_lighting=0.2 )
metadata.set_index("location", inplace=True )<feature_engineering>
class OptimizedRounder(object):
def __init__(self):
self.coef_ = 0
def _kappa_loss(self, coef, X, y):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
ll = metrics.cohen_kappa_score(y, X_p, weights='quadratic')
 return -ll
def fit(self, X, y):
loss_partial = partial(self._kappa_loss, X=X, y=y)
 initial_coef = [0.5, 1.5, 2.5, 3.5]
self.coef_ = sp.optimize.minimize(loss_partial, initial_coef, method='nelder-mead')
 print(-loss_partial(self.coef_['x']))
def predict(self, X, coef):
X_p = np.copy(X)
 for i, pred in enumerate(X_p):
if pred < coef[0]:
X_p[i] = 0
elif pred >= coef[0] and pred < coef[1]:
X_p[i] = 1
elif pred >= coef[1] and pred < coef[2]:
X_p[i] = 2
elif pred >= coef[2] and pred < coef[3]:
X_p[i] = 3
else:
X_p[i] = 4
return X_p
def coefficients(self):
return self.coef_['x']
data_test["log_cfm"] = 0
data_test["log_ftl"] = 0<sort_values>
sample_df = pd.read_csv(PATH/'sample_submission.csv')
 sample_df.head()
data_test_known = data_test.loc[data_test.rel_date <= data_train.rel_date.max() , :].sort_values(["rel_date", "location"] ).reset_index(drop=True)
 data_test_unknown = data_test.loc[data_test.rel_date > data_train.rel_date.max() , :].sort_values(["rel_date", "location"] ).reset_index(drop=True)
 data_train.sort_values(["rel_date", "location"], inplace=True )<feature_engineering>
def quadratic_kappa(y_hat, y):
return torch.tensor(cohen_kappa_score(torch.round(y_hat), y, weights='quadratic'),device='cuda:0')
 data =(
 src.transform(tfms,size=224)
.databunch() 
.normalize(imagenet_stats)
)
 learn = cnn_learner(data, base_arch=models.resnet50 ,metrics=[quadratic_kappa],model_dir='/kaggle',pretrained=True)
 learn.data.add_test(ImageList.from_df(sample_df,PATH,folder='test_images',suffix='.png'))
learn.load('resnet50_0823' )
data_test_known[["log_cfm", "log_ftl"]] = data_train.loc[data_train.rel_date >= data_test.rel_date.min() , ["log_cfm", "log_ftl"]].values<concatenate>
preds,y = learn.get_preds(DatasetType.Test )
data_test_input = pd.concat([data_test_known, data_test_unknown], axis=0)
 input_len = 30
data_test_input = pd.concat([data_train.loc[(data_train.rel_date >= data_test_unknown.rel_date.min() - input_len)\
&(data_train.rel_date < data_test_known.rel_date.min()), data_test_input.columns],
data_test_input], axis=0 ).sort_values(["rel_date", "location"] ).reset_index(drop=True )<categorify>
optR = OptimizedRounder()
coefficients = [0.563203, 1.400153, 2.43412, 3.439786]
test_predictions = optR.predict(preds, coefficients )
class TrainDataset(Dataset):
def __init__(self, df, metadata, x_cols, y_cols, input_len=30):
self.df = df
self.metadata = metadata
self.x_cols = x_cols
self.y_cols = y_cols
self.input_len = input_len
self.loc_list = list(self.df.location.unique())
 self.num_pos_train_period = self.df.rel_date.max() - self.input_len + 1
def __len__(self):
return len(self.loc_list)* self.num_pos_train_period
def __getitem__(self, idx):
cur_loc = self.loc_list[idx // self.num_pos_train_period]
input_beg = idx % self.num_pos_train_period
input_end = input_beg + self.input_len
in_input_period =(self.df.rel_date >= input_beg)&(self.df.rel_date < input_end)
 is_cur_loc = self.df.location == cur_loc
inputs = self.df.loc[(is_cur_loc)&(in_input_period), self.x_cols].values
meta_inputs = self.metadata.loc[cur_loc, :].values
targets = self.df.loc[(is_cur_loc)&(self.df.rel_date == input_end), self.y_cols].values.reshape(( -1))
return torch.tensor(inputs, dtype=torch.float32),\
torch.tensor(meta_inputs, dtype=torch.float32),\
torch.tensor(targets, dtype=torch.float32)
 @staticmethod
def get_dataloader(dataset, batch_size, shuffle=True):
return DataLoader(dataset, batch_size=batch_size, shuffle=shuffle )<split>
preds5 = test_predictions.astype(int )
rand_seed = 42
loc_list = data_train.location.unique()
loc_train, loc_val = train_test_split(loc_list, test_size=0.2, random_state=rand_seed)
 data_train.set_index("location", inplace=True, drop=True)
 data_val = data_train.loc[loc_val, :]
data_train = data_train.loc[loc_train, :]
data_train.reset_index(drop=False, inplace=True)
 data_val.reset_index(drop=False, inplace=True )<prepare_x_and_y>
preds1 = preds1.flatten()
preds2 = preds2.flatten()
preds3 = preds3.flatten()
preds4 = preds4.flatten()
preds5 = preds5.flatten()
x_cols = ["log_cfm", "log_ftl", "rel_date_pct", "since_day_zero"] + lockdown_cols
y_cols = ["log_cfm", "log_ftl"]
batch_size = 512
d_in = len(x_cols)
 d_meta = len(metadata.columns)
 ds_train = TrainDataset(data_train, metadata, x_cols, y_cols, input_len)
 dl_train = TrainDataset.get_dataloader(ds_train, batch_size)
 ds_val = TrainDataset(data_val, metadata, x_cols, y_cols, input_len)
 dl_val = TrainDataset.get_dataloader(ds_val, batch_size, shuffle=False )<categorify>
score = [0.783, 0.750, 0.783, 0.741, 0.738]
weight = [0.23, 0.195, 0.23, 0.175, 0.170]
predsData = weight[0]*preds1 + weight[1]*preds2 + weight[2]*preds3 + weight[3]*preds4 + weight[4]*preds5
coef = [0.5, 1.5, 2.5, 3.5]
for i, pred in enumerate(predsData):
if pred < coef[0]:
predsData[i] = 0
elif pred >= coef[0] and pred < coef[1]:
predsData[i] = 1
elif pred >= coef[1] and pred < coef[2]:
predsData[i] = 2
elif pred >= coef[2] and pred < coef[3]:
predsData[i] = 3
else:
predsData[i] = 4
submission = pd.read_csv(".. /input/aptos2019-blindness-detection/sample_submission.csv")
 submission.diagnosis = predsData.astype(int)
 submission.to_csv('submission.csv', index=False )