Kaggle competition: APTOS 2019 Blindness Detection
class TestDataset(Dataset):
def __init__(self, df, metadata, x_cols, test_period_len, input_len=30):
self.df = df
self.metadata = metadata
self.x_cols = x_cols
self.test_period_len = test_period_len
self.input_len = input_len
self.loc_list = list(self.df.location.unique())
 self.test_beg = self.df.rel_date.max() - test_period_len + 1
def __len__(self):
return len(self.df)- self.input_len * len(self.loc_list)
 def __getitem__(self, idx):
df_idx = idx + self.input_len * len(self.loc_list)
 cur_loc = self.df.location[df_idx]
cur_date = self.df.rel_date[df_idx]
input_beg = cur_date - self.input_len
input_end = cur_date
in_input_period =(self.df.rel_date >= input_beg)&(self.df.rel_date < input_end)
 inputs = self.df.loc[(in_input_period)&(self.df.location == cur_loc), self.x_cols].values
inputs = np.expand_dims(inputs, axis=0)
 meta_inputs = self.metadata.loc[cur_loc, :].values
meta_inputs = np.expand_dims(meta_inputs, axis=0)
 return torch.tensor(inputs, dtype=torch.float32),\
torch.tensor(meta_inputs, dtype=torch.float32)
 @staticmethod
def get_dataloader(dataset, shuffle=False):
return DataLoader(dataset, batch_size=None, shuffle=shuffle )<create_dataframe>
KAGGLE_DIR = '.. /input/aptos2019-blindness-detection/'
TRAIN_DF_PATH = KAGGLE_DIR + "train.csv"
TEST_DF_PATH = KAGGLE_DIR + 'test.csv'
TRAIN_IMG_PATH = KAGGLE_DIR + "train_images/"
TEST_IMG_PATH = KAGGLE_DIR + 'test_images/'
WEIGHTS_PATH = os.path.join(KAGGLE_DIR, '.. ','trained-models', 'effnet_modelB5.h5')
 SAVED_MODEL_NAME = 'effnet_modelB5.h5'
seed = 42
np.random.seed(seed)
 tf.set_random_seed(seed)
 t_start = time.time()
print('
for file in os.listdir(KAGGLE_DIR):
print('{}| {} MB'.format(file.ljust(30),
str(round(os.path.getsize(KAGGLE_DIR + file)/ 1000000, 2))))
test_period_len = data_test.rel_date.max() - data_test.rel_date.min() + 1
ds_test = TestDataset(data_test_input, metadata, x_cols, test_period_len)
 dl_test = TestDataset.get_dataloader(ds_test, shuffle=False )<define_variables>
print("Image IDs and Labels(TRAIN)")
 train_df = pd.read_csv(TRAIN_DF_PATH)
 train_df['id_code'] = train_df['id_code'] + ".png"
print(f"Training images: {train_df.shape[0]}")
 display(train_df.head())
 print("Image IDs(TEST)")
 test_df = pd.read_csv(TEST_DF_PATH)
 test_df['id_code'] = test_df['id_code'] + ".png"
print(f"Testing Images: {test_df.shape[0]}")
 display(test_df.head() )
for i, sample in enumerate(dl_test):
for x in sample:
print(x.size())
 if i == 10:
break<categorify>
IMG_WIDTH = 456
IMG_HEIGHT = 456
IMAGE_SIZE = 456
CHANNELS = 3
class CovidTransformer(nn.Module):
def __init__(self, d_in, d_out, d_meta, d_model=256, d_fwd=512, n_head=4,
num_layers=6, dropout=0.1):
super().__init__()
self.linear = nn.Linear(d_in, d_model)
 encoder_layer = nn.TransformerEncoderLayer(d_model, n_head, d_fwd, dropout)
 self.transformer = nn.TransformerEncoder(encoder_layer, num_layers)
 self.meta_linear = nn.Linear(d_meta, d_model)
 self.regressor = nn.Linear(2 * d_model, d_out)
 def forward(self, inputs, meta_inputs):
x = self.linear(inputs)
 features = self.transformer(x)
 features = features[-1, :, :]
meta_features = self.meta_linear(meta_inputs)
 features = torch.cat([features, meta_features], dim=-1)
 output = self.regressor(features)
 return output<compute_test_metric>
def get_preds_and_labels(model, generator):

 preds = []
labels = []
for _ in range(int(np.ceil(generator.samples / BATCH_SIZE))):
x, y = next(generator)
 preds.append(model.predict(x))
labels.append(y)
 return np.concatenate(preds ).ravel() , np.concatenate(labels ).ravel()
class RMSLE(nn.Module):
def forward(self, output, target):
return torch.sqrt(F.mse_loss(output, target))<choose_model_class>
class Metrics(Callback):

 def on_train_begin(self, logs={}):

 self.val_kappas = []
def on_epoch_end(self, epoch, logs={}):

 y_pred, labels = get_preds_and_labels(model, val_generator)
 y_pred = np.rint(y_pred ).astype(np.uint8 ).clip(0, 4)
 _val_kappa = cohen_kappa_score(labels, y_pred, weights='quadratic')
 self.val_kappas.append(_val_kappa)
 print(f"val_kappa: {round(_val_kappa, 4)}")
 if _val_kappa == max(self.val_kappas):
print("Validation Kappa has improved.Saving model.")
 self.model.save(SAVED_MODEL_NAME)
 return
model = CovidTransformer(d_in, 2, d_meta)
 if torch.cuda.is_available() :
model = model.cuda()
criterion = RMSLE()
optimizer = AdamW(model.parameters())
 epochs = 20
scheduler = ExponentialLR(optimizer, 0.707 )<choose_model_class>
def info_image(im):
cy = im.shape[0]//2
midline = im[cy,:]
midline = np.where(midline>midline.mean() /3)[0]
if len(midline)>im.shape[1]//2:
x_start, x_end = np.min(midline), np.max(midline)
 else:
x_start, x_end = im.shape[1]//10, 9*im.shape[1]//10
cx =(x_start + x_end)/2
r =(x_end - x_start)/2
return cx, cy, r
def resize_image(im, augmentation=False):
cx, cy, r = info_image(im)
 scaling = IMAGE_SIZE/(2*r)
 rotation = 0
if augmentation:
scaling *= 1 + 0.3 *(np.random.rand() -0.5)
 rotation = 360 * np.random.rand()
M = cv2.getRotationMatrix2D(( cx,cy), rotation, scaling)
 M[0,2] -= cx - IMAGE_SIZE/2
M[1,2] -= cy - IMAGE_SIZE/2
return cv2.warpAffine(im,M,(IMAGE_SIZE,IMAGE_SIZE))
def subtract_median_bg_image(im):
k = 45
bg = cv2.medianBlur(im, k)
 return cv2.addWeighted(im, 4, bg, -4, 128)
 def subtract_gaussian_bg_image(im):
k = np.max(im.shape)/15
bg = cv2.GaussianBlur(im ,(0,0),k)
 return cv2.addWeighted(im, 4, bg, -4, 128)
 def contrast_stretching(img):
rr, gg, bb = cv2.split(img)
 imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
 im = imgray
ih, iw = imgray.shape 
(minVal, maxVal, minLoc, maxLoc)= cv2.minMaxLoc(imgray)
 for i in range(ih):
for j in range(iw):
im[i, j] = 255 *(( gg[i, j] - minVal)/(maxVal - minVal))
limg = cv2.merge(( rr, im, bb))
return limg
def histogram_normalization(image):
hist,bins = np.histogram(image.flatten() ,256,[0,256])
 cdf = hist.cumsum()
cdf_m = np.ma.masked_equal(cdf,0)
 cdf_m =(cdf_m - cdf_m.min())*255/(cdf_m.max() -cdf_m.min())
 cdf = np.ma.filled(cdf_m,0 ).astype('uint8')
 img2 = cdf[image]
return img2
PARAM = 96
def Radius_Reduction(img,PARAM):
h,w,c=img.shape
Frame=np.zeros(( h,w,c),dtype=np.uint8)
 cv2.circle(Frame,(int(math.floor(w/2)) ,int(math.floor(h/2))),int(math.floor(( h*PARAM)/float(2*100))),(255,255,255), -1)
 Frame1=cv2.cvtColor(Frame, cv2.COLOR_BGR2GRAY)
 img1 =cv2.bitwise_and(img,img,mask=Frame1)
 return img1
def preprocess_imagev2(image, sigmaX=10):

 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 image = resize_image(image)
 image = subtract_gaussian_bg_image(image)
 image = Radius_Reduction(image, PARAM)
 return image
engine = Engine()
engine.compile(model, criterion, optimizer, scheduler )<train_model>
def info_image(im):
cy = im.shape[0]//2
midline = im[cy,:]
midline = np.where(midline>midline.mean() /3)[0]
if len(midline)>im.shape[1]//2:
x_start, x_end = np.min(midline), np.max(midline)
 else:
x_start, x_end = im.shape[1]//10, 9*im.shape[1]//10
cx =(x_start + x_end)/2
r =(x_end - x_start)/2
return cx, cy, r
def resize_image(im, augmentation=False):
cx, cy, r = info_image(im)
 scaling = IMAGE_SIZE/(2*r)
 rotation = 0
if augmentation:
scaling *= 1 + 0.3 *(np.random.rand() -0.5)
 rotation = 360 * np.random.rand()
M = cv2.getRotationMatrix2D(( cx,cy), rotation, scaling)
 M[0,2] -= cx - IMAGE_SIZE/2
M[1,2] -= cy - IMAGE_SIZE/2
return cv2.warpAffine(im,M,(IMAGE_SIZE,IMAGE_SIZE))
def adaptive_histo_equalization(res_image):
lab= cv2.cvtColor(res_image, cv2.COLOR_BGR2LAB)
 l, a, b = cv2.split(lab)
 clahe = cv2.createCLAHE(clipLimit=4.0, tileGridSize=(8,8))
cl = clahe.apply(l)
 limg = cv2.merge(( cl,a,b))
final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
 return final
def preprocess_imagev3(image, sigmaX=10):

 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 image = resize_image(image)
 image = adaptive_histo_equalization(image)
 return image
engine.fit(epochs, dl_train, dl_val )<predict_on_test>
def Kirsch(crop):
Input=crop[:,:,1]
a,b=Input.shape
Kernel=np.zeros(( 3,3,8))
Kernel[:,:,0]=np.array([[5,5,5],[-3,0,-3],[-3,-3,-3]])
 Kernel[:,:,1]=np.array([[-3,5,5],[-3,0,5],[-3,-3,-3]])
 Kernel[:,:,2]=np.array([[-3,-3,5],[-3,0,5],[-3,-3,5]])
 Kernel[:,:,3]=np.array([[-3,-3,-3],[-3,0,5],[-3,5,5]])
 Kernel[:,:,4]=np.array([[-3,-3,-3],[-3,0,-3],[5,5,5]])
 Kernel[:,:,5]=np.array([[-3,-3,-3],[5,0,-3],[5,5,-3]])
 Kernel[:,:,6]=np.array([[5,-3,-3],[5,0,-3],[5,-3,-3]])
 Kernel[:,:,7]=np.array([[5,5,-3],[5,0,-3],[-3,-3,-3]])
 dst=np.zeros(( a,b,8))
for x in range(0,8):
dst[:,:,x] = cv2.filter2D(Input,-1,Kernel[:,:,x])
 Out=np.zeros(( a,b))
for y in range(0,a-1):
for z in range(0,b-1):
Out[y,z]=max(dst[y,z,:])
 Out=np.uint8(Out)
 return Out
def preprocess_imagev4(image):

 image = preprocess_image(image, sigmaX=10)
 image_edge = Kirsch(image)
 return image_edge
def make_predictions(dl_test, engine, y_cols):
progress = tqdm(total=len(dl_test))
for i, sample in enumerate(dl_test):
idx = i + dl_test.dataset.input_len * len(dl_test.dataset.loc_list)
 cur_loc = dl_test.dataset.df.location[idx]
cur_date = dl_test.dataset.df.rel_date[idx]
output = engine.predict_batch(sample ).detach().cpu().numpy()
prev_date = cur_date - 1
prev_output = dl_test.dataset.df.loc[(dl_test.dataset.df.rel_date == prev_date)
 &(dl_test.dataset.df.location == cur_loc), y_cols].values
output = np.maximum(output, prev_output)
 dl_test.dataset.df.loc[(dl_test.dataset.df.rel_date == cur_date)
 &(dl_test.dataset.df.location == cur_loc), y_cols] = output
progress.update(1)
 return dl_test.dataset.df<predict_on_test>
def info_image(im):
cy = im.shape[0]//2
midline = im[cy,:]
midline = np.where(midline>midline.mean() /3)[0]
if len(midline)>im.shape[1]//2:
x_start, x_end = np.min(midline), np.max(midline)
 else:
x_start, x_end = im.shape[1]//10, 9*im.shape[1]//10
cx =(x_start + x_end)/2
r =(x_end - x_start)/2
return cx, cy, r
def resize_image(im, augmentation=False):
cx, cy, r = info_image(im)
 scaling = IMAGE_SIZE/(2*r)
 rotation = 0
if augmentation:
scaling *= 1 + 0.3 *(np.random.rand() -0.5)
 rotation = 360 * np.random.rand()
M = cv2.getRotationMatrix2D(( cx,cy), rotation, scaling)
 M[0,2] -= cx - IMAGE_SIZE/2
M[1,2] -= cy - IMAGE_SIZE/2
return cv2.warpAffine(im,M,(IMAGE_SIZE,IMAGE_SIZE))
def adaptive_histo_equalization(res_image):
lab= cv2.cvtColor(res_image, cv2.COLOR_BGR2LAB)
 l, a, b = cv2.split(lab)
 l = l.astype(np.uint8)
 a = a.astype(np.uint8)
 b = b.astype(np.uint8)
 clahe = cv2.createCLAHE(clipLimit=4.0, tileGridSize=(8,8))
cl = clahe.apply(l)
 limg = cv2.merge(( cl,a,b))
final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
 return final
def preprocess_imagev5(image, sigmaX=5):

 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 image = crop_image_from_gray(image)
 image = circle_crop_v2(image)
 image = cv2.resize(image,(IMG_WIDTH, IMG_HEIGHT))
image = adaptive_histo_equalization(image)
 image = cv2.addWeighted(image,4, cv2.GaussianBlur(image,(0,0),sigmaX), -4, 128)
 return image.astype(np.uint8 )
predictions = make_predictions(dl_test, engine, y_cols )<feature_engineering>
y_labels = train_df['diagnosis'].values
predictions["ConfirmedCases"] = predictions["log_cfm"].map(np.expm1)
 predictions["Fatalities"] = predictions["log_ftl"].map(np.expm1 )<filter>
BATCH_SIZE = 4
train_datagen = ImageDataGenerator(rotation_range=360,
horizontal_flip=True,
vertical_flip=True,
validation_split=0.10,
preprocessing_function=preprocess_image,
rescale=1 / 128.
)
 train_generator = train_datagen.flow_from_dataframe(train_df,
x_col='id_code',
y_col='diagnosis',
directory = TRAIN_IMG_PATH,
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=BATCH_SIZE,
class_mode='other',
subset='training')
 val_generator = train_datagen.flow_from_dataframe(train_df,
x_col='id_code',
y_col='diagnosis',
directory = TRAIN_IMG_PATH,
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=BATCH_SIZE,
class_mode='other',
subset='validation' )
predictions = predictions.loc[~predictions.ForecastId.isna() , :]<sort_values>
class RAdam(keras.optimizers.Optimizer):

 def __init__(self, lr=0.001, beta_1=0.9, beta_2=0.999,
epsilon=None, decay=0., weight_decay=0., amsgrad=False,
total_steps=0, warmup_proportion=0.1, min_lr=0., **kwargs):
super(RAdam, self ).__init__(**kwargs)
 with K.name_scope(self.__class__.__name__):
self.iterations = K.variable(0, dtype='int64', name='iterations')
 self.lr = K.variable(lr, name='lr')
 self.beta_1 = K.variable(beta_1, name='beta_1')
 self.beta_2 = K.variable(beta_2, name='beta_2')
 self.decay = K.variable(decay, name='decay')
 self.weight_decay = K.variable(weight_decay, name='weight_decay')
 self.total_steps = K.variable(total_steps, name='total_steps')
 self.warmup_proportion = K.variable(warmup_proportion, name='warmup_proportion')
 self.min_lr = K.variable(lr, name='min_lr')
 if epsilon is None:
epsilon = K.epsilon()
self.epsilon = epsilon
self.initial_decay = decay
self.initial_weight_decay = weight_decay
self.initial_total_steps = total_steps
self.amsgrad = amsgrad
def get_updates(self, loss, params):
grads = self.get_gradients(loss, params)
 self.updates = [K.update_add(self.iterations, 1)]
lr = self.lr
if self.initial_decay > 0:
lr = lr *(1./(1.+ self.decay * K.cast(self.iterations, K.dtype(self.decay))))
t = K.cast(self.iterations, K.floatx())+ 1
if self.initial_total_steps > 0:
warmup_steps = self.total_steps * self.warmup_proportion
decay_steps = self.total_steps - warmup_steps
lr = K.switch(
 t <= warmup_steps,
lr *(t / warmup_steps),
lr *(1.0 - K.minimum(t, decay_steps)/ decay_steps), 
)
 ms = [K.zeros(K.int_shape(p), dtype=K.dtype(p), name='m_' + str(i)) for(i, p)in enumerate(params)]
vs = [K.zeros(K.int_shape(p), dtype=K.dtype(p), name='v_' + str(i)) for(i, p)in enumerate(params)]
if self.amsgrad:
vhats = [K.zeros(K.int_shape(p), dtype=K.dtype(p), name='vhat_' + str(i)) for(i, p)in enumerate(params)]
else:
vhats = [K.zeros(1, name='vhat_' + str(i)) for i in range(len(params)) ]
self.weights = [self.iterations] + ms + vs + vhats
beta_1_t = K.pow(self.beta_1, t)
 beta_2_t = K.pow(self.beta_2, t)
 sma_inf = 2.0 /(1.0 - self.beta_2)- 1.0
sma_t = sma_inf - 2.0 * t * beta_2_t /(1.0 - beta_2_t)
 for p, g, m, v, vhat in zip(params, grads, ms, vs, vhats):
m_t =(self.beta_1 * m)+(1.- self.beta_1)* g
v_t =(self.beta_2 * v)+(1.- self.beta_2)* K.square(g)
 m_corr_t = m_t /(1.0 - beta_1_t)
 if self.amsgrad:
vhat_t = K.maximum(vhat, v_t)
 v_corr_t = K.sqrt(vhat_t /(1.0 - beta_2_t)+ self.epsilon)
 self.updates.append(K.update(vhat, vhat_t))
else:
v_corr_t = K.sqrt(v_t /(1.0 - beta_2_t)+ self.epsilon)
 r_t = K.sqrt(( sma_t - 4.0)/(sma_inf - 4.0)* 
(sma_t - 2.0)/(sma_inf - 2.0)*
sma_inf / sma_t)
 p_t = K.switch(sma_t > 5, r_t * m_corr_t / v_corr_t, m_corr_t)
 if self.initial_weight_decay > 0:
p_t += self.weight_decay * p
p_t = p - lr * p_t
self.updates.append(K.update(m, m_t))
self.updates.append(K.update(v, v_t))
new_p = p_t
if getattr(p, 'constraint', None)is not None:
new_p = p.constraint(new_p)
 self.updates.append(K.update(p, new_p))
return self.updates
def get_config(self):
config = {
'lr': float(K.get_value(self.lr)) ,
'beta_1': float(K.get_value(self.beta_1)) ,
'beta_2': float(K.get_value(self.beta_2)) ,
'decay': float(K.get_value(self.decay)) ,
'weight_decay': float(K.get_value(self.weight_decay)) ,
'epsilon': self.epsilon,
'amsgrad': self.amsgrad,
'total_steps': float(K.get_value(self.total_steps)) ,
'warmup_proportion': float(K.get_value(self.warmup_proportion)) ,
'min_lr': float(K.get_value(self.min_lr)) ,
}
base_config = super(RAdam, self ).get_config()
return dict(list(base_config.items())+ list(config.items()))
predictions = predictions.sort_values(["ForecastId"] ).reset_index(drop=True )<data_type_conversions>
class GroupNormalization(Layer):

 def __init__(self,
groups=32,
axis=-1,
epsilon=1e-5,
center=True,
scale=True,
beta_initializer='zeros',
gamma_initializer='ones',
beta_regularizer=None,
gamma_regularizer=None,
beta_constraint=None,
gamma_constraint=None,
**kwargs):
super(GroupNormalization, self ).__init__(**kwargs)
 self.supports_masking = True
self.groups = groups
self.axis = axis
self.epsilon = epsilon
self.center = center
self.scale = scale
self.beta_initializer = initializers.get(beta_initializer)
 self.gamma_initializer = initializers.get(gamma_initializer)
 self.beta_regularizer = regularizers.get(beta_regularizer)
 self.gamma_regularizer = regularizers.get(gamma_regularizer)
 self.beta_constraint = constraints.get(beta_constraint)
 self.gamma_constraint = constraints.get(gamma_constraint)
 def build(self, input_shape):
dim = input_shape[self.axis]
if dim is None:
raise ValueError('Axis ' + str(self.axis)+ ' of '
'input tensor should have a defined dimension '
'but the layer received an input with shape ' +
str(input_shape)+ '.')
 if dim < self.groups:
raise ValueError('Number of groups(' + str(self.groups)+ ')cannot be '
'more than the number of channels(' +
str(dim)+ ' ).')
 if dim % self.groups != 0:
raise ValueError('Number of groups(' + str(self.groups)+ ')must be a '
'multiple of the number of channels(' +
str(dim)+ ' ).')
 self.input_spec = InputSpec(ndim=len(input_shape),
axes={self.axis: dim})
 shape =(dim,)
 if self.scale:
self.gamma = self.add_weight(shape=shape,
name='gamma',
initializer=self.gamma_initializer,
regularizer=self.gamma_regularizer,
constraint=self.gamma_constraint)
 else:
self.gamma = None
if self.center:
self.beta = self.add_weight(shape=shape,
name='beta',
initializer=self.beta_initializer,
regularizer=self.beta_regularizer,
constraint=self.beta_constraint)
 else:
self.beta = None
self.built = True
def call(self, inputs, **kwargs):
input_shape = K.int_shape(inputs)
 tensor_input_shape = K.shape(inputs)
 reduction_axes = list(range(len(input_shape)))
 del reduction_axes[self.axis]
broadcast_shape = [1] * len(input_shape)
 broadcast_shape[self.axis] = input_shape[self.axis] // self.groups
broadcast_shape.insert(1, self.groups)
 reshape_group_shape = K.shape(inputs)
 group_axes = [reshape_group_shape[i] for i in range(len(input_shape)) ]
group_axes[self.axis] = input_shape[self.axis] // self.groups
group_axes.insert(1, self.groups)
 group_shape = [group_axes[0], self.groups] + group_axes[2:]
group_shape = K.stack(group_shape)
 inputs = K.reshape(inputs, group_shape)
 group_reduction_axes = list(range(len(group_axes)))
 group_reduction_axes = group_reduction_axes[2:]
mean = K.mean(inputs, axis=group_reduction_axes, keepdims=True)
 variance = K.var(inputs, axis=group_reduction_axes, keepdims=True)
 inputs =(inputs - mean)/(K.sqrt(variance + self.epsilon))
inputs = K.reshape(inputs, group_shape)
 outputs = inputs
if self.scale:
broadcast_gamma = K.reshape(self.gamma, broadcast_shape)
 outputs = outputs * broadcast_gamma
if self.center:
broadcast_beta = K.reshape(self.beta, broadcast_shape)
 outputs = outputs + broadcast_beta
outputs = K.reshape(outputs, tensor_input_shape)
 return outputs
def get_config(self):
config = {
'groups': self.groups,
'axis': self.axis,
'epsilon': self.epsilon,
'center': self.center,
'scale': self.scale,
'beta_initializer': initializers.serialize(self.beta_initializer),
'gamma_initializer': initializers.serialize(self.gamma_initializer),
'beta_regularizer': regularizers.serialize(self.beta_regularizer),
'gamma_regularizer': regularizers.serialize(self.gamma_regularizer),
'beta_constraint': constraints.serialize(self.beta_constraint),
'gamma_constraint': constraints.serialize(self.gamma_constraint)
 }
base_config = super(GroupNormalization, self ).get_config()
return dict(list(base_config.items())+ list(config.items()))
def compute_output_shape(self, input_shape):
return input_shape
submission = predictions[["ForecastId", "ConfirmedCases", "Fatalities"]]
submission["ForecastId"] = submission["ForecastId"].astype(int )<save_to_csv>
effnet = EfficientNetB5(weights=None,
include_top=False,
input_shape=(IMG_WIDTH, IMG_HEIGHT, CHANNELS))
effnet.load_weights('.. /input/efficientnet-keras-weights-b0b5/efficientnet-b5_imagenet_1000_notop.h5' )
submission.to_csv("submission.csv", index=False )<save_to_csv>
for i, layer in enumerate(effnet.layers):
if "batch_normalization" in layer.name:
effnet.layers[i] = GroupNormalization(groups=32, axis=-1, epsilon=0.00001 )
submission.to_csv("submission.csv", index=False )<set_options>
def build_model() :

 model = Sequential()
model.add(effnet)
 model.add(GlobalAveragePooling2D())
 model.add(Dropout(0.5))
model.add(Dense(5, activation=elu))
model.add(Dense(1, activation="linear"))
model.compile(loss='mse',
optimizer=RAdam(lr=0.00005),
metrics=['mse', 'acc'])
 print(model.summary())
 return model
model = build_model()
warnings.filterwarnings("ignore" )<load_from_csv>
class_weight = {0: 1.,
1: 5.,
2: 1.75,
3: 5.,
4: 5.}
train=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/train.csv')
 test=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/test.csv')
 submission=pd.read_csv('/kaggle/input/covid19-global-forecasting-week-4/submission.csv' )<set_options>
kappa_metrics = Metrics()
es = EarlyStopping(monitor='val_loss', mode='auto', verbose=1, patience=12)
 rlr = ReduceLROnPlateau(monitor='val_loss',
factor=0.5,
patience=4,
verbose=1,
mode='auto',
epsilon=0.0001)
 model.fit_generator(train_generator,
steps_per_epoch=train_generator.samples // BATCH_SIZE,
epochs=25,
validation_data=val_generator,
validation_steps = val_generator.samples // BATCH_SIZE,
class_weight = class_weight,
callbacks=[kappa_metrics, es, rlr] )
%matplotlib inline<prepare_x_and_y>
model.load_weights(SAVED_MODEL_NAME)
X_train=train[['Id']]
test['Id']=test['ForecastId']
X_test=test[['Id']]
y_train_cc=train[['ConfirmedCases']]
y_train_ft=train[['Fatalities']]<split>
y_train_preds, train_labels = get_preds_and_labels(model, train_generator)
 y_train_preds = np.rint(y_train_preds ).astype(np.uint8 ).clip(0, 4)
 train_score = cohen_kappa_score(train_labels, y_train_preds, weights="quadratic")
 y_val_preds, val_labels = get_preds_and_labels(model, val_generator)
 y_val_preds = np.rint(y_val_preds ).astype(np.uint8 ).clip(0, 4)
 val_score = cohen_kappa_score(val_labels, y_val_preds, weights="quadratic" )
X_tr=np.array_split(X_train,313)
 y_cc=np.array_split(y_train_cc,313)
 y_ft=np.array_split(y_train_ft,313)
 X_te=np.array_split(X_test,313 )<define_variables>
print(f"The Training Cohen Kappa Score is: {round(train_score, 5)}")
 print(f"The Validation Cohen Kappa Score is: {round(val_score, 5)}" )
a=np.max(X_tr[0] ).values
b=a-71
b=b[0]<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 = 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')
 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']
X_te[0]=X_te[0]+a
for i in range(312):
X_te[i+1]=X_te[0]<define_variables>
y_val_preds, val_labels = get_preds_and_labels(model, val_generator)
 optR = OptimizedRounder()
optR.fit(y_val_preds, val_labels)
 coefficients = optR.coefficients()
opt_val_predictions = optR.predict(y_val_preds, coefficients)
 new_val_score = cohen_kappa_score(val_labels, opt_val_predictions, weights="quadratic" )
for i in range(312):
X_tr[i+1]=X_tr[0]<predict_on_test>
print(f"Optimized Thresholds:
{coefficients}
")
 print(f"The Validation Quadratic Weighted Kappa(QWK)
 \
with optimized rounding thresholds is: {round(new_val_score, 5)}
")
 print(f"This is an improvement of {round(new_val_score - val_score, 5)}
\
over the unoptimized rounding" )
poly=PolynomialFeatures(2)
 y_pred_cc=[]
for i in range(313):
X_tr[i]=poly.fit_transform(X_tr[i])
 X_te[i]=poly.fit_transform(X_te[i])
 model=Lasso()
model.fit(X_tr[i],y_cc[i]);
y_pr_cc=model.predict(X_te[i])
 y_cc[i]= y_cc[i][71:]
y_pr_cc=y_pr_cc[b:]
y_pr_cc=np.append(y_cc[i], y_pr_cc)
 y_pred_cc.append(y_pr_cc); 
<train_model>
test_df['diagnosis'] = np.zeros(test_df.shape[0])
 test_generator = ImageDataGenerator(preprocessing_function=preprocess_image,
rescale=1 / 128.).flow_from_dataframe(test_df,
x_col='id_code',
y_col='diagnosis',
directory=TEST_IMG_PATH,
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=BATCH_SIZE,
class_mode='other',
shuffle=False )
<define_variables><EOS> 
y_test, _ = get_preds_and_labels(model, test_generator)
 y_test = optR.predict(y_test, coefficients ).astype(np.uint8)
 test_df['diagnosis'] = y_test
test_df['id_code'] = test_df['id_code'].str.replace(r'.png$', '')
 test_df.to_csv('submission.csv', index=False )