Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<train_on_grid>
import numpy as np
import pandas as pd
import os
import time
import matplotlib.pyplot as plt
from keras import Sequential
from keras.layers import Dense, Dropout, InputLayer, Lambda, Input
from keras.preprocessing.image import load_img, img_to_array
from sklearn.model_selection import train_test_split
knn_grid = GridSearchCV(first_knn, knn_params, cv=5 )<train_model>
labels_df = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')
 sample = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv' )
%%time
knn_grid.fit(X_train, y_train )<find_best_params>
print('no of images in train dataset: {}'.format(len(labels_df)))
 print('no of images in test dataset: {}'.format(len(sample)) )
knn_grid.best_score_, knn_grid.best_params_<find_best_params>
def images_to_array(directory, label_dataframe, target_size =(331, 331, 3)) :
images = np.zeros([len(label_dataframe), target_size[0], target_size[1], target_size[2]], dtype=np.uint8)
 img = ''
for ix, image_name in enumerate(label_dataframe['id'].values):
img_dir = os.path.join(directory, image_name + '.jpg')
 img = load_img(img_dir, target_size = target_size)
 images[ix] = img_to_array(img)
 del img
label_dict = dict(enumerate(label_dataframe['breed'].unique()))
return images, label_dict
tree_grid.best_estimator_<predict_on_test>
t = time.time()
train_images, labels = images_to_array('/kaggle/input/dog-breed-identification/train', labels_df[:])
 print('runtime in seconds: {}'.format(time.time() - t))
tree_grid.predict(X_valid )<predict_on_test>
def get_feature(model_name, preprocess_input, images, target_size =(331,331,3)) :
base_model = model_name(input_shape = target_size, include_top=False, pooling = 'avg')
 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
tree_valid_pred = tree_grid.predict(X_valid )<compute_test_metric>
inception_preprocess = preprocess_input
inception_feature = get_feature(InceptionV3, preprocess_input, train_images )
tree_grid.score(X_valid, y_valid )<feature_engineering>
nasnet_preprocessor = preprocess_input
nasnet_features = get_feature(NASNetLarge, nasnet_preprocessor, train_images )
1 - np.mean(y )<save_to_csv>
xception_preprocess = preprocess_input
xception_feature = get_feature(Xception, xception_preprocess, train_images )
export_graphviz(tree_grid.best_estimator_, out_file='loan_tree.dot', feature_names=X.columns, filled=True )<set_options>
resnet_preprocess = preprocess_input
resnet_feature = get_feature(InceptionResNetV2, resnet_preprocess, train_images )
!dot -Tpng loan_tree.dot -o loan_tree.png<set_options>
final_features = np.concatenate([inception_feature, nasnet_features, xception_feature, resnet_feature], axis = 1)
 print('final features shape: {}'.format(final_features.shape))
del train_images, inception_feature, nasnet_features, xception_feature, resnet_feature
warnings.simplefilter('ignore' )<load_from_csv>
class_to_index = dict({labels[ix]:ix for ix in labels.keys() })
 index_to_class = labels
train_df = pd.read_csv('.. /input/career-con-2019/X_train.csv')
 test_df = pd.read_csv('.. /input/career-con-2019/X_test.csv')
 target = pd.read_csv('.. /input/career-con-2019/y_train.csv')
 sub = pd.read_csv('.. /input/career-con-2019/sample_submission.csv')
<concatenate>
labels = labels_df['breed'].map(class_to_index )
def quaternion_to_euler(x, y, z, w):
t0 = +2.0 *(w * x + y * z)
 t1 = +1.0 - 2.0 *(x * x + y * y)
 X = math.atan2(t0, t1)
 t2 = +2.0 *(w * y - z * x)
 t2 = +1.0 if t2 > +1.0 else t2
t2 = -1.0 if t2 < -1.0 else t2
Y = math.asin(t2)
 t3 = +2.0 *(w * z + x * y)
 t4 = +1.0 - 2.0 *(y * y + z * z)
 Z = math.atan2(t3, t4)
 return X, Y, Z
def feature_eng(data):
data['total_angular_velocity'] =(data['angular_velocity_X'] ** 2 + data['angular_velocity_Y'] ** 2 + data['angular_velocity_Z'] ** 2)** 0.5
data['total_linear_acceleration'] =(data['linear_acceleration_X'] ** 2 + data['linear_acceleration_Y'] ** 2 + data['linear_acceleration_Z'] ** 2)** 0.5
data['acc_vs_vel'] = data['total_linear_acceleration'] / data['total_angular_velocity']
x, y, z, w = data['orientation_X'].tolist() , data['orientation_Y'].tolist() , data['orientation_Z'].tolist() , data['orientation_W'].tolist()
nx, ny, nz = [], [], []
for i in range(len(x)) :
xx, yy, zz = quaternion_to_euler(x[i], y[i], z[i], w[i])
 nx.append(xx)
 ny.append(yy)
 nz.append(zz)
 data['euler_x'] = nx
data['euler_y'] = ny
data['euler_z'] = nz
data['total_angle'] =(data['euler_x'] ** 2 + data['euler_y'] ** 2 + data['euler_z'] ** 2)** 0.5
data['angle_vs_acc'] = data['total_angle'] / data['total_linear_acceleration']
data['angle_vs_vel'] = data['total_angle'] / data['total_angular_velocity']
df = pd.DataFrame()
for col in data.columns:
if col in ['row_id', 'series_id', 'measurement_number']:
continue
df[col + '_mean'] = data.groupby(['series_id'])[col].mean()
df[col + '_min'] = data.groupby(['series_id'])[col].min()
df[col + '_max'] = data.groupby(['series_id'])[col].max()
df[col + '_std'] = data.groupby(['series_id'])[col].std()
df[col + '_max_to_min'] = df[col + '_max'] / df[col + '_min']
df[col + '_abs_max'] = data.groupby('series_id')[col].apply(lambda x: np.max(np.abs(x)))
 df[col + '_abs_min'] = data.groupby('series_id')[col].apply(lambda x: np.min(np.abs(x)))
 return df 
<prepare_x_and_y>
def create_model(features_shape = 1024):
model = Sequential()
model.add(InputLayer(input_shape =(features_shape,)))
 model.add(Dropout(0.7))
model.add(Dense(len(class_to_index), activation = 'softmax'))
model.compile(loss = 'sparse_categorical_crossentropy', optimizer ='Adam', metrics = ['accuracy'])
 return model
new_training_data = feature_eng(train_df)
 new_test_data = feature_eng(test_df )<categorify>
new_training_data.fillna(0, inplace = True)
 new_test_data.fillna(0, inplace = True)
 new_training_data.replace(-np.inf, 0, inplace = True)
 new_training_data.replace(np.inf, 0, inplace = True)
 new_test_data.replace(-np.inf, 0, inplace = True)
 new_test_data.replace(np.inf, 0, inplace = True )<categorify>
model.fit(final_features, labels, epochs = 6, validation_split = 0.2 )
le = LabelEncoder()
target = le.fit_transform(target['surface'] )<define_search_space>
del final_features, labels
rf_param_grid = {
'max_depth' : list(range(8,100,10)) ,
'n_estimators': list(range(50,2000,100)) ,
'min_samples_split': [2,6,8],
'min_samples_leaf': [2,6,8],
'bootstrap': [True, False]
} 
<choose_model_class>
def images_to_array(directory, label_dataframe, target_size =(331, 331,3)) :
images = np.zeros([len(label_dataframe), target_size[0], target_size[1], target_size[2]], dtype=np.uint8)
 img = ''
for ix, image_name in enumerate(label_dataframe['id'].values):
img_dir = os.path.join(directory, image_name + '.jpg')
 img = load_img(img_dir, target_size = target_size)
 images[ix] = img_to_array(img)
 del img
return images
m = RandomForestClassifier(n_jobs=-1 )<choose_model_class>
t = time.time()
test_images = images_to_array('/kaggle/input/dog-breed-identification/test', sample)
 print('runtime in seconds: {}'.format(time.time() - t))
m_r = GridSearchCV(param_grid=rf_param_grid, estimator = m, scoring = "accuracy", cv = 4 )<train_model>
resnet_feature = get_feature(InceptionResNetV2, resnet_preprocess, test_images)
 xception_feature = get_feature(Xception, xception_preprocess, test_images)
 nasnet_features = get_feature(NASNetLarge, nasnet_preprocessor, test_images)
 inception_feature = get_feature(InceptionV3, preprocess_input, test_images )
<choose_model_class>
final_features = np.concatenate([inception_feature, nasnet_features, xception_feature, resnet_feature], axis = 1)
 print('final features shape: {}'.format(final_features.shape))
del test_images, inception_feature, nasnet_features, xception_feature, resnet_feature
m_r = RandomizedSearchCV(param_distributions=rf_param_grid, estimator = m, scoring = "accuracy", cv = 4,n_jobs=-1,n_iter=10 )<train_model>
prediction = model.predict(final_features)
 submission = pd.DataFrame({'id':sample.id})
 prediction = pd.DataFrame(prediction)
 prediction.columns = class_to_index.keys()
<find_best_score><EOS> 
submission = pd.concat([submission, prediction], axis = 1)
 submission.to_csv('submission.csv', index = False )