{"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":30733,"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-07-08T18:03:56.821337Z","iopub.execute_input":"2024-07-08T18:03:56.821625Z","iopub.status.idle":"2024-07-08T18:03:57.779588Z","shell.execute_reply.started":"2024-07-08T18:03:56.821599Z","shell.execute_reply":"2024-07-08T18:03:57.778338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Install py7zr Package\n\n#####  py7zr is a library and utility to support 7zip archive compression, decompression, encryption and decryption written by Python programming language.","metadata":{}},{"cell_type":"code","source":"pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-07-08T18:04:47.735296Z","iopub.execute_input":"2024-07-08T18:04:47.736162Z","iopub.status.idle":"2024-07-08T18:05:03.247016Z","shell.execute_reply.started":"2024-07-08T18:04:47.736132Z","shell.execute_reply":"2024-07-08T18:05:03.245825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extract Training Dataset from 7zr archive","metadata":{}},{"cell_type":"code","source":"import py7zr\n\n# Define paths for extract and archive\narchive_path = '../input/cifar-10/train.7z'\nextract_folder = '/kaggle/temp'\n\n# Extract the 7zr archive\nwith py7zr.SevenZipFile(archive_path, mode='r') as archive:\n    archive.extractall(path=extract_folder)\n\n# Load the labels CSV file\ntrain_labels = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\n\n# Define paths for train and validation datsets\ndataset_path_train = '/kaggle/temp/train'\ndataset_path_valid = '/kaggle/temp/valid'\n\n# Define the class names\nclasses = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']\n\n# Create directories for each class in train and validation folders\nfor label in classes:\n    train_path = os.path.join(dataset_path_train, label)\n    os.makedirs(train_path, exist_ok=True)\n    \n    valid_path = os.path.join(dataset_path_valid, label)\n    os.makedirs(valid_path, exist_ok=True)\n\n# Iterate over each label in the CSV    \nfor (int_ind, row) in train_labels.iterrows():\n    # Construct the image file name\n    id = str(row[\"id\"]) + \".png\"\n    source_path = os.path.join(dataset_path_train, id)\n    \n    # Check if the image file exists\n    if not os.path.exists(source_path):\n        print(f\"Missing source File: {source_path}\")\n        continue\n        \n    # Randomly assign the image to train (80%) or validation (20%) set\n    p = np.random.random()\n    if p <= 0.8:\n        target_path = os.path.join(dataset_path_train, row[\"label\"], id)\n    else:\n        target_path = os.path.join(dataset_path_valid, row[\"label\"], id)\n    \n    # Move the image file to the corresponding target directory\n    os.replace(source_path, target_path)\n    \nprint(\"Processing Completed\")","metadata":{"execution":{"iopub.status.busy":"2024-07-08T18:05:05.675908Z","iopub.execute_input":"2024-07-08T18:05:05.676603Z","iopub.status.idle":"2024-07-08T18:05:37.049977Z","shell.execute_reply.started":"2024-07-08T18:05:05.676562Z","shell.execute_reply":"2024-07-08T18:05:37.048973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Extract Test Dataset from 7zr archive","metadata":{}},{"cell_type":"code","source":"import py7zr\narchive_path = '/kaggle/input/cifar-10/test.7z'\nextract_folder = '/kaggle/temp/test'\n\nwith py7zr.SevenZipFile(archive_path, mode='r') as archive:\n    archive.extractall(path=extract_folder)","metadata":{"execution":{"iopub.status.busy":"2024-07-08T18:05:39.024913Z","iopub.execute_input":"2024-07-08T18:05:39.025576Z","iopub.status.idle":"2024-07-08T18:08:11.528965Z","shell.execute_reply.started":"2024-07-08T18:05:39.025540Z","shell.execute_reply":"2024-07-08T18:08:11.528113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('/kaggle/temp/test/test'))\npath = '/kaggle/temp/test/test'\nf = os.listdir(path)\nnew = [str(path) + '/'+s for s in f]","metadata":{"execution":{"iopub.status.busy":"2024-07-08T18:08:11.530731Z","iopub.execute_input":"2024-07-08T18:08:11.531168Z","iopub.status.idle":"2024-07-08T18:08:11.960576Z","shell.execute_reply.started":"2024-07-08T18:08:11.531134Z","shell.execute_reply":"2024-07-08T18:08:11.959821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.applications.inception_v3 import InceptionV3\n# Define paths\ndata_dir = '/kaggle/temp'\ntrain_dir = os.path.join(data_dir, 'train')\nvalid_dir = os.path.join(data_dir, 'valid')\n\n# Define image data generators\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    horizontal_flip=True,\n    zoom_range=0.2,\n    shear_range=0.2,\n    rotation_range=20\n)\n\nvalid_datagen = ImageDataGenerator(rescale=1./255)\n\n# Create data generators\nbatch_size = 32\ntrain_generator = train_datagen.flow_from_directory(\n    train_dir,\n    target_size=(224, 224),\n    batch_size=batch_size,\n    class_mode='categorical'\n)\n\nvalid_generator = valid_datagen.flow_from_directory(\n    valid_dir,\n    target_size=(224, 224),\n    batch_size=batch_size,\n    class_mode='categorical'\n)\n\n\n\n# Load the pre-trained InceptionV3 model and exclude the top (classification) layer\nbase_model = InceptionV3(weights='imagenet', include_top=False)\n\n\n# Create a new model on top\nmodel = Sequential([\n    base_model,\n    GlobalAveragePooling2D(),\n    Dense(len(train_generator.class_indices), activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer=Adam(learning_rate=0.001),\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Train the model\nepochs = 5\nhistory = model.fit(\n    train_generator,\n    epochs=epochs,\n    validation_data=valid_generator\n)\n\n# Save the model\nmodel.save('model.h5')\n\n# Example to show training results\nimport matplotlib.pyplot as plt\n\n# Plot training & validation accuracy values\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('Model accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()\n\n# Plot training & validation loss values\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-07-08T18:22:14.523615Z","iopub.execute_input":"2024-07-08T18:22:14.524286Z","iopub.status.idle":"2024-07-08T18:59:01.559345Z","shell.execute_reply.started":"2024-07-08T18:22:14.524251Z","shell.execute_reply":"2024-07-08T18:59:01.558325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.models import load_model\n\n# Define the class names\nclasses = {0: 'airplane', 1: 'automobile', 2: 'bird', 3: 'cat', 4: 'deer', 5: 'dog', 6: 'frog', 7: 'horse', 8: 'ship', 9: 'truck'}\n\n# Define path to test data\ntest_dir = os.path.join('/kaggle/temp', 'test')\n\n# Define image data generator for test data\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\n# Create the test data generator\nbatch_size = 32\ntest_generator = test_datagen.flow_from_directory(\n    test_dir,\n    target_size=(224, 224),\n    batch_size=batch_size,\n    class_mode=None,\n    shuffle=False\n)\n\n# Load the pretrained model\nmodel = load_model('model.h5')\n\n# Make predictions on test set\ntest_generator.reset()\npredictions = model.predict(test_generator, verbose=1)\npredicted_class_indices = np.argmax(predictions, axis=1)\n\n# Check the size of predicted_class_indices\nprint(\"Number of predictions:\", len(predicted_class_indices))  # Debug print\n\n# Map indices to class labels\npredicted_classes = [classes[idx] for idx in predicted_class_indices if idx in classes]\n\n# For debugging purposes, let's print the first 10 predicted classes\nprint(predicted_classes[:10])\n\n# If you need a fixed-size array (e.g., for 300,000 entries), you can do:\npredicted_classes_array = np.empty(shape=300000, dtype=np.dtype('U20'))\npredicted_classes_array[:len(predicted_classes)] = predicted_classes\n\n# Print the first 10 entries of the array for verification\nprint(predicted_classes_array[:10])\n","metadata":{"execution":{"iopub.status.busy":"2024-07-08T19:27:20.722848Z","iopub.execute_input":"2024-07-08T19:27:20.723866Z","iopub.status.idle":"2024-07-08T19:33:40.158066Z","shell.execute_reply.started":"2024-07-08T19:27:20.723832Z","shell.execute_reply":"2024-07-08T19:33:40.157038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\n\n# Define path to test data\ndirectory = '/kaggle/temp/test/test'\n\n# Get the list of image filenames\nimage_files = os.listdir(directory)\n\n# Extract the numeric part of filenames to get the Image IDs\nImageId = [int(''.join(filter(str.isdigit, name))) for name in image_files]\n\n# Create the submission DataFrame\nsubmission = pd.DataFrame({\n    \"id\": ImageId,\n    \"label\": predicted_classes_array\n})\n\n# Ensure the 'id' column is of integer type\nsubmission['id'] = submission['id'].astype(int)\n\n# Save the submission DataFrame to a CSV file\nsubmission.to_csv('submission.csv', index=False)\nprint(\"Submission file created successfully.\")\n","metadata":{"execution":{"iopub.status.busy":"2024-07-06T20:55:55.989462Z","iopub.execute_input":"2024-07-06T20:55:55.990188Z","iopub.status.idle":"2024-07-06T20:55:57.035009Z","shell.execute_reply.started":"2024-07-06T20:55:55.990157Z","shell.execute_reply":"2024-07-06T20:55:57.034029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-06T20:55:59.637871Z","iopub.execute_input":"2024-07-06T20:55:59.638719Z","iopub.status.idle":"2024-07-06T20:55:59.651911Z","shell.execute_reply.started":"2024-07-06T20:55:59.638685Z","shell.execute_reply":"2024-07-06T20:55:59.650980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}