Kaggle competition: APTOS 2019 Blindness Detection
<SOS> metric: QuadraticWeightedKappa Kaggle data source: aptos-2019-blindness-detection<data_type_conversions>
train_csv = ".. /input/aptos2019-blindness-detection/train.csv"
test_csv = ".. /input/aptos2019-blindness-detection/test.csv"
train_dir = ".. /input/aptos2019-blindness-detection/train_images/"
test_dir = ".. /input/aptos2019-blindness-detection/test_images/"
test['key'] = pd.to_datetime(test['key'],infer_datetime_format = True)
 test['pickup_datetime'] = pd.to_datetime(test['pickup_datetime'],infer_datetime_format = True )<compute_test_metric>
df = pd.read_csv(train_csv)
 size = 256,256
def haversine_distance(lat1,long1, lat2,long2):
data = [train,test]
for i in data:
r = 6371
phi1 = np.radians(i[lat1])
 phi2 = np.radians(i[lat2])
 delta_phi = np.radians(i[lat2]-i[lat1])
 delta_lambda = np.radians(i[long2]-i[long1])
 a = np.sin(delta_phi /2.0)**2 + np.cos(phi1)* np.cos(phi2)* np.sin(delta_lambda / 2.0)** 2
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1-a))
d =(r * c)
 i['H_Distance'] = d
return d<concatenate>
def load_image(path):
img = cv2.resize(cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB), size)
 img = get_cropped_image(img)
 return img
haversine_distance('pickup_latitude', 'pickup_longitude', 'dropoff_latitude', 'dropoff_longitude' )<feature_engineering>
labels = df["diagnosis"].values.tolist()
labels = keras.utils.to_categorical(labels )
data = [train,test]
for i in data:
i['Year'] = i['pickup_datetime'].dt.year
i['Month'] = i['pickup_datetime'].dt.month
i['Date'] = i['pickup_datetime'].dt.day
i['Day of Week'] = i['pickup_datetime'].dt.dayofweek
i['Hour'] = i['pickup_datetime'].dt.hour<filter>
images, x_val, labels, y_val = train_test_split(images, labels, test_size = 0.15 )
train.loc[(( train['pickup_latitude']==0)&(train['pickup_longitude']==0)) &(( train['dropoff_latitude']!=0)&(train['dropoff_longitude']!=0)) &(train['fare_amount']==0)]<drop_column>
train_aug = ImageDataGenerator(horizontal_flip = True,
zoom_range = 0.25,
rotation_range = 360,
vertical_flip = True)
 train_generator = train_aug.flow(images, labels, batch_size = 8 )
train = train.drop(train.loc[(( train['pickup_latitude']==0)&(train['pickup_longitude']==0)) &(( train['dropoff_latitude']!=0)&(train['dropoff_longitude']!=0)) &(train['fare_amount']==0)].index, axis=0 )<filter>
input_layer = Input(shape =(256,256,3))
base_model = DenseNet121(include_top = False, input_tensor = input_layer, weights = ".. /input/densenet-keras/DenseNet-BC-121-32-no-top.h5")
 x = GlobalAveragePooling2D()(base_model.output)
 x = Dropout(0.5 )(x)
 out = Dense(5, activation = 'softmax' )(x)
 model = Model(inputs = input_layer, outputs = out )
train.loc[(( train['pickup_latitude']!=0)&(train['pickup_longitude']!=0)) &(( train['dropoff_latitude']==0)&(train['dropoff_longitude']==0)) &(train['fare_amount']==0)]<drop_column>
optimizer = keras.optimizers.Adam(lr=3e-4)
 es = EarlyStopping(monitor='val_loss', mode='min', patience = 5, restore_best_weights = True)
 rlrop = ReduceLROnPlateau(monitor='val_loss', mode='min', patience = 2, factor = 0.5, min_lr=1e-6)
 callback_list = [es, rlrop]
model.compile(optimizer = optimizer, loss = "categorical_crossentropy", metrics = ["accuracy"] )
train = train.drop(train.loc[(( train['pickup_latitude']!=0)&(train['pickup_longitude']!=0)) &(( train['dropoff_latitude']==0)&(train['dropoff_longitude']==0)) &(train['fare_amount']==0)].index, axis=0 )<filter>
model.fit_generator(generator = train_generator, steps_per_epoch = len(train_generator), epochs = 20, validation_data =(x_val, y_val), callbacks = callback_list )
high_distance = train.loc[(train['H_Distance']>200)&(train['fare_amount']!=0)]<feature_engineering>
del train_generator, images
gc.collect()
high_distance['H_Distance'] = high_distance.apply(
 lambda row:(row['fare_amount'] - 2.50)/1.56,
axis=1
)<train_model>
predprobs = model.predict(test_images )
train.update(high_distance )<filter>
predictions = []
for i in predprobs:
predictions.append(np.argmax(i))
<filter><EOS> 
id_code = test_df["id_code"].values.tolist()
subfile = pd.DataFrame({"id_code":id_code, "diagnosis":predictions})
 subfile.to_csv('submission.csv',index=False )