Kaggle competition: Dog Breed Identification
dataset3 = dataset2.copy()<drop_column>
import matplotlib.pyplot as plt
import seaborn as sns
import os
import gc
from sklearn.model_selection import train_test_split
import tensorflow as tf
from tqdm.autonotebook import tqdm
import numpy as np
import pandas as pd
from keras import Sequential
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam, SGD
from keras.callbacks import ReduceLROnPlateau
from keras.layers import Flatten,Dense,BatchNormalization,Activation,Dropout
from keras.layers import Lambda, Input, GlobalAveragePooling2D,BatchNormalization
from keras.utils import to_categorical
from tensorflow.keras.models import Model
from keras.preprocessing.image import load_img
dataset3 = dataset3.drop(['Name' , 'Sex' , 'Embarked'] , axis=1 )<create_dataframe>
print("GPU", "available(YESS!!!!)" if tf.config.list_physical_devices("GPU")else "not available :(")
 tf.config.list_physical_devices("GPU" )
dataset4 = dataset2.copy()<define_variables>
labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')
 labels.head()
features = ["Sex"]<categorify>
Image(".. /input/dog-breed-identification/train/43572ba7edf772a95f539e57afd9eb43.jpg" )
dataset5 = pd.get_dummies(dataset[features] )<feature_engineering>
classes = sorted(list(set(labels['breed'])))
 n_classes = len(classes)
 print('Total unique breed {}'.format(n_classes))
class_to_num = dict(zip(classes, range(n_classes)))
 class_to_num
dataset4['Sex_female'] = dataset5['Sex_female']
dataset4['Sex_male'] = dataset5['Sex_male']<create_dataframe>
input_shape =(331,331,3)
 def images_to_array(directory, label_dataframe, target_size = input_shape):
image_labels = label_dataframe['breed']
images = np.zeros([len(label_dataframe), target_size[0], target_size[1], target_size[2]],dtype=np.uint8)
 y = np.zeros([len(label_dataframe),1],dtype = np.uint8)
 for ix, image_name in enumerate(tqdm(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
del img
dog_breed = image_labels[ix]
y[ix] = class_to_num[dog_breed]
y = to_categorical(y)
 return images,y
dataset6 = dataset4.copy()<drop_column>
t = time.time()
X,y = images_to_array('/kaggle/input/dog-breed-identification/train', labels[:])
 print('runtime in seconds: {}'.format(time.time() - t))
dataset6.pop('Sex')
 dataset6.pop('Name' )<categorify>
dataset7 = pd.get_dummies(dataset2['Embarked'] )<feature_engineering>
lrr= ReduceLROnPlateau(monitor='val_acc', factor=.01, patience=3, min_lr=1e-5,verbose = 1)
 EarlyStop = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True)
dataset6['C'] = dataset7['C']
dataset6['Q'] = dataset7['Q']
dataset6['S'] = dataset7['S']<drop_column>
batch_size= 128
epochs=50
learn_rate=.001
sgd=SGD(lr=learn_rate,momentum=.9,nesterov=False)
 adam=Adam(lr=learn_rate, beta_1=0.9, beta_2=0.999, epsilon=None, amsgrad=False )
dataset6.pop('Embarked' )<drop_column>
img_size =(331,331,3)
 def get_features(model_name, model_preprocessor, input_size, data):
input_layer = Input(input_size)
 preprocessor = Lambda(model_preprocessor )(input_layer)
 base_model = model_name(weights='imagenet', include_top=False,
input_shape=input_size )(preprocessor)
 avg = GlobalAveragePooling2D()(base_model)
 feature_extractor = Model(inputs = input_layer, outputs = avg)
 feature_maps = feature_extractor.predict(data, verbose=1)
 print('Feature maps shape: ', feature_maps.shape)
 return feature_maps
dataset6.pop('Fare')
 dataset6.pop('Age' )<load_from_csv>
inception_preprocessor = preprocess_input
inception_features = get_features(InceptionV3,
inception_preprocessor,
img_size, X )
dataset_test = pd.read_csv("/kaggle/input/titanic/test.csv")
 dataset_test.head()<count_missing_values>
xception_preprocessor = preprocess_input
xception_features = get_features(Xception,
xception_preprocessor,
img_size, X )
dataset_test.isnull().sum()<correct_missing_values>
inc_resnet_preprocessor = preprocess_input
inc_resnet_features = get_features(InceptionResNetV2,
inc_resnet_preprocessor,
img_size, X )
dataset_test1 = dataset_test.dropna(axis = 1 )<create_dataframe>
nasnet_preprocessor = preprocess_input
nasnet_features = get_features(NASNetLarge,
nasnet_preprocessor,
img_size, X )
dataset_test2 = dataset_test1.copy()<define_variables>
del X
gc.collect()
features_test = ['Sex' , 'Embarked']<categorify>
model = Sequential()
model.add(Dropout(0.7,input_shape=(final_features.shape[1],)))
 model.add(Dense(n_classes,activation= 'softmax'))
model.compile(optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy'])
 history = model.fit(final_features, y,
batch_size=batch_size,
epochs=epochs,
validation_split=0.2,
callbacks=[lrr,EarlyStop] )
dataset_test3 = pd.get_dummies(dataset_test2[features_test] )<feature_engineering>
del inception_features
del xception_features
del nasnet_features
del inc_resnet_features
del final_features
gc.collect()
dataset_test2['Sex_female'] = dataset_test3['Sex_female']
dataset_test2['Sex_male'] = dataset_test3['Sex_male']
dataset_test2['C'] = dataset_test3['Embarked_C']
dataset_test2['Q'] = dataset_test3['Embarked_Q']
dataset_test2['S'] = dataset_test3['Embarked_S']<drop_column>
dataset_test2.pop('Name')
 dataset_test2.pop('Sex')
 dataset_test2.pop('Ticket')
 dataset_test2.pop('Embarked' )<create_dataframe>
def images_to_array_test(test_path, img_size =(331,331,3)) :
test_filenames = [test_path + fname for fname in os.listdir(test_path)]
data_size = len(test_filenames)
 images = np.zeros([data_size, img_size[0], img_size[1], 3], dtype=np.uint8)
 for ix,img_dir in enumerate(tqdm(test_filenames)) :
img = load_img(img_dir, target_size = img_size)
 images[ix]=img
del img
print('Ouptut Data Size: ', images.shape)
 return images
test_data = images_to_array_test('/kaggle/input/dog-breed-identification/test/', img_size )
dataset8 = dataset6.copy()<drop_column>
def extact_features(data):
inception_features = get_features(InceptionV3, inception_preprocessor, img_size, data)
 xception_features = get_features(Xception, xception_preprocessor, img_size, data)
 nasnet_features = get_features(NASNetLarge, nasnet_preprocessor, img_size, data)
 inc_resnet_features = get_features(InceptionResNetV2, inc_resnet_preprocessor, img_size, data)
 final_features = np.concatenate([inception_features,
xception_features,
nasnet_features,
inc_resnet_features],axis=-1)
 print('Final feature maps shape', final_features.shape)
 del inception_features
del xception_features
del nasnet_features
del inc_resnet_features
gc.collect()
return final_features
test_features = extact_features(test_data )
dataset8.pop('PassengerId' )<create_dataframe>
del test_data
gc.collect()
dataset9 = dataset8.copy()<drop_column>
pred = model.predict(test_features )
dataset9.pop('C')
 dataset9.pop('Q')
 dataset9.pop('S' )<prepare_x_and_y>
print(pred[0])
 print(f"Max value(probability of prediction): {np.max(pred[0])}")
 print(f"Sum: {np.sum(pred[0])}")
 print(f"Max index: {np.argmax(pred[0])}")
 print(f"Predicted label: {classes[np.argmax(pred[0])]}" )
y = dataset8.pop('Survived' )<create_dataframe>
preds_df = pd.DataFrame(columns=["id"] + list(classes))
preds_df.head()
dataset_test4 = dataset_test2.copy()<drop_column>
test_path = "/kaggle/input/dog-breed-identification/test/"
preds_df["id"] = [os.path.splitext(path)[0] for path in os.listdir(test_path)]
preds_df.head()
dataset_test4.pop('PassengerId' )<create_dataframe>
preds_df.loc[:,list(classes)]= pred
preds_df.to_csv('submission.csv',index=None)
 preds_df.head()
dataset_test5 = dataset_test4.copy()<drop_column>
Image('.. /input/goldend/DBS_GoldRetriever_1280.jpg' )
dataset_test5.pop('C')
 dataset_test5.pop('Q')
 dataset_test5.pop('S' )<import_modules>
img_g = load_img('.. /input/goldend/DBS_GoldRetriever_1280.jpg',target_size = img_size)
 img_g = np.expand_dims(img_g, axis=0)
<create_dataframe><EOS> 
test_features = extact_features(img_g)
 predg = model.predict(test_features)
 print(f"Predicted label: {classes[np.argmax(predg[0])]}")
 print(f"Probability of prediction): {round(np.max(predg[0])) * 100} %" )