Kaggle competition: Leaf Classification
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 )<count_values>
import pandas as pd
import numpy as np
from sklearn.model_selection import GridSearchCV
import xgboost as xgb
from sklearn.model_selection import cross_val_score
from sklearn.metrics import log_loss
from sklearn.preprocessing import LabelBinarizer
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler
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))<categorify>
train = pd.read_csv('.. /input/leaf-classification/train.csv.zip' , index_col = False)
 train
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'] )<prepare_x_and_y>
x_train = train.drop(['id', 'species'], axis=1 ).values
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()<import_modules>
le = LabelEncoder().fit(train['species'])
 y_train = le.transform(train['species'] )
from tensorflow.keras import layers
from tensorflow.keras import regularizers<choose_model_class>
scaler = StandardScaler().fit(x_train)
 x_train = scaler.transform(x_train )
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']
)<define_variables>
clf = LogisticRegression(solver='lbfgs', multi_class='multinomial')
 clf.fit(x_train, y_train )
MAX_STEPS_PER_EPOCH = int(BALANCED_TRAIN_SIZE/BATCH_SIZE)
 MAX_VALIDATION_STEPS = int(VALIDATION_SIZE/BATCH_SIZE)* 5<train_model>
test = pd.read_csv('.. /input/leaf-classification/test.csv.zip' ,index_col = False)
 test
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] )<load_pretrained>
test_ids = test.pop('id')
 x_test = test.values
model.load_weights('weights.hdf5' )<predict_on_test>
x_test = test.values
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 )<compute_test_metric>
x_test = scaler.transform(x_test)
 y_test = clf.predict_proba(x_test )
print(accuracy_score(validation_set['has_cactus'], np.round(predictions ).astype('int32')) )<compute_test_metric>
submission = pd.DataFrame(y_test, index=test_ids, columns=le.classes_ )
print(roc_auc_score(validation_set['has_cactus'], predictions))<load_from_csv>
submission.to_csv('./submission_leaf_classification.csv')
 print('Done' )