Kaggle competition: APTOS 2019 Blindness Detection
train_data.drop(['key', 'pickup_datetime'], axis=1, inplace=True)
 train_data.head()<feature_engineering>
train_csv = '.. /input/aptos2019-blindness-detection/train.csv'
image_dataset = '.. /input/aptos2019-blindness-detection/train_images'
sample_csv = '.. /input/aptos2019-blindness-detection/sample_submission.csv'
test_dataset = '.. /input/aptos2019-blindness-detection/test_images'
resnet_weights = '.. /input/resnet50-image-weights/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
xception_weights = '.. /input/xception-weights/xception_weights_tf_dim_ordering_tf_kernels_notop.h5'
train_data['pickup_month_cos']=np.cos(( train_data['pickup_month']-1)*(2*(np.pi/12)))
 train_data['pickup_month_sin']=np.sin(( train_data['pickup_month']-1)*(2*(np.pi/12)))
 train_data['pickup_day_cos']=np.cos(( train_data['pickup_day']-1)*(2*(np.pi/30)))
 train_data['pickup_day_sin']=np.sin(( train_data['pickup_day']-1)*(2*(np.pi/30)))
 train_data['pickup_quarter_cos']=np.cos(( train_data['pickup_quarter']-1)*(2*(np.pi/4)))
 train_data['pickup_quarter_sin']=np.sin(( train_data['pickup_quarter']-1)*(2*(np.pi/4)))
 train_data['pickup_hour_cos']=np.cos(( train_data['pickup_hour']-1)*(2*(np.pi/24)))
 train_data['pickup_hour_sin']=np.sin(( train_data['pickup_hour']-1)*(2*(np.pi/24)))
 train_data.head()<feature_engineering>
def cohens_kappa(y_true, y_pred):
y_true_classes = tf.argmax(y_true, 1)
 y_pred_classes = tf.argmax(y_pred, 1)
 return tf.contrib.metrics.cohen_kappa(y_true_classes, y_pred_classes, 5)[1]
train_data['pickup_year_age']=2020-train_data['pickup_year']
train_data.head()<drop_column>

 img = Input(shape=(224,224,3))
base_model = Xception(include_top = False,
weights=xception_weights,
input_tensor=img,
pooling='avg',
input_shape = None)
 final_layer = base_model.layers[-1].output
final_layer = Dropout(0.2 )(final_layer)
 dense_layer1 = Dense(512, activation='relu' )(final_layer)
 dense_layer1 = Dropout(0.3 )(dense_layer1)
 dense_layer2 = Dense(128,activation= 'relu' )(dense_layer1)
 dense_layer2 = Dropout(0.3 )(dense_layer2)
 output_layer = Dense(5,activation='softmax' )(dense_layer2)
 model = Model(img,output_layer)
 for layer in base_model.layers:
layer.trainable = True
model.compile(optimizer = 'adam',loss = 'categorical_crossentropy',metrics = ['accuracy'])
 model.summary()
train_data.drop(['pickup_year','pickup_quarter','pickup_month','pickup_day','pickup_hour'], axis=1, inplace=True)
 train_data.head()<drop_column>
reduce_lr = ReduceLROnPlateau(monitor='val_loss',
min_delta=0.0004,
patience=5,
factor=0.5,
min_lr=1e-6,
mode='auto',
verbose=1 )
train_data.dropna(inplace=True)
 train_data = train_data[(train_data.fare_amount > 0)]<filter>
early_stop = EarlyStopping(monitor='val_loss',
min_delta=0.0001,
patience=10,
verbose=1,
mode='auto' )
train_data = train_data[(train_data.passenger_count > 0)&(train_data.passenger_count < 10)]<compute_test_metric>
df_train = pd.read_csv(train_csv)
 df_train["id_code"] = df_train["id_code"].apply(lambda x:x+".png")
 df_train["diagnosis"] = df_train['diagnosis'].astype('str')
 train_datagen = ImageDataGenerator(rescale = 1/255.,
horizontal_flip = True,
vertical_flip = False,
width_shift_range = 0.1,
height_shift_range = 0.1,
fill_mode = 'nearest',
validation_split = 0.15,
zoom_range = 0.2,
preprocessing_function=preprocess_image,
rotation_range = 10)
 train_generator = train_datagen.flow_from_dataframe(
 dataframe = df_train,
directory = image_dataset,
validation_split = 0.2,
x_col = 'id_code',
y_col = 'diagnosis',
target_size =(224,224),
class_mode = 'categorical',
batch_size = 64,
shaffle=True,
subset = 'training' 
)
 val_generator = train_datagen.flow_from_dataframe(
 dataframe = df_train,
x_col = 'id_code',
y_col = 'diagnosis',
directory = image_dataset,
class_mode = "categorical",
batch_size = 64,
shaffle=True,
target_size =(224,224),
subset = "validation" 
)
 optimizer = Adam(lr=0.0005)
 save_callback = ModelCheckpoint(filepath='Blindness-detection.h5', monitor='val_loss', save_best_only=True)
 model.compile(loss = 'categorical_crossentropy',optimizer = 'adam',metrics = ['accuracy'])
 history = model.fit_generator(
 train_generator,
epochs = 70,
steps_per_epoch = 20,
validation_data = val_generator,
validation_steps = 7,
callbacks = [reduce_lr,early_stop,save_callback]
)
def distance(pickup_latitude, pickup_longitude, dropoff_latitude, dropoff_longitude):
radius = 6371
pickup_latitude, pickup_longitude, dropoff_latitude, dropoff_longitude = map(np.radians,[pickup_latitude, pickup_longitude, dropoff_latitude, dropoff_longitude])
 distance_latitude = dropoff_latitude - pickup_latitude
distance_longitude = dropoff_longitude - pickup_longitude
calculation = np.sin(distance_latitude/2.0)**2 + np.cos(pickup_latitude)* np.cos(dropoff_latitude)* np.sin(distance_longitude/2.0)**2
return 2 * radius * np.arcsin(np.sqrt(calculation))
train_data['distance'] = distance(train_data['pickup_latitude'], train_data['pickup_longitude'], train_data['dropoff_latitude'], train_data['dropoff_longitude'] )<define_variables>
test_datagen = ImageDataGenerator(rescale=1./255,
preprocessing_function=preprocess_image)
 sample_df = pd.read_csv(sample_csv)
 sample_df["id_code"]=sample_df["id_code"].apply(lambda x:x+".png")
 test_generator = test_datagen.flow_from_dataframe(
 dataframe=sample_df,
directory = test_dataset,
x_col="id_code",
target_size =(224,224),
batch_size = 1,
shuffle = False,
class_mode = None 
)
 preds = model.predict_generator(
 test_generator,
steps=len(test_generator.filenames)
)
train_data = train_data[(train_data.pickup_longitude > -80)&(train_data.pickup_longitude < -70)&(train_data.pickup_latitude > 35)&(train_data.pickup_latitude < 45)&(train_data.dropoff_longitude > -80)&(train_data.dropoff_longitude < -70)& 
(train_data.dropoff_latitude > 35)&(train_data.dropoff_latitude < 45)]<split>
y_true = val_generator.classes
y_pred = np.argmax(model.predict_generator(val_generator),axis=1)
 print(confusion_matrix(y_true,y_pred))
target_names = ['0','1','2','3','4']
print(classification_report(val_generator.classes, y_pred, target_names=target_names))
<train_on_grid><EOS> 
filenames= test_generator.filenames
results=pd.DataFrame({"id_code":filenames,
"diagnosis":np.argmax(preds,axis = 1)})
 results['id_code'] = results['id_code'].map(lambda x: str(x)[:-4])
 results.to_csv("submission.csv",index=False )