Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<drop_column>
tf.enable_eager_execution()
tf.set_random_seed(32 )
for col in df_train.columns:
if any(df_train[col])== False:
df_train.drop([col], axis=1, inplace=True)
 df_test.drop([col], axis=1, inplace=True )<prepare_x_and_y>
BASE_DIR = '/kaggle/input/aerial-cactus-identification'
IMAGE_PATH = '{}/train/train'.format(BASE_DIR)
 train_df = pd.read_csv("{}/train.csv".format(BASE_DIR))
ids = df_test['Id']
y = df_train['SalePrice']<drop_column>
def read_image(image_id, base_dir=IMAGE_PATH, transformation=False):
train_image = mplimg.imread("{}/{}".format(base_dir, image_id)) / 255.0
if not transformation:
return train_image
train_image = ndimage.rotate(train_image, np.random.choice([0, 1, 2, 3])* 90, mode='nearest')
 if np.random.rand() > 0.5:
train_image = np.flip(train_image, np.random.choice([(0),(1),(0, 1)]))
train_image +(np.random.rand() * 0.2)- 0.1
return train_image
df_train.drop(['Id', 'SalePrice'], axis=1, inplace=True)
 df_test.drop(['Id'], axis=1, inplace=True )<normalization>
shuffled_data = train_df.sample(frac=1)
 TRAIN_SIZE = int(len(train_df)* 0.8)
 VALIDATION_SIZE = len(train_df)- TRAIN_SIZE
train_set = shuffled_data[0:TRAIN_SIZE]
validation_set = shuffled_data[TRAIN_SIZE:]
print(TRAIN_SIZE)
 print(VALIDATION_SIZE )
scaler = StandardScaler()
scaler.fit(df_train)
 train = scaler.transform(df_train)
 test = scaler.transform(df_test )<split>
print("Training distribution:
------")
 print("Positives: {}
Negatives: {}".format(
 list(train_set['has_cactus'] ).count(1),
list(train_set['has_cactus'] ).count(0)))
 print(" = {}
".format(list(train_set['has_cactus'] ).count(1)/ TRAIN_SIZE))
print("Validation distribution:
------")
 print("Positives: {}
Negatives: {}".format(
 list(validation_set['has_cactus'] ).count(1),
list(validation_set['has_cactus'] ).count(0)))
 print(" = {}".format(list(validation_set['has_cactus'] ).count(1)/ VALIDATION_SIZE))
X_train, X_test, y_train, y_test = train_test_split(train, y, test_size=0.25, random_state=1)
 X_test, X_val, y_test, y_val = train_test_split(X_test, y_test, test_size=0.5, random_state=123 )<import_modules>
def train_gen() :
for _, row in balanced_train_set.iterrows() :
yield(read_image(row['id'], transformation=True), row['has_cactus'])
 def validation_gen() :
for _, row in validation_set.iterrows() :
yield(read_image(row['id'], transformation=True), row['has_cactus'] )
from sklearn.metrics import mean_squared_error<import_modules>
BATCH_SIZE = 32
train_ds = tf.data.Dataset.from_generator(train_gen, output_types=(tf.float32, tf.int16)).batch(BATCH_SIZE ).repeat()
validation_ds = tf.data.Dataset.from_generator(validation_gen, output_types=(tf.float32, tf.int16)).batch(BATCH_SIZE ).repeat()
from sklearn.metrics import mean_squared_error<init_hyperparams>
from tensorflow.keras import layers
from tensorflow.keras import regularizers
param_init = {
"max_depth": 5,
"n_estimators": 3000,
"learning_rate": 0.01,
"subsample": 0.5,
"colsample_bytree": 0.7,
"min_child_weight": 1.5,
"reg_alpha": 0.75,
"reg_lambda": 0.4,
"seed": 42,
}<choose_model_class>
model = tf.keras.Sequential([
layers.Conv2D(16,(3, 3), activation='relu', input_shape=IMAGE_DIMENSION),
layers.Conv2D(32,(3, 3), activation='relu'),
layers.BatchNormalization() ,
layers.Conv2D(64,(3, 3), activation='relu'),
layers.Dropout(0.5),
layers.MaxPooling2D(( 2, 2)) ,
layers.Conv2D(64,(3, 3), activation='relu'),
layers.Conv2D(128,(3, 3), activation='relu', kernel_regularizer=regularizers.l2(0.01)) ,
layers.BatchNormalization() ,
layers.Conv2D(256,(3, 3), activation='relu', kernel_regularizer=regularizers.l2(0.01)) ,
layers.Dropout(0.5),
layers.MaxPooling2D(( 2, 2)) ,
layers.Flatten() ,
layers.Dense(1, activation=tf.nn.sigmoid)
 ])
 model.summary()
model.compile(
 optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy']
)
xgb_model = xgboost.XGBRegressor(**param_init )<train_model>
MAX_STEPS_PER_EPOCH = int(BALANCED_TRAIN_SIZE/BATCH_SIZE)
 MAX_VALIDATION_STEPS = int(VALIDATION_SIZE/BATCH_SIZE)* 5
param_fit = {
"eval_metric": "rmse",
"early_stopping_rounds": 500,
"verbose": 200,
"eval_set": [(X_val, y_val)]
}<train_model>
early_stopping = tf.keras.callbacks.EarlyStopping(monitor = 'val_loss', patience=5)
 checkpointer = tf.keras.callbacks.ModelCheckpoint(filepath='weights.hdf5', verbose=1, save_best_only=True)
 logdir = "/tensorboard/scalars/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
 tensorboard = tf.keras.callbacks.TensorBoard(log_dir=logdir)
 history = model.fit_generator(train_ds,
epochs=20,
validation_data=validation_ds,
steps_per_epoch=MAX_STEPS_PER_EPOCH,
validation_steps=MAX_VALIDATION_STEPS,
callbacks=[early_stopping, checkpointer, tensorboard] )
xgb_model = xgb_model.fit(X_train, y_train, **param_fit )<predict_on_test>
model.load_weights('weights.hdf5' )
y_pred_xgb = xgb_model.predict(X_test )<compute_test_metric>
def prediction_gen() :
for _, row in validation_set.iterrows() :
yield read_image(row['id'], transformation=True)
 prediction_ds = tf.data.Dataset.from_generator(prediction_gen, output_types=(tf.float32)).batch(BATCH_SIZE)
 multiple_predictions = [
model.predict_generator(prediction_ds)
 for i in range(10)
 ]
predictions = np.mean(np.array(multiple_predictions), axis=0 )
mean_squared_error(y_test, y_pred_xgb, squared=False )<train_model>
print(accuracy_score(validation_set['has_cactus'], np.round(predictions ).astype('int32')) )
regressorTree = DecisionTreeRegressor(random_state = 0, min_samples_split=2, max_depth=6)
 regressorTree.fit(X_train, y_train )<compute_train_metric>
print(roc_auc_score(validation_set['has_cactus'], predictions))
y_pred_Tree = regressorTree.predict(X_test)
 mean_squared_error(y_test, y_pred_Tree, squared=False )<train_model>
sample_submission = pd.read_csv('{}/sample_submission.csv'.format(BASE_DIR))
sample_submission.head()
regressorGB = GradientBoostingRegressor(
 max_depth=5,
n_estimators=10000,
learning_rate=0.25 
)
 regressorGB.fit(X_train, y_train )<compute_train_metric>
test_ids = os.listdir('{}/test/test'.format(BASE_DIR))
y_pred_GB = regressorGB.predict(X_test)
 mean_squared_error(y_test, y_pred_GB, squared=False )<train_model>
def test_gen() :
for test_id in test_ids:
yield read_image(test_id, base_dir='{}/test/test'.format(BASE_DIR), transformation=True)
 test_ds = tf.data.Dataset.from_generator(test_gen, output_types=(tf.float32)).batch(BATCH_SIZE)
 multiple_test_predictions = [
model.predict_generator(test_ds)
 for i in range(10)
 ]
test_predictions = np.mean(np.array(multiple_test_predictions), axis=0 )
regressorLasso = Lasso(alpha=0.0007)
 regressorLasso.fit(X_train, y_train )<compute_train_metric>
submission = pd.DataFrame({'id': test_ids, 'has_cactus': test_predictions.flatten() })
 submission.head()
<predict_on_test><EOS> 
submission.to_csv('submission.csv', index = False, header = True )