Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<split>
np.random.seed(42)
 %matplotlib inline
train_df = comp_df[comp_df.Survived.notnull() ]
test_df = comp_df[comp_df.Survived.isnull() ]<concatenate>
def get_num_files(path):

 if not os.path.exists(path):
return 0
return sum([len(files)for r, d, files in os.walk(path)] )
train_df.shape, test_df.shape
train_df = train_df[train_df.Fare<=100]
comp_df = pd.concat([train_df,test_df] )<count_missing_values>
train_dir = '/kaggle/input/dog-breed-identification/train'
test_dir = '/kaggle/input/dog-breed-identification/test'
data_size = get_num_files(train_dir)
 test_size = get_num_files(test_dir)
 print('Data samples size: ', data_size)
 print('Test samples size: ', test_size )
comp_df.Embarked.isnull().sum()<count_values>
labels_dataframe = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')
 sample_df = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv')
 labels_dataframe.head(5 )
comp_df.Embarked.value_counts()<drop_column>
class_to_num = dict(zip(dog_breeds, range(n_classes)) )
comp_df.Embarked.fillna('S',inplace=True )<count_missing_values>
def images_to_array(data_dir, labels_dataframe, img_size =(224,224,3)) :

 images_names = labels_dataframe['id']
images_labels = labels_dataframe['breed']
data_size = len(images_names)
 X = np.zeros([data_size, img_size[0], img_size[1], img_size[2]], dtype=np.uint8)
 y = np.zeros([data_size,1], dtype=np.uint8)
 for i in tqdm(range(data_size)) :
image_name = images_names[i]
img_dir = os.path.join(data_dir, image_name+'.jpg')
 img_pixels = load_img(img_dir, target_size=img_size)
 X[i] = img_pixels
image_breed = images_labels[i]
y[i] = class_to_num[image_breed]
y = to_categorical(y)
 ind = np.random.permutation(data_size)
 X = X[ind]
y = y[ind]
print('Ouptut Data Size: ', X.shape)
 print('Ouptut Label Size: ', y.shape)
 return X, y
comp_df.isnull().sum()<feature_engineering>
img_size =(331,331,3)
 X, y = images_to_array(train_dir, labels_dataframe, img_size )
comp_df['family_size'] = comp_df.SibSp + comp_df.Parch<drop_column>
inception_preprocessor = preprocess_input
inception_features = get_features(InceptionV3,
inception_preprocessor,
img_size, X )
comp_df.drop(['SibSp','Parch'], axis=1, inplace=True )<feature_engineering>
xception_preprocessor = preprocess_input
xception_features = get_features(Xception,
xception_preprocessor,
img_size, X )
comp_df['title'] = comp_df.Name.str.extract(r'([\w]+[.])' )<categorify>
nasnet_preprocessor = preprocess_input
nasnet_features = get_features(NASNetLarge,
nasnet_preprocessor,
img_size, X )
comp_df.Sex = comp_df.Sex.map({'male':1,'female':0})
 comp_df.Embarked = comp_df.Embarked.map({'C':0, 'Q':1,'S':2} )<categorify>
inc_resnet_preprocessor = preprocess_input
inc_resnet_features = get_features(InceptionResNetV2,
inc_resnet_preprocessor,
img_size, X )
comp_df.family_size= comp_df.apply(lambda x:4 if x.family_size>4 else x.family_size,axis=1 )<feature_engineering>
del X
comp_df['title'] = comp_df['title'].str.replace(r'Sir.', 'Mr.')
 comp_df['title'] = comp_df['title'].str.replace(r'Rev.','Mr.')
 comp_df['title'] = comp_df['title'].str.replace(r'Lady.','Ms.')
 comp_df['title'] = comp_df['title'].str.replace(r'Mrs.','Ms.')
 comp_df['title'] = comp_df['title'].str.replace(r'Miss.','Ms.')
 comp_df['title'] = comp_df['title'].str.replace(r'Mlle.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Mme.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Dona.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Countess.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Major.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Jonkheer.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Col.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Capt.','other')
 comp_df['title'] = comp_df['title'].str.replace(r'Master.','scholar')
 comp_df['title'] = comp_df['title'].str.replace(r'Dr.','scholar')
 comp_df['title'] = comp_df['title'].str.replace('Don.','scholar' )<drop_column>
EarlyStop_callback = keras.callbacks.EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
 my_callback=[EarlyStop_callback]
comp_df.drop('Name',axis=1, inplace=True )<categorify>
dnn = keras.models.Sequential([
InputLayer(final_features.shape[1:]),
Dropout(0.7),
Dense(n_classes, activation='softmax')
 ])
 dnn.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
 h = dnn.fit(final_features, y,
batch_size=128,
epochs=60,
validation_split=0.1,
callbacks=my_callback )
comp_df.title = comp_df.title.map({'other':0, 'scholar':1, 'Ms.':2, 'Mr.':3} )<categorify>
def images_to_array2(data_dir, labels_dataframe, img_size =(224,224,3)) :

 images_names = labels_dataframe['id']
data_size = len(images_names)
 X = np.zeros([data_size, img_size[0], img_size[1], 3], dtype=np.uint8)
 for i in tqdm(range(data_size)) :
image_name = images_names[i]
img_dir = os.path.join(data_dir, image_name+'.jpg')
 img_pixels = tf.keras.preprocessing.image.load_img(img_dir, target_size=img_size)
 X[i] = img_pixels
print('Ouptut Data Size: ', X.shape)
 return X
test_data = images_to_array2(test_dir, sample_df, img_size )
comp_df.Age = pd.cut(comp_df.Age, 7, labels=[0,1,2,3,4,5,6])
 comp_df.Fare = pd.cut(comp_df.Fare, 7, labels=[0,1,2,3,4,5,6])
 comp_df.Age = comp_df.Age.astype('int')
 comp_df.Fare = comp_df.Fare.astype('int' )<split>
inception_features = get_features(InceptionV3, inception_preprocessor, img_size, test_data)
 xception_features = get_features(Xception, xception_preprocessor, img_size, test_data)
 nasnet_features = get_features(NASNetLarge, nasnet_preprocessor, img_size, test_data)
 inc_resnet_features = get_features(InceptionResNetV2, inc_resnet_preprocessor, img_size, test_data)
 test_features = np.concatenate([inception_features,
xception_features,
nasnet_features,
inc_resnet_features],axis=-1)
 print('Final feature maps shape', test_features.shape )
train_df = comp_df[comp_df.Survived.notnull() ]
test_df = comp_df[comp_df.Survived.isnull() ]<drop_column>
del test_data
test_df.drop('Survived',axis=1, inplace=True )<prepare_x_and_y>
y_pred = dnn.predict(test_features, batch_size=128 )
x_train = train_df.drop('Survived',axis=1)
 y_train = train_df.Survived<import_modules>
for b in dog_breeds:
sample_df[b] = y_pred[:,class_to_num[b]]
sample_df.to_csv('pred.csv', index=None )