{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-23T15:06:15.365621Z","iopub.execute_input":"2024-03-23T15:06:15.366084Z","iopub.status.idle":"2024-03-23T15:06:16.301014Z","shell.execute_reply.started":"2024-03-23T15:06:15.366052Z","shell.execute_reply":"2024-03-23T15:06:16.300071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import os, re, time, json\nimport PIL.Image, PIL.ImageFont, PIL.ImageDraw\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nimport tensorflow_datasets as tfds\nimport matplotlib.pyplot as plt\n\nprint(f'tensorflow version{tf.__version__}')","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:06:16.302491Z","iopub.execute_input":"2024-03-23T15:06:16.302871Z","iopub.status.idle":"2024-03-23T15:06:29.978352Z","shell.execute_reply.started":"2024-03-23T15:06:16.302844Z","shell.execute_reply":"2024-03-23T15:06:29.977414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Parameters","metadata":{}},{"cell_type":"code","source":"BATCH_SIZE = 32 \nclasses = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:06:29.979554Z","iopub.execute_input":"2024-03-23T15:06:29.980141Z","iopub.status.idle":"2024-03-23T15:06:29.984572Z","shell.execute_reply.started":"2024-03-23T15:06:29.980113Z","shell.execute_reply":"2024-03-23T15:06:29.983530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#@title Visualization Utilities[RUN ME]\n#Matplotlib config\nplt.rc('image', cmap='gray')\nplt.rc('grid', linewidth=0)\nplt.rc('xtick', top=False, bottom=False, labelsize='large')\nplt.rc('ytick', left=False, right=False, labelsize='large')\nplt.rc('axes', facecolor='F8F8F8', titlesize=\"large\", edgecolor='white')\nplt.rc('text', color='a8151a')\nplt.rc('figure', facecolor='F0F0F0')# Matplotlib fonts\nMATPLOTLIB_FONT_DIR = os.path.join(os.path.dirname(plt.__file__), \"mpl-data/fonts/ttf\")\n# utility to display a row of digits with their predictions\ndef display_images(digits, predictions, labels, title):\n\n  n = 10\n\n  indexes = np.random.choice(len(predictions), size=n)\n  n_digits = digits[indexes]\n  n_predictions = predictions[indexes]\n  n_predictions = n_predictions.reshape((n,))\n  n_labels = labels[indexes]\n \n  fig = plt.figure(figsize=(20, 4))\n  plt.title(title)\n  plt.yticks([])\n  plt.xticks([])\n\n  for i in range(10):\n    ax = fig.add_subplot(1, 10, i+1)\n    class_index = n_predictions[i]\n    \n    plt.xlabel(classes[class_index])\n    plt.xticks([])\n    plt.yticks([])\n    plt.imshow(n_digits[i])\n\n# utility to display training and validation curves\ndef plot_metrics(metric_name, title, ylim=5):\n  plt.title(title)\n  plt.ylim(0,ylim)\n  plt.plot(history.history[metric_name],color='blue',label=metric_name)\n  plt.plot(history.history['val_' + metric_name],color='green',label='val_' + metric_name)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:06:29.987256Z","iopub.execute_input":"2024-03-23T15:06:29.987685Z","iopub.status.idle":"2024-03-23T15:06:30.047597Z","shell.execute_reply.started":"2024-03-23T15:06:29.987652Z","shell.execute_reply":"2024-03-23T15:06:30.046662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loading and Preprocessing Data\n\n(training_images, training_labels) , (validation_images, validation_labels) = tf.keras.datasets.cifar10.load_data()","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:06:30.048665Z","iopub.execute_input":"2024-03-23T15:06:30.048929Z","iopub.status.idle":"2024-03-23T15:06:49.908084Z","shell.execute_reply.started":"2024-03-23T15:06:30.048901Z","shell.execute_reply":"2024-03-23T15:06:49.907151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualize Dataset","metadata":{}},{"cell_type":"code","source":"display_images(training_images, training_labels, training_labels, \"Training Data\" )","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:06:49.909200Z","iopub.execute_input":"2024-03-23T15:06:49.909476Z","iopub.status.idle":"2024-03-23T15:06:50.645587Z","shell.execute_reply.started":"2024-03-23T15:06:49.909452Z","shell.execute_reply":"2024-03-23T15:06:50.644666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_images(validation_images, validation_labels, validation_labels, \"Training Data\" )","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:06:50.646747Z","iopub.execute_input":"2024-03-23T15:06:50.647043Z","iopub.status.idle":"2024-03-23T15:06:51.339608Z","shell.execute_reply.started":"2024-03-23T15:06:50.647018Z","shell.execute_reply":"2024-03-23T15:06:51.338433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_images[0].astype('float32').shape","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:06:51.340991Z","iopub.execute_input":"2024-03-23T15:06:51.341329Z","iopub.status.idle":"2024-03-23T15:06:51.349550Z","shell.execute_reply.started":"2024-03-23T15:06:51.341293Z","shell.execute_reply":"2024-03-23T15:06:51.348426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preprocessing Dataset","metadata":{}},{"cell_type":"code","source":"def preprocess_image_input(input_images):\n  input_images = input_images.astype('float32')\n  output_ims = tf.keras.applications.resnet50.preprocess_input(input_images)\n  return output_ims","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:06:51.351061Z","iopub.execute_input":"2024-03-23T15:06:51.351439Z","iopub.status.idle":"2024-03-23T15:06:51.358193Z","shell.execute_reply.started":"2024-03-23T15:06:51.351406Z","shell.execute_reply":"2024-03-23T15:06:51.356682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = preprocess_image_input(training_images)\nvalid_X = preprocess_image_input(validation_images)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:06:51.361507Z","iopub.execute_input":"2024-03-23T15:06:51.362256Z","iopub.status.idle":"2024-03-23T15:06:51.725727Z","shell.execute_reply.started":"2024-03-23T15:06:51.362225Z","shell.execute_reply":"2024-03-23T15:06:51.724687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Define the Network","metadata":{}},{"cell_type":"code","source":"'''\nFeature Extraction is performed by ResNet50 pretrained on imagenet weights. \nInput size is 224 x 224.\n'''\ndef feature_extractor(inputs):\n\n  feature_extractor = tf.keras.applications.resnet.ResNet50(input_shape=(224, 224, 3),\n                                               include_top=False,\n                                               weights='imagenet')(inputs)\n  return feature_extractor\n\n\n'''\nDefines final dense layers and subsequent softmax layer for classification.\n'''\ndef classifier(inputs):\n    x = tf.keras.layers.GlobalAveragePooling2D()(inputs)\n    x = tf.keras.layers.Flatten()(x)\n    x = tf.keras.layers.Dense(1024, activation=\"relu\")(x)\n    x = tf.keras.layers.Dense(512, activation=\"relu\")(x)\n    x = tf.keras.layers.Dense(10, activation=\"softmax\", name=\"classification\")(x)\n    return x\n\n'''\nSince input image size is (32 x 32), first upsample the image by factor of (7x7) to transform it to (224 x 224)\nConnect the feature extraction and \"classifier\" layers to build the model.\n'''\ndef final_model(inputs):\n\n    resize = tf.keras.layers.UpSampling2D(size=(7,7))(inputs)\n\n    resnet_feature_extractor = feature_extractor(resize)\n    classification_output = classifier(resnet_feature_extractor)\n\n    return classification_output\n\n'''\nDefine the model and compile it. \nUse Stochastic Gradient Descent as the optimizer.\nUse Sparse Categorical CrossEntropy as the loss function.\n'''\ndef define_compile_model():\n  inputs = tf.keras.layers.Input(shape=(32,32,3))\n  \n  classification_output = final_model(inputs) \n  model = tf.keras.Model(inputs=inputs, outputs = classification_output)\n \n  model.compile(optimizer='SGD', \n                loss='sparse_categorical_crossentropy',\n                metrics = ['accuracy'])\n  \n  return model\n\n\nmodel = define_compile_model()\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:06:51.726846Z","iopub.execute_input":"2024-03-23T15:06:51.727144Z","iopub.status.idle":"2024-03-23T15:06:58.433235Z","shell.execute_reply.started":"2024-03-23T15:06:51.727120Z","shell.execute_reply":"2024-03-23T15:06:58.432357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train the model","metadata":{}},{"cell_type":"code","source":"# this will take around 20 minutes to complete\nEPOCHS = 4\nhistory = model.fit(train_X, training_labels, epochs=EPOCHS, validation_data = (valid_X, validation_labels), batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:06:58.434367Z","iopub.execute_input":"2024-03-23T15:06:58.434639Z","iopub.status.idle":"2024-03-23T15:22:06.164111Z","shell.execute_reply.started":"2024-03-23T15:06:58.434603Z","shell.execute_reply":"2024-03-23T15:22:06.163244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluate Model","metadata":{}},{"cell_type":"code","source":"loss, accuracy = model.evaluate(valid_X, validation_labels, batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:22:06.165568Z","iopub.execute_input":"2024-03-23T15:22:06.165871Z","iopub.status.idle":"2024-03-23T15:22:27.034976Z","shell.execute_reply.started":"2024-03-23T15:22:06.165846Z","shell.execute_reply":"2024-03-23T15:22:27.034141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plot loss and Accuracy Curves","metadata":{}},{"cell_type":"code","source":"plot_metrics(\"loss\", \"Loss\")","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:22:27.036468Z","iopub.execute_input":"2024-03-23T15:22:27.036780Z","iopub.status.idle":"2024-03-23T15:22:27.407203Z","shell.execute_reply.started":"2024-03-23T15:22:27.036754Z","shell.execute_reply":"2024-03-23T15:22:27.406268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_metrics(\"accuracy\", \"Accuracy\")","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:22:27.408504Z","iopub.execute_input":"2024-03-23T15:22:27.408801Z","iopub.status.idle":"2024-03-23T15:22:27.709415Z","shell.execute_reply.started":"2024-03-23T15:22:27.408774Z","shell.execute_reply":"2024-03-23T15:22:27.708526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualizing Predictions","metadata":{}},{"cell_type":"code","source":"probabilities = model.predict(valid_X, batch_size=64)\nprobabilities = np.argmax(probabilities, axis = 1)\n\ndisplay_images(validation_images, probabilities, validation_labels, \"Bad predictions indicated in red.\")","metadata":{"execution":{"iopub.status.busy":"2024-03-23T15:22:27.710744Z","iopub.execute_input":"2024-03-23T15:22:27.711095Z","iopub.status.idle":"2024-03-23T15:22:47.480333Z","shell.execute_reply.started":"2024-03-23T15:22:27.711063Z","shell.execute_reply":"2024-03-23T15:22:47.478956Z"},"trusted":true},"execution_count":null,"outputs":[]}]}