Kaggle competition: Cats vs Dogs vs More
<SOS> metric: categorizationaccuracy Kaggle data source: cats-vs-dogs-vs-more<feature_engineering>
import numpy as np
import pandas as pd
import keras as ks
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import tqdm
from keras.models import Sequential, Model, Input
from keras.layers import Activation, Flatten, Dense, Dropout, ZeroPadding2D, Conv2D, MaxPool2D, BatchNormalization, GlobalAveragePooling2D, Average
from keras.optimizers import SGD, RMSprop, Adam
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
import os
import math
train["Salutation"] = train["Name"].map(lambda x: x.split(',')[1].split() [0][:-1] )<categorify>
CATEGORIES = ['airplane','car','cat','dog','flower','fruit','motorbike','person']
IMG_WIDTH = 100
IMG_HEIGHT = 100
TRAIN_PATH = '.. /input/natural_images/natural_images/'
TEST_PATH = '.. /input/evaluate/evaluate/'
get_salutation_map(train,"Salutation","Rare" )<feature_engineering>
folders = os.listdir(TRAIN_PATH)
 images = []
for folder in folders:
files = os.listdir(TRAIN_PATH + folder)
 images += [(folder, file, folder + '/' + file)for file in files]
image_locs = pd.DataFrame(images, columns=('class','filename','file_loc'))
display(image_locs.head(10))
display(image_locs.shape )
train["Salutation"] = train["Name"].map(lambda x: x.split(',')[1].split() [0][:-1])
 train["Salutation"] = train["Salutation"].map(get_salutation_map(train,'Salutation','Rare'))
train.head(2 )<count_unique_values>
row_count = len(image_locs_shuffled.index)
 val_split = 0.1
train_split = 1 - val_split
train_image_locs = image_locs_shuffled[:math.floor(train_split * row_count)]
val_image_locs = image_locs_shuffled[-math.ceil(val_split * row_count):]
display(train_image_locs.shape)
 display(val_image_locs.shape )
train['SibSp'].nunique()<count_values>
train_datagen = ImageDataGenerator(
 rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True 
)
 test_datagen = ImageDataGenerator(
 rescale=1./255
)
train["Family_Size"].value_counts(normalize=True)*100<categorify>
train_generator = train_datagen.flow_from_dataframe(
 train_image_locs,
directory=TRAIN_PATH,
x_col='file_loc',
target_size=(IMG_WIDTH, IMG_HEIGHT)
)
 val_generator = test_datagen.flow_from_dataframe(
 val_image_locs,
directory=TRAIN_PATH,
x_col='file_loc',
target_size=(IMG_WIDTH, IMG_HEIGHT),
shuffle=False 
)
 test_generator = test_datagen.flow_from_directory(
 directory='.. /input/evaluate/',
target_size=(IMG_WIDTH, IMG_HEIGHT),
class_mode=None,
batch_size=1,
shuffle=False
)
def get_family_size_map(df,var):
fam_dict = {}
for size, pct in(df[var].value_counts(normalize=True)*100 ).to_dict().items() :
if size == 0:
fam_dict[size] = "Alone"
elif(size != 0)&(pct > 10.0):
fam_dict[size] = "Small"
else:
fam_dict[size] = "Large"
return fam_dict<categorify>
def build_model(weights_path=None):
model = Sequential()
model.add(Conv2D(32,(3,3), activation='relu', padding='same', input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)))
 model.add(Conv2D(32,(3,3), activation ='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Conv2D(64,(3,3), activation='relu', padding='same'))
model.add(Conv2D(64,(3,3), activation ='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(len(CATEGORIES), activation='softmax'))
if weights_path is not None:
model.load_weights(weights_path)
 model.compile(Adam() , loss='categorical_crossentropy', metrics=['categorical_accuracy'])
 return model
train["Family_Size"] = train["Family_Size"].map(get_family_size_map(train,'Family_Size'))
train.head(2 )<count_missing_values>
def build_model_2(weights_path=None):
model = Sequential()
model.add(Conv2D(16,(3,3), activation='relu', padding='same', input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)))
 model.add(Conv2D(16,(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.1))
model.add(Conv2D(32,(3,3), activation='relu', padding='same'))
model.add(Conv2D(32,(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.1))
model.add(Conv2D(64,(3,3), activation='relu', padding='same'))
model.add(Conv2D(64,(3,3), activation='relu', padding='same'))
model.add(Conv2D(64,(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.1))
model.add(Conv2D(len(CATEGORIES),(1, 1)))
 model.add(GlobalAveragePooling2D())
 model.add(Activation('softmax'))
if weights_path is not None:
model.load_weights(weights_path)
 model.compile(Adam() , loss='categorical_crossentropy', metrics=['categorical_accuracy'])
 return model
train.isnull().mean()<feature_engineering>
def build_model_3(weights_path=None):
model = Sequential()
model.add(Conv2D(8,(3,3), activation='relu', padding='same', input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)))
 model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.1))
model.add(Conv2D(16,(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.1))
model.add(Conv2D(32,(3,3), activation='relu', padding='same'))
model.add(Conv2D(32,(3,3), activation='relu', padding='same'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=2))
model.add(Dropout(0.1))
model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(len(CATEGORIES), activation='softmax'))
if weights_path is not None:
model.load_weights(weights_path)
 model.compile(Adam() , loss='categorical_crossentropy', metrics=['categorical_accuracy'])
 return model
train['had_Cabin'] = np.where(train['Cabin'].isna() ,0,1 )<count_values>
for(i, final_model)in ensemble_final_models:
display(final_model.evaluate_generator(generator=val_generator, steps=val_steps))
train['Cabin'].dropna().map(lambda x:x[0] ).value_counts()<feature_engineering>
for(i, final_model)in ensemble_final_models:
val_generator.reset()
val_predictions = final_model.predict_generator(
 val_generator,
steps=val_steps,
verbose=1 
)
 display(val_predictions.shape)
 val_predictions_labels = np.argmax(val_predictions, axis=1)
 val_true_labels = val_generator.classes[:val_predictions.shape[0]]
display(confusion_matrix(val_true_labels, val_predictions_labels))
train['Cabin'] = train['Cabin'].fillna("M")
 train['Cabin'] = train['Cabin'].map(lambda x: x[0] )<groupby>
model_input = Input(shape=(IMG_WIDTH, IMG_HEIGHT, 3))
yModels=[m(model_input)for i, m in ensemble_final_models]
overall_model = Model(
 model_input,
Average()(yModels),
name='ensemble'
)
train.groupby(['Salutation','had_Cabin'] )<feature_engineering>
test_generator.reset()
predictions = overall_model.predict_generator(
 test_generator,
steps=test_generator.n,
verbose=1 
)
 predicted_class_indices = np.argmax(predictions, axis=1)
 display(predicted_class_indices )
mean_dict = {}
for k, df in train.groupby(['Salutation','Family_Size','had_Cabin']):
if df['Age'].isnull().sum() != 0:
mean_dict[k] = df["Age"].mean()
mean_dict<feature_engineering>
labels = train_generator.class_indices
labels = dict(( v,k)for k,v in labels.items())
 display(labels )
for k,v in mean_dict.items() :
train.loc[(train["Salutation"] == k[0])&(train["Family_Size"] == k[1])&(train["had_Cabin"] == k[2])&(train["Age"].isna()), "Age"] = v<count_values>
filenames = [fn.split('/')[1] for fn in test_generator.filenames]
predictions = [labels[k] for k in predicted_class_indices]
display(filenames[:10])
 display(predictions[:10] )
train['Embarked'].value_counts()<feature_engineering>
df = pd.DataFrame()
df['filename'] = filenames
df['label'] = predictions
df = df.sort_values(by='filename')
 df.to_csv('results.csv', header=True, index=False )