{"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":"code","source":"! kaggle competitions download dogs-vs-cats #download data with kaggle api","metadata":{"id":"yVgSnmD_Is7D","outputId":"32e256ae-8254-488c-aebc-4d6e46c45aa8"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Importing libraries**","metadata":{"id":"1FqGadwxInKV"}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom zipfile import ZipFile\nimport os\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Input, Dense, Conv2D, Flatten, MaxPooling2D, Dropout, BatchNormalization,\\\nRandomFlip , RandomRotation\nfrom tensorflow.keras.utils import to_categorical\nimport datetime\nimport matplotlib.pyplot as plt\nimport os\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\nimport warnings\nwarnings.filterwarnings('ignore')\nprint(\"TF version:\", tf.__version__)\n\n# Check for GPU\nprint(\"GPU\", \"available (YESS!!!!)\" if tf.config.list_physical_devices(\"GPU\") else \"not available!\")","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:02:10.889518Z","iopub.execute_input":"2023-01-03T19:02:10.890196Z","iopub.status.idle":"2023-01-03T19:02:10.898643Z","shell.execute_reply.started":"2023-01-03T19:02:10.890161Z","shell.execute_reply":"2023-01-03T19:02:10.897633Z"},"id":"znKXXvcfInKW","outputId":"7cb81fa1-9da4-417c-e7bb-7525671cd08c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Unzip the training and test data and store them in  `tmp` folder**","metadata":{"id":"LyivAR0AInKY"}},{"cell_type":"code","source":"def extract_files(dir):\n    ''' This function extracts files in the given dir  '''\n    with ZipFile(dir,'r') as zip:\n        zip.extractall()\n        zip.close()\n    print('Extracting files ...')","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:02:17.209915Z","iopub.execute_input":"2023-01-03T19:02:17.210347Z","iopub.status.idle":"2023-01-03T19:02:41.523382Z","shell.execute_reply.started":"2023-01-03T19:02:17.210294Z","shell.execute_reply":"2023-01-03T19:02:41.522346Z"},"id":"Ex6aq6C-InKY","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"full = '/content/dogs-vs-cats.zip'\nextract_files(full)\nextract_test = '/content/test1.zip'\nextract_files(extract_test)\nextract_train = '/content/train.zip'\nextract_files(extract_train)","metadata":{"id":"4Jf8HxejNSgc","outputId":"ec1bca11-1a6a-41a5-93b1-2cf019ef3eed"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**confirm how many images are in the training set**","metadata":{"id":"lVN-PqOZInKY"}},{"cell_type":"code","source":"train_dir = '/content/train/'\nprint(f\"There are {len(os.listdir(train_dir))} images of dogs and cats.\")","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:02:41.526436Z","iopub.execute_input":"2023-01-03T19:02:41.526846Z","iopub.status.idle":"2023-01-03T19:02:41.557811Z","shell.execute_reply.started":"2023-01-03T19:02:41.526795Z","shell.execute_reply":"2023-01-03T19:02:41.556742Z"},"id":"VE1dvZz1InKZ","outputId":"5427ba85-7bd4-40d7-c5d8-3db349f33aef","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Store the paths in a list and assign labels to each image**","metadata":{"id":"3PHfFjw9InKa"}},{"cell_type":"code","source":"def label_image(folder_path):\n  file_names = []\n  labels = []\n  for subdir, dirs, files in os.walk(folder_path):\n      for file in files:\n          filepath = subdir + file\n          if filepath.endswith(\".jpg\"):\n              file_names.append(filepath)\n              if file[:3]=='dog':\n                  labels.append('dog')\n              else:\n                  labels.append('cat')\n  return file_names , labels","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:02:46.170184Z","iopub.execute_input":"2023-01-03T19:02:46.171603Z","iopub.status.idle":"2023-01-03T19:02:46.219389Z","shell.execute_reply.started":"2023-01-03T19:02:46.171552Z","shell.execute_reply":"2023-01-03T19:02:46.218487Z"},"id":"J90nDqnFInKa","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path , train_labels = label_image(train_dir)","metadata":{"id":"6crbvXXnPOE5"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# validate the output of the function","metadata":{"id":"8Rnr_cdyPyJ7"}},{"cell_type":"code","source":"train_path[:5] , train_labels[:5]","metadata":{"id":"-BjF7hsBPbkL","outputId":"dfb76884-99e1-47d8-b7d3-01bff9fc2c0a"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique_labels = np.unique(train_labels)\nmodel_label = LabelEncoder().fit_transform(train_labels)\nmodel_label = to_categorical(model_label)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:02:48.655792Z","iopub.execute_input":"2023-01-03T19:02:48.656427Z","iopub.status.idle":"2023-01-03T19:02:48.676490Z","shell.execute_reply.started":"2023-01-03T19:02:48.656389Z","shell.execute_reply":"2023-01-03T19:02:48.675601Z"},"id":"-MDIAILlInKb","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**confirm we have the same number of labels and files**","metadata":{"id":"64zuVoC2InKc"}},{"cell_type":"code","source":"print(f'There are {len(train_path)} file Paths')\nprint(f'There are {len(train_labels)} Labels') ","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:02:51.387889Z","iopub.execute_input":"2023-01-03T19:02:51.388257Z","iopub.status.idle":"2023-01-03T19:02:51.393953Z","shell.execute_reply.started":"2023-01-03T19:02:51.388224Z","shell.execute_reply":"2023-01-03T19:02:51.392968Z"},"id":"8sfXdJYNInKc","outputId":"ea0e8460-7503-4c51-d91d-02641ce77e54","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Create Functions to organize the pipeline of the model**","metadata":{"id":"ghiY2KVrInKd"}},{"cell_type":"code","source":"def process_image(image_path=None,img_size=240,channels=3):\n  \"\"\"\n  Takes an image file path and returns the tensor of the resized image \n  \"\"\"\n  image = tf.io.read_file(image_path) \n  # turn the jpg image into numerical tensors\n  image = tf.image.decode_jpeg(image,channels) \n  # convert  the colour channel values from 0-255 to 0 -1 (normalization)\n  image = tf.image.convert_image_dtype(image,tf.float32)\n  image = tf.image.resize(image,size=[img_size,img_size])\n\n  return image","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:02:53.529820Z","iopub.execute_input":"2023-01-03T19:02:53.530206Z","iopub.status.idle":"2023-01-03T19:02:53.537097Z","shell.execute_reply.started":"2023-01-03T19:02:53.530172Z","shell.execute_reply":"2023-01-03T19:02:53.535818Z"},"id":"2lM0dUbMInKd","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_image_label(image_path,label):\n  \"\"\"\n  Takes an image path with label \n  returns a tuple of (image,label).\n  \"\"\"\n  image = process_image(image_path)\n  return image , label","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:02:56.144763Z","iopub.execute_input":"2023-01-03T19:02:56.145154Z","iopub.status.idle":"2023-01-03T19:02:56.152376Z","shell.execute_reply.started":"2023-01-03T19:02:56.145121Z","shell.execute_reply":"2023-01-03T19:02:56.149224Z"},"id":"ra_yIZjZInKd","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_data(X,y=None,val_data=False,test_data=False,augument=False,batch_size=32):\n  \"\"\"\n  if val_data: returns a resized image and label tuple in the form of a tensor with the specified batch size.\n  if test_data: returns a resized image in the form of a tensor with the specified batch size.\n  if train_data: returns a resized and augumented image with labels tuple in the form of a tensor with the specified batch size.\n  \"\"\"\n  AUTOTUNE = tf.data.AUTOTUNE\n  # set up image augmentation \n  data_augmentation = Sequential([RandomFlip(\"horizontal_and_vertical\"),RandomRotation(0.1)])\n\n  if test_data: # no labels required\n    print('creating test data batches...')\n    data = tf.data.Dataset.from_tensor_slices(tf.constant(X))\n    data = data.map(process_image)\n    data_batch = data.batch(batch_size).prefetch(buffer_size=AUTOTUNE)\n    \n    return data_batch\n  \n  if val_data: \n    print('creating validation data batches...')\n    data = tf.data.Dataset.from_tensor_slices((tf.constant(X),\n                                              tf.constant(y)))\n    data = data.map(get_image_label)\n    data_batch = data.batch(batch_size).prefetch(buffer_size=AUTOTUNE)\n    return data_batch\n  \n  else:\n    print('creating training data batches...')\n    data = tf.data.Dataset.from_tensor_slices((tf.constant(X),\n                                              tf.constant(y)))\n    data = data.shuffle(buffer_size=len(X))\n    data = data.map(get_image_label)\n    if augument:\n        data = data.map(lambda x, y: (data_augmentation(x, training=True), y), \n                num_parallel_calls=AUTOTUNE)\n    data_batch = data.batch(batch_size).prefetch(buffer_size=AUTOTUNE)\n    return data_batch","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:02:58.277694Z","iopub.execute_input":"2023-01-03T19:02:58.278418Z","iopub.status.idle":"2023-01-03T19:02:58.290437Z","shell.execute_reply.started":"2023-01-03T19:02:58.278381Z","shell.execute_reply":"2023-01-03T19:02:58.289094Z"},"id":"mcdD1Vr-InKe","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_images(images,labels,num=None,nrows=None,ncols=None):\n    \"\"\" Displays a plot of n images and thier labels from a data batch \"\"\"\n    # set up the figure\n    plt.figure(figsize=(10,10))\n    #loop through\n    for i in range(num):\n      ax = plt.subplot(nrows,ncols,i+1)\n      plt.imshow(images[i])\n      plt.title(unique_labels[np.argmax(labels[i])])\n      plt.axis('off')\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:03:01.086974Z","iopub.execute_input":"2023-01-03T19:03:01.087395Z","iopub.status.idle":"2023-01-03T19:03:01.095665Z","shell.execute_reply.started":"2023-01-03T19:03:01.087359Z","shell.execute_reply":"2023-01-03T19:03:01.094584Z"},"id":"V3VmSztuInKe","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_model(model,suffix=None):\n    \"\"\"\n    Saves a given model in a models directory and append a suffix (string)\n    \"\"\"\n    modeldir = os.path.join('/models', datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n    model_path = modeldir + '_' + '.h5'  #save format of a model\n    model.save(model_path)\n    \n    return model_path","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:03:03.096040Z","iopub.execute_input":"2023-01-03T19:03:03.096455Z","iopub.status.idle":"2023-01-03T19:03:03.102641Z","shell.execute_reply.started":"2023-01-03T19:03:03.096417Z","shell.execute_reply":"2023-01-03T19:03:03.101177Z"},"id":"l9QeNPntInKf","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_model(model_path):\n  \"\"\"\n  Loads a model from the specified path\n  \"\"\"\n  print(f\"Loading model from: {model_path}...\")\n  model = tf.keras.models.load_model(model_path)\n\n  return model","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:03:05.392120Z","iopub.execute_input":"2023-01-03T19:03:05.392938Z","iopub.status.idle":"2023-01-03T19:03:05.397928Z","shell.execute_reply.started":"2023-01-03T19:03:05.392895Z","shell.execute_reply":"2023-01-03T19:03:05.396925Z"},"id":"lwvd2W2sInKf","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_pred_label(prediction_prob):\n  \"\"\"\n  Turns an array of prediction probabilities into a label \n  \"\"\"\n  return unique_labels[np.argmax(prediction_prob)]","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:03:07.533223Z","iopub.execute_input":"2023-01-03T19:03:07.533985Z","iopub.status.idle":"2023-01-03T19:03:07.538981Z","shell.execute_reply.started":"2023-01-03T19:03:07.533945Z","shell.execute_reply":"2023-01-03T19:03:07.537891Z"},"id":"h7Omwu2oInKf","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_pred(prediction_prob,labels,images, num=0,nrows=None,ncols=None):\n  \"\"\"\n  View the prediction ground truth and images for sample n\n  \"\"\"\n  # Create a Figure\n  plt.figure(figsize=(5*ncols,3*nrows))\n  for i in range(num):\n      pred_prob , true_label , image = prediction_prob[i] , labels[i] , images[i]\n      # get the pred label\n      pred_label = get_pred_label(pred_prob)\n      ax = plt.subplot(nrows,ncols,i+1)\n      # change the color of the title depending on the prediction\n      if pred_label == true_label:\n        color = 'green' # True Prediction\n      else:\n        color = 'red' # Wrong Prediction\n      # change plot title to be predicted class and probability\n      plt.title((f\"This image is {np.max(pred_prob)*100:2.0f}% {pred_label}\"),\n                color = color, fontweight='bold', fontsize=15)\n      plt.imshow(image)\n      plt.axis('off')\n  plt.tight_layout()\n  plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:03:11.605703Z","iopub.execute_input":"2023-01-03T19:03:11.606090Z","iopub.status.idle":"2023-01-03T19:03:11.614025Z","shell.execute_reply.started":"2023-01-03T19:03:11.606056Z","shell.execute_reply":"2023-01-03T19:03:11.612841Z"},"id":"R0ulHi1YInKf","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unbatch_data(batch_data,test=False):\n  \"\"\" Takes a bacthed dataset of (image,label) Tensors and returns separate arrays of \"\"\"\n  if test:  \n      images = []\n      for image in batch_data.unbatch().as_numpy_iterator():\n        images.append(image)\n      return images\n  else:\n      images = []\n      labels = []\n      for image , label in batch_data.unbatch().as_numpy_iterator():\n        images.append(image)\n        labels.append(unique_labels[np.argmax(label)])\n      return images , labels","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:03:13.923161Z","iopub.execute_input":"2023-01-03T19:03:13.923939Z","iopub.status.idle":"2023-01-03T19:03:13.930324Z","shell.execute_reply.started":"2023-01-03T19:03:13.923903Z","shell.execute_reply":"2023-01-03T19:03:13.929206Z"},"id":"AuUWnMelInKg","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def model_diagnostic_plot(history,loss=False,accuracy=False):\n    if loss:# plot loss\n        plt.figure(figsize=(10,5))\n        plt.subplot(211)\n        plt.title('Training and validation Loss')\n        plt.plot(history.history['loss'], color='blue', label='train')\n        plt.plot(history.history['val_loss'], color='orange', label='test')\n        plt.legend()\n        plt.show()\n    if accuracy: # plot accuracy\n        plt.figure(figsize=(10,5))\n        plt.subplot(212)\n        plt.title('Training and validation Accuracy')\n        plt.plot(history.history['accuracy'], color='blue', label='train')\n        plt.plot(history.history['val_accuracy'], color='orange', label='test')\n        plt.legend()\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:03:21.322981Z","iopub.execute_input":"2023-01-03T19:03:21.323446Z","iopub.status.idle":"2023-01-03T19:03:21.332419Z","shell.execute_reply.started":"2023-01-03T19:03:21.323408Z","shell.execute_reply":"2023-01-03T19:03:21.331357Z"},"id":"RdTPWnAaInKg","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Split the data into train and validation set**","metadata":{"id":"P_8kflvoInKg"}},{"cell_type":"code","source":"X = train_path\ny = model_label\nX_train , X_val , Y_train , Y_val = train_test_split(X, y, random_state=42, test_size=0.15)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:03:25.111255Z","iopub.execute_input":"2023-01-03T19:03:25.112036Z","iopub.status.idle":"2023-01-03T19:03:25.128133Z","shell.execute_reply.started":"2023-01-03T19:03:25.111994Z","shell.execute_reply":"2023-01-03T19:03:25.127328Z"},"id":"m8easRMIInKh","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Create a train and validation batch**","metadata":{"id":"1Sx0fZIJInKh"}},{"cell_type":"code","source":"train_data = process_data(X=X_train, y=Y_train,augument=True)\nval_data = process_data(X=X_val, y=Y_val, val_data=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:03:38.261288Z","iopub.execute_input":"2023-01-03T19:03:38.261673Z","iopub.status.idle":"2023-01-03T19:03:38.515578Z","shell.execute_reply.started":"2023-01-03T19:03:38.261639Z","shell.execute_reply":"2023-01-03T19:03:38.514301Z"},"id":"DMrDG4vkInKh","outputId":"1acf93ab-49e7-467e-d22a-febd8673ecc3","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**visualize a subject of the training data**","metadata":{"id":"inZsUe7_InKh"}},{"cell_type":"code","source":"train_images , train_labels = next(train_data.as_numpy_iterator())","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:45:01.356052Z","iopub.execute_input":"2023-01-03T17:45:01.356438Z","iopub.status.idle":"2023-01-03T17:45:01.676731Z","shell.execute_reply.started":"2023-01-03T17:45:01.356404Z","shell.execute_reply":"2023-01-03T17:45:01.675566Z"},"id":"M0Ox1s9kInKh","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(train_images[:15],train_labels[:15],num=15,nrows=3,ncols=5)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:45:03.748424Z","iopub.execute_input":"2023-01-03T17:45:03.748824Z","iopub.status.idle":"2023-01-03T17:45:04.751100Z","shell.execute_reply.started":"2023-01-03T17:45:03.748784Z","shell.execute_reply":"2023-01-03T17:45:04.750229Z"},"id":"8cI6PK-FInKi","outputId":"3e3f4365-02c4-4e91-9d36-93813037ad24","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Setup the network**","metadata":{"id":"W2ZNl2tYInKi"}},{"cell_type":"code","source":"def make_model(img_size=None,output_shape=None):\n\n    INPUT_SHAPE = (img_size,img_size,3)\n    model = Sequential()\n    model.add(Input(shape= INPUT_SHAPE,name ='input'))\n    model.add(Conv2D(filters=32,kernel_size=[5,5],activation='relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2), strides=2))\n    model.add(Dropout(0.2))\n\n    model.add(Conv2D(filters=64,kernel_size=[5,5],activation='relu'))\n    model.add(BatchNormalization())\n    model.add(MaxPooling2D(pool_size=(2, 2), strides=2))\n    model.add(Dropout(0.2))\n    \n    model.add(Flatten())\n    model.add(Dense(128,activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Dropout(rate=0.4))\n    \n    model.add(Dense(64,activation='relu'))\n    model.add(BatchNormalization())\n    model.add(Dropout(rate=0.5))\n    \n    model.add(Dense(output_shape,activation='sigmoid'))\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:45:08.997245Z","iopub.execute_input":"2023-01-03T17:45:08.997651Z","iopub.status.idle":"2023-01-03T17:45:09.008035Z","shell.execute_reply.started":"2023-01-03T17:45:08.997618Z","shell.execute_reply":"2023-01-03T17:45:09.006222Z"},"id":"KCzaD3JbInKi","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 240\nOUTPUT_SHAPE = len(unique_labels)\nmodel = make_model(img_size=IMG_SIZE,output_shape=OUTPUT_SHAPE)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:45:17.749864Z","iopub.execute_input":"2023-01-03T17:45:17.750220Z","iopub.status.idle":"2023-01-03T17:45:17.880936Z","shell.execute_reply.started":"2023-01-03T17:45:17.750190Z","shell.execute_reply":"2023-01-03T17:45:17.879501Z"},"id":"SEow0EVLInKi","outputId":"4e319e58-90e1-4694-f730-8224b1933422","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\n# create a Tensorboard session\n# tensorbaord = create_tensorboard_callback()\n# early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy',patience=3)\n# fit the model to the data and pass it the callbacks\nbatch_size = 32\nhistory = model.fit(x=train_data,\n                    validation_data=val_data, \n                    epochs=20) #callbacks = [tensorbaord,early_stopping]","metadata":{"execution":{"iopub.status.busy":"2023-01-03T17:50:35.757740Z","iopub.execute_input":"2023-01-03T17:50:35.758110Z","iopub.status.idle":"2023-01-03T18:50:04.179939Z","shell.execute_reply.started":"2023-01-03T17:50:35.758080Z","shell.execute_reply":"2023-01-03T18:50:04.178936Z"},"id":"JPwgylnAInKi","outputId":"721a9248-79b6-452f-aaa9-f91c1ced803c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# save model\nsave_path = save_model(model)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T18:50:25.334724Z","iopub.execute_input":"2023-01-03T18:50:25.335096Z","iopub.status.idle":"2023-01-03T18:50:25.893464Z","shell.execute_reply.started":"2023-01-03T18:50:25.335066Z","shell.execute_reply":"2023-01-03T18:50:25.892493Z"},"id":"rUrF6E8tInKj","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# load model\nloaded_model = load_model(save_path)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:03:47.079260Z","iopub.execute_input":"2023-01-03T19:03:47.080008Z","iopub.status.idle":"2023-01-03T19:03:49.601781Z","shell.execute_reply.started":"2023-01-03T19:03:47.079971Z","shell.execute_reply":"2023-01-03T19:03:49.600691Z"},"id":"ET8NqjQfInKj","outputId":"bd2c1964-68c4-4d54-c8a4-5ef82575ccfb","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model diagnostics plot**","metadata":{"id":"y5I-FernInKj"}},{"cell_type":"code","source":"model_diagnostic_plot(history,loss=True, accuracy=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:07:45.378835Z","iopub.execute_input":"2023-01-03T19:07:45.379287Z","iopub.status.idle":"2023-01-03T19:07:45.384040Z","shell.execute_reply.started":"2023-01-03T19:07:45.379240Z","shell.execute_reply":"2023-01-03T19:07:45.383056Z"},"id":"FSc-PziVInKj","outputId":"8a8a9d67-a3dc-4805-8c5c-d04f97cfed14","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_pred = loaded_model.predict(val_data)","metadata":{"id":"u4kN8mc5hP9Y","outputId":"607ca712-37f2-4d7c-e69f-b16a6188534e"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**plot the predicted image and the probability of model on validation data**","metadata":{"id":"b3P1gYmthST0"}},{"cell_type":"code","source":"val_images , val_labels = unbatch_data(val_data)","metadata":{"id":"IYahNO2ShWym"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_pred(prediction_prob=val_pred,labels= val_labels,images=val_images,num=15,nrows=3,ncols=5)","metadata":{"id":"3mrHz_1_hQV9","outputId":"845aa226-ac57-4341-b443-8808f9564add"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Loading the test data**","metadata":{"id":"b73qGarhInKk"}},{"cell_type":"code","source":"test_path = '/content/test1/'\ntest_dir = []\nfor subdir, dirs, files in os.walk(test_path):\n    for file in files:\n        filepath = subdir + file\n        if filepath.endswith(\".jpg\"):\n            test_dir.append(filepath)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:07:51.154035Z","iopub.execute_input":"2023-01-03T19:07:51.154448Z","iopub.status.idle":"2023-01-03T19:07:51.181388Z","shell.execute_reply.started":"2023-01-03T19:07:51.154415Z","shell.execute_reply":"2023-01-03T19:07:51.180308Z"},"id":"mtGfsMSPInKk","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**processing the test data with the process_data function**","metadata":{"id":"XuXhohy9InKk"}},{"cell_type":"code","source":"test_data = process_data(X=test_dir, test_data=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:07:53.727203Z","iopub.execute_input":"2023-01-03T19:07:53.727611Z","iopub.status.idle":"2023-01-03T19:07:53.756591Z","shell.execute_reply.started":"2023-01-03T19:07:53.727577Z","shell.execute_reply":"2023-01-03T19:07:53.755439Z"},"id":"vWRxA7fuInKk","outputId":"7401ba84-1f17-4c16-d514-a040abe49d8a","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**making predictions on the test data**","metadata":{"id":"YcLNAxAqInKl"}},{"cell_type":"code","source":"test_pred = loaded_model.predict(test_data)","metadata":{"execution":{"iopub.status.busy":"2023-01-03T19:07:58.186133Z","iopub.execute_input":"2023-01-03T19:07:58.186826Z","iopub.status.idle":"2023-01-03T19:08:39.413357Z","shell.execute_reply.started":"2023-01-03T19:07:58.186787Z","shell.execute_reply":"2023-01-03T19:08:39.412197Z"},"id":"-sFYLqUbInKl","outputId":"869898db-76e2-4dbb-cc59-a5563c368bce","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Submission**","metadata":{"id":"uNmwSrnBInKm"}},{"cell_type":"code","source":"df = pd.read_csv('../content/sample_submission.csv')\nlabel = [get_pred_label((i,x)) for i,x in test_pred]\ndf['label'] = label\ndf['label'] = df['label'].map({'cat': 0, 'dog': 1})\ndf.head()","metadata":{"id":"9gGv_t1OInKm","outputId":"a7400802-919b-4e6f-8b68-80a197806ec0"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('/content/submission.csv',index=False)","metadata":{"id":"KjyLXHSjvWZe"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**TensorBoard and early stopping callback to track models progress**\n","metadata":{"id":"KSQhhkXVInKm"}},{"cell_type":"code","source":"def create_tensorboard_callback():\n  # create a log directory for storing tensorboard logs\n  logdir = os.path.join('logs/',\n                        datetime.datetime.now().strftime(\"%Y%m%d-%H%M%S\"))\n  return tf.keras.callbacks.TensorBoard(logdir)","metadata":{"id":"3KE34binInKm"},"execution_count":null,"outputs":[]}]}