{"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":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"},{"sourceId":106889,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":89583,"modelId":113792}],"dockerImageVersionId":30761,"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":"2025-03-20T00:54:42.280847Z","iopub.execute_input":"2025-03-20T00:54:42.281188Z","iopub.status.idle":"2025-03-20T00:54:42.618945Z","shell.execute_reply.started":"2025-03-20T00:54:42.281157Z","shell.execute_reply":"2025-03-20T00:54:42.617960Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install py7zr -q\n","metadata":{"execution":{"iopub.status.busy":"2025-03-20T00:54:43.746358Z","iopub.execute_input":"2025-03-20T00:54:43.746850Z","iopub.status.idle":"2025-03-20T00:54:57.302618Z","shell.execute_reply.started":"2025-03-20T00:54:43.746817Z","shell.execute_reply":"2025-03-20T00:54:57.301379Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Import important Libraries","metadata":{}},{"cell_type":"code","source":"import py7zr\nimport numpy as np\nimport pandas as pd\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport os","metadata":{"execution":{"iopub.status.busy":"2025-03-20T00:54:57.304671Z","iopub.execute_input":"2025-03-20T00:54:57.304963Z","iopub.status.idle":"2025-03-20T00:55:08.930724Z","shell.execute_reply.started":"2025-03-20T00:54:57.304937Z","shell.execute_reply":"2025-03-20T00:55:08.929997Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Loading my Pre- trained Model \nThe model used in this notebook was trained in a separate Kaggle notebook. You can check the training details here:  \n[Object Identification with CNNs (92% Accuracy)](https://www.kaggle.com/code/sheemamasood/object-identification-with-cnns-with-92-accuracy)\n","metadata":{}},{"cell_type":"code","source":"# Load your trained model\nmodel_path = '/kaggle/input/object-identification-in-images-accuracy92/keras/default/1/my_cnn_model.keras'\nmodel = load_model(model_path)","metadata":{"execution":{"iopub.status.busy":"2025-03-20T00:55:08.931785Z","iopub.execute_input":"2025-03-20T00:55:08.932251Z","iopub.status.idle":"2025-03-20T00:55:10.699789Z","shell.execute_reply.started":"2025-03-20T00:55:08.932223Z","shell.execute_reply":"2025-03-20T00:55:10.698793Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2025-03-20T00:55:10.702375Z","iopub.execute_input":"2025-03-20T00:55:10.703138Z","iopub.status.idle":"2025-03-20T00:55:10.743967Z","shell.execute_reply.started":"2025-03-20T00:55:10.703094Z","shell.execute_reply":"2025-03-20T00:55:10.743027Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Path to the .7z file\narchive_path = '/kaggle/input/cifar-10/test.7z'\nextract_path = '/kaggle/working/test_images/'  # Directory where you want to extract files\n\n# Extract the .7z file\nwith py7zr.SevenZipFile(archive_path, mode='r') as archive:\n    archive.extractall(path=extract_path)\n\n# Verify extraction\nprint(f\"Files extracted to: {extract_path}\")\n\n","metadata":{"execution":{"iopub.status.busy":"2025-03-20T00:55:10.745350Z","iopub.execute_input":"2025-03-20T00:55:10.746118Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_submission_path = '/kaggle/input/cifar-10/sampleSubmission.csv'\nsample_submission = pd.read_csv('/kaggle/input/cifar-10/sampleSubmission.csv')","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Verifying the extraction","metadata":{}},{"cell_type":"code","source":"#import os\n\n# List files and directories in the extract path\n#for root, dirs, files in os.walk(extract_path):\n    #print(f\"Root: {root}\")\n    #print(f\"Directories: {dirs}\")\n    #print(f\"Files: {files}\")\n","metadata":{"_kg_hide-output":true,"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## setting up image generator for test data","metadata":{}},{"cell_type":"code","source":"# Set up image data generator for test data\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\n# Load the test images from the extracted directory\ntest_generator = test_datagen.flow_from_directory(\n    directory=extract_path,\n    target_size=(32, 32),  # Ensure this matches the input size of your model\n    batch_size=1,\n    class_mode=None,\n    shuffle=False,\n    seed=42\n)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Make Predictions on test images given for competition on kaggle","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test_generator, steps=len(test_generator), verbose=1)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Get the predicted class labels\nclass_labels = np.argmax(predictions, axis=1)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Map class indices to class names\nclass_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']\npredicted_labels = [class_names[label] for label in class_labels]","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Prepare submission DataFrame\nsubmission_df = pd.DataFrame({\n    'id': sample_submission[\"id\"],  # This is temporary and will be replaced with sequential IDs\n    'label': predicted_labels\n})\n\n# Replace the 'id' with a sequential number starting from 1\nsubmission_df['id'] = range(1, len(submission_df) + 1)\n\n# Save submission file without a header\nsubmission_df.to_csv('submission.csv', index=False)\n\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Check the file exists and display the first few rows\nimport pandas as pd\n\n# Load the submission file\nsubmission_df = pd.read_csv('submission.csv')\n\n# Display the first few rows\nsubmission_df.head()\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission_df.to_csv('/kaggle/working/submission.csv', index=False)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{},"outputs":[],"execution_count":null}]}