Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<feature_engineering>
from keras.models import Sequential
from keras.layers import Conv2D, Dense, Flatten, BatchNormalization, Dropout, LeakyReLU, DepthwiseConv2D, Flatten
from keras.layers.pooling import GlobalAveragePooling2D
from keras.optimizers import Adam
from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint, EarlyStopping
from sklearn.metrics import confusion_matrix, roc_auc_score, classification_report
from sklearn.model_selection import train_test_split
from tqdm import tqdm, tqdm_notebook
import numpy as np
import pandas as pd
import cv2 as cv
import seaborn as sns
import matplotlib.pyplot as plt
import os
def TransfromCabin(df_aux):
df_aux.loc[(df_aux["Cabin"].isnull()), 'HasOrNotCabinNumber'] = "Has Not Cabin Number"
df_aux.loc[(df_aux["Cabin"].notnull()), 'HasOrNotCabinNumber'] = "Has Cabin Number"
return df_aux.drop(columns=["Cabin","Ticket"] )<filter>
train_data = pd.read_csv('.. /input/train.csv' )
df_train[df_train["Embarked"].isnull() ]<feature_engineering>
def create_model() :
model = Sequential()
model.add(Conv2D(3, kernel_size = 3, activation = 'relu', input_shape =(32, 32, 3)))
 model.add(Conv2D(filters = 16, kernel_size = 3, activation = 'relu'))
model.add(Conv2D(filters = 16, kernel_size = 3, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 1, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 32, kernel_size = 1, activation = 'relu'))
model.add(Conv2D(filters = 64, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 2, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 128, kernel_size = 1, activation = 'relu'))
model.add(Conv2D(filters = 256, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 1, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 256, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 512, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 2, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 512, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 1024, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(DepthwiseConv2D(kernel_size = 3, strides = 1, padding = 'Same', use_bias = True))
model.add(Conv2D(filters = 1024, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 2048, kernel_size = 1, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(470, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(256, activation = 'relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(128, activation = 'tanh'))
model.add(Dense(1, activation = 'sigmoid'))
model.compile(optimizer = 'rmsprop', loss = 'mean_squared_error', metrics = ['accuracy'])
 return model
def TransfromEmbarked(df_aux):
df_aux.loc[(df_aux["Embarked"].isnull())&(df_aux["Pclass"]==1), 'Embarked'] = "S"
return df_aux<feature_engineering>
file_path = 'weights-aerial-cactus.h5'
callbacks = [
ModelCheckpoint(file_path, monitor = 'val_acc', verbose = 1, save_best_only = True, mode = 'max'),
ReduceLROnPlateau(monitor = 'val_loss', factor = 0.2, patience = 3, verbose = 1, mode = 'min', min_lr = 0.00001),
EarlyStopping(monitor = 'val_loss', min_delta = 1e-10, patience = 15, verbose = 1, restore_best_weights = True)
 ]
training_path = '.. /input/train/train/'
test_path = '.. /input/test/test/'
def TransfromFamiliy(df_aux):
df_aux["familiySize"] = df_aux["SibSp"] + df_aux["Parch"] + 1
AloneTravel =(df_aux['SibSp'] == 0)&(df_aux['Parch'] == 0)
 CoupleTravel =(df_aux['SibSp'] == 0)&(df_aux['Parch'] == 1)
 siblingsTravel =(df_aux['SibSp'] == 1)&(df_aux['Parch'] == 0)
 SmallFamilly =(df_aux['familiySize'] <= 3)&(df_aux['familiySize'] >= 2)
 BigFamilly =(df_aux['familiySize'] >3)
 df_aux.loc[AloneTravel, 'familiy'] = "Alone Travel"
df_aux.loc[CoupleTravel, 'familiy'] = "Couple Travel"
df_aux.loc[siblingsTravel, 'familiy'] = "siblings Travel"
df_aux.loc[SmallFamilly, 'familiy'] = "Small Familly"
df_aux.loc[BigFamilly, 'familiy'] = "Big Familly"
return df_aux<categorify>
images_train = []
labels_train = []
images = train_data['id'].values
for image_id in tqdm_notebook(images):
image = np.array(cv.imread(training_path + image_id))
label = train_data[train_data['id'] == image_id]['has_cactus'].values[0]
images_train.append(image)
 labels_train.append(label)
 images_train.append(np.flip(image))
labels_train.append(label)
 images_train.append(np.flipud(image))
labels_train.append(label)
 images_train.append(np.fliplr(image))
labels_train.append(label)
 images_train = np.asarray(images_train)
 images_train = images_train.astype('float32')
 images_train /= 255.
 labels_train = np.asarray(labels_train )
dfTrain = TransfromTitle(df_train)
 dfTrain = TransfromAge(dfTrain)
 dfTrain = TransfromFare(dfTrain)
 dfTrain = TransfromCabin(dfTrain)
 dfTrain = TransfromEmbarked(dfTrain)
 dfTrain = TransfromFamiliy(dfTrain )<drop_column>
x_tr = images_train
y_tr = labels_train
images_train = x_tr
labels_train = y_tr
dfTrain = dfTrain[['familiySize','SibSp','Parch','title','GroupAge','GroupFare','HasOrNotCabinNumber',
'Sex','Embarked','Pclass','familiy','Survived']]<categorify>
test_images_names = []
for filename in os.listdir(test_path):
test_images_names.append(filename)
 test_images_names.sort()
images_test = []
for image_id in tqdm_notebook(test_images_names):
images_test.append(np.array(cv.imread(test_path + image_id)))
 images_test = np.asarray(images_test)
 images_test = images_test.astype('float32')
 images_test /= 255
dfTest = TransfromTitle(df_test)
 dfTest = TransfromAge(dfTest)
 dfTest = TransfromFare(dfTest)
 dfTest = TransfromCabin(dfTest)
 dfTest = TransfromEmbarked(dfTest)
 dfTest = TransfromFamiliy(dfTest )<drop_column>
x_train, x_test, y_train, y_test = train_test_split(images_train, labels_train, test_size = 0.2, stratify = labels_train )
dfTest = dfTest[['familiySize','SibSp','Parch','title','GroupAge','GroupFare','HasOrNotCabinNumber',
'Sex','Embarked','Pclass','familiy']]<import_modules>
model = create_model()
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.impute import KNNImputer
from sklearn.preprocessing import MinMaxScaler, StandardScaler, RobustScaler<prepare_x_and_y>
history = model.fit(x_train,
y_train,
batch_size = 128,
epochs = 80,
validation_data =(x_test, y_test),
verbose = 1,
callbacks = callbacks )
X_train = dfTrain.drop(columns = ['Survived'] ).values
y = dfTrain.Survived.values
X_test = dfTest.values<categorify>
model.load_weights(file_path )
for i in range(len(X_train)) :
X_train[i,9] = str(X_train[i,9])
 X_train[:,0:3] = StandardScaler().fit_transform(X_train[:,0:3])
 onehotencoder_1 = OneHotEncoder()
u1 = onehotencoder_1.fit_transform(X_train[:,3:] ).toarray()
X_train2 = np.concatenate(( X_train[:,0:3], u1), axis=1)
 X_train2.shape<normalization>
predictions = model.predict(images_test, verbose = 1 )
<categorify><EOS> 
test_df = pd.read_csv('.. /input/sample_submission.csv')
 X_test = []
images_test = test_df['id'].values
for img_id in tqdm_notebook(images_test):
X_test.append(cv.imread(test_path + img_id))
X_test = np.asarray(X_test)
 X_test = X_test.astype('float32')
 X_test /= 255
y_test_pred = model.predict_proba(X_test)
 test_df['has_cactus'] = y_test_pred
test_df.to_csv('aerial-cactus-submission.csv', index = False )