Kaggle competition: Plant Seedlings Classification
<SOS> metric: MeanFScore Kaggle data source: plant-seedlings-classification<compute_train_metric>
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import tensorflow as tf
import os
from IPython.display import clear_output
sgd_clf = SGDClassifier(max_iter=100, random_state=42, tol=0.001)
 sgd_scores = cross_val_score(sgd_clf, X_train, y_train, cv=10)
 sgd_scores.mean()<compute_train_metric>
CATEGORIES = os.listdir("/kaggle/input/plant-seedlings-classification/train")
 CATEGORIES
knn_clf = KNeighborsClassifier(n_jobs=-1, weights='distance', n_neighbors=4)
 knn_scores = cross_val_score(knn_clf, X_train, y_train, cv=10)
 knn_scores.mean()<feature_engineering>
X=[]
Y=[]
IMG_S1,IMG_S2 = 140,140
def createTrainingData() :

 a=0
pathh = "/kaggle/input/plant-seedlings-classification/"
types = ["train"]
for typ in types:
datadir = os.path.join(pathh,typ)
 for ele in CATEGORIES:
PATH = os.path.join(datadir,ele)
 class_num = CATEGORIES.index(ele)
 for img in os.listdir(PATH):
image = os.path.join(PATH, img)
 image = cv2.imread(image, cv2.IMREAD_ANYCOLOR)
 image = cv2.resize(image ,(IMG_S1, IMG_S2))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 X.append(image)
 X.append(cv2.flip(image,-1))
Y.append(class_num)
 Y.append(class_num)
 a+=1
clear_output()
print(a)
train_data['AgeBucket'] = train_data['Age'] // 15 * 15
train_data[['AgeBucket','Survived']].groupby(['AgeBucket'] ).mean()<create_dataframe>
createTrainingData()
train_data["RelativesOnboard"] = train_data["SibSp"] + train_data["Parch"]
pd.pivot_table(train_data, index="RelativesOnboard", values="Survived", aggfunc="mean" )<categorify>
def one_hottie(labels,C):

 One_hot_matrix = tf.one_hot(labels,C)
 return tf.keras.backend.eval(One_hot_matrix)
 Y = one_hottie(Y, 12)
 print("Y shape: " + str(Y.shape))
age_ix, sibsp_ix, parch_ix = 1, 2, 3
class CombinedAttributesAdder(BaseEstimator, TransformerMixin):
def __init__(self, add_attributes = True):
self.add_attributes = add_attributes
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
if self.add_attributes:
age_bucket = X[:, age_ix] // 15 * 15
relatives_onboard = X[:, sibsp_ix] + X[:, parch_ix]
return np.c_[X, age_bucket, relatives_onboard]
else:
return np.c_[X]<define_variables>
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.20, random_state = 8)
 del X
del Y
num_pipeline = Pipeline([ 
('selector', DataFrameSelector(["Age", "SibSp", "Parch", "Fare"])) , 
('imputer', SimpleImputer(strategy="median")) , 
('attribs_adder', CombinedAttributesAdder()), 
('std_scaler', StandardScaler())
 ] )<feature_engineering>
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(64, 1, activation=None,kernel_regularizer=tf.keras.regularizers.l2(0.1), input_shape=(140,140,3)) ,
tf.keras.layers.BatchNormalization(axis=3),
tf.keras.layers.LeakyReLU(0.1),
tf.keras.layers.MaxPool2D(strides=2),
tf.keras.layers.Conv2D(128, 3, activation=None,padding="same",kernel_regularizer=tf.keras.regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization(axis=3),
tf.keras.layers.LeakyReLU(0.1),
tf.keras.layers.MaxPool2D(strides=2),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Conv2D(256, 5, activation=None,kernel_regularizer=tf.keras.regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization(axis=3),
tf.keras.layers.LeakyReLU(0.1),
tf.keras.layers.MaxPool2D(strides=2),
tf.keras.layers.Conv2D(64, 5, activation=None,padding="same",kernel_regularizer=tf.keras.regularizers.l2(0.01)) ,
tf.keras.layers.BatchNormalization(axis=3),
tf.keras.layers.LeakyReLU(0.1),
tf.keras.layers.MaxPool2D(strides=2),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(100,kernel_regularizer=tf.keras.regularizers.l2(0.01), activation=None),
tf.keras.layers.BatchNormalization(axis=1),
tf.keras.layers.ReLU() ,
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(50,kernel_regularizer=tf.keras.regularizers.l2(0.01), activation=None),
tf.keras.layers.BatchNormalization(axis=1),
tf.keras.layers.ReLU() ,
tf.keras.layers.Dense(12, kernel_regularizer=tf.keras.regularizers.l2(0.01),activation='softmax')
 ])
num_pipeline.fit_transform(train_data )<categorify>
initial_learning_rate = 0.001
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
 initial_learning_rate,
decay_steps=800,
decay_rate=0.5,
staircase=True )
preprocess_pipeline = FeatureUnion(transformer_list=[ 
("num_pipeline", num_pipeline), 
("cat_pipeline", cat_pipeline),
] )<train_on_grid>
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=lr_schedule),loss=tf.keras.losses.CategoricalCrossentropy() ,
metrics=['accuracy'] )
param_grid = [
{'n_estimators': [50, 100, 150],
'max_depth':[5, 10]}
]
forest_clf = RandomForestClassifier(random_state=42)
 grid_search = GridSearchCV(forest_clf, param_grid, cv=10, return_train_score=True)
 grid_search.fit(X_train, y_train )<compute_train_metric>
result = model.fit(x=X_train,y=Y_train,batch_size=64,epochs=120,verbose=1,shuffle=False,initial_epoch=0,
validation_split=0.2 )
cvres = grid_search.cv_results_
for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
print(mean_score, params )<find_best_params>
valid = model.evaluate(X_test,Y_test,verbose=2 )
grid_search.best_params_<train_on_grid>
Records = []
param_distribs = {
'n_estimators': randint(low=20, high=150),
'max_features': randint(low=5, high=12),
'max_depth': randint(low=5, high=12)
 }
forest_clf = RandomForestClassifier(random_state=42)
 forest_rnd_search = RandomizedSearchCV(forest_clf, param_distributions=param_distribs, n_iter=50, cv=10, return_train_score=True,
random_state=42, verbose=2, n_jobs=-1)
 forest_rnd_search.fit(X_train, y_train )<find_best_params>
Records.append(valid )
forest_rnd_search.best_params_<find_best_params>
species = model.predict_classes(X)
 print(species.shape)
 print(species )
cvres = forest_rnd_search.cv_results_
for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
print(mean_score, params )<find_best_params>
ans = pd.DataFrame(file,columns = ["file"] )
final_model = forest_rnd_search.best_estimator_<categorify>
ans = ans.join(pd.DataFrame(species,columns=["species"]))
ans["species"] = ans["species"].apply(lambda x: CATEGORIES[int(x)])
 ans.head(20 )
X_test = preprocess_pipeline.transform(test_data )<predict_on_test>
ans.to_csv("answers.csv",index=False )
<save_to_csv><EOS> 
model.save("saved_model" )