Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<import_modules>
EFFNET = 5
import seaborn as sns<define_variables>
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/'
SAVED_MODEL_NAME = 'effnet_b{}_model.h5'.format(EFFNET)
 seed = 1234
rn.seed(seed)
 np.random.seed(seed)
 tf.set_random_seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 t_start = time.time()
sub_path = ".. /input/siimisic-submission-files"
all_files = os.listdir(sub_path)
 all_files<load_from_csv>
!pip install.. /input/efficientnet100
!pip install.. /input/kerasradam/
!pip install.. /input/kerascontrib/
!pip install.. /input/ipythonautotime/
%load_ext autotime
outs = [pd.read_csv(os.path.join(sub_path, f), index_col=0)for f in all_files]
concat_sub = pd.concat(outs, axis=1)
 cols = list(map(lambda x: "target" + str(x), range(len(concat_sub.columns))))
concat_sub.columns = cols
concat_sub.reset_index(inplace=True)
 concat_sub.head()
ncol = concat_sub.shape[1]<feature_engineering>
exec('
concat_sub['target_max'] = concat_sub.iloc[:, 1:ncol].max(axis=1)
 concat_sub['target_min'] = concat_sub.iloc[:, 1:ncol].min(axis=1)
 concat_sub['target_mean'] = concat_sub.iloc[:, 1:ncol].mean(axis=1)
 concat_sub['target_median'] = concat_sub.iloc[:, 1:ncol].median(axis=1 )<define_variables>
TRAIN_TEST_RATIO = 1 -(70 + 15)/(70 + 15 + 15)
 TRAIN_VAL_RATIO = 1 - 70 /(70 + 15 )
cutoff_lo = 0.002
cutoff_hi = 0.11<save_to_csv>
print("Image IDs and Labels(TRAIN)")
 train_df = pd.read_csv(TRAIN_DF_PATH ).sample(frac=1, random_state=seed)
 train_df['id_code'] = train_df['id_code'] + ".png"
print(f"Training images: {train_df.shape[0]}")
 print("Image IDs(TEST)")
 test_df = pd.read_csv(TEST_DF_PATH)
 test_df['id_code'] = test_df['id_code'] + ".png"
test_df = test_df.rename(columns={'diagnosis': 'label'})
 print(f"Testing Images: {test_df.shape[0]}")
concat_sub['target'] = concat_sub['target_mean']
concat_sub[['image_name', 'target']].to_csv('submission_mean.csv',
index=False, float_format='%.6f' )<save_to_csv>
effnet_to_img_size = dict(enumerate(
 [(224, 224),(240, 240),(260, 260),(300, 300),(380, 380),(456, 456),(528, 528),(600, 600)] 
))
IMG_WIDTH, IMG_HEIGHT = effnet_to_img_size[EFFNET]
CHANNELS = 3
effnet_to_nb_epochs = dict(enumerate(
 [30, 30, 30, 30, 30, 25, 20, 15] 
))
EPOCHS = effnet_to_nb_epochs[EFFNET]
concat_sub['target'] = concat_sub['target_median']
concat_sub[['image_name', 'target']].to_csv('submission_median.csv',
index=False, float_format='%.6f' )<save_to_csv>
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().astype(int )
concat_sub['target'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1), 1,
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
0, concat_sub['target_median']))
concat_sub[['image_name', 'target']].to_csv('submission_pushout_median.csv',
index=False, float_format='%.6f' )<feature_engineering>
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
concat_sub['target'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['target_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['target_min'],
concat_sub['target_mean']))
concat_sub[['image_name', 'target']].to_csv('submission_minmax_mean.csv',
index=False, float_format='%.6f' )<feature_engineering>
y_labels = train_df['diagnosis'].values
concat_sub['target'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['target_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['target_min'],
concat_sub['target_median']))
concat_sub[['image_name', 'target']].to_csv('submission_minmax_median.csv',
index=False, float_format='%.6f' )<install_modules>
BATCH_SIZE = 4
train_datagen = ImageDataGenerator(rotation_range=360,
horizontal_flip=True,
vertical_flip=True,
validation_split=TRAIN_VAL_RATIO,
preprocessing_function=preprocess_image,
rescale=1 / 255.)
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',
seed=seed)
 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',
seed=seed )
!pip install -q efficientnet<import_modules>
effnet = EfficientNet(weights=None,
include_top=False,
input_shape=(IMG_WIDTH, IMG_HEIGHT, CHANNELS))
effnet.load_weights(
 '.. /input/efficientnet-weights-for-keras/advprop/notop/efficientnet-b{}_weights_imagenet_1000_advprop_notop.h5'.format(
 EFFNET 
)
 )
import os
import re
import numpy as np
import pandas as pd
import math
from matplotlib import pyplot as plt
from sklearn import metrics
from sklearn.model_selection import train_test_split
import tensorflow as tf
import tensorflow.keras.layers as L
import efficientnet.tfkeras as efn
from kaggle_datasets import KaggleDatasets<define_variables>
for i, layer in enumerate(effnet.layers):
if "batch_normalization" in layer.name:
effnet.layers[i] = GroupNormalization(groups=32, axis=-1, epsilon=0.00001 )
TRAINING = 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(learning_rate=0.00005),
metrics=['mse', 'acc'])
 print(model.summary())
 return model
model = build_model()
try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
print("REPLICAS: ", strategy.num_replicas_in_sync )<define_variables>
kappa_metrics = Metrics()
es = EarlyStopping(monitor='val_loss', mode='auto', verbose=1, patience=EPOCHS // 2)
 rlr = ReduceLROnPlateau(monitor='val_loss',
factor=0.5,
patience=EPOCHS // 4,
verbose=1,
mode='auto',
epsilon=0.0001)
 model.fit_generator(train_generator,
steps_per_epoch=train_generator.samples // BATCH_SIZE,
epochs=EPOCHS,
validation_data=val_generator,
validation_steps = val_generator.samples // BATCH_SIZE,
callbacks=[kappa_metrics, es, rlr] )
AUTO = tf.data.experimental.AUTOTUNE
GCS_PATH = KaggleDatasets().get_gcs_path('siim-isic-melanoma-classification')
 EPOCHS = 5
BATCH_SIZE = 8 * strategy.num_replicas_in_sync
IMAGE_SIZE = [1024, 1024]<concatenate>
model.load_weights(SAVED_MODEL_NAME )
def append_path(pre):
return np.vectorize(lambda file: os.path.join(GCS_DS_PATH, pre, file))<load_from_csv>
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.astype(int)
 def coefficients(self):

 return self.coef_['x']
sub = pd.read_csv('/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv')
 TRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/tfrecords/train*.tfrec')
 TEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/tfrecords/test*.tfrec' )<split>
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")
 new_acc_val_score = accuracy_score(val_labels, opt_val_predictions )
VALIDATION_FILENAMES = TRAINING_FILENAMES[int(0.8*len(TRAINING_FILENAMES)) :]
TRAINING_FILENAMES = TRAINING_FILENAMES[:int(0.8*len(TRAINING_FILENAMES)) ]<categorify>
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"The Validation Accuracy
\
with optimized rounding thresholds is: {round(new_acc_val_score, 5)}
" )
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"target": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['target'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"image_name": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['image_name']
return image, idnum
def load_dataset(filenames, labeled=True, ordered=False):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
def data_augment(image, label):
image = tf.image.random_flip_left_right(image)
 return image, label
def get_training_dataset() :
dataset = load_dataset(TRAINING_FILENAMES, labeled=True)
 dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_validation_dataset(ordered=False):
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 NUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
print('Dataset: {} training images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_TEST_IMAGES))<choose_model_class>
test_df['diagnosis'] = np.zeros(test_df.shape[0])
 test_generator = ImageDataGenerator(preprocessing_function=preprocess_image,
rescale=1 / 255.).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,
seed=seed )
<choose_model_class><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 )