{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Introduction\nGoal of this notebook is to predict the breed of a dog","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom PIL import Image\nimport cv2\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:55:48.482815Z","iopub.execute_input":"2023-10-30T13:55:48.483218Z","iopub.status.idle":"2023-10-30T13:56:01.204545Z","shell.execute_reply.started":"2023-10-30T13:55:48.483192Z","shell.execute_reply":"2023-10-30T13:56:01.203004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mixed_labels = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv',index_col = 'id')\nmixed_labels.describe()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:01.207498Z","iopub.execute_input":"2023-10-30T13:56:01.20845Z","iopub.status.idle":"2023-10-30T13:56:01.287319Z","shell.execute_reply.started":"2023-10-30T13:56:01.2084Z","shell.execute_reply":"2023-10-30T13:56:01.286023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mixed_labels[\"breed\"].value_counts().plot.bar(figsize=(20, 10));","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:01.289173Z","iopub.execute_input":"2023-10-30T13:56:01.289898Z","iopub.status.idle":"2023-10-30T13:56:02.471439Z","shell.execute_reply.started":"2023-10-30T13:56:01.289862Z","shell.execute_reply":"2023-10-30T13:56:02.470257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mixed_labels[\"breed\"].value_counts().median()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:02.474001Z","iopub.execute_input":"2023-10-30T13:56:02.474535Z","iopub.status.idle":"2023-10-30T13:56:02.484205Z","shell.execute_reply.started":"2023-10-30T13:56:02.474509Z","shell.execute_reply":"2023-10-30T13:56:02.483384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"so we have on average 82 image of each breed to learn from, nice!\n","metadata":{}},{"cell_type":"code","source":"# find unique label values\nunique_breeds = np.unique(mixed_labels['breed'])\nprint(unique_breeds)\nprint('number of dogs breeds: ',len(unique_breeds))","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:02.485404Z","iopub.execute_input":"2023-10-30T13:56:02.485708Z","iopub.status.idle":"2023-10-30T13:56:02.503178Z","shell.execute_reply.started":"2023-10-30T13:56:02.485669Z","shell.execute_reply":"2023-10-30T13:56:02.50194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load the data\ndogs_images = []\nhow_many = 10 # only for visuals\nfolder_path = '/kaggle/input/dog-breed-identification/train'\nlabels = []\nfor filename in os.listdir(folder_path)[:how_many]:\n    if filename.endswith('.jpg') or filename.endswith('.png'):\n        labels.append(mixed_labels.loc[mixed_labels.index == filename[:-4]].values[0][0]) # get the proper label for every image\n        image_path = os.path.join(folder_path, filename)\n        image = cv2.imread(image_path)\n        image = cv2.resize(image,(200,200))\n        dogs_images.append(image)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:02.505048Z","iopub.execute_input":"2023-10-30T13:56:02.505695Z","iopub.status.idle":"2023-10-30T13:56:02.796318Z","shell.execute_reply.started":"2023-10-30T13:56:02.505662Z","shell.execute_reply":"2023-10-30T13:56:02.794747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# first image\nex = dogs_images[0]\nplt.imshow(ex)\nprint('image label: ',labels[0])\nprint('image shape: ',ex.shape)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:02.798108Z","iopub.execute_input":"2023-10-30T13:56:02.798706Z","iopub.status.idle":"2023-10-30T13:56:03.125181Z","shell.execute_reply.started":"2023-10-30T13:56:02.798605Z","shell.execute_reply":"2023-10-30T13:56:03.123097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5,9):\n    plt.imshow(dogs_images[i])\n    plt.show()\n    print('label: ',labels[i])\n    print(dogs_images[i].shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:03.127105Z","iopub.execute_input":"2023-10-30T13:56:03.127469Z","iopub.status.idle":"2023-10-30T13:56:04.192263Z","shell.execute_reply.started":"2023-10-30T13:56:03.127442Z","shell.execute_reply":"2023-10-30T13:56:04.190764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Parameters","metadata":{}},{"cell_type":"code","source":"IMG_SIZE = 200\nBATCH_SIZE = 32\nNUM_EPOCHS = 100","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:04.194136Z","iopub.execute_input":"2023-10-30T13:56:04.1955Z","iopub.status.idle":"2023-10-30T13:56:04.199524Z","shell.execute_reply.started":"2023-10-30T13:56:04.195457Z","shell.execute_reply":"2023-10-30T13:56:04.198456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# preprocessing\nTurning images into tensors, and adjust labels to be proper format","metadata":{}},{"cell_type":"code","source":"# how it would look like one-hot encoded\nprint(labels[0])\nprint((labels[0] == unique_breeds).astype(int))\nprint(labels[1])\nprint((labels[1] == unique_breeds).astype(int))","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:04.203816Z","iopub.execute_input":"2023-10-30T13:56:04.205184Z","iopub.status.idle":"2023-10-30T13:56:04.215838Z","shell.execute_reply.started":"2023-10-30T13:56:04.20515Z","shell.execute_reply":"2023-10-30T13:56:04.214266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_label(label):\n    \"\"\"\n    Takes simple label(string) as an argument and returns one-hot encoded target tensor\n    \"\"\"\n    label = (label == unique_breeds)\n    #label = tf.constant(label)\n    return label","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:04.217285Z","iopub.execute_input":"2023-10-30T13:56:04.21764Z","iopub.status.idle":"2023-10-30T13:56:04.23496Z","shell.execute_reply.started":"2023-10-30T13:56:04.217605Z","shell.execute_reply":"2023-10-30T13:56:04.233428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_image(image_path,img_size=IMG_SIZE):\n    \"\"\"\n    Takes an image path and requested image size, returns tensor version of an image\n    \"\"\"\n    image = tf.io.read_file(image_path)\n    image = tf.image.decode_jpeg(image,channels=3) # turning into a tensor\n    image = tf.image.convert_image_dtype(image,tf.float32)\n    image = tf.image.resize(image,size=[img_size,img_size]) # resizing\n    \n    return image","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:04.237258Z","iopub.execute_input":"2023-10-30T13:56:04.23788Z","iopub.status.idle":"2023-10-30T13:56:04.25079Z","shell.execute_reply.started":"2023-10-30T13:56:04.237823Z","shell.execute_reply":"2023-10-30T13:56:04.248975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we are using this function in order to do mini batching\ndef get_tuples(image_path,label):\n    \"\"\"\n    returns (image,label) tuples\n    \"\"\"\n    image = process_image(image_path,IMG_SIZE)\n    label = process_label(label)\n    \n    return (image,label)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:04.252794Z","iopub.execute_input":"2023-10-30T13:56:04.253983Z","iopub.status.idle":"2023-10-30T13:56:04.263742Z","shell.execute_reply.started":"2023-10-30T13:56:04.253937Z","shell.execute_reply":"2023-10-30T13:56:04.262343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data imports\nX - array with image filepaths\ny - array label as strings","metadata":{}},{"cell_type":"code","source":"X,y = [],[]\nfor filename in os.listdir(folder_path):\n    if filename.endswith('.jpg') or filename.endswith('.png'):\n        image_path = os.path.join(folder_path, filename)\n        label_name = mixed_labels.loc[mixed_labels.index == filename[:-4]].values[0][0]\n        X.append(image_path)\n        y.append(label_name)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:04.26566Z","iopub.execute_input":"2023-10-30T13:56:04.26635Z","iopub.status.idle":"2023-10-30T13:56:12.474291Z","shell.execute_reply.started":"2023-10-30T13:56:04.266315Z","shell.execute_reply":"2023-10-30T13:56:12.473211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y[0])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:12.475922Z","iopub.execute_input":"2023-10-30T13:56:12.476292Z","iopub.status.idle":"2023-10-30T13:56:12.483281Z","shell.execute_reply.started":"2023-10-30T13:56:12.476264Z","shell.execute_reply":"2023-10-30T13:56:12.48142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n# create training and validation set\nX_train,X_val,y_train,y_val = train_test_split(X,y,test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:12.485749Z","iopub.execute_input":"2023-10-30T13:56:12.486188Z","iopub.status.idle":"2023-10-30T13:56:13.827931Z","shell.execute_reply.started":"2023-10-30T13:56:12.486154Z","shell.execute_reply":"2023-10-30T13:56:13.825789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train[:5])\nprint(y_train[:5])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:13.830133Z","iopub.execute_input":"2023-10-30T13:56:13.831163Z","iopub.status.idle":"2023-10-30T13:56:13.83743Z","shell.execute_reply.started":"2023-10-30T13:56:13.83109Z","shell.execute_reply":"2023-10-30T13:56:13.836291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mini-batching\ncreate function to get mini-batch sample","metadata":{}},{"cell_type":"code","source":"def get_batch(X,y=None,batch_size=BATCH_SIZE,valid_data=False,test_data=False):\n    \"\"\"\n    Create batches of data out of image (X - filepath) and label (y - labels) pairs.\n    Shuffles the data if it's training data but doesn't shuffle if it's validation data.\n    Also accepts test data as input (no labels).\n    side note: it's easier to shuffle filepaths rather than whole images\n    \"\"\"\n    if test_data:\n        print('Creating test data batches...')\n        data = tf.data.Dataset.from_tensor_slices((tf.constant(X))) # every filepath as new example\n        data_batch = data.map(process_image).batch(BATCH_SIZE) # from filename to image, then get a batch\n        return data_batch\n    elif valid_data:\n        print(\"Creating validation data batches...\")\n        data = tf.data.Dataset.from_tensor_slices((tf.constant(X),tf.constant(y)))\n        data_batch = data.map(get_tuples).batch(BATCH_SIZE)\n        return data_batch\n    else:\n        print(\"Creating training data batches...\")\n        # Turn filepaths and labels into Tensors\n        data = tf.data.Dataset.from_tensor_slices((tf.constant(X),tf.constant(y)))\n        \n        # shuffling pathnames and labels is far easier than shuffling images\n        data = data.shuffle(buffer_size=len(X))\n        \n        # get (image,label) tuples\n        data = data.map(get_tuples)\n        \n        # Turning into batch\n        data_batch = data.batch(BATCH_SIZE)\n        \n        return data_batch\n        ","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:13.83868Z","iopub.execute_input":"2023-10-30T13:56:13.839028Z","iopub.status.idle":"2023-10-30T13:56:13.855384Z","shell.execute_reply.started":"2023-10-30T13:56:13.838999Z","shell.execute_reply":"2023-10-30T13:56:13.853972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# create first batches\ntrain_data = get_batch(X_train,y_train)\nval_data = get_batch(X_val,y_val,valid_data=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:13.856904Z","iopub.execute_input":"2023-10-30T13:56:13.85725Z","iopub.status.idle":"2023-10-30T13:56:14.171329Z","shell.execute_reply.started":"2023-10-30T13:56:13.857221Z","shell.execute_reply":"2023-10-30T13:56:14.169695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create a function for viewing images in a data batch\ndef show_25_images(images, labels):\n  \"\"\"\n  Displays a plot of 25 images and their labels from a data batch.\n  \"\"\"\n  # Setup the figure\n  plt.figure(figsize=(10, 10))\n  for i in range(25):\n    # Create subplots (5 rows, 5 columns)\n    ax = plt.subplot(5, 5, i+1)\n    # Display an image\n    plt.imshow(images[i])\n    # Add the image label as the title\n    plt.title(unique_breeds[labels[i].argmax()])\n    # Turn the grid lines off\n    plt.tight_layout()\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:14.173119Z","iopub.execute_input":"2023-10-30T13:56:14.173531Z","iopub.status.idle":"2023-10-30T13:56:14.181438Z","shell.execute_reply.started":"2023-10-30T13:56:14.173463Z","shell.execute_reply":"2023-10-30T13:56:14.179401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# showing simple batch\ntrain_images, train_labels = next(train_data.as_numpy_iterator())\nshow_25_images(train_images, train_labels)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:14.183835Z","iopub.execute_input":"2023-10-30T13:56:14.184307Z","iopub.status.idle":"2023-10-30T13:56:19.044199Z","shell.execute_reply.started":"2023-10-30T13:56:14.184279Z","shell.execute_reply":"2023-10-30T13:56:19.042938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model creation","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense,Conv2D,MaxPool2D,Dropout,Input,Flatten,Softmax\nfrom tensorflow.keras.regularizers import l2\nlambda_ = 0.001\n\nmodel = Sequential([\n    Input(shape=(200,200,3)),\n    Conv2D(filters=32,kernel_size=(3,3),padding='same',activation='relu'),\n    MaxPool2D(pool_size=(2,2)),\n    Conv2D(filters=64,kernel_size=(3,3),padding='same',activation='relu'),\n    MaxPool2D(pool_size=(2,2)),\n    Conv2D(filters=128,kernel_size=(3,3),padding='same',activation='relu'),\n    MaxPool2D(pool_size=(2,2)),\n    Flatten(),\n    Dropout(0.5), # 50% of nodes will be deactivated in order to regularize the model\n    Dense(256,activation='relu'),\n    Dense(120,activation='softmax')\n    \n])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:19.045818Z","iopub.execute_input":"2023-10-30T13:56:19.046482Z","iopub.status.idle":"2023-10-30T13:56:19.299724Z","shell.execute_reply.started":"2023-10-30T13:56:19.046452Z","shell.execute_reply":"2023-10-30T13:56:19.298179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.losses import CategoricalCrossentropy\nmodel.compile(optimizer='adam',loss=CategoricalCrossentropy(from_logits=False),metrics=['accuracy','Recall'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:19.301709Z","iopub.execute_input":"2023-10-30T13:56:19.302138Z","iopub.status.idle":"2023-10-30T13:56:19.35613Z","shell.execute_reply.started":"2023-10-30T13:56:19.302104Z","shell.execute_reply":"2023-10-30T13:56:19.354264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# callbacks\nfrom tensorflow.keras.callbacks import EarlyStopping\n\ncallback_one = EarlyStopping(monitor='loss', patience=2, min_delta=0.001)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:19.357299Z","iopub.execute_input":"2023-10-30T13:56:19.357629Z","iopub.status.idle":"2023-10-30T13:56:19.365327Z","shell.execute_reply.started":"2023-10-30T13:56:19.357602Z","shell.execute_reply":"2023-10-30T13:56:19.363551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import datetime\n\n# Create a function to build a TensorBoard callback\ndef create_tensorboard_callback():\n  # Create a log directory for storing TensorBoard logs\n  logdir = os.path.join(\"/kaggle/working/logs\",\n                        # Make it so the logs get tracked whenever we run an experiment\n                        datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n\n\n  return tf.keras.callbacks.TensorBoard(logdir)\n\ntensorboard = create_tensorboard_callback()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:19.367498Z","iopub.execute_input":"2023-10-30T13:56:19.367885Z","iopub.status.idle":"2023-10-30T13:56:19.379438Z","shell.execute_reply.started":"2023-10-30T13:56:19.367833Z","shell.execute_reply":"2023-10-30T13:56:19.377752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_history(history,epochs):\n    acc = history.history['accuracy']\n    val_acc = history.history['val_accuracy']\n    \n    loss = history.history['loss']\n    val_loss = history.history['val_loss']\n\n    epochs_range = range(epochs)\n    \n    plt.figure(figsize=(12,6))\n    plt.subplot(1,2,1)\n    plt.plot(epochs_range,acc,label='Training accuracy')\n    plt.plot(epochs_range,val_acc,label='Validation accuracy')\n    plt.legend()\n    plt.title('Training and Validation Accuracy')\n    \n    plt.subplot(1,2,2)\n    plt.plot(epochs_range,loss,label='Training loss')\n    plt.plot(epochs_range,val_loss,label='Validation loss')\n    plt.legend()\n    plt.title('Training and Validation Loss')\n","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:19.381018Z","iopub.execute_input":"2023-10-30T13:56:19.381351Z","iopub.status.idle":"2023-10-30T13:56:19.399535Z","shell.execute_reply.started":"2023-10-30T13:56:19.381321Z","shell.execute_reply":"2023-10-30T13:56:19.398354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model training","metadata":{}},{"cell_type":"code","source":"history = model.fit(x=train_data,\n         epochs=NUM_EPOCHS,\n         validation_data=val_data,\n         callbacks=[callback_one,tensorboard])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:56:19.400672Z","iopub.execute_input":"2023-10-30T13:56:19.401063Z","iopub.status.idle":"2023-10-30T13:57:21.432561Z","shell.execute_reply.started":"2023-10-30T13:56:19.401029Z","shell.execute_reply":"2023-10-30T13:57:21.428818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history(history,23)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:57:21.433801Z","iopub.status.idle":"2023-10-30T13:57:21.434249Z","shell.execute_reply.started":"2023-10-30T13:57:21.434063Z","shell.execute_reply":"2023-10-30T13:57:21.434084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Deal with overfitting, data augmentation","metadata":{}},{"cell_type":"code","source":"\n# unbatching data\ntrain_data = tf.data.Dataset.unbatch(train_data)\nimages,labels = [],[]\nfor item in train_data:\n    image,label = item\n    images.append(image)\n    labels.append(label)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:57:28.597253Z","iopub.execute_input":"2023-10-30T13:57:28.597684Z","iopub.status.idle":"2023-10-30T13:57:42.829385Z","shell.execute_reply.started":"2023-10-30T13:57:28.597649Z","shell.execute_reply":"2023-10-30T13:57:42.827276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = np.array(images)\nlabels = np.array(labels)\nprint(images.shape)\nprint(labels.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:57:42.832709Z","iopub.execute_input":"2023-10-30T13:57:42.833151Z","iopub.status.idle":"2023-10-30T13:57:45.183605Z","shell.execute_reply.started":"2023-10-30T13:57:42.833117Z","shell.execute_reply":"2023-10-30T13:57:45.182393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    validation_split=0.1)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:57:45.185125Z","iopub.execute_input":"2023-10-30T13:57:45.185479Z","iopub.status.idle":"2023-10-30T13:57:45.195875Z","shell.execute_reply.started":"2023-10-30T13:57:45.185449Z","shell.execute_reply":"2023-10-30T13:57:45.194136Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train = train_datagen.flow(images,labels)","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:57:45.200051Z","iopub.execute_input":"2023-10-30T13:57:45.20119Z","iopub.status.idle":"2023-10-30T13:57:45.2068Z","shell.execute_reply.started":"2023-10-30T13:57:45.201148Z","shell.execute_reply":"2023-10-30T13:57:45.205434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# example\nimage,label = next(data_train)\nfor i in range(5):\n    plt.imshow(image[i])\n    plt.title(unique_breeds[label[i]==True][0])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:57:45.208281Z","iopub.execute_input":"2023-10-30T13:57:45.209399Z","iopub.status.idle":"2023-10-30T13:57:46.92808Z","shell.execute_reply.started":"2023-10-30T13:57:45.209292Z","shell.execute_reply":"2023-10-30T13:57:46.926022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Trying imported model from the tf\nour model behaved poorly, it's overfitting, in order to make better model we will head start it by implementing model from the tf and also we added more data with image augmentation","metadata":{}},{"cell_type":"code","source":"shape = (IMG_SIZE,IMG_SIZE,3)\n\n# imported model\nbase_model = tf.keras.applications.Xception( \n    include_top=False,\n    weights=\"imagenet\",\n    classes=120,\n    input_shape=shape,\n)\n\n# our added layers\ninputs = Input(shape=shape)\nx = base_model(inputs)\nx = tf.keras.layers.GlobalAveragePooling2D()(x) # adding GAP in order to reduce overfitting\nx = Dropout(0.65)(x)\noutputs = Dense(120, activation='softmax')(x)\n\n\nmodel_1 = tf.keras.Model(inputs, outputs)\nbase_model.trainable = False\nmodel_1.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:57:46.929648Z","iopub.execute_input":"2023-10-30T13:57:46.930041Z","iopub.status.idle":"2023-10-30T13:57:49.614011Z","shell.execute_reply.started":"2023-10-30T13:57:46.930002Z","shell.execute_reply":"2023-10-30T13:57:49.612915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:57:49.61532Z","iopub.execute_input":"2023-10-30T13:57:49.615697Z","iopub.status.idle":"2023-10-30T13:57:49.658383Z","shell.execute_reply.started":"2023-10-30T13:57:49.615665Z","shell.execute_reply":"2023-10-30T13:57:49.656477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history1 = model_1.fit(data_train,epochs=NUM_EPOCHS,\n         validation_data=val_data,\n         callbacks=[callback_one,tensorboard])","metadata":{"execution":{"iopub.status.busy":"2023-10-30T13:57:49.660619Z","iopub.execute_input":"2023-10-30T13:57:49.661041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_history(history1,13)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# thoughs on results\n80% validation accuracy looks good for me, which is 20 times better than the \"hand made\" previous model","metadata":{}},{"cell_type":"markdown","source":"# Testing\n","metadata":{}},{"cell_type":"code","source":"X_test = []\nfolder_path_test = '/kaggle/input/dog-breed-identification/test'\nfor filename in os.listdir(folder_path_test):\n    if filename.endswith('.jpg') or filename.endswith('.png'):\n        image_path = os.path.join(folder_path_test, filename)\n        X_test.append(image_path)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test = get_batch(X_val,test_data=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# taking single batch for visuals\nsingle_batch = next(iter_)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting single batch\ny_test_predict = model_1.predict(single_batch)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# showing images with predicted labels\nshow_25_images(single_batch,y_test_predict)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"pretty good results","metadata":{}},{"cell_type":"markdown","source":"# predicting test samples","metadata":{}},{"cell_type":"code","source":"# taking all test images\ntest_filenames = []\nimages = []\nfor filename in os.listdir(folder_path_test):\n    if filename.endswith('.jpg') or filename.endswith('.png'):\n        image_path = os.path.join(folder_path_test, filename)\n        test_filenames.append(filename[:-4])\n        images.append(process_image(image_path))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predicting\nimages = tf.data.Dataset.from_tensor_slices((tf.constant(images)))\ny_test_predict = model_1.predict(images)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(tf.nn.softmax(y_test_predict), columns = unique_breeds)\n\nsubmission.insert(0, 'id', test_filenames)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predict.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# What learned\n- working with tensors\n- turning images into a tensors\n- mini-batching\n- dropout layers\n- data augmentation\n- Average, Global Average Pooling\n- head starting the model with another trained model","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}