Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<save_to_csv>
!pip install -U '.. /input/install/efficientnet-0.0.3-py2.py3-none-any.whl'
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['fare_amount'] = rf_predict
submission.to_csv('submission_1.csv', index=False)
 submission.head(20 )<choose_model_class>
df_train = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 df_test = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv' )
lr = linear_model.LinearRegression()
lr.fit(x_train, y_train)
 lr_predict = lr.predict(x_test )<save_to_csv>
y = to_categorical(y, num_classes=NUM_CLASSES)
 train_x, valid_x, train_y, valid_y = train_test_split(x, y, test_size=0.15,
stratify=y, random_state=8)
 print(train_x.shape)
 print(train_y.shape)
 print(valid_x.shape)
 print(valid_y.shape )
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['fare_amount'] = lr_predict
submission.to_csv('submission_2.csv', index=False)
 submission.head(20 )<load_from_csv>
class My_Generator(Sequence):
def __init__(self, image_filenames, labels,
batch_size, is_train=True,
mix=False, augment=False):
self.image_filenames, self.labels = image_filenames, labels
self.batch_size = batch_size
self.is_train = is_train
self.is_augment = augment
if(self.is_train):
self.on_epoch_end()
self.is_mix = mix
def __len__(self):
return int(np.ceil(len(self.image_filenames)/ float(self.batch_size)))
 def __getitem__(self, idx):
batch_x = self.image_filenames[idx * self.batch_size:(idx + 1)* self.batch_size]
batch_y = self.labels[idx * self.batch_size:(idx + 1)* self.batch_size]
if(self.is_train):
return self.train_generate(batch_x, batch_y)
 return self.valid_generate(batch_x, batch_y)
 def on_epoch_end(self):
if(self.is_train):
self.image_filenames, self.labels = shuffle(self.image_filenames, self.labels)
 else:
pass
def mix_up(self, x, y):
lam = np.random.beta(0.2, 0.4)
 ori_index = np.arange(int(len(x)))
 index_array = np.arange(int(len(x)))
 np.random.shuffle(index_array)
 mixed_x = lam * x[ori_index] +(1 - lam)* x[index_array]
mixed_y = lam * y[ori_index] +(1 - lam)* y[index_array]
return mixed_x, mixed_y
def train_generate(self, batch_x, batch_y):
batch_images = []
for(sample, label)in zip(batch_x, batch_y):
img = cv2.imread('.. /input/aptos2019-blindness-detection/train_images/'+sample+'.png')
 img = cv2.resize(img,(SIZE, SIZE))
if(self.is_augment):
img = seq.augment_image(img)
 batch_images.append(img)
 batch_images = np.array(batch_images, np.float32)/ 255
batch_y = np.array(batch_y, np.float32)
 if(self.is_mix):
batch_images, batch_y = self.mix_up(batch_images, batch_y)
 return batch_images, batch_y
def valid_generate(self, batch_x, batch_y):
batch_images = []
for(sample, label)in zip(batch_x, batch_y):
img = cv2.imread('.. /input/aptos2019-blindness-detection/train_images/'+sample+'.png')
 img = cv2.resize(img,(SIZE, SIZE))
batch_images.append(img)
 batch_images = np.array(batch_images, np.float32)/ 255
batch_y = np.array(batch_y, np.float32)
 return batch_images, batch_y
n_train = 1_000_000
n_features = 2
dtype_map = {
'fare_amount': np.float32,
'pickup_longitude': np.float64,
'pickup_latitude': np.float64,
'dropoff_longitude': np.float64,
'dropoff_latitude': np.float64,
'passenger_count': np.int8
}
df = pd.read_csv(".. /input/train.csv", nrows = n_train, parse_dates=["pickup_datetime"], dtype=dtype_map)
 df_test = pd.read_csv('.. /input/test.csv', parse_dates=["pickup_datetime"], dtype=dtype_map )<feature_engineering>
in_lay = Input(shape=(256,256,3))
base_model = EfficientNetB5(weights=None,
input_shape=(SIZE,SIZE,3),
include_top=False 
)
 base_model.load_weights(".. /input/efficientnet-keras-weights-b0b5/efficientnet-b5_imagenet_1000_notop.h5")
 pt_depth = base_model.get_output_shape_at(0)[-1]
pt_features = base_model(in_lay)
 bn_features = BatchNormalization()(pt_features )
def add_travel_vector_features(df):
df['abs_diff_longitude'] =(df.dropoff_longitude - df.pickup_longitude ).abs()
df['abs_diff_latitude'] =(df.dropoff_latitude - df.pickup_latitude ).abs()
df['circ_diff'] = np.sqrt(df.abs_diff_longitude**2 + df.abs_diff_latitude**2)
 add_travel_vector_features(df)
 add_travel_vector_features(df_test)
 print(df.isnull().sum())
 print('Old size: %d' % len(df))
df = df.dropna(how='any', axis='rows')
 print('New size: %d' % len(df))<feature_engineering>
attn_layer = Conv2D(64, kernel_size =(1,1), padding = 'same', activation = 'relu' )(Dropout(0.5 )(bn_features))
attn_layer = Conv2D(16, kernel_size =(1,1), padding = 'same', activation = 'relu' )(attn_layer)
 attn_layer = Conv2D(8, kernel_size =(1,1), padding = 'same', activation = 'relu' )(attn_layer)
 attn_layer = Conv2D(1,
kernel_size =(1,1),
padding = 'valid',
activation = 'sigmoid' )(attn_layer)
 up_c2_w = np.ones(( 1, 1, 1, pt_depth))
up_c2 = Conv2D(pt_depth, kernel_size =(1,1), padding = 'same',
activation = 'linear', use_bias = False, weights = [up_c2_w])
 up_c2.trainable = False
attn_layer = up_c2(attn_layer)
 mask_features = multiply([attn_layer, bn_features])
 gap_features = GlobalAveragePooling2D()(mask_features)
 gap_mask = GlobalAveragePooling2D()(attn_layer)
 gap = Lambda(lambda x: x[0]/x[1], name = 'RescaleGAP' )([gap_features, gap_mask])
 gap_dr = Dropout(0.25 )(gap)
 dr_steps = Dropout(0.25 )(Dense(128, activation = 'relu' )(gap_dr))
out_layer = Dense(5, activation = 'softmax' )(dr_steps)
 retina_model = Model(inputs = [in_lay], outputs = [out_layer] )
min_year = df.pickup_datetime.apply(lambda t: t.year ).min()
df['pickup_year'] = df.pickup_datetime.apply(lambda t: t.year - min_year)
 df['pickup_hour'] = df.pickup_datetime.apply(lambda t: t.hour)
 df['pickup_day'] = df.pickup_datetime.apply(lambda t: t.timetuple().tm_yday)
 df_test['pickup_year'] = df_test.pickup_datetime.apply(lambda t: t.year - min_year)
 df_test['pickup_hour'] = df_test.pickup_datetime.apply(lambda t: t.hour)
 df_test['pickup_day'] = df_test.pickup_datetime.apply(lambda t: t.timetuple().tm_yday)
<split>
EarlyStopping, ReduceLROnPlateau,CSVLogger)
 epochs = 30; batch_size = 16
checkpoint = ModelCheckpoint('.. /working/model_.h5', monitor='val_loss', verbose=1,
save_best_only=True, mode='min', save_weights_only = True)
 reduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=4,
verbose=1, mode='auto', epsilon=0.0001)
 early = EarlyStopping(monitor="val_loss",
mode="min",
patience=9)
 csv_logger = CSVLogger(filename='.. /working/training_log.csv',
separator=',',
append=True)
 train_generator = My_Generator(train_x, train_y, 128, is_train=True)
 train_mixup = My_Generator(train_x, train_y, batch_size, is_train=True, mix=False, augment=True)
 valid_generator = My_Generator(valid_x, valid_y, batch_size, is_train=False )
df_train, df_val = train_test_split(df, test_size=0.1)
 len(df_val )<prepare_x_and_y>
def kappa_loss(y_true, y_pred, y_pow=2, eps=1e-12, N=5, bsize=32, name='kappa'):
with tf.name_scope(name):
y_true = tf.to_float(y_true)
 repeat_op = tf.to_float(tf.tile(tf.reshape(tf.range(0, N), [N, 1]), [1, N]))
repeat_op_sq = tf.square(( repeat_op - tf.transpose(repeat_op)))
 weights = repeat_op_sq / tf.to_float(( N - 1)** 2)
 pred_ = y_pred ** y_pow
try:
pred_norm = pred_ /(eps + tf.reshape(tf.reduce_sum(pred_, 1), [-1, 1]))
except Exception:
pred_norm = pred_ /(eps + tf.reshape(tf.reduce_sum(pred_, 1), [bsize, 1]))
hist_rater_a = tf.reduce_sum(pred_norm, 0)
 hist_rater_b = tf.reduce_sum(y_true, 0)
 conf_mat = tf.matmul(tf.transpose(pred_norm), y_true)
 nom = tf.reduce_sum(weights * conf_mat)
 denom = tf.reduce_sum(weights * tf.matmul(
 tf.reshape(hist_rater_a, [N, 1]), tf.reshape(hist_rater_b, [1, N])) /
tf.to_float(bsize))
return nom*0.5 /(denom + eps)+ categorical_crossentropy(y_true, y_pred)*0.5
def get_input_matrix(df):
return np.column_stack(( df.abs_diff_longitude, df.abs_diff_latitude, df.circ_diff,
df.dropoff_longitude, df.dropoff_latitude,
df.pickup_longitude, df.pickup_latitude,
df.passenger_count, df.pickup_year,
df.pickup_hour, df.pickup_day))
x_train, x_val = get_input_matrix(df_train), get_input_matrix(df_val)
 y_train, y_val = np.array(df_train.fare_amount), np.array(df_val.fare_amount )<choose_model_class>
class QWKEvaluation(Callback):
def __init__(self, validation_data=() , batch_size=64, interval=1):
super(Callback, self ).__init__()
self.interval = interval
self.batch_size = batch_size
self.valid_generator, self.y_val = validation_data
self.history = []
def on_epoch_end(self, epoch, logs={}):
if epoch % self.interval == 0:
y_pred = self.model.predict_generator(generator=self.valid_generator,
steps=np.ceil(float(len(self.y_val)) / float(self.batch_size)) ,
workers=1, use_multiprocessing=False,
verbose=1)
 def flatten(y):
return np.argmax(y, axis=1 ).reshape(-1)
 score = cohen_kappa_score(flatten(self.y_val),
flatten(y_pred),
labels=[0,1,2,3,4],
weights='quadratic')
 print("
epoch: %d - QWK_score: %.6f
" %(epoch+1, score))
self.history.append(score)
 if score >= max(self.history):
print('saving checkpoint: ', score)
 self.model.save('.. /working/model_bestqwk.h5')
 qwk = QWKEvaluation(validation_data=(valid_generator, valid_y),
batch_size=batch_size, interval=1 )
reg = RandomForestRegressor(max_depth=28, n_estimators=500, oob_score=True, n_jobs=-1,min_samples_split=10, verbose=1 )<train_model>
for layer in retina_model.layers:
layer.trainable = False
for i in range(-3,0):
retina_model.layers[i].trainable = True
retina_model.compile(
 loss='categorical_crossentropy',
optimizer=Adam(1e-3))
retina_model.fit_generator(
 train_generator,
steps_per_epoch=np.ceil(float(len(train_y)) / float(128)) ,
epochs=2,
workers=WORKERS, use_multiprocessing=True,
verbose=1,
callbacks=[qwk] )
reg.fit(x_train, y_train )<find_best_params>
for layer in retina_model.layers:
layer.trainable = True
callbacks_list = [checkpoint, csv_logger, reduceLROnPlat, early, qwk]
retina_model.compile(
 loss=kappa_loss,
optimizer=Adam(lr=1e-4))
retina_model.fit_generator(
 train_mixup,
steps_per_epoch=np.ceil(float(len(train_x)) / float(batch_size)) ,
validation_data=valid_generator,
validation_steps=np.ceil(float(len(valid_x)) / float(batch_size)) ,
epochs=epochs,
verbose=1,
workers=1, use_multiprocessing=False,
callbacks=callbacks_list )
reg.oob_score_<predict_on_test>
layer_names = []
for layer in outputs:
layer_names.append(layer.name.split("/")[0])
 print("Layers going to be used for visualization: ")
 print(layer_names )
y_pred = reg.predict(x_val)
<compute_test_metric>
def show_random_sample(idx):

 img_id = df_train.iloc[idx]['id_code']
img_path = '.. /input/aptos2019-blindness-detection/train_images/'+str(img_id)+'.png'
sample_image = cv2.imread(img_path)
 sample_image = cv2.cvtColor(sample_image, cv2.COLOR_BGR2RGB)
 sample_image = cv2.resize(sample_image,(256, 256))
sample_label = df_train.iloc[idx]["diagnosis"]
sample_image_processed = np.expand_dims(sample_image, axis=0)
 sample_image_processed = preprocess_input(sample_image_processed)
 activations = vis_model.predict(sample_image_processed)
 pred_label = np.argmax(retina_model.predict(sample_image_processed), axis=-1)[0]
sample_activation = activations[1][0,:,:,8]
sample_activation-=sample_activation.mean()
sample_activation/=sample_activation.std()
sample_activation *=255
sample_activation = np.clip(sample_activation, 0, 255 ).astype(np.uint8)
 heatmap = get_CAM(sample_image_processed, pred_label)
 heatmap = cv2.resize(heatmap,(sample_image.shape[0], sample_image.shape[1]))
heatmap = heatmap *255
heatmap = np.clip(heatmap, 0, 255 ).astype(np.uint8)
 heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
 super_imposed_image = heatmap * 0.5 + sample_image
super_imposed_image = np.clip(super_imposed_image, 0,255 ).astype(np.uint8)
 f,ax = plt.subplots(2,2, figsize=(10,8))
ax[0,0].imshow(sample_image)
 ax[0,0].set_title(f"True label: {sample_label}
Predicted label: {pred_label}")
 ax[0,0].axis('off')
 ax[0,1].imshow(sample_activation)
 ax[0,1].set_title("Random feature map")
 ax[0,1].axis('off')
 ax[1,0].imshow(heatmap)
 ax[1,0].set_title("Class Activation Map")
 ax[1,0].axis('off')
 ax[1,1].imshow(super_imposed_image)
 ax[1,1].set_title("Activation map superimposed")
 ax[1,1].axis('off')
 plt.show()
return activations
score = r2_score(y_val, y_pred)
 score<compute_test_metric>
submit = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 retina_model.load_weights('.. /working/model_bestqwk.h5')
 predicted = []
reg.score(x_val, y_val )<compute_test_metric>
for i, name in tqdm(enumerate(submit['id_code'])) :
path = os.path.join('.. /input/aptos2019-blindness-detection/test_images/', name+'.png')
 image = cv2.imread(path)
 image = cv2.resize(image,(SIZE, SIZE))
score_predict = retina_model.predict(( image[np.newaxis])/255)
 label_predict = np.argmax(score_predict)
 predicted.append(str(label_predict))
<compute_test_metric><EOS> 
submit['diagnosis'] = predicted
submit.to_csv('submission.csv', index=False)
 submit.head()