{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30786,"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-11-04T16:23:03.493649Z","iopub.execute_input":"2024-11-04T16:23:03.494013Z","iopub.status.idle":"2024-11-04T16:23:04.514904Z","shell.execute_reply.started":"2024-11-04T16:23:03.493977Z","shell.execute_reply":"2024-11-04T16:23:04.513681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Python package to open .7z archives\n!pip install py7zr;","metadata":{"execution":{"iopub.status.busy":"2024-11-01T12:05:20.505125Z","iopub.execute_input":"2024-11-01T12:05:20.505858Z","iopub.status.idle":"2024-11-01T12:05:36.398096Z","shell.execute_reply.started":"2024-11-01T12:05:20.505809Z","shell.execute_reply":"2024-11-01T12:05:36.396656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Import libraries\n\n# Loading and processing\nimport glob\nimport cv2\nimport py7zr\n\n# Data manipulation\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\n# Tensorflow\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2024-11-01T07:48:24.011106Z","iopub.execute_input":"2024-11-01T07:48:24.011864Z","iopub.status.idle":"2024-11-01T07:48:24.017117Z","shell.execute_reply.started":"2024-11-01T07:48:24.011823Z","shell.execute_reply":"2024-11-01T07:48:24.016168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Class labels\nCLASS_NAMES = ['airplane', 'automobile', 'bird', 'cat', 'deer',\n               'dog', 'frog', 'horse', 'ship', 'truck']","metadata":{"execution":{"iopub.status.busy":"2024-11-01T07:48:28.139544Z","iopub.execute_input":"2024-11-01T07:48:28.140302Z","iopub.status.idle":"2024-11-01T07:48:28.144547Z","shell.execute_reply.started":"2024-11-01T07:48:28.140261Z","shell.execute_reply":"2024-11-01T07:48:28.143618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Open training archive file\n\nif not len(glob.glob('/kaggle/working/train/*.png')):\n    with py7zr.SevenZipFile('/kaggle/input/cifar-10/train.7z', mode='r') as z:\n        z.extractall()\n","metadata":{"execution":{"iopub.status.busy":"2024-11-01T07:48:31.378125Z","iopub.execute_input":"2024-11-01T07:48:31.378900Z","iopub.status.idle":"2024-11-01T07:48:57.207913Z","shell.execute_reply.started":"2024-11-01T07:48:31.378856Z","shell.execute_reply":"2024-11-01T07:48:57.207008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert the filepath to an integer image ID\ndef filename_to_id(filename):\n    return int(filename.split('/')[-1].split('.')[0])\n\n# Load all of the images into an array, also return the image IDs\ndef load_imgs_and_ids(img_directory_path):\n    image_filenames = glob.glob(img_directory_path + '/*.png')\n    list_of_images = []\n    image_ids = []\n    for f in image_filenames:\n        image_id = filename_to_id(f) # E.G. '/kaggle/working/train/44240.png' -> 44240\n        image_ids.append(image_id)\n        data_array = cv2.imread(f)\n        list_of_images.append(data_array)\n\n    X = np.array(list_of_images)\n    \n    return X, image_ids\n    \nX_train, train_img_ids = load_imgs_and_ids('/kaggle/working/train')\n\n# Read the labels from the CSV files and convert the array of IDs into a 0-9 code\ntrain_labels_df = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\nimg_codes = [CLASS_NAMES.index(train_labels_df.loc[img_id-1]['label']) for img_id in train_img_ids]\ny_train = np.array(img_codes)\n\nX_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-11-01T07:50:05.760256Z","iopub.execute_input":"2024-11-01T07:50:05.760631Z","iopub.status.idle":"2024-11-01T07:50:12.938331Z","shell.execute_reply.started":"2024-11-01T07:50:05.760596Z","shell.execute_reply":"2024-11-01T07:50:12.937258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# RGB pixel values lie on a [0,...,255] scale \n# from 0 to 1 inclusive\n\nX_train = X_train / 255.0","metadata":{"execution":{"iopub.status.busy":"2024-11-01T07:50:20.061460Z","iopub.execute_input":"2024-11-01T07:50:20.062447Z","iopub.status.idle":"2024-11-01T07:50:20.438297Z","shell.execute_reply.started":"2024-11-01T07:50:20.062402Z","shell.execute_reply":"2024-11-01T07:50:20.437205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# To get a sense of the data we work with\n\nplt.rcParams[\"figure.figsize\"] = (10,10)\nfig,axes = plt.subplots(10,10)\nfor i,ax in enumerate(axes.flatten()):\n  ax.imshow(X_train[i])\n  ax.set_title(f'{CLASS_NAMES[y_train[i]]}')\n  ax.set_xticks([])\n  ax.set_yticks([])\nfig.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-11-01T07:50:24.820320Z","iopub.execute_input":"2024-11-01T07:50:24.820701Z","iopub.status.idle":"2024-11-01T07:50:30.787833Z","shell.execute_reply.started":"2024-11-01T07:50:24.820666Z","shell.execute_reply":"2024-11-01T07:50:30.786620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# CNN model, with 6 convolutional layers, 3 pooling layers, and 3 dense layers. Softmax output layer.\nmodel = models.Sequential()\n\nmodel.add(layers.Conv2D(64, (5,5), padding='same', activation='relu', input_shape=(32,32,3)))\nmodel.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\nmodel.add(layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)))\n\nmodel.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\nmodel.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\nmodel.add(layers.MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\nmodel.add(layers.Conv2D(128, (3,3), padding='same', activation='relu'))\nmodel.add(layers.MaxPooling2D(pool_size=(2,2)))\n\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(128, activation='relu'))\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dense(10, activation='softmax'))\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-01T07:50:38.356239Z","iopub.execute_input":"2024-11-01T07:50:38.356650Z","iopub.status.idle":"2024-11-01T07:50:39.296617Z","shell.execute_reply.started":"2024-11-01T07:50:38.356612Z","shell.execute_reply":"2024-11-01T07:50:39.295695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-11-01T07:50:45.856265Z","iopub.execute_input":"2024-11-01T07:50:45.856662Z","iopub.status.idle":"2024-11-01T07:50:45.871596Z","shell.execute_reply.started":"2024-11-01T07:50:45.856625Z","shell.execute_reply":"2024-11-01T07:50:45.870674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# We augment the training data within the mini-batches to try to reduce generalization error\ndatagen = ImageDataGenerator(\n    horizontal_flip=True,\n    channel_shift_range=0.05,\n    rotation_range=10,\n    validation_split=0.15\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-01T07:50:49.319805Z","iopub.execute_input":"2024-11-01T07:50:49.320194Z","iopub.status.idle":"2024-11-01T07:50:49.325138Z","shell.execute_reply.started":"2024-11-01T07:50:49.320157Z","shell.execute_reply":"2024-11-01T07:50:49.324049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# An example transformation\n\nimg = X_train[4]\n\nfig, ax = plt.subplots(1,2)\nax[0].imshow(img)\nax[0].set_title(\"Original image\")\nax[1].imshow(datagen.apply_transform(img,{'theta':-10,'channel_shift_intensity':0.05,'flip_horizontal':True}))\nax[1].set_title(\"Example transformation\")\n\nfor i in range(2):\n  ax[i].set_xticks([])\n  ax[i].set_yticks([])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-01T07:50:53.050910Z","iopub.execute_input":"2024-11-01T07:50:53.051302Z","iopub.status.idle":"2024-11-01T07:50:53.265841Z","shell.execute_reply.started":"2024-11-01T07:50:53.051266Z","shell.execute_reply":"2024-11-01T07:50:53.264147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\n\n# Define early stopping\nearly_stopping = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=10)\n\n# Set up checkpoint to save model every few epochs\ncheckpoint_callback = ModelCheckpoint(\n    filepath='/kaggle/working/model_checkpoint.weights.h5',  # Save path\n    save_weights_only=True,                          # Only save weights\n    save_freq='epoch',                               # Save at end of each epoch\n    verbose=1\n)\n\n# Configure training with datagen flow and validation\ntraining_flow = datagen.flow(X_train, y_train, subset='training')\nvalidation_flow = datagen.flow(X_train, y_train, subset='validation', shuffle=False)\n\n# Train model with checkpointing and early stopping\nhistory = model.fit(\n    training_flow,\n    validation_data=validation_flow,\n    epochs=1,\n    verbose=1,\n    callbacks=[checkpoint_callback, early_stopping]\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-01T07:50:58.613658Z","iopub.execute_input":"2024-11-01T07:50:58.614480Z","iopub.status.idle":"2024-11-01T07:51:52.411609Z","shell.execute_reply.started":"2024-11-01T07:50:58.614432Z","shell.execute_reply":"2024-11-01T07:51:52.410778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load weights from the last checkpoint\nmodel.load_weights('/kaggle/working/model_checkpoint.weights.h5')\n\n# Resume training\nhistory = model.fit(\n    training_flow,\n    validation_data=validation_flow,\n    epochs=10,  # Continue \n    initial_epoch=len(history.epoch),  # Start from last completed epoch\n    verbose=1,\n    callbacks=[checkpoint_callback, early_stopping]\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-01T07:52:08.379433Z","iopub.execute_input":"2024-11-01T07:52:08.380275Z","iopub.status.idle":"2024-11-01T07:58:49.535679Z","shell.execute_reply.started":"2024-11-01T07:52:08.380233Z","shell.execute_reply":"2024-11-01T07:58:49.534619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load weights from the last checkpoint\nmodel.load_weights('/kaggle/working/model_checkpoint.weights.h5')\n\n# Resume training\nhistory = model.fit(\n    training_flow,\n    validation_data=validation_flow,\n    epochs=25,  # Continue from where it left off\n    initial_epoch=len(history.epoch),  # Start from last completed epoch\n    verbose=1,\n    callbacks=[checkpoint_callback, early_stopping]\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-01T08:49:00.231825Z","iopub.execute_input":"2024-11-01T08:49:00.232330Z","iopub.status.idle":"2024-11-01T08:55:45.505529Z","shell.execute_reply.started":"2024-11-01T08:49:00.232284Z","shell.execute_reply":"2024-11-01T08:55:45.504759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load weights from the last checkpoint\nmodel.load_weights('/kaggle/working/model_checkpoint.weights.h5')\n\n# Resume training\nhistory = model.fit(\n    training_flow,\n    validation_data=validation_flow,\n    epochs=50,  # Continue from where it left off\n    initial_epoch=len(history.epoch),  # Start from last completed epoch\n    verbose=1,\n    callbacks=[checkpoint_callback, early_stopping]\n)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-01T09:25:21.007249Z","iopub.execute_input":"2024-11-01T09:25:21.007699Z","iopub.status.idle":"2024-11-01T09:55:25.135620Z","shell.execute_reply.started":"2024-11-01T09:25:21.007658Z","shell.execute_reply":"2024-11-01T09:55:25.134784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the evolution of training/validation error and accuracy\nevaluation = model.evaluate(X_train, y_train)\nprint(f'Test Accuracy : {evaluation[1] * 100:.2f}%')\n\ntrain_metrics = history.history\n\nfig, ax = plt.subplots(1,2)\nax[0].plot(train_metrics['loss'])\nax[0].plot(train_metrics['val_loss'])\nax[0].legend(['train','val'], loc='upper right')\nax[0].set_xlabel(\"Epoch\")\nax[0].set_ylabel(\"Loss\")\nax[0].set_title(\"Loss over the training epochs\")\n\nax[1].plot(train_metrics['accuracy'])\nax[1].plot(train_metrics['val_accuracy'])\nax[1].legend(['train','val'], loc='upper right')\nax[1].set_xlabel(\"Epoch\")\nax[1].set_ylabel(\"Accuracy\")\nax[1].set_title(\"Accuracy over the training epochs\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-01T09:57:58.214838Z","iopub.execute_input":"2024-11-01T09:57:58.215233Z","iopub.status.idle":"2024-11-01T09:58:04.463147Z","shell.execute_reply.started":"2024-11-01T09:57:58.215198Z","shell.execute_reply":"2024-11-01T09:58:04.462175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Example predictions\n\nsample_set = X_train[:25]\n\nsample_predictions = np.argmax(model.predict(sample_set),axis=1)\n\nfig,axes = plt.subplots(5,5)\nfor i,ax in enumerate(axes.flatten()):\n  ax.imshow(sample_set[i])\n  ax.set_title(f'{CLASS_NAMES[sample_predictions[i]]}')\n  ax.set_xticks([])\n  ax.set_yticks([])\nfig.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-11-01T09:58:18.058157Z","iopub.execute_input":"2024-11-01T09:58:18.058927Z","iopub.status.idle":"2024-11-01T09:58:19.635143Z","shell.execute_reply.started":"2024-11-01T09:58:18.058883Z","shell.execute_reply":"2024-11-01T09:58:19.634204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract test images if not already extracted\nif not len(glob.glob('/kaggle/working/test/*.png')):\n    with py7zr.SevenZipFile('/kaggle/input/cifar-10/test.7z', mode='r') as z:\n        z.extractall(path='/kaggle/working/test')\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\n\ndef load_test_images(img_directory_path):\n    image_filenames = glob.glob(img_directory_path + '/*.png')\n    list_of_images = []\n    image_ids = []\n    for f in tqdm(image_filenames):\n        image_id = int(f.split('/')[-1].split('.')[0])  # Extract ID from filename\n        image_ids.append(image_id)\n        data_array = cv2.imread(f)\n        data_array = cv2.resize(data_array, (32, 32))  # Resize to 32x32 pixels\n        data_array = data_array.astype('float32') / 255.0  # Normalize pixel values\n        list_of_images.append(data_array)\n        \n    X_test = np.array(list_of_images)\n    return X_test, image_ids\n\n# Load and preprocess test images\nX_test, test_img_ids = load_test_images('/kaggle/working/test/test')","metadata":{"execution":{"iopub.status.busy":"2024-11-01T09:17:36.458706Z","iopub.execute_input":"2024-11-01T09:17:36.459139Z","iopub.status.idle":"2024-11-01T09:18:15.833326Z","shell.execute_reply.started":"2024-11-01T09:17:36.459101Z","shell.execute_reply":"2024-11-01T09:18:15.832018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(X_test)\npredicted_labels = [CLASS_NAMES[np.argmax(pred)] for pred in predictions]","metadata":{"execution":{"iopub.status.busy":"2024-11-01T09:58:59.605696Z","iopub.execute_input":"2024-11-01T09:58:59.606662Z","iopub.status.idle":"2024-11-01T09:59:31.907385Z","shell.execute_reply.started":"2024-11-01T09:58:59.606617Z","shell.execute_reply":"2024-11-01T09:59:31.906331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare the submission DataFrame\nsubmission_df = pd.DataFrame({\n    'id': test_img_ids,\n    'label': predicted_labels\n})","metadata":{"execution":{"iopub.status.busy":"2024-11-01T09:59:35.143006Z","iopub.execute_input":"2024-11-01T09:59:35.143898Z","iopub.status.idle":"2024-11-01T09:59:35.295720Z","shell.execute_reply.started":"2024-11-01T09:59:35.143849Z","shell.execute_reply":"2024-11-01T09:59:35.294785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sort by 'id' to match the required format\nsubmission_df = submission_df.sort_values(by='id').reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2024-11-01T09:59:39.081709Z","iopub.execute_input":"2024-11-01T09:59:39.082440Z","iopub.status.idle":"2024-11-01T09:59:39.129675Z","shell.execute_reply.started":"2024-11-01T09:59:39.082399Z","shell.execute_reply":"2024-11-01T09:59:39.128604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save to CSV file for submission\nsubmission_df.to_csv('sampleSubmission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-11-01T09:59:47.113223Z","iopub.execute_input":"2024-11-01T09:59:47.114175Z","iopub.status.idle":"2024-11-01T09:59:47.525192Z","shell.execute_reply.started":"2024-11-01T09:59:47.114132Z","shell.execute_reply":"2024-11-01T09:59:47.523936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-11-01T10:00:03.861693Z","iopub.execute_input":"2024-11-01T10:00:03.862575Z","iopub.status.idle":"2024-11-01T10:00:03.872131Z","shell.execute_reply.started":"2024-11-01T10:00:03.862536Z","shell.execute_reply":"2024-11-01T10:00:03.871062Z"},"trusted":true},"execution_count":null,"outputs":[]}]}