Kaggle competition: OSIC Pulmonary Fibrosis Progression
<SOS> metric: LaplaceLogLikelihood Kaggle data source: osic-pulmonary-fibrosis-progression<define_variables>
!pip install.. /input/kerasapplications/keras-team-keras-applications-3b180cb -f./ --no-index
!pip install.. /input/efficientnet/efficientnet-1.1.0/ -f./ --no-index
num_classes = 5<choose_model_class>
def seed_everything(seed=2020):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 seed_everything(42 )
model = models.densenet201(pretrained=True)
 for param in model.parameters() :
param.requires_grad = False
model.classifier = nn.Sequential(
 nn.BatchNorm1d(1920),
nn.Dropout(p=0.5, inplace=False),
nn.Linear(in_features=1920, out_features=512),
nn.ReLU(inplace=True),
nn.BatchNorm1d(512),
nn.Dropout(p=0.5, inplace=False),
nn.Linear(in_features=512, out_features=num_classes),
nn.Linear(in_features=num_classes, out_features=1, bias=False),
Bias(num_classes - 1)
)
 model = model.cuda()<load_pretrained>
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config )
checkpoint = torch.load('.. /input/aptos-pytorch/checkpoint.tar')
 model.load_state_dict(checkpoint['model'])
<define_variables>
train = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/train.csv' )
tta_count = 1<find_best_params>
def get_tab(df):
vector = [(df.Age.values[0] - 30)/ 30]
if df.Sex.values[0] == 'male':
vector.append(0)
 else:
vector.append(1)
 if df.SmokingStatus.values[0] == 'Never smoked':
vector.extend([0,0])
 elif df.SmokingStatus.values[0] == 'Ex-smoker':
vector.extend([1,1])
 elif df.SmokingStatus.values[0] == 'Currently smokes':
vector.extend([0,1])
 else:
vector.extend([1,0])
 return np.array(vector )
diagnoses = defaultdict(list)
 with torch.no_grad() :
for i in tqdm(range(tta_count)) :
id_code = []
diagnosis = []
for inputs, id_codes in tqdm(te_dl):
inputs = inputs.cuda()
preds = model(inputs ).tolist()
diagnosis.extend(preds)
 id_code.extend(id_codes)
 for id_c, diag in zip(id_code, diagnosis):
diagnoses[id_c].append(diag )<categorify>
A = {}
TAB = {}
P = []
for i, p in tqdm(enumerate(train.Patient.unique())) :
sub = train.loc[train.Patient == p, :]
fvc = sub.FVC.values
weeks = sub.Weeks.values
c = np.vstack([weeks, np.ones(len(weeks)) ] ).T
a, b = np.linalg.lstsq(c, fvc)[0]
A[p] = a
TAB[p] = get_tab(sub)
 P.append(p )
def coral_out_to_class(out):
out = torch.sigmoid(torch.tensor(out))
out = out > 0.5
return torch.sum(out, dim=1 )<categorify>
class IGenerator(Sequence):
BAD_ID = ['ID00011637202177653955184', 'ID00052637202186188008618']
def __init__(self, keys, a, tab, batch_size=32):
self.keys = [k for k in keys if k not in self.BAD_ID]
self.a = a
self.tab = tab
self.batch_size = batch_size
self.train_data = {}
for p in train.Patient.values:
self.train_data[p] = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/')
 def __len__(self):
return 1000
def __getitem__(self, idx):
x = []
a, tab = [], []
keys = np.random.choice(self.keys, size = self.batch_size)
 for k in keys:
try:
i = np.random.choice(self.train_data[k], size=1)[0]
img = get_img(f'.. /input/osic-pulmonary-fibrosis-progression/train/{k}/{i}')
 x.append(img)
 a.append(self.a[k])
 tab.append(self.tab[k])
 except:
print(k, i)
 x,a,tab = np.array(x), np.array(a), np.array(tab)
 x = np.expand_dims(x, axis=-1)
 return [x, tab] , a
id_code = []
diagnosis = []
for k, v in diagnoses.items() :
id_code.append(k)
 diagnosis.append(np.argmax(np.bincount(coral_out_to_class(v ).numpy())) )<save_to_csv>
Dense, Dropout, Activation, Flatten, Input, BatchNormalization, GlobalAveragePooling2D, Add, Conv2D, AveragePooling2D,
LeakyReLU, Concatenate 
)
 def get_efficientnet(model, shape):
models_dict = {
'b0': efn.EfficientNetB0(input_shape=shape,weights=None,include_top=False),
'b1': efn.EfficientNetB1(input_shape=shape,weights=None,include_top=False),
'b2': efn.EfficientNetB2(input_shape=shape,weights=None,include_top=False),
'b3': efn.EfficientNetB3(input_shape=shape,weights=None,include_top=False),
'b4': efn.EfficientNetB4(input_shape=shape,weights=None,include_top=False),
'b5': efn.EfficientNetB5(input_shape=shape,weights=None,include_top=False),
'b6': efn.EfficientNetB6(input_shape=shape,weights=None,include_top=False),
'b7': efn.EfficientNetB7(input_shape=shape,weights=None,include_top=False)
 }
return models_dict[model]
def build_model(shape=(512, 512, 1), model_class=None):
inp = Input(shape=shape)
 base = get_efficientnet(model_class, shape)
 x = base(inp)
 x = GlobalAveragePooling2D()(x)
 inp2 = Input(shape=(4,))
x2 = tf.keras.layers.GaussianNoise(0.2 )(inp2)
 x = Concatenate()([x, x2])
 x = Dropout(0.5 )(x)
 x = Dense(1 )(x)
 model = Model([inp, inp2] , x)
 weights = [w for w in os.listdir('.. /input/osic-model-weights')if model_class in w][0]
model.load_weights('.. /input/osic-model-weights/' + weights)
 return model
model_classes = ['b7']
models = [build_model(shape=(512, 512, 1), model_class=m)for m in model_classes]
print('Number of models: ' + str(len(models)) )
df = pd.DataFrame({'id_code':id_code, 'diagnosis':diagnosis})
 df.to_csv('submission.csv', index=False )<normalization>
tr_p, vl_p = train_test_split(P,
shuffle=True,
train_size= 0.8 )
def scale_radius(src, img_size, padding=False):
x = src[src.shape[0] // 2,...].sum(axis=1)
 r =(x > x.mean() / 10 ).sum() // 2
yx = src.sum(axis=2)
 region_props = measure.regionprops(( yx > yx.mean() / 10 ).astype('uint8'))
yc, xc = np.round(region_props[0].centroid ).astype('int')
 x1 = max(xc - r, 0)
 x2 = min(xc + r, src.shape[1] - 1)
 y1 = max(yc - r, 0)
 y2 = min(yc + r, src.shape[0] - 1)
 dst = src[y1:y2, x1:x2]
dst = cv2.resize(dst, dsize=None, fx=img_size/(2*r), fy=img_size/(2*r))
if padding:
pad_x =(img_size - dst.shape[1])// 2
pad_y =(img_size - dst.shape[0])// 2
dst = np.pad(dst,(( pad_y, pad_y),(pad_x, pad_x),(0, 0)) , 'constant')
 return dst
class Dataset(torch.utils.data.Dataset):
def __init__(self, img_paths, labels, transform=None, img_size=576, save_img=True):
self.img_paths = img_paths
self.labels = labels
self.transform = transform
self.img_size = img_size
self.save_img = save_img
def __getitem__(self, index):
img_path, label = self.img_paths[index], self.labels[index]
if 'train' in img_path:
img = cv2.imread('.. /input/aptos2019-dataset/images_288_scaled/images_288_scaled/%s' %os.path.basename(img_path))
else:
if os.path.exists('processed/%s' %os.path.basename(img_path)) :
img = cv2.imread('processed/%s' %os.path.basename(img_path))
else:
img = cv2.imread(img_path)
 try:
img = scale_radius(img, img_size=self.img_size, padding=False)
 except Exception as e:
img = img
if self.save_img:
cv2.imwrite('processed/%s' %os.path.basename(img_path), img)
 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 img = Image.fromarray(img)
 if self.transform is not None:
img = self.transform(img)
 return img, label
def __len__(self):
return len(self.img_paths)
 def get_model(model_name='resnet18', num_outputs=None, pretrained=True,
freeze_bn=False, dropout_p=0, **kwargs):
pretrained = 'imagenet' if pretrained else None
model = pretrainedmodels.__dict__[model_name](num_classes=1000,
pretrained=pretrained)
 if 'dpn' in model_name:
in_channels = model.last_linear.in_channels
model.last_linear = nn.Conv2d(in_channels, num_outputs,
kernel_size=1, bias=True)
 else:
if 'resnet' in model_name:
model.avgpool = nn.AdaptiveAvgPool2d(1)
 else:
model.avg_pool = nn.AdaptiveAvgPool2d(1)
 in_features = model.last_linear.in_features
if dropout_p == 0:
model.last_linear = nn.Linear(in_features, num_outputs)
 else:
model.last_linear = nn.Sequential(
 nn.Dropout(p=dropout_p),
nn.Linear(in_features, num_outputs), 
)
 if freeze_bn:
for m in model.modules() :
if isinstance(m, nn.BatchNorm2d):
m.weight.requires_grad = False
m.bias.requires_grad = False
return model
class RAdam(Optimizer):
def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0):
defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay)
 self.buffer = [[None, None, None] for ind in range(10)]
super(RAdam, self ).__init__(params, defaults)
 def __setstate__(self, state):
super(RAdam, self ).__setstate__(state)
 def step(self, closure=None):
loss = None
if closure is not None:
loss = closure()
for group in self.param_groups:
for p in group['params']:
if p.grad is None:
continue
grad = p.grad.data.float()
if grad.is_sparse:
raise RuntimeError('RAdam does not support sparse gradients')
 p_data_fp32 = p.data.float()
state = self.state[p]
if len(state)== 0:
state['step'] = 0
state['exp_avg'] = torch.zeros_like(p_data_fp32)
 state['exp_avg_sq'] = torch.zeros_like(p_data_fp32)
 else:
state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32)
 state['exp_avg_sq'] = state['exp_avg_sq'].type_as(p_data_fp32)
 exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq']
beta1, beta2 = group['betas']
exp_avg_sq.mul_(beta2 ).addcmul_(1 - beta2, grad, grad)
 exp_avg.mul_(beta1 ).add_(1 - beta1, grad)
 state['step'] += 1
buffered = self.buffer[int(state['step'] % 10)]
if state['step'] == buffered[0]:
N_sma, step_size = buffered[1], buffered[2]
else:
buffered[0] = state['step']
beta2_t = beta2 ** state['step']
N_sma_max = 2 /(1 - beta2)- 1
N_sma = N_sma_max - 2 * state['step'] * beta2_t /(1 - beta2_t)
 buffered[1] = N_sma
if N_sma >= 5:
step_size = group['lr'] * math.sqrt(( 1 - beta2_t)*(N_sma - 4)/(N_sma_max - 4)*(N_sma - 2)/ N_sma * N_sma_max /(N_sma_max - 2)) /(1 - beta1 ** state['step'])
 else:
step_size = group['lr'] /(1 - beta1 ** state['step'])
 buffered[2] = step_size
if group['weight_decay'] != 0:
p_data_fp32.add_(-group['weight_decay'] * group['lr'], p_data_fp32)
 if N_sma >= 5:
denom = exp_avg_sq.sqrt().add_(group['eps'])
 p_data_fp32.addcdiv_(-step_size, exp_avg, denom)
 else:
p_data_fp32.add_(-step_size, exp_avg)
 p.data.copy_(p_data_fp32)
 return loss
class AverageMeter(object):

 def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def quadratic_weighted_kappa(y_pred, y_true):
if torch.is_tensor(y_pred):
y_pred = y_pred.data.cpu().numpy()
if torch.is_tensor(y_true):
y_true = y_true.data.cpu().numpy()
if y_pred.shape[1] == 1:
y_pred = y_pred[:, 0]
else:
y_pred = np.argmax(y_pred, axis=1)
 return metrics.cohen_kappa_score(y_pred, y_true, weights='quadratic')
 def train(train_loader, model, criterion, optimizer, epoch):
losses = AverageMeter()
scores = AverageMeter()
model.train()
for i,(input, target)in tqdm(enumerate(train_loader), total=len(train_loader)) :
input = input.cuda()
target = target.cuda()
output = model(input)
 loss = criterion(output.view(-1), target.float())
 optimizer.zero_grad()
loss.backward()
optimizer.step()
thrs = [0.5, 1.5, 2.5, 3.5]
output[output < thrs[0]] = 0
output[(output >= thrs[0])&(output < thrs[1])] = 1
output[(output >= thrs[1])&(output < thrs[2])] = 2
output[(output >= thrs[2])&(output < thrs[3])] = 3
output[output >= thrs[3]] = 4
target[target < thrs[0]] = 0
target[(target >= thrs[0])&(target < thrs[1])] = 1
target[(target >= thrs[1])&(target < thrs[2])] = 2
target[(target >= thrs[2])&(target < thrs[3])] = 3
target[target >= thrs[3]] = 4
score = quadratic_weighted_kappa(output, target)
 losses.update(loss.item() , input.size(0))
scores.update(score, input.size(0))
return losses.avg, scores.avg
def validate(val_loader, model, criterion):
losses = AverageMeter()
scores = AverageMeter()
model.eval()
with torch.no_grad() :
for i,(input, target)in tqdm(enumerate(val_loader), total=len(val_loader)) :
input = input.cuda()
target = target.cuda()
output = model(input)
 loss = criterion(output.view(-1), target.float())
 thrs = [0.5, 1.5, 2.5, 3.5]
output[output < thrs[0]] = 0
output[(output >= thrs[0])&(output < thrs[1])] = 1
output[(output >= thrs[1])&(output < thrs[2])] = 2
output[(output >= thrs[2])&(output < thrs[3])] = 3
output[output >= thrs[3]] = 4
score = quadratic_weighted_kappa(output, target)
 losses.update(loss.item() , input.size(0))
scores.update(score, input.size(0))
return losses.avg, scores.avg<load_from_csv>
def score(fvc_true, fvc_pred, sigma):
sigma_clip = np.maximum(sigma, 70)
 delta = np.abs(fvc_true - fvc_pred)
 delta = np.minimum(delta, 1000)
 sq2 = np.sqrt(2)
 metric =(delta / sigma_clip)*sq2 + np.log(sigma_clip* sq2)
 return np.mean(metric )
pseudo_probs = {}
aptos2019_df = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 aptos2019_img_paths = '.. /input/aptos2019-blindness-detection/train_images/' + aptos2019_df['id_code'].values + '.png'
aptos2019_labels = aptos2019_df['diagnosis'].values
test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 dir_name = '.. /input/aptos2019-blindness-detection/test_images'
test_img_paths = dir_name + '/' + test_df['id_code'].values + '.png'
test_labels = np.zeros(len(test_img_paths))
test_transform = transforms.Compose([
transforms.Resize(( 512, 512)) ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
 test_set = Dataset(
 test_img_paths,
test_labels,
transform=test_transform)
 test_loader = torch.utils.data.DataLoader(
 test_set,
batch_size=16,
shuffle=False,
num_workers=2 )<load_pretrained>
subs = []
for model in models:
metric = []
for q in tqdm(range(1, 10)) :
m = []
for p in vl_p:
x = []
tab = []
if p in ['ID00011637202177653955184', 'ID00052637202186188008618']:
continue
ldir = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/')
 for i in ldir:
if int(i[:-4])/ len(ldir)< 0.8 and int(i[:-4])/ len(ldir)> 0.15:
x.append(get_img(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/{i}'))
tab.append(get_tab(train.loc[train.Patient == p, :]))
if len(x)< 1:
continue
tab = np.array(tab)
 x = np.expand_dims(x, axis=-1)
 _a = model.predict([x, tab])
 a = np.quantile(_a, q / 10)
 percent_true = train.Percent.values[train.Patient == p]
fvc_true = train.FVC.values[train.Patient == p]
weeks_true = train.Weeks.values[train.Patient == p]
fvc = a *(weeks_true - weeks_true[0])+ fvc_true[0]
percent = percent_true[0] - a * abs(weeks_true - weeks_true[0])
 m.append(score(fvc_true, fvc, percent))
print(np.mean(m))
metric.append(np.mean(m))
q =(np.argmin(metric)+ 1)/ 10
sub = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/sample_submission.csv')
 test = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv')
 A_test, B_test, P_test,W, FVC= {}, {}, {},{},{}
STD, WEEK = {}, {}
for p in test.Patient.unique() :
x = []
tab = []
ldir = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/test/{p}/')
 for i in ldir:
if int(i[:-4])/ len(ldir)< 0.8 and int(i[:-4])/ len(ldir)> 0.15:
x.append(get_img(f'.. /input/osic-pulmonary-fibrosis-progression/test/{p}/{i}'))
tab.append(get_tab(test.loc[test.Patient == p, :]))
if len(x)<= 1:
continue
tab = np.array(tab)
 x = np.expand_dims(x, axis=-1)
 _a = model.predict([x, tab])
 a = np.quantile(_a, q)
 A_test[p] = a
B_test[p] = test.FVC.values[test.Patient == p] - a*test.Weeks.values[test.Patient == p]
P_test[p] = test.Percent.values[test.Patient == p]
WEEK[p] = test.Weeks.values[test.Patient == p]
for k in sub.Patient_Week.values:
p, w = k.split('_')
 w = int(w)
 fvc = A_test[p] * w + B_test[p]
sub.loc[sub.Patient_Week == k, 'FVC'] = fvc
sub.loc[sub.Patient_Week == k, 'Confidence'] =(
 P_test[p] - A_test[p] * abs(WEEK[p] - w)
)
 _sub = sub[["Patient_Week","FVC","Confidence"]].copy()
subs.append(_sub )
model = get_model(model_name='se_resnext50_32x4d',
num_outputs=1,
pretrained=False,
freeze_bn=True,
dropout_p=0)
 model = model.cuda()
model.eval()
probs = []
for fold in range(5):
print('Fold [%d/%d]' %(fold+1, 5))
model.load_state_dict(torch.load('.. /input/se-resnext50-32x4d-080922/model_%d.pth' %(fold+1)))
 probs_fold = []
with torch.no_grad() :
for i,(input, _)in tqdm(enumerate(test_loader), total=len(test_loader)) :
input = input.cuda()
output = model(input)
 probs_fold.extend(output.data.cpu().numpy() [:, 0])
 probs_fold = np.array(probs_fold)
 probs.append(probs_fold)
 probs = np.mean(probs, axis=0)
 pseudo_probs['se_resnext50_32x4d'] = probs
del model
torch.cuda.empty_cache()
!nvidia-smi<load_pretrained>
N = len(subs)
 sub = subs[0].copy()
sub["FVC"] = 0
sub["Confidence"] = 0
for i in range(N):
sub["FVC"] += subs[0]["FVC"] *(1/N)
 sub["Confidence"] += subs[0]["Confidence"] *(1/N )
model = get_model(model_name='se_resnext101_32x4d',
num_outputs=1,
pretrained=False,
freeze_bn=True,
dropout_p=0, 
)
 model = model.cuda()
model.eval()
probs = []
for fold in range(5):
print('Fold [%d/%d]' %(fold+1, 5))
model.load_state_dict(torch.load('.. /input/se-resnext101-32x4d-081208/model_%d.pth' %(fold+1)))
 probs_fold = []
with torch.no_grad() :
for i,(input, _)in tqdm(enumerate(test_loader), total=len(test_loader)) :
input = input.cuda()
output = model(input)
 probs_fold.extend(output.data.cpu().numpy() [:, 0])
 probs_fold = np.array(probs_fold)
 probs.append(probs_fold)
 probs = np.mean(probs, axis=0)
 pseudo_probs['se_resnext101_32x4d'] = probs
del model
torch.cuda.empty_cache()
!nvidia-smi<load_pretrained>
sub[["Patient_Week","FVC","Confidence"]].to_csv("submission_img.csv", index=False )
model = get_model(model_name='senet154',
num_outputs=1,
pretrained=False,
freeze_bn=True,
dropout_p=0)
 model = model.cuda()
model.eval()
probs = []
for fold in range(5):
print('Fold [%d/%d]' %(fold+1, 5))
model.load_state_dict(torch.load('.. /input/senet154-082510/model_%d.pth' %(fold+1)))
 probs_fold = []
with torch.no_grad() :
for i,(input, _)in tqdm(enumerate(test_loader), total=len(test_loader)) :
input = input.cuda()
output = model(input)
 probs_fold.extend(output.data.cpu().numpy() [:, 0])
 probs_fold = np.array(probs_fold)
 probs.append(probs_fold)
 probs = np.mean(probs, axis=0)
 pseudo_probs['senet154'] = probs
del model
torch.cuda.empty_cache()
!nvidia-smi<load_from_csv>
img_sub = sub[["Patient_Week","FVC","Confidence"]].copy()
aptos2019_df = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 aptos2019_img_paths = '.. /input/aptos2019-blindness-detection/train_images/' + aptos2019_df['id_code'].values + '.png'
aptos2019_labels = aptos2019_df['diagnosis'].values
test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 test_img_paths = '.. /input/aptos2019-blindness-detection/test_images/' + test_df['id_code'].values + '.png'
test_labels = 0.4 * pseudo_probs['se_resnext50_32x4d'] + 0.3 * pseudo_probs['se_resnext101_32x4d'] + 0.3 * pseudo_probs['senet154']
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=41)
 kf = KFold(n_splits=5, shuffle=True, random_state=41)
 img_paths = []
labels = []
for(train_idx1, val_idx1),(train_idx2, val_idx2)in zip(skf.split(aptos2019_img_paths, aptos2019_labels), kf.split(test_img_paths)) :
img_paths.append(( np.hstack(( aptos2019_img_paths[train_idx1], test_img_paths[val_idx2])) , aptos2019_img_paths[val_idx1]))
labels.append(( np.hstack(( aptos2019_labels[train_idx1], test_labels[val_idx2])) , aptos2019_labels[val_idx1]))<find_best_params>
ROOT = ".. /input/osic-pulmonary-fibrosis-progression"
BATCH_SIZE=128
tr = pd.read_csv(f"{ROOT}/train.csv")
 tr.drop_duplicates(keep=False, inplace=True, subset=['Patient','Weeks'])
 chunk = pd.read_csv(f"{ROOT}/test.csv")
 print("add infos")
 sub = pd.read_csv(f"{ROOT}/sample_submission.csv")
 sub['Patient'] = sub['Patient_Week'].apply(lambda x:x.split('_')[0])
 sub['Weeks'] = sub['Patient_Week'].apply(lambda x: int(x.split('_')[-1]))
sub = sub[['Patient','Weeks','Confidence','Patient_Week']]
sub = sub.merge(chunk.drop('Weeks', axis=1), on="Patient" )
model = get_model(model_name='se_resnext50_32x4d',
num_outputs=1,
pretrained=False,
freeze_bn=True,
dropout_p=0, 
)
 model = model.cuda()
criterion = nn.MSELoss().cuda()
best_losses = []
best_scores = []
for fold,(( train_img_paths, val_img_paths),(train_labels, val_labels)) in enumerate(zip(img_paths, labels)) :
print('Fold [%d/%d]' %(fold+1, len(img_paths)))
 train_set = Dataset(
 train_img_paths,
train_labels,
transform=train_transform,
img_size=576,
save_img=True)
 train_loader = torch.utils.data.DataLoader(
 train_set,
batch_size=16,
shuffle=True,
num_workers=2, 
)
 val_set = Dataset(
 val_img_paths,
val_labels,
transform=val_transform,
save_img=True)
 val_loader = torch.utils.data.DataLoader(
 val_set,
batch_size=16,
shuffle=False,
num_workers=2)
 model.load_state_dict(torch.load('.. /input/se-resnext50-32x4d-080922/model_%d.pth' %(fold+1)))
 optimizer = RAdam(
 filter(lambda p: p.requires_grad, model.parameters()), lr=1e-3)
 scheduler = lr_scheduler.CosineAnnealingLR(
 optimizer, T_max=10, eta_min=1e-5)
 os.makedirs('se_resnext50_32x4d', exist_ok=True)
 best_loss = float('inf')
 best_score = 0
for epoch in range(10):
print('Epoch [%d/%d]' %(epoch + 1, 10))
train_loss, train_score = train(
 train_loader, model, criterion, optimizer, epoch)
 val_loss, val_score = validate(val_loader, model, criterion)
 scheduler.step()
print('loss %.4f - score %.4f - val_loss %.4f - val_score %.4f'
%(train_loss, train_score, val_loss, val_score))
if val_loss < best_loss:
torch.save(model.state_dict() , 'se_resnext50_32x4d/model_%d.pth' %(fold+1))
best_loss = val_loss
best_score = val_score
print("=> saved best model")
 print('val_loss: %f' % best_loss)
 print('val_score: %f' % best_score)
 del model
torch.cuda.empty_cache()
!nvidia-smi<load_from_csv>
tr['WHERE'] = 'train'
chunk['WHERE'] = 'val'
sub['WHERE'] = 'test'
data = tr.append([chunk, sub] )
test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 dir_name = '.. /input/aptos2019-blindness-detection/test_images'
test_img_paths = dir_name + '/' + test_df['id_code'].values + '.png'
test_labels = np.zeros(len(test_img_paths))
test_transform = transforms.Compose([
transforms.Resize(( 512, 512)) ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
 test_set = Dataset(
 test_img_paths,
test_labels,
transform=test_transform)
 test_loader = torch.utils.data.DataLoader(
 test_set,
batch_size=16,
shuffle=False,
num_workers=2)
 model = get_model(model_name='se_resnext50_32x4d',
num_outputs=1,
pretrained=False,
freeze_bn=True,
dropout_p=0)
 model = model.cuda()
model.eval()
probs = []
for fold in range(5):
print('Fold [%d/%d]' %(fold+1, 5))
model.load_state_dict(torch.load('se_resnext50_32x4d/model_%d.pth' %(fold+1)))
 probs_fold = []
with torch.no_grad() :
for i,(input, _)in tqdm(enumerate(test_loader), total=len(test_loader)) :
input = input.cuda()
output = model(input)
 probs_fold.extend(output.data.cpu().numpy() [:, 0])
 probs_fold = np.array(probs_fold)
 probs.append(probs_fold)
 probs = np.mean(probs, axis=0)
 l1_probs['se_resnext50_32x4d'] = probs
del model
torch.cuda.empty_cache()
!nvidia-smi<load_from_csv>
print(tr.shape, chunk.shape, sub.shape, data.shape)
 print(tr.Patient.nunique() , chunk.Patient.nunique() , sub.Patient.nunique() ,
data.Patient.nunique())
aptos2019_df = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 aptos2019_img_paths = '.. /input/aptos2019-blindness-detection/train_images/' + aptos2019_df['id_code'].values + '.png'
aptos2019_labels = aptos2019_df['diagnosis'].values
test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 test_img_paths = '.. /input/aptos2019-blindness-detection/test_images/' + test_df['id_code'].values + '.png'
test_labels = 0.4 * pseudo_probs['se_resnext50_32x4d'] + 0.3 * pseudo_probs['se_resnext101_32x4d'] + 0.3 * pseudo_probs['senet154']
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=41)
 kf = KFold(n_splits=5, shuffle=True, random_state=42)
 img_paths = []
labels = []
for(train_idx1, val_idx1),(train_idx2, val_idx2)in zip(skf.split(aptos2019_img_paths, aptos2019_labels), kf.split(test_img_paths)) :
img_paths.append(( np.hstack(( aptos2019_img_paths[train_idx1], test_img_paths[val_idx2])) , aptos2019_img_paths[val_idx1]))
labels.append(( np.hstack(( aptos2019_labels[train_idx1], test_labels[val_idx2])) , aptos2019_labels[val_idx1]))<choose_model_class>
data['min_week'] = data['Weeks']
data.loc[data.WHERE=='test','min_week'] = np.nan
data['min_week'] = data.groupby('Patient')['min_week'].transform('min' )
model = get_model(model_name='se_resnext101_32x4d',
num_outputs=1,
pretrained=False,
freeze_bn=True,
dropout_p=0)
 model = model.cuda()
criterion = nn.MSELoss().cuda()
best_losses = []
best_scores = []
for fold,(( train_img_paths, val_img_paths),(train_labels, val_labels)) in enumerate(zip(img_paths, labels)) :
print('Fold [%d/%d]' %(fold+1, len(img_paths)))
 train_set = Dataset(
 train_img_paths,
train_labels,
transform=train_transform,
img_size=576,
save_img=True)
 train_loader = torch.utils.data.DataLoader(
 train_set,
batch_size=16,
shuffle=True,
num_workers=2)
 val_set = Dataset(
 val_img_paths,
val_labels,
transform=val_transform,
save_img=True)
 val_loader = torch.utils.data.DataLoader(
 val_set,
batch_size=16,
shuffle=False,
num_workers=2)
 model.load_state_dict(torch.load('.. /input/se-resnext101-32x4d-081208/model_%d.pth' %(fold+1)))
 optimizer = RAdam(
 filter(lambda p: p.requires_grad, model.parameters()), lr=1e-3)
 scheduler = lr_scheduler.CosineAnnealingLR(
 optimizer, T_max=10, eta_min=1e-5)
 os.makedirs('se_resnext101_32x4d', exist_ok=True)
 best_loss = float('inf')
 best_score = 0
for epoch in range(10):
print('Epoch [%d/%d]' %(epoch + 1, 10))
train_loss, train_score = train(
 train_loader, model, criterion, optimizer, epoch)
 val_loss, val_score = validate(val_loader, model, criterion)
 scheduler.step()
print('loss %.4f - score %.4f - val_loss %.4f - val_score %.4f'
%(train_loss, train_score, val_loss, val_score))
if val_loss < best_loss:
torch.save(model.state_dict() , 'se_resnext101_32x4d/model_%d.pth' %(fold+1))
best_loss = val_loss
best_score = val_score
print("=> saved best model")
 print('val_loss: %f' % best_loss)
 print('val_score: %f' % best_score)
 del model
torch.cuda.empty_cache()
!nvidia-smi<load_from_csv>
base = data.loc[data.Weeks == data.min_week]
base = base[['Patient','FVC']].copy()
base.columns = ['Patient','min_FVC']
base['nb'] = 1
base['nb'] = base.groupby('Patient')['nb'].transform('cumsum')
 base = base[base.nb==1]
base.drop('nb', axis=1, inplace=True )
test_df = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 dir_name = '.. /input/aptos2019-blindness-detection/test_images'
test_img_paths = dir_name + '/' + test_df['id_code'].values + '.png'
test_labels = np.zeros(len(test_img_paths))
test_transform = transforms.Compose([
transforms.Resize(( 512,512)) ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
 test_set = Dataset(
 test_img_paths,
test_labels,
transform=test_transform)
 test_loader = torch.utils.data.DataLoader(
 test_set,
batch_size=16,
shuffle=False,
num_workers=2)
 model = get_model(model_name='se_resnext101_32x4d',
num_outputs=1,
pretrained=False,
freeze_bn=True,
dropout_p=0)
 model = model.cuda()
model.eval()
probs = []
for fold in range(5):
print('Fold [%d/%d]' %(fold+1, 5))
model.load_state_dict(torch.load('se_resnext101_32x4d/model_%d.pth' %(fold+1)))
 probs_fold = []
with torch.no_grad() :
for i,(input, _)in tqdm(enumerate(test_loader), total=len(test_loader)) :
input = input.cuda()
output = model(input)
 probs_fold.extend(output.data.cpu().numpy() [:, 0])
 probs_fold = np.array(probs_fold)
 probs.append(probs_fold)
 probs = np.mean(probs, axis=0)
 l1_probs['se_resnext101_32x4d'] = probs
del model
torch.cuda.empty_cache()
!nvidia-smi<define_variables>
data = data.merge(base, on='Patient', how='left')
 data['base_week'] = data['Weeks'] - data['min_week']
del base
preds1 = 0.0 * l1_probs['se_resnext50_32x4d'] + 1.0 * l1_probs['se_resnext101_32x4d']
preds2 = 0.5 * l1_probs['se_resnext50_32x4d'] + 0.5 * l1_probs['se_resnext101_32x4d']<categorify>
COLS = ['Sex','SmokingStatus']
FE = []
for col in COLS:
for mod in data[col].unique() :
FE.append(mod)
 data[mod] =(data[col] == mod ).astype(int )
thrs = [0.5, 1.5, 2.5, 3.5]
preds1[preds1 < thrs[0]] = 0
preds1[(preds1 >= thrs[0])&(preds1 < thrs[1])] = 1
preds1[(preds1 >= thrs[1])&(preds1 < thrs[2])] = 2
preds1[(preds1 >= thrs[2])&(preds1 < thrs[3])] = 3
preds1[preds1 >= thrs[3]] = 4
preds1 = preds1.astype('int')
 preds2[preds2 < thrs[0]] = 0
preds2[(preds2 >= thrs[0])&(preds2 < thrs[1])] = 1
preds2[(preds2 >= thrs[1])&(preds2 < thrs[2])] = 2
preds2[(preds2 >= thrs[2])&(preds2 < thrs[3])] = 3
preds2[preds2 >= thrs[3]] = 4
preds2 = preds2.astype('int')
 test_df['diagnosis'] = preds1
test_df.to_csv('submission.csv', index=False)
 test_df['diagnosis'] = preds2
test_df.to_csv('submission2.csv', index=False )<set_options>
data['age'] =(data['Age'] - data['Age'].min())/(data['Age'].max() - data['Age'].min())
 data['BASE'] =(data['min_FVC'] - data['min_FVC'].min())/(data['min_FVC'].max() - data['min_FVC'].min())
 data['week'] =(data['base_week'] - data['base_week'].min())/(data['base_week'].max() - data['base_week'].min())
 data['percent'] =(data['Percent'] - data['Percent'].min())/(data['Percent'].max() - data['Percent'].min())
 FE += ['age','percent','week','BASE']
device = torch.device("cuda:0")
 ImageFile.LOAD_TRUNCATED_IMAGES = True 
<set_options>
tr = data.loc[data.WHERE=='train']
chunk = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
del data
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
seed_everything(2020 )<define_variables>
C1, C2 = tf.constant(70, dtype='float32'), tf.constant(1000, dtype="float32")
 def score(y_true, y_pred):
tf.dtypes.cast(y_true, tf.float32)
 tf.dtypes.cast(y_pred, tf.float32)
 sigma = y_pred[:, 2] - y_pred[:, 0]
fvc_pred = y_pred[:, 1]
sigma_clip = tf.maximum(sigma, C1)
 delta = tf.abs(y_true[:, 0] - fvc_pred)
 delta = tf.minimum(delta, C2)
 sq2 = tf.sqrt(tf.dtypes.cast(2, dtype=tf.float32))
metric =(delta / sigma_clip)*sq2 + tf.math.log(sigma_clip* sq2)
 return K.mean(metric)
 def qloss(y_true, y_pred):
qs = [0.2, 0.50, 0.8]
q = tf.constant(np.array([qs]), dtype=tf.float32)
 e = y_true - y_pred
v = tf.maximum(q*e,(q-1)*e)
 return K.mean(v)
 def mloss(_lambda):
def loss(y_true, y_pred):
return _lambda * qloss(y_true, y_pred)+(1 - _lambda)*score(y_true, y_pred)
 return loss
def make_model(nh):
z = L.Input(( nh,), name="Patient")
 x = L.Dense(100, activation="relu", name="d1" )(z)
 x = L.Dense(100, activation="relu", name="d2" )(x)
 p1 = L.Dense(3, activation="linear", name="p1" )(x)
 p2 = L.Dense(3, activation="relu", name="p2" )(x)
 preds = L.Lambda(lambda x: x[0] + tf.cumsum(x[1], axis=1),
name="preds" )([p1, p2])
 model = M.Model(z, preds, name="CNN")
 model.compile(loss=mloss(0.8), optimizer=tf.keras.optimizers.Adam(lr=0.1, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.01, amsgrad=False), metrics=[score])
 return model
learning_rate=1e-5
epochs=15
num_workers=2
batch_size=16
IMG_SIZE = 256<load_from_csv>
y = tr['FVC'].values
z = tr[FE].values
ze = sub[FE].values
nh = z.shape[1]
pe = np.zeros(( ze.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))
train2015df = pd.read_csv('.. /input/retinopathy-train-2015/rescaled_train_896/trainLabels.csv')
 train2015df["id_code"]=train2015df["image"]
train2015df["diagnosis"]=train2015df["level"]
train2015df=train2015df.drop(columns=["image","level"])
 traindf = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 traindf2 = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 _,cvdf=train_test_split(traindf2, test_size=0.25, random_state=2019, stratify=traindf2.diagnosis)
 testdf = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 traindf=traindf.append(train2015df )<define_variables>
NFOLD = 50
kf = KFold(n_splits=NFOLD )
train = '.. /input/aptos2019-blindness-detection/train_images/'
test = '.. /input/aptos2019-blindness-detection/test_images/'
train_2015 = '.. /input/retinopathy-train-2015/rescaled_train_896/rescaled_train_896/'
def expand_path(p):
p = str(p)
 if isfile(train + p + ".png"):
return train +(p + ".png")
 if isfile(train_2015 + p + '.png'):
return train_2015 +(p + ".png")
 if isfile(test + p + ".png"):
return test +(p + ".png")
 return p
def p_show(imgs, label_name=None, per_row=3):
n = len(imgs)
 rows =(n + per_row - 1)//per_row
cols = min(per_row, n)
 fig, axes = plt.subplots(rows,cols, figsize=(15,15))
for ax in axes.flatten() : ax.axis('off')
 for i,(p, ax)in enumerate(zip(imgs, axes.flatten())) :
img = Image.open(expand_path(p))
ax.imshow(img)
 ax.set_title(train_df[train_df.id_code == p].diagnosis.values )<categorify>
%%time
cnt = 0
EPOCHS = 2000
for tr_idx, val_idx in kf.split(z):
cnt += 1
print(f"FOLD {cnt}")
 net = make_model(nh)
 net.fit(z[tr_idx], y[tr_idx], batch_size=BATCH_SIZE, epochs=EPOCHS,
validation_data=(z[val_idx], y[val_idx]), verbose=0)
 print("train", net.evaluate(z[tr_idx], y[tr_idx], verbose=0, batch_size=BATCH_SIZE))
print("val", net.evaluate(z[val_idx], y[val_idx], verbose=0, batch_size=BATCH_SIZE))
print("predict val...")
 pred[val_idx] = net.predict(z[val_idx], batch_size=BATCH_SIZE, verbose=0)
 print("predict test...")
 pe += net.predict(ze, batch_size=BATCH_SIZE, verbose=0)/ NFOLD
class RetinopathyDatasetTest(Dataset):
def __init__(self, data, transform, dtype='train',mode='train'):
self.data = data
self.transform = transform
self.dtype=dtype
self.mode=mode
def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
img_name=expand_path(self.data.loc[idx, 'id_code'])
 image = cv2.imread(img_name)
 image = load_ben_color(image, sigmaX=10)
 image = Image.fromarray(image)
 image = self.transform(image)
 label=self.data['diagnosis'][idx]
if(self.mode=='test'):
return {'image': image}
elif(self.mode=='train'):
return image,label
else :
return {'image': image},label<load_from_csv>
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )
train_df=traindf
train_df=train_df.reset_index(drop=True)
 cvdf=cvdf.reset_index(drop=True)
 sample_df=pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 transform = transforms.Compose([
transforms.RandomHorizontalFlip() ,
transforms.RandomRotation(( -120, 120)) ,
transforms.ToTensor() ,
transforms.Normalize([0.55387044 ,0.5336158 , 0.5232084 ], [0.2063595 , 0.18827829, 0.15147245])
 ])
 test_transform = transforms.Compose([
transforms.ToTensor() ,
transforms.Normalize([0.55387044 ,0.5336158 , 0.5232084 ], [0.2063595 , 0.18827829, 0.15147245])
 ])
 train_dataset = RetinopathyDatasetTest(data=train_df,
transform=transform,dtype='train',mode='train')
 test_dataset = RetinopathyDatasetTest(sample_df,
transform=test_transform,dtype='test',mode='test')
 cv_dataset= RetinopathyDatasetTest(cvdf,
transform=transform,dtype='train',mode='train')
 tr, val = train_test_split(train_df.diagnosis, stratify=train_df.diagnosis, test_size=0.25)
 train_sampler = SubsetRandomSampler(list(tr.index))
valid_sampler = SubsetRandomSampler(list(val.index))
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size,sampler=train_sampler, num_workers=num_workers)
 valid_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size,sampler=valid_sampler, num_workers=num_workers)
 cv_loader=torch.utils.data.DataLoader(cv_dataset, batch_size=batch_size, num_workers=num_workers )<load_pretrained>
sub['FVC1'] = 1.*pe[:, 1]
sub['Confidence1'] = pe[:, 2] - pe[:, 0]
subm = sub[['Patient_Week','FVC','Confidence','FVC1','Confidence1']].copy()
subm.loc[~subm.FVC1.isnull() ].head(10 )
model = torch.load(".. /input/pytorch-resnext/complete_modelv50.pth" )<choose_model_class>
subm.loc[~subm.FVC1.isnull() ,'FVC'] = subm.loc[~subm.FVC1.isnull() ,'FVC1']
if sigma_mean<70:
subm['Confidence'] = sigma_opt
else:
subm.loc[~subm.FVC1.isnull() ,'Confidence'] = subm.loc[~subm.FVC1.isnull() ,'Confidence1']
def finetune(model,learningRate,n_epochs=epochs):
criterion=nn.MSELoss()
optimizer=optim.Adam(model.parameters() ,lr=learningRate,weight_decay=1e-5)
 scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.5)
 save_file_name='resnetx101.pt'
max_epochs_stop=5
print_every=1
for param in model.parameters() :
param.requires_grad = True
optimizer=optim.Adam(model.parameters() ,lr=learningRate,weight_decay=1e-5)
 scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.5)
 epochs_no_improve = 0
valid_loss_min = np.Inf
valid_max_acc = 0
history = []
try:
print(f'Model has been trained for: {model.epochs} epochs.
 ')
 except:
model.epochs = 0
print(f'Starting Training from Scratch.
 ')
 overall_start = timer()
for epoch in(range(n_epochs)) :
torch.cuda.empty_cache()
train_loss = 0.0
valid_loss = 0.0
train_mse = 0
valid_mse = 0
model.train()
start = timer()
for ii,(data, target)in enumerate(( train_loader)) :
data, target =data.cuda() , target.cuda().float()
target=target.unsqueeze(1)
 optimizer.zero_grad()
output = model(data)
 loss = criterion(output, target)
 loss.backward()
optimizer.step()
train_loss += loss.item()
print(
 f'Epoch: {epoch}\t{100 *(ii + 1)/ len(train_loader):.2f}% complete.{timer() - start:.2f} seconds elapsed in epoch.',
end='\r')
 else:
model.epochs += 1
with torch.no_grad() :
model.eval()
for data, target in cv_loader:
data, target = data.cuda() , target.cuda().float()
target=target.unsqueeze(1)
 output = model(data)
 loss = criterion(output, target)
 valid_loss += loss.item()
train_loss = train_loss / len(train_loader)
 valid_loss = valid_loss / len(cv_loader)
 history.append([train_loss, valid_loss,scheduler.get_lr() [0]])
 if(epoch + 1)% print_every == 0:
print(
 f'
Epoch: {epoch} \tTraining Loss: {train_loss:.4f} \tValidation Loss: {valid_loss:.4f}' 
)
 print('lr:', scheduler.get_lr() [0])
 if valid_loss < valid_loss_min:
torch.save(model.state_dict() , save_file_name)
 epochs_no_improve = 0
valid_loss_min = valid_loss
best_epoch = epoch
else:
epochs_no_improve += 1
if epochs_no_improve >= max_epochs_stop:
print(f'
Early Stopping! Total epochs: {epoch}.Best epoch: {best_epoch} with loss: {valid_loss_min:.2f}')
 total_time = timer() - overall_start
print(f'{total_time:.2f} total seconds elapsed.{total_time /(epoch+1):.2f} seconds per epoch.')
 model.load_state_dict(torch.load(save_file_name))
model.optimizer = optimizer
history = pd.DataFrame(history,columns=['train_loss', 'valid_loss','Learning_rate'])
 return model,history
scheduler.step()
model.optimizer = optimizer
total_time = timer() - overall_start
print(f'
Best epoch: {best_epoch} with loss: {valid_loss_min:.2f} ')
 print(f'{total_time:.2f} total seconds elapsed.{total_time /(epoch+1):.2f} seconds per epoch.')
 history = pd.DataFrame(history,columns=['train_loss', 'valid_loss','Learning_rate'])
 return model,history<compute_test_metric>
otest = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv')
 for i in range(len(otest)) :
subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'FVC'] = otest.FVC[i]
subm.loc[subm['Patient_Week']==otest.Patient[i]+'_'+str(otest.Weeks[i]), 'Confidence'] = 0.1
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'] 
<predict_on_test>
subm[["Patient_Week","FVC","Confidence"]].to_csv("submission_regression.csv", index=False )
def Run_validation() :
torch.cuda.empty_cache()
valid_preds = []
labels=[]
tk0 = tqdm(cv_loader)
 for ii,(x_batch,label)in enumerate(tk0):
with torch.no_grad() :
pred = model(x_batch.to(device))
valid_preds.append(pred.detach().cpu().squeeze().numpy().ravel().reshape(-1, 1))
labels.append(label)
 predict_valid=valid_preds[0]
for i in range(1,len(cv_loader)) :
predict_valid=np.append(predict_valid,valid_preds[i])
 label_valid=labels[0].numpy()
for i in range(1,len(cv_loader)) :
label_valid=np.append(label_valid,labels[i].numpy())
 return predict_valid,label_valid<define_variables>
reg_sub = subm[["Patient_Week","FVC","Confidence"]].copy()
predict_valid,label_valid=Run_validation()<choose_model_class>
df1 = img_sub.sort_values(by=['Patient_Week'], ascending=True ).reset_index(drop=True)
 df2 = reg_sub.sort_values(by=['Patient_Week'], ascending=True ).reset_index(drop=True )
optR = OptimizedRounder()
optR.fit(predict_valid,label_valid)
 coefficients=optR.coefficients()
print(coefficients)
 preds_valid=optR.predict(predict_valid,coefficients )<compute_test_metric>
df = df1[['Patient_Week']].copy()
df['FVC'] = 0.25*df1['FVC'] + 0.75*df2['FVC']
df['Confidence'] = 0.26*df1['Confidence'] + 0.74*df2['Confidence']
df.head()
<compute_test_metric><EOS> 
df.to_csv('submission.csv', index=False )