Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<feature_engineering>
import numpy as np
import pandas as pd
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import random
import time
import os
data['Name'] = data['Name'].apply(lambda x: x.split(',')[1] )<feature_engineering>
labels_dataframe = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')
 sample = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv' )
data['Name'] = data['Name'].apply(lambda x: x.split('.')[0] )<categorify>
ix = np.random.permutation(len(labels_dataframe))
labels_dataframe = labels_dataframe.iloc[ix]
data1 = pd.get_dummies(data['Name'] )<create_dataframe>
def load_from_dataframe(dataframe, image_shape, img_dir, x_col = None, y_col = None,):
no_of_images = len(dataframe)
 images = np.zeros(( no_of_images, image_shape[0], image_shape[1], image_shape[2]), dtype = np.uint8)
 if y_col:
labels = np.zeros(( no_of_images, 1), dtype = np.uint8)
 for ix in range(no_of_images):
filename = dataframe.loc[ix, x_col]
path = os.path.join(img_dir, filename + '.jpg')
 image = load_img(path, target_size =(image_shape[0], image_shape[1]))
image = img_to_array(image)
 images[ix] = image
labels[ix] = dataframe.loc[ix, y_col]
print('Found {} validated image filenames belonging to {} classes.'.format(no_of_images, np.unique(labels ).size))
return images, labels
else:
for ix in range(no_of_images):
filename = dataframe.loc[ix, x_col]
path = os.path.join(img_dir, filename + '.jpg')
 image = load_img(path, target_size =(image_shape[0], image_shape[1]))
image = img_to_array(image)
 images[ix] = image
print('Found {} validated image filenames'.format(no_of_images))
return images
dataset11 = dataset8.copy() 
<feature_engineering>
params = dict(dataframe = labels_dataframe, image_shape = image_shape, img_dir = '/kaggle/input/dog-breed-identification/train', x_col = 'id', y_col = 'breed')
 images, labels = run_with_timer(load_from_dataframe, params )
t = [' Col' , ' Dr' , ' Master' , ' Miss' ,' Mr', ' Mrs' , ' Ms' , ' Rev']
for i in t:
dataset11[i] = data1[i]<count_missing_values>
def get_feature(model_name, preprocess_input, images, pooling = 'avg', target_size =(331,331,3)) :
base_model = model_name(input_shape = target_size, include_top=False, pooling = pooling)
 model = Sequential()
model.add(InputLayer(input_shape = target_size))
model.add(Lambda(preprocess_input))
model.add(base_model)
 feature = model.predict(images)
 print('feature-map shape: {}'.format(feature.shape))
return feature
data1.isnull().sum()<create_dataframe>
inception_preprocess = preprocess_input
params = dict(model_name = InceptionV3, preprocess_input = inception_preprocess, images = images, pooling = 'avg')
 inception_feature = run_with_timer(get_feature, params )
dataset_test6 = dataset_test.copy()<feature_engineering>
nasnet_preprocessor = preprocess_input
params = dict(model_name = NASNetLarge, preprocess_input = nasnet_preprocessor, images = images, pooling = 'avg')
 nasnet_features = run_with_timer(get_feature, params )
dataset_test6['Name'] = dataset_test6['Name'].apply(lambda x: x.split(',')[1] )<feature_engineering>
xception_preprocess = preprocess_input
params = dict(model_name = Xception, preprocess_input = xception_preprocess, images = images, pooling = 'avg')
 xception_feature = run_with_timer(get_feature, params )
dataset_test6['Name'] = dataset_test6['Name'].apply(lambda x: x.split('.')[0] )<categorify>
resnet_preprocess = preprocess_input
params = dict(model_name = InceptionResNetV2, preprocess_input = resnet_preprocess, images = images, pooling = 'avg')
 resnet_feature = run_with_timer(get_feature, params )
dataset_test7 = pd.get_dummies(dataset_test6['Name'] )<feature_engineering>
final_features = np.concatenate([inception_feature, nasnet_features, xception_feature, resnet_feature], axis = 1)
 print('final features shape: {}'.format(final_features.shape))
del images, inception_feature, nasnet_features, xception_feature, resnet_feature
for i in dataset_test7.keys() :
dataset_test4[i] = dataset_test7[i]<drop_column>
def create_model(features_shape = 1024):
model = Sequential()
model.add(InputLayer(input_shape =(features_shape,)))
 model.add(Dropout(0.6))
model.add(Dense(4096, activation = 'relu'))
model.add(Dropout(0.6))
model.add(Dense(len(class_to_index), activation = 'softmax'))
model.compile(loss = 'sparse_categorical_crossentropy', optimizer ='Adam', metrics = ['accuracy'])
 return model
dataset_test4.pop(' Dona' )<count_missing_values>
model.fit(final_features, labels, batch_size = 128, epochs = 6, validation_split = 0.2 )
dataset_test4.isnull().sum()<count_missing_values>
params = dict(dataframe = sample, image_shape = image_shape, img_dir = '/kaggle/input/dog-breed-identification/test', x_col = 'id')
 images = run_with_timer(load_from_dataframe, params )
dataset11.isnull().sum()<create_dataframe>
inception_feature = run_with_timer(get_feature, dict(model_name = InceptionV3, preprocess_input = inception_preprocess, images = images, pooling = 'avg'))
nasnet_features = run_with_timer(get_feature, dict(model_name = NASNetLarge, preprocess_input = nasnet_preprocessor, images = images, pooling = 'avg'))
xception_feature = run_with_timer(get_feature, dict(model_name = Xception, preprocess_input = xception_preprocess, images = images, pooling = 'avg'))
resnet_feature = run_with_timer(get_feature, dict(model_name = InceptionResNetV2, preprocess_input = resnet_preprocess, images = images, pooling = 'avg'))
dataset_final = dataset_test4.copy()<feature_engineering>
final_features = np.concatenate([inception_feature, nasnet_features, xception_feature, resnet_feature], axis = 1)
 print('final features shape: {}'.format(final_features.shape))
del images, inception_feature, nasnet_features, xception_feature, resnet_feature
dataset_final['Total_SibSp'] = dataset_test4['SibSp']+dataset_test4['Parch']<drop_column>
prediction = model.predict(final_features)
 submission = pd.DataFrame({'id':sample.id.values})
 submission['id'] = submission['id'].apply(lambda x : x.split('.')[0])
 prediction = pd.DataFrame(prediction)
 prediction.columns = class_to_index.keys()
<create_dataframe><EOS> 
submission = pd.concat([submission, prediction], axis = 1)
 submission.to_csv('submission.csv', index = False )