Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<feature_engineering>
import numpy as np
import pandas as pd
import os
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<find_best_params>
%matplotlib inline
fpr, tpr, thresholds = metrics.roc_curve(y_train_pred_final.Survived, y_train_pred_final.Survived_Prob, drop_intermediate = False )<feature_engineering>
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'))
numbers = [float(x)/10 for x in range(10)]
for i in numbers:
y_train_pred_final[i]= y_train_pred_final.Survived_Prob.map(lambda x: 1 if x > i else 0)
 y_train_pred_final.head()<create_dataframe>
train_dir = pathlib.Path(train_dir)
 image_count = len(list(train_dir.glob('*/*.png')))
 print(image_count )
cutoff_df = pd.DataFrame(columns = ['prob','accuracy','sensi','speci'])
 num = [0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9]
for i in num:
cm1 = metrics.confusion_matrix(y_train_pred_final.Survived, y_train_pred_final[i])
 total1=sum(sum(cm1))
accuracy =(cm1[0,0]+cm1[1,1])/total1
speci = cm1[0,0]/(cm1[0,0]+cm1[0,1])
 sensi = cm1[1,1]/(cm1[1,0]+cm1[1,1])
 cutoff_df.loc[i] =[ i ,accuracy,sensi,speci]
print(cutoff_df )<feature_engineering>
batch_size = 32
img_height = 224
img_width = 224
y_train_pred_final['final_predicted'] = y_train_pred_final.Survived_Prob.map(lambda x: 1 if x > 0.56 else 0)
 y_train_pred_final.head()<compute_test_metric>
train_ds = tf.keras.preprocessing.image_dataset_from_directory(train_dir,
validation_split = 0.1,
subset = "training",
seed = 1337,
image_size =(img_height, img_width),
batch_size = batch_size)
 val_ds = tf.keras.preprocessing.image_dataset_from_directory(train_dir,
validation_split = 0.1,
subset = "validation",
seed = 1337,
image_size =(img_height, img_width),
batch_size = batch_size )
metrics.accuracy_score(y_train_pred_final.Survived, y_train_pred_final.final_predicted )<compute_test_metric>
class_names = train_ds.class_names
num_classes = len(class_names)
 print(class_names)
 print(num_classes)
confusion2 = metrics.confusion_matrix(y_train_pred_final.Survived, y_train_pred_final.final_predicted)
 confusion2<feature_engineering>
img_augmentation = Sequential(
 [
preprocessing.RandomRotation(factor=0.15),
preprocessing.RandomFlip() ,
preprocessing.RandomContrast(factor=0.1),
],
name="img_augmentation",
)
X_test_sm = sm.add_constant(X_test )<predict_on_test>
def input_preprocess(image, label):
label = tf.one_hot(label, 12)
 return image, label
y_test_pred = res.predict(X_test_sm)
 y_test_pred[:10]<prepare_output>
train_ds = train_ds.map(
 input_preprocess, num_parallel_calls=tf.data.experimental.AUTOTUNE 
)
 val_ds = val_ds.map(input_preprocess )
y_pred_1 = pd.DataFrame(y_test_pred)
 y_pred_1.head()<create_dataframe>
strategy = tf.distribute.MirroredStrategy()
y_test_df = pd.DataFrame(y_test)
 y_test_df['PassengerId'] = y_test_df.index
y_pred_1.reset_index(drop=True, inplace=True)
 y_test_df.reset_index(drop=True, inplace=True)
 y_pred_final = pd.concat([y_test_df, y_pred_1],axis=1)
 y_pred_final.head()<rename_columns>
with strategy.scope() :
inputs = layers.Input(shape=(img_height, img_width, 3))
x = img_augmentation(inputs)
 outputs = EfficientNetB0(include_top=True, weights=None, classes=12 )(x)
 model = tf.keras.Model(inputs, outputs)
 model.compile(
 optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] 
)
 model.summary()
epochs = 40
hist = model.fit(train_ds, epochs=epochs, batch_size = batch_size, validation_data=val_ds, verbose=2 )
y_pred_final= y_pred_final.rename(columns={ 0 : 'Survived_Prob'})
 y_pred_final.head()<feature_engineering>
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_pred_final['final_predicted'] = y_pred_final.Survived_Prob.map(lambda x: 1 if x > 0.47 else 0)
 y_pred_final.head()<compute_test_metric>
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))
metrics.accuracy_score(y_pred_final.Survived, y_pred_final.final_predicted )<prepare_output>
one_hot_prediction = model.predict(x_test)
 one_hot_prediction[0:2]
data_val_Id = data_val['PassengerId']
data_val = data_val[col]
data_val.head()<predict_on_test>
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 )
data_val_sm = sm.add_constant(data_val)
 y_val_pred = res.predict(data_val_sm)
 y_val_pred[:10]<prepare_output>
my_submission = pd.DataFrame({'file': test['file'], 'species': prediction})
 print(my_submission.head(5))
print(my_submission.shape )
y_val_1 = pd.DataFrame(y_val_pred)
 y_val_1.head()<feature_engineering>
my_submission.to_csv('output.csv', index=False )