{"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":30747,"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-08-01T12:01:52.776485Z","iopub.execute_input":"2024-08-01T12:01:52.776836Z","iopub.status.idle":"2024-08-01T12:01:53.130214Z","shell.execute_reply.started":"2024-08-01T12:01:52.776810Z","shell.execute_reply":"2024-08-01T12:01:53.129309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_path = \"/kaggle/input/cifar-10/train.7z\"\ntrain_label_path = \"/kaggle/input/cifar-10/trainLabels.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:01:54.898381Z","iopub.execute_input":"2024-08-01T12:01:54.899301Z","iopub.status.idle":"2024-08-01T12:01:54.903678Z","shell.execute_reply.started":"2024-08-01T12:01:54.899268Z","shell.execute_reply":"2024-08-01T12:01:54.902625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr --quiet","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:02:11.963654Z","iopub.execute_input":"2024-08-01T12:02:11.964602Z","iopub.status.idle":"2024-08-01T12:02:26.553335Z","shell.execute_reply.started":"2024-08-01T12:02:11.964566Z","shell.execute_reply":"2024-08-01T12:02:26.552061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:02:26.555770Z","iopub.execute_input":"2024-08-01T12:02:26.556180Z","iopub.status.idle":"2024-08-01T12:02:26.727499Z","shell.execute_reply.started":"2024-08-01T12:02:26.556146Z","shell.execute_reply":"2024-08-01T12:02:26.726616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"extract_path = '/kaggle/working/'\n\nwith py7zr.SevenZipFile(train_image_path, mode='r') as archive:\n    archive.extractall(path=extract_path)\n\nprint(f\"Files extracted to {extract_path}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:02:26.729012Z","iopub.execute_input":"2024-08-01T12:02:26.729567Z","iopub.status.idle":"2024-08-01T12:02:51.213599Z","shell.execute_reply.started":"2024-08-01T12:02:26.729533Z","shell.execute_reply":"2024-08-01T12:02:51.212605Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv(train_label_path)\nunique_labels = labels['label'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:08:10.775265Z","iopub.execute_input":"2024-08-01T12:08:10.775992Z","iopub.status.idle":"2024-08-01T12:08:10.800190Z","shell.execute_reply.started":"2024-08-01T12:08:10.775958Z","shell.execute_reply":"2024-08-01T12:08:10.799507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:03:27.963176Z","iopub.execute_input":"2024-08-01T12:03:27.963781Z","iopub.status.idle":"2024-08-01T12:03:27.973722Z","shell.execute_reply.started":"2024-08-01T12:03:27.963748Z","shell.execute_reply":"2024-08-01T12:03:27.972895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_dict = {}\nfor i, label in enumerate(unique_labels):\n    label_dict[label] = i\n\nprint(label_dict)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:08:13.475344Z","iopub.execute_input":"2024-08-01T12:08:13.476175Z","iopub.status.idle":"2024-08-01T12:08:13.480601Z","shell.execute_reply.started":"2024-08-01T12:08:13.476141Z","shell.execute_reply":"2024-08-01T12:08:13.479765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels['label'] = labels['label'].map(label_dict)\n\nprint(labels)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:08:15.311837Z","iopub.execute_input":"2024-08-01T12:08:15.312188Z","iopub.status.idle":"2024-08-01T12:08:15.323892Z","shell.execute_reply.started":"2024-08-01T12:08:15.312160Z","shell.execute_reply":"2024-08-01T12:08:15.322868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = labels.drop(labels.columns[:1], axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:08:20.925355Z","iopub.execute_input":"2024-08-01T12:08:20.926225Z","iopub.status.idle":"2024-08-01T12:08:20.933101Z","shell.execute_reply.started":"2024-08-01T12:08:20.926181Z","shell.execute_reply":"2024-08-01T12:08:20.932279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:09:37.751307Z","iopub.execute_input":"2024-08-01T12:09:37.751913Z","iopub.status.idle":"2024-08-01T12:09:37.756234Z","shell.execute_reply.started":"2024-08-01T12:09:37.751879Z","shell.execute_reply":"2024-08-01T12:09:37.755382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label\n\n","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:09:39.870419Z","iopub.execute_input":"2024-08-01T12:09:39.870796Z","iopub.status.idle":"2024-08-01T12:09:39.877113Z","shell.execute_reply.started":"2024-08-01T12:09:39.870767Z","shell.execute_reply":"2024-08-01T12:09:39.876157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import to_categorical \n","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:10:35.670979Z","iopub.execute_input":"2024-08-01T12:10:35.671599Z","iopub.status.idle":"2024-08-01T12:10:47.377087Z","shell.execute_reply.started":"2024-08-01T12:10:35.671567Z","shell.execute_reply":"2024-08-01T12:10:47.376113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_label = to_categorical(train_label)\ntrain_label","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:11:23.026133Z","iopub.execute_input":"2024-08-01T12:11:23.026852Z","iopub.status.idle":"2024-08-01T12:11:23.034866Z","shell.execute_reply.started":"2024-08-01T12:11:23.026816Z","shell.execute_reply":"2024-08-01T12:11:23.033898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\n\nextracted_path = '/kaggle/working/train' \ncontents = os.listdir(extracted_path)\n\ndef extract_number(filename):\n    match = re.search(r'\\d+', filename)\n    return int(match.group()) if match else -1\n\nsorted_contents = sorted(contents, key=extract_number)\n\nprint(sorted_contents[:20])","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:27:58.242581Z","iopub.execute_input":"2024-08-01T12:27:58.242942Z","iopub.status.idle":"2024-08-01T12:27:58.374989Z","shell.execute_reply.started":"2024-08-01T12:27:58.242918Z","shell.execute_reply":"2024-08-01T12:27:58.374041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\n\n\ntrain_images = []\nimage_size = (32,32)\n\nfor filename in sorted_contents:\n    file_path = os.path.join(extracted_path, filename)\n    with Image.open(file_path) as img:\n        img = img.convert('RGB')\n        img = img.resize(image_size)\n        img_array = np.array(img)\n        train_images.append(img_array)\n\n\ntrain_images = np.array(train_images)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:31:37.274767Z","iopub.execute_input":"2024-08-01T12:31:37.275117Z","iopub.status.idle":"2024-08-01T12:31:46.835033Z","shell.execute_reply.started":"2024-08-01T12:31:37.275089Z","shell.execute_reply":"2024-08-01T12:31:46.834230Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images.shape","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:31:58.422941Z","iopub.execute_input":"2024-08-01T12:31:58.423298Z","iopub.status.idle":"2024-08-01T12:31:58.430743Z","shell.execute_reply.started":"2024-08-01T12:31:58.423271Z","shell.execute_reply":"2024-08-01T12:31:58.429937Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_here = np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:34:30.470117Z","iopub.execute_input":"2024-08-01T12:34:30.470499Z","iopub.status.idle":"2024-08-01T12:34:30.475141Z","shell.execute_reply.started":"2024-08-01T12:34:30.470446Z","shell.execute_reply":"2024-08-01T12:34:30.474234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Define the number of images to display\nnum_images_to_show = 10\n\n# Create a figure with a specified size\nplt.figure(figsize=(15, 5))\n\ndef get_key_from_value(d, value):\n    for key, val in d.items():\n        if val == value:\n            return key\n    return none\n        \nfor i in range(num_images_to_show):\n    plt.subplot(1, num_images_to_show, i + 1)\n    plt.imshow(train_images[i])\n    plt.axis('off')\n    label_real = get_key_from_value(label_dict,labels_here[i])\n    plt.title(f'{label_real}', fontsize=12, pad=10)\n\nplt.tight_layout() \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:39:36.410068Z","iopub.execute_input":"2024-08-01T12:39:36.411090Z","iopub.status.idle":"2024-08-01T12:39:37.081596Z","shell.execute_reply.started":"2024-08-01T12:39:36.411056Z","shell.execute_reply":"2024-08-01T12:39:37.080644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices = np.random.randint(1,20000,4)\nindices","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:40:16.163478Z","iopub.execute_input":"2024-08-01T12:40:16.164246Z","iopub.status.idle":"2024-08-01T12:40:16.170011Z","shell.execute_reply.started":"2024-08-01T12:40:16.164214Z","shell.execute_reply":"2024-08-01T12:40:16.169184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 3))\n\nfor i, idx in enumerate(indices):\n    plt.subplot(1, num_images_to_show, i + 1)\n    plt.imshow(train_images[idx])\n    plt.axis('off')\n    label_real = get_key_from_value(label_dict, labels_here[idx])\n    plt.title(f'{label_real}', fontsize=12, pad=10)\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-08-01T12:41:43.291381Z","iopub.execute_input":"2024-08-01T12:41:43.291789Z","iopub.status.idle":"2024-08-01T12:41:43.629018Z","shell.execute_reply.started":"2024-08-01T12:41:43.291758Z","shell.execute_reply":"2024-08-01T12:41:43.627904Z"},"trusted":true},"execution_count":null,"outputs":[]}]}