{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[],"dockerImageVersionId":31260,"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","trusted":true,"execution":{"iopub.status.busy":"2026-02-12T11:06:52.789499Z","iopub.execute_input":"2026-02-12T11:06:52.789778Z","iopub.status.idle":"2026-02-12T11:06:53.058605Z","shell.execute_reply.started":"2026-02-12T11:06:52.789754Z","shell.execute_reply":"2026-02-12T11:06:53.058030Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nprint(\"GPU is\", \"available\" if tf.config.list_physical_devices('GPU') else \"NOT AVAILABLE\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-12T11:06:53.059806Z","iopub.execute_input":"2026-02-12T11:06:53.060171Z","iopub.status.idle":"2026-02-12T11:07:07.001363Z","shell.execute_reply.started":"2026-02-12T11:06:53.060148Z","shell.execute_reply":"2026-02-12T11:07:07.000621Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.applications import ResNet50\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nprint(\"TensorFlow version:\", tf.__version__)\nprint(\"GPU Available:\", tf.config.list_physical_devices('GPU'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-12T11:07:07.002312Z","iopub.execute_input":"2026-02-12T11:07:07.003325Z","iopub.status.idle":"2026-02-12T11:07:07.017861Z","shell.execute_reply.started":"2026-02-12T11:07:07.003290Z","shell.execute_reply":"2026-02-12T11:07:07.017192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load the dataset directly from Keras\n(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()\n\n# Normalize pixel values to be between 0 and 1\nx_train, x_test = x_train / 255.0, x_test / 255.0\n\n# Define the class names for reference\nclass_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', \n               'dog', 'frog', 'horse', 'ship', 'truck']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-12T11:07:07.019360Z","iopub.execute_input":"2026-02-12T11:07:07.019573Z","iopub.status.idle":"2026-02-12T11:07:11.482506Z","shell.execute_reply.started":"2026-02-12T11:07:07.019553Z","shell.execute_reply":"2026-02-12T11:07:11.481641Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i in range(9):\n    plt.subplot(3,3,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(x_train[i])\n    plt.xlabel(class_names[y_train[i][0]])\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-12T11:07:11.483670Z","iopub.execute_input":"2026-02-12T11:07:11.484004Z","iopub.status.idle":"2026-02-12T11:07:11.733879Z","shell.execute_reply.started":"2026-02-12T11:07:11.483954Z","shell.execute_reply":"2026-02-12T11:07:11.733224Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.applications.resnet50 import preprocess_input\n\n# 1. One-hot encode the labels (converts '3' to [0,0,0,1,0,0,0,0,0,0])\ny_train_cat = to_categorical(y_train, 10)\ny_test_cat = to_categorical(y_test, 10)\n\n# 2. Preprocess images specifically for ResNet50 requirements\n# We do this instead of just dividing by 255.0 because ResNet expects \n# a specific color distribution.\nx_train_pre = preprocess_input(x_train * 255.0)\nx_test_pre = preprocess_input(x_test * 255.0)\n\nprint(\"Data is preprocessed and ready!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-12T11:07:11.734785Z","iopub.execute_input":"2026-02-12T11:07:11.735059Z","iopub.status.idle":"2026-02-12T11:07:12.518499Z","shell.execute_reply.started":"2026-02-12T11:07:11.735034Z","shell.execute_reply":"2026-02-12T11:07:12.517857Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 1. First, we define HOW the model is built\ndef build_resnet_model():\n    inputs = layers.Input(shape=(32, 32, 3))\n    upsample = layers.UpSampling2D(size=(7, 7))(inputs)\n    \n    # Load ResNet50 body\n    base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=upsample)\n    base_model.trainable = False  # Freeze it\n    \n    # Add the \"Brain\" (Classification head)\n    x = layers.GlobalAveragePooling2D()(base_model.output)\n    x = layers.Flatten()(x)\n    x = layers.Dense(1024, activation='relu')(x)\n    x = layers.Dropout(0.3)(x)\n    x = layers.Dense(512, activation='relu')(x)\n    outputs = layers.Dense(10, activation='softmax')(x)\n    \n    return models.Model(inputs=inputs, outputs=outputs)\n\n# 2. Second, we actually CREATE the model and call it \"model\"\nmodel = build_resnet_model()\n\n# 3. Third, we tell it how to measure success\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n\nprint(\"Model identified and created successfully!\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-12T11:07:12.519450Z","iopub.execute_input":"2026-02-12T11:07:12.519782Z","iopub.status.idle":"2026-02-12T11:07:15.916353Z","shell.execute_reply.started":"2026-02-12T11:07:12.519746Z","shell.execute_reply":"2026-02-12T11:07:15.915721Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    x_train_pre, y_train_cat,\n    batch_size=64,\n    epochs=5,\n    validation_data=(x_test_pre, y_test_cat),\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-12T11:07:15.917360Z","iopub.execute_input":"2026-02-12T11:07:15.917668Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# This saves your hard work to a file\nmodel.save('cifar10_resnet_model.h5')\nprint(\"Model saved! You won't have to train from scratch if the notebook resets.\")","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}