{"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-06-29T10:17:54.506533Z","iopub.execute_input":"2024-06-29T10:17:54.506856Z","iopub.status.idle":"2024-06-29T10:17:55.502628Z","shell.execute_reply.started":"2024-06-29T10:17:54.506830Z","shell.execute_reply":"2024-06-29T10:17:55.501548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-06-29T10:17:55.504567Z","iopub.execute_input":"2024-06-29T10:17:55.505093Z","iopub.status.idle":"2024-06-29T10:18:11.507939Z","shell.execute_reply.started":"2024-06-29T10:17:55.505057Z","shell.execute_reply":"2024-06-29T10:18:11.507015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import py7zr\nfrom PIL import Image\nimport os\n\n# Ensure the file paths are correct and point to valid 7z files\ntrain_seven_zip_file_path = '/kaggle/input/cifar-10/train.7z'\ntest_seven_zip_file_path = '/kaggle/input/cifar-10/test.7z'\n\n# List to store the images for train and test sets\ntrain_images = []\ntest_images = []\n\n# List to store the Image IDs\ntest_image_ids = []\n\n# Helper function to recursively find image files\ndef find_images(directory):\n    image_files = []\n    for root, _, files in os.walk(directory):\n        for file in files:\n            if file.endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):\n                image_files.append(os.path.join(root, file))\n    return image_files\n\n# Extract the 7z files and read the images for train set\ntry:\n    with py7zr.SevenZipFile(train_seven_zip_file_path, mode='r') as z:\n        z.extractall(path='train_extracted_files')\n    \n    # Find all image files in the extracted directory\n    train_image_files = find_images('train_extracted_files')\n    \n    # Iterate through the image files and load them into the list\n    for image_file_path in train_image_files:\n        img = Image.open(image_file_path)\n        train_images.append(img)\n    \n    # Check the number of images loaded for train set\n    print(f\"Loaded {len(train_images)} images for train set.\")\n\nexcept Exception as e:\n    print(f\"An error occurred during train set processing: {e}\")\n\n# Extract the 7z files and read the images for test set\ntry:\n    with py7zr.SevenZipFile(test_seven_zip_file_path, mode='r') as z:\n        z.extractall(path='test_extracted_files')\n    \n    # Find all image files in the extracted directory\n    test_image_files = find_images('test_extracted_files')\n    \n    # Iterate through the image files and load them into the list\n    for image_file_path in test_image_files:\n        img = Image.open(image_file_path)\n        test_images.append(img)\n        \n        # Extract Image ID from the filename and append to the list\n        image_id = int(''.join(filter(str.isdigit, os.path.basename(image_file_path))))\n        test_image_ids.append(image_id)\n    \n    # Check the number of images loaded for test set\n    print(f\"Loaded {len(test_images)} images for test set.\")\n\nexcept Exception as e:\n    print(f\"An error occurred during test set processing: {e}\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\n# Initialize empty lists to store the image data arrays for train and test sets\ntrain_image_arrays = []\ntest_image_arrays = []\n\n# Iterate through the list of images for train set\nfor img in train_images:\n    # Convert the PIL Image object to a NumPy array\n    img_array = np.array(img)\n    \n    # Append the NumPy array to the list\n    train_image_arrays.append(img_array)\n\n# Iterate through the list of images for test set\nfor img in test_images:\n    # Convert the PIL Image object to a NumPy array\n    img_array = np.array(img)\n    \n    # Append the NumPy array to the list\n    test_image_arrays.append(img_array)\n\n# Convert lists to numpy arrays\ntrain_image_arrays = np.array(train_image_arrays)\ntest_image_arrays = np.array(test_image_arrays)\n\n# Print the shape of the resulting 3D arrays\nprint(\"Shape of train images array:\", train_image_arrays.shape)\nprint(\"Shape of test images array:\", test_image_arrays.shape)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\n# Define a mapping dictionary for labels\nlabel_mapping = {\n    'airplane': 0,\n    'automobile': 1,\n    'bird': 2,\n    'cat': 3,\n    'deer': 4,\n    'dog': 5,\n    'frog': 6,\n    'horse': 7,\n    'ship': 8,\n    'truck': 9\n}\n\n# Replace labels in 'label' column with numerical values using the mapping dictionary\ny_train['label'] = y_train['label'].map(label_mapping)\n\n# Drop the 'id' column while preserving the rest of the DataFrame\ny_train_numerical = y_train.drop(columns=['id'])\n\n# Let's print the updated y_train without the 'id' column\nprint(y_train_numerical)\n\ny_train_array = y_train_numerical.values","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX = train_image_arrays\ny = y_train_array\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\n# Load the CIFAR-10 dataset\ncifar10 = tf.keras.datasets.cifar10\n\n# Load the data into train and test sets\n(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n\n# Normalize the pixel values to the range [0, 1]\nx_train, x_test = x_train / 255.0, x_test / 255.0\n\n# Print the shape of the training and test sets\nprint(\"Shape of x_train:\", x_train.shape)\nprint(\"Shape of y_train:\", y_train.shape)\nprint(\"Shape of x_test:\", x_test.shape)\nprint(\"Shape of y_test:\", y_test.shape)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\nnum_classes =10\ny_train = to_categorical(y_train, num_classes)\ny_test = to_categorical(y_test, num_classes)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, BatchNormalization, Dropout\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.regularizers import l2\n\nmodel = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),\n    MaxPooling2D((2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D((2, 2)),\n    Dropout(0.5),\n    Conv2D(64, (3, 3), activation='relu'),\n    Flatten(),\n    Dense(64, activation='relu'),\n    Dense(10, activation='softmax')\n])\n    \n# Compile the model\nopt = Adam()\nmodel.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Print the model summary\nmodel.summary()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(x_train, y_train, epochs=50, validation_data=(x_test, y_test))\n\n# Evaluate the model\nloss, accuracy = model.evaluate(x_test, y_test)\nprint(\"Test Loss:\", loss)\nprint(\"Test Accuracy:\", accuracy)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model\nloss, accuracy = model.evaluate(x_test, y_test)\nprint(\"Validation Accuracy: {:.2f}%\".format(accuracy * 100))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\n\n# Dictionary mapping numerical labels to class names\nclass_names = {\n    0: 'airplane',\n    1: 'automobile',\n    2: 'bird',\n    3: 'cat',\n    4: 'deer',\n    5: 'dog',\n    6: 'frog',\n    7: 'horse',\n    8: 'ship',\n    9: 'truck'\n}\n\n# Convert test images to arrays\ntest_image_arrays = [np.array(img) for img in test_images]\n\n# Stack the test images into a 4D NumPy array\ntest_images_array = np.stack(test_image_arrays)\n\n# Predict labels for the test set\npredictions = model.predict(test_images_array)\n\n# Convert numerical labels to class names\npredicted_class_names = [class_names[np.argmax(label)] for label in predictions]\n\n# Create a DataFrame with 'id' and 'label' (class names)\nsubmission = pd.DataFrame({\n    'id': test_image_ids,\n    'label': predicted_class_names\n})\n\n# Check if the DataFrame is empty\nif submission.empty:\n    print(\"DataFrame is empty. Check your predictions.\")\nelse:\n    # Save the DataFrame to a CSV file\n    submission.to_csv('/kaggle/working/submission.csv', index=False)\n    print(\"Predictions saved to submission.csv.\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}