Kaggle competition: APTOS 2019 Blindness Detection
test['year'] = [test.pickup_datetime[i].year for i in test.index]<feature_engineering>
sns.set()
%matplotlib inline
EPOCHS = 50
BATCH_SIZE = 16
SEED = 20031976
LRATE = 0.00005
VERBOSE=0
df['day'] = [df.pickup_datetime[i].day for i in df.index]<feature_engineering>
np.random.seed(SEED)
 tf.set_random_seed(SEED)
 train_df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/train.csv')
 test_df = pd.read_csv('/kaggle/input/aptos2019-blindness-detection/test.csv')
 print("Datasets loaded.. ")
 print('Train DF Shape', train_df.shape )
test['day'] = [test.pickup_datetime[i].day for i in test.index]<drop_column>
x_resampled, y_resampled = SMOTE(random_state=SEED ).fit_sample(x_train.reshape(x_train.shape[0], -1), train_df['diagnosis'].ravel())
 print("x_resampled.shape=",x_resampled.shape)
 print("y_resampled.shape=",y_resampled.shape)
 x_train = x_resampled.reshape(x_resampled.shape[0], 224, 224, 3)
 y_train = pd.get_dummies(y_resampled ).values
print("x_train.shape=",x_train.shape)
 print("y_train.shape=",y_train.shape )
feat = df.drop(['key','pickup_datetime'],axis=1)
 test = test.drop(['key','pickup_datetime'],axis=1 )<categorify>
y_train_multi = np.empty(y_train.shape, dtype=y_train.dtype)
 y_train_multi[:, 4] = y_train[:, 4]
for i in range(3, -1, -1):
y_train_multi[:, i] = np.logical_or(y_train[:, i], y_train_multi[:, i+1])
 print("Original y_train:", y_train.sum(axis=0))
print("Multilabel version:", y_train_multi.sum(axis=0))
feat = pd.concat([feat,pd.get_dummies(feat.year)],axis=1)
 test = pd.concat([test,pd.get_dummies(test.year)],axis=1 )<drop_column>
x_sptrain, x_spval, y_sptrain, y_spval = train_test_split(
 x_train, y_train_multi,
test_size=0.10,
random_state=SEED 
)
 print("train-validation splitted..." )
feat = feat.drop('year',axis=1 )<drop_column>
def create_datagen() :
return ImageDataGenerator(
 zoom_range=0.10,
fill_mode='constant',
cval=0.,
horizontal_flip=True,
vertical_flip=True, 
)
 data_generator = create_datagen().flow(x_sptrain, y_sptrain, batch_size=BATCH_SIZE, seed=SEED)
 print("Image data augmentated..." )
test = test.drop('year',axis=1 )<define_variables>
def precision(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
 precision = true_positives /(predicted_positives + K.epsilon())
 return precision
def recall(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
 possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
 recall = true_positives /(possible_positives + K.epsilon())
 return recall
def fbeta_score(y_true, y_pred, beta=1):
if beta < 0:
raise ValueError('The lowest choosable beta is zero(only precision ).')
 if K.sum(K.round(K.clip(y_true, 0, 1)))== 0:
return 0
p = precision(y_true, y_pred)
 r = recall(y_true, y_pred)
 bb = beta ** 2
fbeta_score =(1 + bb)*(p * r)/(bb * p + r + K.epsilon())
 return fbeta_score
def fmeasure(y_true, y_pred):
return fbeta_score(y_true, y_pred, beta=1)
 def mean_pred(y_true, y_pred):
return K.mean(y_pred)
 def f1_score(y_true, y_pred):
p = precision(y_true, y_pred)
 r = recall(y_true, y_pred)
 return 2*(p*r)/(p+r+K.epsilon())
 print("Evaluation metrics defined...")
label = feat['fare_amount']<drop_column>
from keras.applications import DenseNet169
feat = feat.drop('fare_amount',axis=1 )<split>
densenet = DenseNet169(
 weights='/kaggle/input/densenet-keras/DenseNet-BC-169-32-no-top.h5',
include_top=False,
input_shape=(224,224,3)
)
 model = Sequential()
model.add(densenet)
 model.add(layers.GlobalAveragePooling2D())
 model.add(layers.Dropout(0.2))
model.add(layers.Dense(5, activation='sigmoid'))
model.compile(
 loss='binary_crossentropy',
optimizer=Adam(lr=LRATE),
metrics=['accuracy',mean_pred, precision, recall, f1_score, fbeta_score, fmeasure] 
)
 model.summary()
xtr,xts,ytr,yts = train_test_split(feat,label )<prepare_x_and_y>
class KappaMetrics(Callback):
def on_train_begin(self, logs={}):
self.val_kappas = []
def on_epoch_end(self, epoch, logs={}):
X_val, y_val = self.validation_data[:2]
y_val = y_val.sum(axis=1)- 1
y_pred = self.model.predict(X_val)> 0.5
y_pred = y_pred.astype(int ).sum(axis=1)- 1
_val_kappa = cohen_kappa_score(
 y_val,
y_pred,
weights='quadratic' 
)
 self.val_kappas.append(_val_kappa)
 print(f"Epoch: {epoch+1} val_kappa: {_val_kappa:.4f}")
 if _val_kappa == max(self.val_kappas):
print("Validation Kappa has improved.Saving model.")
 self.model.save('model.h5')
 return
kappa_score = KappaMetrics()
history = model.fit_generator(
 data_generator,
steps_per_epoch=x_train.shape[0] / BATCH_SIZE,
epochs=EPOCHS,
validation_data=(x_spval, y_spval),
callbacks=[kappa_score],
verbose=VERBOSE
)
xgbtrain = xgboost.DMatrix(xtr,ytr)
 xgbtest = xgboost.DMatrix(xts,yts)
 xgbfinaltest = xgboost.DMatrix(test )<init_hyperparams>
with open('history.json', 'w')as f:
json.dump(history.history, f)
 history_df = pd.DataFrame(history.history)
 history_df.head(EPOCHS )
<train_model><EOS> 
y_test = model.predict(x_test)> 0.5
y_test = y_test.astype(int ).sum(axis=1)- 1
test_df['diagnosis'] = y_test
test_df.to_csv('submission.csv',index=False)
 display(test_df.head(5))