Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<prepare_x_and_y>
%matplotlib inline
NFOLDS = 5
folds = KFold(n_splits=NFOLDS)
 columns = X.columns
splits = folds.split(X, y)
 y_preds_xgb = np.zeros(test_df.shape[0])
 y_train_xgb = np.zeros(X.shape[0])
 y_oof_xgb = np.zeros(X.shape[0])
 train_df_set = xgb.DMatrix(X)
 test_set = xgb.DMatrix(test_df)
 for fold_n,(train_index, valid_index)in enumerate(splits):
print('Fold:',fold_n+1)
 X_train, X_valid = X[columns].iloc[train_index], X[columns].iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
train_set = xgb.DMatrix(X_train, y_train)
 val_set = xgb.DMatrix(X_valid, y_valid)
 clf = xgb.train(params_xgb, train_set, num_boost_round=xgb_num_boost_round_max, evals=[(train_set, 'train'),(val_set, 'val')], verbose_eval=100)
 y_train_xgb += clf.predict(train_df_set)/ NFOLDS
y_preds_xgb += clf.predict(test_set)/ NFOLDS
del X_train, X_valid, y_train, y_valid
gc.collect()<normalization>
input_path = '.. /input/'
train_path = input_path + 'train/train/'
test_path = input_path + 'test/test/'
train_df = pd.read_csv(input_path + 'train.csv')
 sample = pd.read_csv(input_path + 'sample_submission.csv')
 train_id = train_df['id']
labels = train_df['has_cactus']
test_id = sample['id']
%%time
scaler = StandardScaler()
train_log = pd.DataFrame(
 scaler.fit_transform(X),
columns=X.columns,
index=X.index 
)
 test_log = pd.DataFrame(
 scaler.transform(test_df),
columns=test_df.columns,
index=test_df.index
)<train_on_grid>
x_train, x_val, y_train, y_val = train_test_split(train_id, labels, test_size=0.2 )
logreg = LogisticRegression()
logreg.fit(train_log, y)
 coeff_logreg = pd.DataFrame(train_log.columns.delete(0))
coeff_logreg.columns = ['feature']
coeff_logreg["score_logreg"] = pd.Series(logreg.coef_[0])
 coeff_logreg.sort_values(by='score_logreg', ascending=False )<load_pretrained>
def fetch_images(ids, filepath):
arr = []
for img_id in ids:
img = plt.imread(filepath + img_id)
 arr.append(img)
 arr = np.array(arr ).astype('float32')
 arr = arr / 255
return arr
eli5.show_weights(logreg )<predict_on_test>
x_train = fetch_images(ids=x_train, filepath=train_path)
 x_val = fetch_images(ids=x_val, filepath=train_path)
 test = fetch_images(ids=test_id, filepath=test_path)
 img_dim = x_train.shape[1:]
y_logreg_train = logreg.predict(train_log)
 y_logreg_pred = logreg.predict(test_log )<compute_test_metric>
from keras.models import Model
from keras.layers import Input, Dense, Flatten, Dropout, LeakyReLU, Activation
from keras.layers.normalization import BatchNormalization
from keras.applications.densenet import DenseNet201
def plot_cm(y_true, y_pred, title, figsize=(7,6)) :
y_pred = y_pred.round().astype(int)
 cm = confusion_matrix(y_true, y_pred, labels=np.unique(y_true))
cm_sum = np.sum(cm, axis=1, keepdims=True)
 cm_perc = cm / cm_sum.astype(float)* 100
annot = np.empty_like(cm ).astype(str)
 nrows, ncols = cm.shape
for i in range(nrows):
for j in range(ncols):
c = cm[i, j]
p = cm_perc[i, j]
if i == j:
s = cm_sum[i]
annot[i, j] = '%.1f%%
%d/%d' %(p, c, s)
 elif c == 0:
annot[i, j] = ''
else:
annot[i, j] = '%.1f%%
%d' %(p, c)
 cm = pd.DataFrame(cm, index=np.unique(y_true), columns=np.unique(y_true))
cm.index.name = 'Actual'
cm.columns.name = 'Predicted'
fig, ax = plt.subplots(figsize=figsize)
 plt.title(title)
 sns.heatmap(cm, cmap= "YlGnBu", annot=annot, fmt='', ax=ax )<prepare_output>
batch_size = 64
epochs = 30
steps = x_train.shape[0] // batch_size
y_preds = w_lgb*y_preds_lgb + w_xgb*y_preds_xgb + w_logreg*y_logreg_pred<prepare_output>
inputs = Input(shape=img_dim)
 densenet201 = DenseNet201(weights='imagenet', include_top=False )(inputs)
 flat1 = Flatten()(densenet201)
 dense1 = Dense(units=256, use_bias=True )(flat1)
 batchnorm1 = BatchNormalization()(dense1)
 act1 = Activation(activation='relu' )(batchnorm1)
 drop1 = Dropout(rate=0.5 )(act1)
 out = Dense(units=1, activation='sigmoid' )(drop1)
 model = Model(inputs=inputs, outputs=out)
 model.compile(optimizer='adam', loss='binary_crossentropy' )
sub['Pred'] = y_preds
sub.head()<save_to_csv>
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.3, patience=3, verbose=2, mode='max')
 img_aug = ImageDataGenerator(rotation_range=20, vertical_flip=True, horizontal_flip=True)
 img_aug.fit(x_train )
sub.to_csv('submission.csv', index=False )<save_to_csv>
model.fit_generator(img_aug.flow(x_train, y_train, batch_size=batch_size),
steps_per_epoch=steps, epochs=epochs,
validation_data=(x_val, y_val), callbacks=[reduce_lr],
verbose=2 )
sub.to_csv('submission.csv', index=False )<set_options>
test_pred = model.predict(test, verbose=2 )
pd.set_option('display.max_columns', 500)
 pd.set_option('display.max_rows', 50)
<import_modules>
sample['has_cactus'] = test_pred
sample.to_csv('densenet_model.csv', index=False )