{"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-04-26T05:18:17.953941Z","iopub.execute_input":"2024-04-26T05:18:17.954614Z","iopub.status.idle":"2024-04-26T05:18:18.899751Z","shell.execute_reply.started":"2024-04-26T05:18:17.954577Z","shell.execute_reply":"2024-04-26T05:18:18.898818Z"},"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-04-26T05:21:06.944147Z","iopub.execute_input":"2024-04-26T05:21:06.944626Z","iopub.status.idle":"2024-04-26T05:21:20.474297Z","shell.execute_reply.started":"2024-04-26T05:21:06.944595Z","shell.execute_reply":"2024-04-26T05:21:20.473282Z"},"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-04-26T05:21:23.088481Z","iopub.execute_input":"2024-04-26T05:21:23.089164Z","iopub.status.idle":"2024-04-26T05:21:23.096025Z","shell.execute_reply.started":"2024-04-26T05:21:23.089130Z","shell.execute_reply":"2024-04-26T05:21:23.094997Z"},"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-04-26T05:21:41.054832Z","iopub.execute_input":"2024-04-26T05:21:41.055641Z","iopub.status.idle":"2024-04-26T05:21:41.066936Z","shell.execute_reply.started":"2024-04-26T05:21:41.055607Z","shell.execute_reply":"2024-04-26T05:21:41.066006Z"},"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-04-26T05:21:44.617525Z","iopub.execute_input":"2024-04-26T05:21:44.618378Z","iopub.status.idle":"2024-04-26T05:21:49.397853Z","shell.execute_reply.started":"2024-04-26T05:21:44.618339Z","shell.execute_reply":"2024-04-26T05:21:49.397014Z"},"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-04-26T05:21:59.750993Z","iopub.execute_input":"2024-04-26T05:21:59.751639Z","iopub.status.idle":"2024-04-26T05:22:00.514172Z","shell.execute_reply.started":"2024-04-26T05:21:59.751606Z","shell.execute_reply":"2024-04-26T05:22:00.513238Z"},"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-04-26T05:22:00.515702Z","iopub.execute_input":"2024-04-26T05:22:00.515996Z","iopub.status.idle":"2024-04-26T05:22:01.578467Z","shell.execute_reply.started":"2024-04-26T05:22:00.515970Z","shell.execute_reply":"2024-04-26T05:22:01.577543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_images[0].astype('float32').shape","metadata":{"execution":{"iopub.status.busy":"2024-04-26T05:22:01.579488Z","iopub.execute_input":"2024-04-26T05:22:01.579747Z","iopub.status.idle":"2024-04-26T05:22:01.586342Z","shell.execute_reply.started":"2024-04-26T05:22:01.579724Z","shell.execute_reply":"2024-04-26T05:22:01.585292Z"},"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-04-26T05:22:37.115878Z","iopub.execute_input":"2024-04-26T05:22:37.116258Z","iopub.status.idle":"2024-04-26T05:22:37.121349Z","shell.execute_reply.started":"2024-04-26T05:22:37.116226Z","shell.execute_reply":"2024-04-26T05:22:37.120365Z"},"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-04-26T05:22:38.163993Z","iopub.execute_input":"2024-04-26T05:22:38.164568Z","iopub.status.idle":"2024-04-26T05:22:38.553759Z","shell.execute_reply.started":"2024-04-26T05:22:38.164529Z","shell.execute_reply":"2024-04-26T05:22:38.552663Z"},"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-04-26T05:22:49.762616Z","iopub.execute_input":"2024-04-26T05:22:49.762975Z","iopub.status.idle":"2024-04-26T05:22:52.340439Z","shell.execute_reply.started":"2024-04-26T05:22:49.762935Z","shell.execute_reply":"2024-04-26T05:22:52.339576Z"},"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-04-26T05:23:09.600837Z","iopub.execute_input":"2024-04-26T05:23:09.601215Z","iopub.status.idle":"2024-04-26T05:38:18.184298Z","shell.execute_reply.started":"2024-04-26T05:23:09.601184Z","shell.execute_reply":"2024-04-26T05:38:18.183276Z"},"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-04-26T05:40:50.285289Z","iopub.execute_input":"2024-04-26T05:40:50.286068Z","iopub.status.idle":"2024-04-26T05:41:02.249459Z","shell.execute_reply.started":"2024-04-26T05:40:50.286025Z","shell.execute_reply":"2024-04-26T05:41:02.248674Z"},"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-04-26T05:41:02.251656Z","iopub.execute_input":"2024-04-26T05:41:02.252560Z","iopub.status.idle":"2024-04-26T05:41:02.571884Z","shell.execute_reply.started":"2024-04-26T05:41:02.252516Z","shell.execute_reply":"2024-04-26T05:41:02.570956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_metrics(\"accuracy\", \"Accuracy\")","metadata":{"execution":{"iopub.status.busy":"2024-04-26T05:41:02.573077Z","iopub.execute_input":"2024-04-26T05:41:02.573371Z","iopub.status.idle":"2024-04-26T05:41:02.940010Z","shell.execute_reply.started":"2024-04-26T05:41:02.573345Z","shell.execute_reply":"2024-04-26T05:41:02.939003Z"},"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-04-26T05:41:02.964858Z","iopub.execute_input":"2024-04-26T05:41:02.965155Z","iopub.status.idle":"2024-04-26T05:41:22.893459Z","shell.execute_reply.started":"2024-04-26T05:41:02.965128Z","shell.execute_reply":"2024-04-26T05:41:22.892499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import classification_report\n\n# Generate classification report\nreport = classification_report(validation_labels, probabilities)\n\n# Print the report\nprint(\"Classification Report:\")\nprint(report)","metadata":{"execution":{"iopub.status.busy":"2024-04-26T05:43:46.686659Z","iopub.execute_input":"2024-04-26T05:43:46.687706Z","iopub.status.idle":"2024-04-26T05:43:46.714702Z","shell.execute_reply.started":"2024-04-26T05:43:46.687671Z","shell.execute_reply":"2024-04-26T05:43:46.713899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}