Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<set_options>
import numpy as np
import pandas as pd
import os
rfe.support_<define_variables>
%matplotlib inline
list(zip(X_train.columns, rfe.support_, rfe.ranking_))<import_modules>
data_dir = '.. /input/plant-seedlings-classification/'
train_dir = os.path.join(data_dir, 'train')
 test_dir = os.path.join(data_dir, 'test')
 sample_submission = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv'))
import statsmodels.api as sm<train_on_grid>
train_dir = pathlib.Path(train_dir)
 image_count = len(list(train_dir.glob('*/*.png')))
 print(image_count )
X_train_sm = sm.add_constant(X_train[col])
 logm1 = sm.GLM(y_train,X_train_sm, family = sm.families.Binomial())
 res = logm1.fit()
res.summary()<predict_on_test>
batch_size = 32
img_height = 260
img_width = 260
y_train_pred = res.predict(X_train_sm)
 y_train_pred[:10]<prepare_x_and_y>
train_ds = tf.keras.preprocessing.image_dataset_from_directory(train_dir,
seed = 7,
image_size =(img_height, img_width),
batch_size = batch_size)
y_train_pred = y_train_pred.values.reshape(-1)
 y_train_pred[:10]<prepare_output>
class_names = train_ds.class_names
num_classes = len(class_names)
 print(class_names)
 print(num_classes)
y_train_pred_final = pd.DataFrame({'Survived':y_train.values, 'Survived_Prob':y_train_pred})
 y_train_pred_final['PassengerId'] = y_train.index
y_train_pred_final.head()<feature_engineering>
img_augmentation = Sequential(
 [
preprocessing.RandomRotation(factor=0.15),
preprocessing.RandomFlip() ,
preprocessing.RandomContrast(factor=0.1),
],
name="img_augmentation",
)
y_train_pred_final['predicted'] = y_train_pred_final.Survived_Prob.map(lambda x: 1 if x > 0.5 else 0)
 y_train_pred_final.head()<import_modules>
def input_preprocess(image, label):
label = tf.one_hot(label, 12)
 return image, label
from sklearn import metrics<compute_test_metric>
train_ds = train_ds.map(
 input_preprocess, num_parallel_calls=tf.data.experimental.AUTOTUNE 
)
confusion = metrics.confusion_matrix(y_train_pred_final.Survived, y_train_pred_final.predicted)
 print(confusion )<compute_test_metric>
strategy = tf.distribute.MirroredStrategy()
print(metrics.accuracy_score(y_train_pred_final.Survived, y_train_pred_final.predicted))<import_modules>
def build_model(num_classes):
inputs = layers.Input(shape=(img_height, img_width, 3))
x = img_augmentation(inputs)
 model = EfficientNetB2(include_top=False, input_tensor=x, weights="imagenet")
 model.trainable = False
x = layers.GlobalAveragePooling2D(name="avg_pool" )(model.output)
 x = layers.BatchNormalization()(x)
 x = layers.Dropout(0.3, name = 'drop2' )(x)
 x = layers.Dense(100, activation = "relu" )(x)
 x = layers.BatchNormalization()(x)
 x = layers.Dropout(0.2, name = 'drop1' )(x)
 x = layers.Dense(50, activation = "relu" )(x)
 x = layers.BatchNormalization()(x)
 top_dropout_rate = 0.2
x = layers.Dropout(top_dropout_rate, name="top_dropout" )(x)
 outputs = layers.Dense(num_classes, activation="softmax", name="pred" )(x)
 model = tf.keras.Model(inputs, outputs, name="EfficientNet")
 optimizer = optimizer=tf.keras.optimizers.Nadam(learning_rate=1e-2)
 model.compile(
 optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"] 
)
 return model
from statsmodels.stats.outliers_influence import variance_inflation_factor<feature_engineering>
epochs = 30
hist = model.fit(train_ds, epochs=epochs, verbose=2 )
vif = pd.DataFrame()
vif['Features'] = X_train[col].columns
vif['VIF'] = [variance_inflation_factor(X_train[col].values, i)for i in range(X_train[col].shape[1])]
vif['VIF'] = round(vif['VIF'], 2)
 vif = vif.sort_values(by = "VIF", ascending = False)
 vif<drop_column>
def unfreeze_model(model):
for layer in model.layers[-35:]:
if not isinstance(layer, layers.BatchNormalization):
layer.trainable = True
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4)
 model.compile(
 optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"] 
)
 unfreeze_model(model)
 model.summary()
col = col.drop(['Parch','SibSp','FamilySize', 'Embarked_S', 'Embarked_Q'], 1)
 col<train_on_grid>
epochs = 30
hist = model.fit(train_ds, epochs=epochs, verbose=2)
 plot_hist(hist )
X_train_sm = sm.add_constant(X_train[col])
 logm2 = sm.GLM(y_train,X_train_sm, family = sm.families.Binomial())
 res = logm2.fit()
res.summary()<predict_on_test>
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-5)
 model.compile(
 optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"] 
)
 epochs = 20
hist = model.fit(train_ds, epochs=epochs, verbose=2)
 plot_hist(hist)
 optimizer = tf.keras.optimizers.Adam(learning_rate=1e-6)
 model.compile(
 optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"] 
)
 epochs = 10
hist = model.fit(train_ds, epochs=epochs, verbose=2)
 plot_hist(hist )
y_train_pred = res.predict(X_train_sm)
 y_train_pred[:10]<prepare_x_and_y>
test = []
for file in os.listdir(test_dir):
test.append(['test/{}'.format(file), file])
 test = pd.DataFrame(test, columns=['filepath', 'file'])
 print(test.head(2))
test.shape
y_train_pred = y_train_pred.values.reshape(-1)
 y_train_pred[:10]<prepare_output>
x_test = np.zeros(( len(test), img_height, img_width, 3), dtype='float32')
 for i, filepath in tqdm(enumerate(test['filepath'])) :
img = keras.preprocessing.image.load_img(os.path.join(data_dir, filepath), target_size =(img_height, img_width))
x = keras.preprocessing.image.img_to_array(img)
 x_test[i] = x
print('test Images shape: {} size: {:,}'.format(x_test.shape, x_test.size))
y_train_pred_final = pd.DataFrame({'Survived':y_train.values, 'Survived_Prob':y_train_pred})
 y_train_pred_final['PassengerId'] = y_train.index
y_train_pred_final.head()<feature_engineering>
one_hot_prediction = model.predict(x_test)
 one_hot_prediction[0:2]
y_train_pred_final['predicted'] = y_train_pred_final.Survived_Prob.map(lambda x: 1 if x > 0.5 else 0)
 y_train_pred_final.head()<compute_test_metric>
preds = np.argmax(one_hot_prediction, axis=1)
 print(preds.shape)
 print(preds)
 prediction = [class_names[i] for i in preds]
print(len(prediction))
print(prediction )
confusion = metrics.confusion_matrix(y_train_pred_final.Survived, y_train_pred_final.predicted)
 print(confusion )<compute_test_metric>
my_submission = pd.DataFrame({'file': test['file'], 'species': prediction})
 print(my_submission.head(5))
print(my_submission.shape )
<import_modules><EOS> 
my_submission.to_csv('output.csv', index=False )