Kaggle competition: APTOS 2019 Blindness Detection
l = df[(df.pickup_latitude > 42.5)|(df.pickup_latitude < 40.0)|(df.dropoff_latitude > 42.5)|(df.dropoff_latitude < 40.0)|(df.pickup_longitude > -73.0)|(df.pickup_longitude < -75.0)|(df.dropoff_longitude > -73.0)|(df.dropoff_longitude < -75.0)].index<drop_column>
sns.set()
%matplotlib inline
EPOCHS = 150
BATCH_SIZE = 16
SEED = 20031976
LRATE = 0.0001
VERBOSE=0
df = df.drop(l,axis=0 )<filter>
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 )
z = df[(df.fare_amount > 350.0)|(df.fare_amount < 0.0)|(df.passenger_count > 7.0)|(df.passenger_count < 0.0)].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 )
df = df.drop(z,axis=0 )<compute_test_metric>
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))
def distlatlong(lon1,lat1,lon2,lat2):
lat1=radians(lat1)
 lat2=radians(lat2)
 lon1 = radians(lon1)
 lon2 = radians(lon2)
 dlon = lon2 - lon1
dlat = lat2 - lat1
a =(sin(dlat/2)) **2 + cos(lat1)* cos(lat2)*(sin(dlon/2)) **2
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = 6373.0 * c
return distance 
<feature_engineering>
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..." )
df['dist'] = [distlatlong(df.pickup_longitude[i],df.pickup_latitude[i],df.dropoff_longitude[i],df.dropoff_latitude[i])for i in df.index]<feature_engineering>
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['dist'] = [distlatlong(test.pickup_longitude[i],test.pickup_latitude[i],test.dropoff_longitude[i],test.dropoff_latitude[i])for i in test.index]<data_type_conversions>
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...")
df['pickup_datetime'] = pd.to_datetime(df['pickup_datetime'] )<data_type_conversions>
from keras.applications import DenseNet169,DenseNet121
test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'] )<feature_engineering>
densenet = DenseNet121(
 weights='/kaggle/input/densenet-keras/DenseNet-BC-121-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()
df['latenights'] = [1 if df.pickup_datetime[i].hour < 5 else 0 for i in df.index]
test['latenights'] = [1 if test.pickup_datetime[i].hour < 5 else 0 for i in test.index]<feature_engineering>
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
)
df['weekday'] = [1 if df.pickup_datetime[i].weekday() > 4 else 0 for i in df.index]
test['weekday'] = [1 if test.pickup_datetime[i].weekday() > 4 else 0 for i in test.index] 
<feature_engineering>
with open('history.json', 'w')as f:
json.dump(history.history, f)
 history_df = pd.DataFrame(history.history)
 history_df.head(EPOCHS )
df['year'] = [df.pickup_datetime[i].year for i in df.index]<feature_engineering>
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))