Kaggle competition: Dogs vs. Cats Redux: Kernels Edition
<SOS> metric: LogLoss Kaggle data source: dogs-vs.-cats-redux:-kernels-edition<compute_test_metric>
import os
import zipfile
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.applications import EfficientNetB0
from tensorflow.keras.applications.efficientnet import preprocess_input
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow import keras
from tensorflow.keras import Model
from tensorflow.keras.optimizers import RMSprop
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
knn_score = knn.score(X_test, y_test)
 accuracies.append(knn_score)
 print(knn_score )<train_model>
TRAIN_PATH = ".. /input/dogs-vs-cats-redux-kernels-edition/train.zip"
TEST_PATH = ".. /input/dogs-vs-cats-redux-kernels-edition/test.zip"
UNZIP_DATA = ".. /kaggle/files/unzipped/"
UNZIP_TRAIN = ".. /kaggle/files/unzipped/train"
UNZIP_TEST = ".. /kaggle/files/unzipped/test"
BATCH_SIZE = 32
SEED = 88888
IMG_SIZE = 224
EPOCHS = 10
rdmf = RandomForestClassifier(n_estimators=20, criterion='entropy')
 rdmf.fit(X_train, y_train )<compute_test_metric>
with zipfile.ZipFile(TRAIN_PATH, 'r')as zipp:
zipp.extractall(UNZIP_DATA)
 print('Done!')
 with zipfile.ZipFile(TEST_PATH, 'r')as zipp:
zipp.extractall(UNZIP_DATA)
 print('Done!' )
rdmf_score = rdmf.score(X_test, y_test)
 rdmf_score_tr = rdmf.score(X_train, y_train)
 accuracies.append(rdmf_score)
 print(rdmf_score)
 print(rdmf_score_tr )<train_model>
training_images_files = os.listdir(".. /kaggle/files/unzipped/train")
 test_image_files =os.listdir(".. /kaggle/files/unzipped/test")
xgb = XGBClassifier()
xgb.fit(X_train, y_train )<compute_test_metric>
classes = [name.split('.')[0] for name in training_images_files]
train_df = pd.DataFrame({
'filename': training_images_files,
'class': classes,
})
 train_df['Path'] = train_df['filename'].apply(
 lambda filename: os.path.join('.. /kaggle/files/unzipped/test', filename))
xgb_score = xgb.score(X_test, y_test)
 accuracies.append(xgb_score)
 print(xgb_score )<define_variables>
test_df = pd.DataFrame(data = test_image_files, columns = ['filename'])
 test_df['id'] = test_df['filename'].apply(lambda f: int(f.split('.')[0]))
test_df.sort_values(by = 'id', inplace = True, ignore_index = True)
 test_df
myLabels = ['Logistic Regression', 'SVM', 'Kernel SVM', 'KNN', 'Random Forest', 'Xgboost']<feature_engineering>
train_df, valid_df = train_test_split(train_df,
test_size =.2,
shuffle=True ,
random_state=SEED)
test_data['Title'] = test_data['Title'].replace('Dona.', 'Mrs.')
 test_data.head()<split>
train_generator = ImageDataGenerator(preprocessing_function=preprocess_input,
rotation_range=45,
shear_range=0.1,
zoom_range=0.2,
horizontal_flip=False,
width_shift_range=0.1,
height_shift_range=0.1, 
)
 train_generator = train_generator.flow_from_dataframe(
 train_df,
UNZIP_TRAIN,
x_col='filename',
y_col='class',
target_size=(IMG_SIZE,IMG_SIZE),
batch_size=32,
class_mode='binary' 
)
 validation_generator = ImageDataGenerator(preprocessing_function=preprocess_input, 
)
 validation_generator = validation_generator.flow_from_dataframe(
 valid_df,
UNZIP_TRAIN,
x_col = 'filename',
y_col = 'class',
target_size =(IMG_SIZE,IMG_SIZE),
batch_size=32,
class_mode='binary' 
)
titanic_test = test_data.iloc[:, 0:11].values<categorify>
pre_trained_model = EfficientNetB0(input_shape =(IMG_SIZE, IMG_SIZE, 3),
include_top = False,
weights = 'imagenet')
 for layer in pre_trained_model.layers:
layer.trainable = False
titanic_test[:, 1] = label_encoder_sex_tr.transform(titanic_test[:, 1])
 titanic_test[:, 5] = label_encoder_title_tr.transform(titanic_test[:, 5])
 titanic_test[:, 4] = label_encoder_embarked_tr.transform(titanic_test[:, 4] )<normalization>
last_layer = pre_trained_model.get_layer('top_activation')
 last_output=last_layer.output
def create_model(last_output):
x=keras.layers.GlobalAveragePooling2D()(last_output)
 x=keras.layers.BatchNormalization()(x)
 x=keras.layers.Dense(1, activation='sigmoid' )(x)
 model = Model(pre_trained_model.input, x)
 model.compile(optimizer = RMSprop(learning_rate=0.001),
loss = 'binary_crossentropy',
metrics = ['acc'])
 return model
model=create_model(last_output)
titanic_test = scaler_x.transform(titanic_test )<predict_on_test>
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=2,
verbose=2,
factor=0.5,
min_delt=0.001,
min_lr=0.00001)
 early_stopping = EarlyStopping(
 monitor = "val_accuracy",
patience = 50,
verbose = 2,
mode = "max", 
)
y_pred = rdmf.predict(titanic_test )<create_dataframe>
history = model.fit(
 train_generator,
validation_data = validation_generator,
epochs = EPOCHS,
callbacks = [learning_rate_reduction, early_stopping],
)
titanic_submission = pd.DataFrame({'PassengerId':passengerId, 'Survived':y_pred} )<save_to_csv>
model.save('./dog_cat_model' )
titanic_submission.to_csv('rdmf_Titanic.csv', index=False )<import_modules>
test_gen = ImageDataGenerator(preprocessing_function=preprocess_input)
 test_generator = test_gen.flow_from_dataframe(
 test_df,
UNZIP_TEST,
x_col='filename',
class_mode= None,
target_size=(IMG_SIZE,IMG_SIZE),
batch_size=BATCH_SIZE,
shuffle=False 
)
 predict = model.predict(test_generator, verbose = 1 )
print("Python version: {}".format(sys.version))
print("pandas version: {}".format(pd.__version__))
print("matplotlib version: {}".format(matplotlib.__version__))
print("NumPy version: {}".format(np.__version__))
print("SciPy version: {}".format(sp.__version__))
print("IPython version: {}".format(IPython.__version__))
print("scikit-learn version: {}".format(sklearn.__version__))
warnings.filterwarnings('ignore')
 print('-'*25)
 print(check_output(["ls", ".. /input"] ).decode("utf8")) 
<set_options>
test_df["predict"] = predict
test_df["label"] = test_df["predict"]
result = test_df[["id", "label"]]
%matplotlib inline
mpl.style.use('ggplot')
 sns.set_style('white')
 pylab.rcParams['figure.figsize'] = 12,8<load_from_csv>
result.to_csv('submission.csv', index=False )