Kaggle competition: Dog Breed Identification
<SOS> metric: MulticlassLoss Kaggle data source: dog-breed-identification<count_missing_values>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pandas_profiling import ProfileReport
import tensorflow as tf
import tensorflow_hub as hub
from IPython.display import display, Image
from matplotlib.pyplot import imread
from sklearn.model_selection import train_test_split
import os
for col in test.columns:
pct_missing = np.mean(test[col].isnull())
 if(round(pct_missing*100)) != 0.0:
print('{} - {}%'.format(col, round(pct_missing*100)) )<data_type_conversions>
print("TF version:", tf.__version__)
 print("GPU", "available(YESS!!!!)" if tf.config.list_physical_devices("GPU")else "not available :(" )
df_numeric = df.select_dtypes(include=[np.number])
 numeric_cols = df_numeric.columns.values
for col in numeric_cols:
missing = df[col].isnull()
num_missing = np.sum(missing)
 if num_missing > 0:
df['{}_ismissing'.format(col)] = missing
med = df[col].median()
df[col] = df[col].fillna(med )<count_missing_values>
labels_csv = pd.read_csv(".. /input/dog-breed-identification/labels.csv")
test_numeric = test.select_dtypes(include=[np.number])
 test_numeric_cols = test_numeric.columns.values
for col in test_numeric_cols:
missing = test[col].isnull()
num_missing = np.sum(missing)
 if num_missing > 0:
test['{}_ismissing'.format(col)] = missing
med = test[col].median()
test[col] = test[col].fillna(med )<feature_engineering>
Labels_Profile = ProfileReport(labels_csv, progress_bar= False)
 Labels_Profile.to_notebook_iframe()
for col in df.columns:
pct_missing = np.mean(df[col].isnull())
 if(round(pct_missing*100)) != 0.0:
print('{} - {}%'.format(col, round(pct_missing*100)) )<feature_engineering>
filenames = [".. /input/dog-breed-identification/train/" + fname + ".jpg" for fname in labels_csv["id"]]
filenames[:5]
for col in test.columns:
pct_missing = np.mean(test[col].isnull())
 if(round(pct_missing*100)) != 0.0:
print('{} - {}%'.format(col, round(pct_missing*100)) )<drop_column>
Image(filenames[42] )
df.drop(df.iloc[:, 771:1284], inplace = True, axis = 1 )<drop_column>
labels = labels_csv["breed"].to_numpy()
labels[:10]
test.drop(test.iloc[:, 770:1288], inplace = True, axis = 1 )<data_type_conversions>
if len(labels)== len(filenames):
print("Number of labels matches number of filenames!")
 else:
print("Number of labels does not match number of filenames, check data directories." )
upper = corr_matrix.where(np.triu(np.ones(corr_matrix.shape), k=1 ).astype(np.bool))
to_drop = [column for column in upper.columns if any(upper[column] > 0.95)]
df.drop(to_drop, axis=1, inplace=True)
 test.drop(to_drop, axis=1, inplace=True )<feature_engineering>
unique_breeds = np.unique(labels)
 len(unique_breeds )
df['loss_fact'] = df['loss']
loss_fact = df['loss']
for i in loss_fact:
if i != 0:
loss_fact = loss_fact.replace(i, 1)
 df['loss_fact'] = loss_fact 
<train_model>
boolean_labels = [label == np.array(unique_breeds)for label in labels]
boolean_labels[:2]
<concatenate>
X = filenames
y = boolean_labels
trainfloatloss = pd.concat([df.select_dtypes(include=[np.float64]), df['loss_fact']], axis=1 )<define_variables>
NUM_IMAGES = 1000
X_train, X_val, y_train, y_val = train_test_split(X[:NUM_IMAGES],
y[:NUM_IMAGES],
test_size=0.2,
random_state=42)
 len(X_train), len(y_train), len(X_val), len(y_val )
float_columns = [c for c in trainfloatloss.columns if trainfloatloss[c].dtype.name == 'float64'] 
<drop_column>
IMG_SIZE = 224
def process_image(image_path):

 image = tf.io.read_file(image_path)
 image = tf.image.decode_jpeg(image, channels=3)
 image = tf.image.convert_image_dtype(image, tf.float32)
 image = tf.image.resize(image, size=[IMG_SIZE, IMG_SIZE])
 return image
test_float_train = testfloat
test_float_train = test_float_train.drop('f5', axis = 1)
 test_float_train = test_float_train.drop('f531', axis = 1 )<define_variables>
def get_image_label(image_path, label):

 image = process_image(image_path)
 return image, label
float_columns_test = [c for c in testfloat.columns if testfloat[c].dtype.name == 'float64']
float_columns_test.remove('f5')
 float_columns_test.remove('f531')
<feature_engineering>
BATCH_SIZE = 32
def create_data_batches(x, y=None, batch_size=BATCH_SIZE, valid_data=False, test_data=False):

 if test_data:
print("Creating test data batches...")
 data = tf.data.Dataset.from_tensor_slices(( tf.constant(x)))
 data_batch = data.map(process_image ).batch(BATCH_SIZE)
 return data_batch
elif valid_data:
print("Creating validation data batches...")
 data = tf.data.Dataset.from_tensor_slices(( tf.constant(x),
tf.constant(y)))
 data_batch = data.map(get_image_label ).batch(BATCH_SIZE)
 return data_batch
else:
print("Creating training data batches...")
 data = tf.data.Dataset.from_tensor_slices(( tf.constant(x),
tf.constant(y)))
 data = data.shuffle(buffer_size=len(x))
data = data.map(get_image_label)
 data_batch = data.batch(BATCH_SIZE)
 return data_batch
for c in float_columns:
trainfloatloss[trainfloatloss[c].name + '_no_out'] = trainfloatloss[c]
loss_fact0_float = trainfloatloss[trainfloatloss['loss_fact'] == 0][c]
loss_fact1_float = trainfloatloss[trainfloatloss['loss_fact'] == 1][c]
loss_fact0_float_no_out = loss_fact0_float[~(loss_fact0_float > loss_fact0_float.mean() + 2*loss_fact0_float.std())]
loss_fact1_float_no_out = loss_fact1_float[~(loss_fact1_float > loss_fact1_float.mean() + 2*loss_fact1_float.std())]
trainfloatloss[trainfloatloss[c].name + '_no_out'] = loss_fact0_float_no_out.append(loss_fact1_float_no_out )<count_missing_values>
train_data = create_data_batches(X_train, y_train)
 val_data = create_data_batches(X_val, y_val, valid_data=True )
trainfloatloss[['f756_no_out']].isna().values.sum()<correct_missing_values>
train_images, train_labels = next(train_data.as_numpy_iterator())
 show_25_images(train_images, train_labels )
trainfloatlossdna = trainfloatloss.dropna(how='any' )<concatenate>
INPUT_SHAPE = [None, IMG_SIZE, IMG_SIZE, 3]
OUTPUT_SHAPE = len(unique_breeds)
 MODEL_URL = "https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/4"
trainfloatlossdnanoout = pd.concat([trainfloatlossdna.filter(regex='no_out'), trainfloatlossdna['loss_fact']], axis=1 )<prepare_x_and_y>
def create_model(input_shape=INPUT_SHAPE, output_shape=OUTPUT_SHAPE, model_url=MODEL_URL):
print("Building model with:", MODEL_URL)
 model = tf.keras.Sequential([
hub.KerasLayer(MODEL_URL),
tf.keras.layers.Dense(units=OUTPUT_SHAPE,
activation="softmax")
 ])
 model.compile(
 loss=tf.keras.losses.CategoricalCrossentropy() ,
optimizer=tf.keras.optimizers.SGD() ,
metrics=["accuracy"] 
)
 model.build(INPUT_SHAPE)
 return model
X= trainfloatlossdnanoout.drop(['loss_fact'], axis=1)
 y= trainfloatlossdnanoout['loss_fact']<split>
%load_ext tensorboard
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.3, random_state=17 )<choose_model_class>
early_stopping = tf.keras.callbacks.EarlyStopping(monitor="val_accuracy",
patience=3 )
first_tree = DecisionTreeClassifier(random_state=17 )<compute_test_metric>
print("GPU", "available(YESS!!!!)" if tf.config.list_physical_devices("GPU")else "not available :(" )
np.mean(cross_val_score(first_tree, X_train, y_train, cv=5))<choose_model_class>
NUM_EPOCHS = 100
def train_model() :

 model = create_model()
tensorboard = create_tensorboard_callback()
model.fit(x=train_data,
epochs=NUM_EPOCHS,
validation_data=val_data,
validation_freq=1,
callbacks=[tensorboard, early_stopping])
 return model
first_knn = KNeighborsClassifier()<compute_test_metric>
model = train_model()
np.mean(cross_val_score(first_knn, X_train, y_train, cv=5))<compute_train_metric>
%tensorboard --logdir./logs
def bernoulli_naive_bayes(X, y):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
 model = BernoulliNB(binarize = 0.01)
 classifier = model.fit(X_train,y_train)
 predict = classifier.predict(X_test)
 cm = confusion_matrix(predict,y_test)
 accuracy = cm.trace() /cm.sum()
acc.append(accuracy)
 ', confusion_matrix(predict,y_test))
', classification_report(predict,y_test))
print('Bernoulli Naive Bayes Classifier')
 acc = []
for i in range(20):
bernoulli_naive_bayes(X, y)
 acc_ = np.array(acc)
 print('Average accuracy in 20 iterations is:', np.average(acc_))<compute_train_metric>
predictions = model.predict(val_data, verbose=1)
 predictions
def logistic_regression(X,y):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
 model = LogisticRegression(penalty='l2', solver='lbfgs', max_iter=1000)
 classifier = model.fit(X_train,y_train)
 predict = classifier.predict(X_test)
 cm = confusion_matrix(predict,y_test)
 accuracy = cm.trace() /cm.sum()
acc.append(accuracy)
 ', confusion_matrix(predict,y_test))
', classification_report(predict,y_test))
print('Logistic Regression')
 acc = []
for i in range(20):
logistic_regression(X,y)
 acc_ = np.array(acc)
 print('Average accuracy in 20 iterations is:', np.average(acc_))<find_best_model_class>
print(predictions[0])
 print(f"Max value(probability of prediction): {np.max(predictions[0])}")
 print(f"Sum: {np.sum(predictions[0])}")
 print(f"Max index: {np.argmax(predictions[0])}")
 print(f"Predicted label: {unique_breeds[np.argmax(predictions[0])]}" )
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
 model = LogisticRegression(penalty='l2', solver='lbfgs', max_iter=1000)
 classifier = model.fit(X_train,y_train)
 predict = classifier.predict(X_test)
 cm = confusion_matrix(predict,y_test)
 accuracy = cm.trace() /cm.sum()<save_to_csv>
def get_pred_label(prediction_probabilities):

 return unique_breeds[np.argmax(prediction_probabilities)]
pred_label = get_pred_label(predictions[0])
 pred_label
def write_to_submission_file(predicted_labels, out_file, train_num=105471,
target='loss', index_label="id"):
predicted_df = pd.DataFrame(predicted_labels,
index = np.arange(train_num + 1,
train_num + 1 +
predicted_labels.shape[0]),
columns=[target])
 predicted_df.to_csv(out_file, index_label=index_label )<predict_on_test>
def unbatchify(data):

 images = []
labels = []
for image, label in data.unbatch().as_numpy_iterator() :
images.append(image)
 labels.append(unique_breeds[np.argmax(label)])
 return images, labels
val_images, val_labels = unbatchify(val_data)
 val_images[0], val_labels[0]
classifier.predict(test_float_train )<save_to_csv>
full_data = create_data_batches(X, y )
write_to_submission_file(classifier.predict(test_float_train), out_file="logistic_regression_loan_default_prediction.csv" )<train_model>
full_model = create_model()
<prepare_x_and_y>
full_model_tensorboard = create_tensorboard_callback()
full_model_early_stopping = tf.keras.callbacks.EarlyStopping(monitor="accuracy",
patience=3 )
X= df.select_dtypes(include=[np.int64] ).drop(['loss', 'loss_fact'], axis=1)
 y= df['loss_fact']<split>
%tensorboard --logdir./logs
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.3, random_state=17 )<compute_train_metric>
full_model.fit(x=full_data,
epochs=NUM_EPOCHS,
callbacks=[full_model_tensorboard,
full_model_early_stopping] )
def bernoulli_naive_bayes(X, y):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
 model = BernoulliNB(binarize = 0.01)
 classifier = model.fit(X_train,y_train)
 predict = classifier.predict(X_test)
 cm = confusion_matrix(predict,y_test)
 accuracy = cm.trace() /cm.sum()
acc.append(accuracy)
 ', confusion_matrix(predict,y_test))
', classification_report(predict,y_test))
print('Bernoulli Naive Bayes Classifier')
 acc = []
for i in range(20):
bernoulli_naive_bayes(X, y)
 acc_ = np.array(acc)
 print('Average accuracy in 20 iterations is:', np.average(acc_))<compute_train_metric>
def save_model(model, suffix=None):

 modeldir = os.path.join("./",
datetime.datetime.now().strftime("%Y%m%d-%H%M%s"))
model_path = modeldir + "-" + suffix + ".h5"
print(f"Saving model to: {model_path}...")
 model.save(model_path)
 return model_path
def logistic_regression(X,y):
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
 model = LogisticRegression(penalty='l2', solver='sag', max_iter=1000)
 classifier = model.fit(X_train,y_train)
 predict = classifier.predict(X_test)
 cm = confusion_matrix(predict,y_test)
 accuracy = cm.trace() /cm.sum()
acc.append(accuracy)
 ', confusion_matrix(predict,y_test))
', classification_report(predict,y_test))
print('Logistic Regression')
 acc = []
for i in range(20):
logistic_regression(X,y)
 acc_ = np.array(acc)
 print('Average accuracy in 20 iterations is:', np.average(acc_))<choose_model_class>
def load_model(model_path):

 print(f"Loading saved model from: {model_path}")
 model = tf.keras.models.load_model(model_path,
custom_objects={"KerasLayer":hub.KerasLayer})
 return model
first_tree = DecisionTreeClassifier(random_state=17 )<compute_test_metric>
save_model(full_model, suffix="all-images-Adam" )
np.mean(cross_val_score(first_tree, X_train, y_train, cv=5))<choose_model_class>
loaded_full_model = load_model('./20210104-08491609750160-all-images-Adam.h5' )
first_knn = KNeighborsClassifier()<compute_test_metric>
test_path = ".. /input/dog-breed-identification/test/"
test_filenames = [test_path + fname for fname in os.listdir(test_path)]
test_filenames[:5]
np.mean(cross_val_score(first_knn, X_train, y_train, cv=5))<define_search_space>
test_data = create_data_batches(test_filenames, test_data=True )
tree_params = {'max_depth': np.arange(1,11), 'max_features': [.5,.7,1]}<train_on_grid>
test_predictions = loaded_full_model.predict(test_data,
verbose=-1 )
tree_grid = GridSearchCV(first_tree, tree_params, cv=5, n_jobs=-1 )<train_model>
preds_df = pd.DataFrame(columns=["id"] + list(unique_breeds))
preds_df.head()
%%time
tree_grid.fit(X_train, y_train )<find_best_score>
test_path = ".. /input/dog-breed-identification/test/"
preds_df["id"] = [os.path.splitext(path)[0] for path in os.listdir(test_path)]
preds_df.head()
tree_grid.best_score_<find_best_params>
preds_df[list(unique_breeds)] = test_predictions
preds_df.head()
<define_search_space><EOS> 
preds_df.to_csv("./full_submission_1_mobilienetV2_adam.csv",
index=False )