Kaggle competition: Aerial Cactus Identification
<SOS> metric: AUC Kaggle data source: aerial-cactus-identification<drop_column>
import cv2
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import json
import os
from tqdm import tqdm, tqdm_notebook
from keras.models import Sequential
import tensorflow.keras.backend as K
from keras.layers import Activation, Dropout, Flatten, Dense
from keras.applications import VGG16
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
for i in range(23, importances_df.shape[0]):
column = importances_df['Feature'][i]
df_combined.drop([column], inplace=True, axis=1)
 df_combined.head()<prepare_x_and_y>
train_dir = ".. /input/train/train/"
test_dir = ".. /input/test/test/"
train_df = pd.read_csv('.. /input/train.csv')
 train_df.head()
x_train = df_combined[:891].copy()
x_test = df_combined[891:].copy()
x_test.reset_index(inplace=True, drop=True )<choose_model_class>
im = cv2.imread(".. /input/train/train/01e30c0ba6e91343a12d2126fcafc0dd.jpg")
 plt.imshow(im )
random_forest = RandomForestClassifier()
random_forest.fit(x_train, y_train)
 y_prediction = random_forest.predict(x_test)
 accuracy_random_forest = round(random_forest.score(x_train, y_train)*100, 2)
 print("The accuracy after removing least important Features is {}, which is same as before removing.".format(accuracy_random_forest))<choose_model_class>
def baseline_model() :
model = Sequential()
model.add(Conv2D(64,(5, 5), input_shape=(32, 32, 3), padding='same', kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=0.3))
model.add(Conv2D(64,(5, 5), padding='same', kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=0.3))
model.add(Conv2D(128,(5, 5), padding='same', kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=0.3))
model.add(Conv2D(128,(5, 5), padding='same', kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=0.3))
model.add(Conv2D(256,(3, 3), padding='same', kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=0.3))
model.add(Conv2D(256,(3, 3), padding='same', kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=0.3))
model.add(Conv2D(512,(3, 3), padding='same', kernel_regularizer=l2(1e-4)))
 model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=0.3))
model.add(GlobalAveragePooling2D())
 model.add(Dense(1, activation='sigmoid'))
return model
tree = DecisionTreeClassifier()
tree.fit(x_train, y_train)
 y_pred_tree = tree.predict(x_test)
 tree_accuracy = round(tree.score(x_train, y_train)*100, 2)
 print("The accuracy after removing least important Features is {}, which is same as before removing.".format(tree_accuracy))<prepare_x_and_y>
d_x_train = xgb.DMatrix(x_train, label=y_train)
 d_x_test = xgb.DMatrix(x_test )<predict_on_test>
model = baseline_model()
model.compile(loss='binary_crossentropy',
optimizer=Adam(lr=0.0001),
metrics=['accuracy'] )
xgb_model = xgb.train(param, d_x_train, steps)
 y_pred_xgb = xgb_model.predict(d_x_test)
 y_pred_xgb_new = np.asarray([np.argmax(line)for line in y_pred_xgb])
<save_to_csv>
X_tr = []
Y_tr = []
imges = train_df['id'].values
for img_id in tqdm_notebook(imges):
X_tr.append(cv2.imread(train_dir + img_id))
Y_tr.append(train_df[train_df['id'] == img_id]['has_cactus'].values[0])
 X_tr = np.asarray(X_tr)
 X_tr = X_tr.astype('float32')
 X_tr /= 255
Y_tr = np.asarray(Y_tr )
output = pd.DataFrame({'PassengerID': test_data.PassengerId,'Survived':y_pred_xgb_new})
 output.to_csv("my_submission.csv", index=False)
 print("Submission successfully saved!!!" )<load_from_url>
batch_size = 16
nb_epoch = 30
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<import_modules>
datagen = ImageDataGenerator(featurewise_center=False,
",
samplewise_center=False,
",
featurewise_std_normalization=False,
",
samplewise_std_normalization=False,
",
zca_whitening=False,
",
zca_epsilon=1e-06,
",
rotation_range=20,
",
width_shift_range=0.2,
",
height_shift_range=0.2,
",
shear_range=0.2,
",
zoom_range=0.2,
",
channel_shift_range=0.,
",
fill_mode='nearest',
validation_split=0.1,
horizontal_flip=True,
vertical_flip=True)
 datagen.fit(X_tr)
 history = model.fit_generator(datagen.flow(X_tr, Y_tr, batch_size=batch_size, subset='training'),
validation_data=datagen.flow(X_tr, Y_tr, batch_size=batch_size, subset='validation'),
steps_per_epoch=len(X_tr)/ batch_size, epochs=nb_epoch, shuffle=True, validation_steps=len(X_tr)/batch_size )
import numpy as np
import pandas as pd
import tensorflow as tf
import tokenization
from plotnine import *
from tensorflow.keras.layers import Dense, Input, Concatenate, BatchNormalization
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping
from collections import Counter
from sklearn.model_selection import StratifiedShuffleSplit<choose_model_class>
%%time
X_tst = []
Test_imgs = []
for img_id in tqdm_notebook(os.listdir(test_dir)) :
X_tst.append(cv2.imread(test_dir + img_id))
Test_imgs.append(img_id)
 X_tst = np.asarray(X_tst)
 X_tst = X_tst.astype('float32')
 X_tst /= 255
%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )<load_from_csv>
test_predictions = model.predict(X_tst )
input_dir = '/kaggle/input/nlp-getting-started/'
output_dir = '/kaggle/working/'
checkpoint_dir = '/kaggle/input/checkpoint-nlp-disaster/'
word1vec_dir = '/kaggle/input/word2vec-google/GoogleNews-vectors-negative300.bin'
df_model, df_submission = pd.read_csv(input_dir + 'train.csv'), pd.read_csv(input_dir + 'test.csv')
 df_model.head()<feature_engineering>
sub_df = pd.DataFrame(test_predictions, columns=['has_cactus'] )
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )<feature_engineering>
sub_df['id'] = ''
cols = sub_df.columns.tolist()
cols = cols[-1:] + cols[:-1]
sub_df=sub_df[cols]
def tokenize_df(df_in):
df_out = df_in.copy()
df_out['str_tokens'] = df_out['text'].apply(tokenizer.tokenize)
 apply_cls_sep = lambda X: ['[CLS]'] + X + ['[SEP]']
df_out['str_tokens'] = df_out['str_tokens'].apply(apply_cls_sep)
 df_out['id_tokens'] = df_out['str_tokens'].apply(tokenizer.convert_tokens_to_ids)
 df_out['n_tokens'] = df_out['id_tokens'].apply(len)
 return df_out
df_model = tokenize_df(df_model)
 df_submission = tokenize_df(df_submission)
 df_model.head()<categorify>
for i, img in enumerate(Test_imgs):
sub_df.set_value(i,'id',img )
<choose_model_class><EOS> 
sub_df.to_csv('submission.csv',index=False )