{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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\nimport os\nfrom tensorflow.keras import layers, models\nfrom sklearn.model_selection import train_test_split\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport tensorflow as tf  # Import TensorFlow\nfrom tensorflow.keras import layers, models\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\n\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":"2023-09-19T15:33:30.731413Z","iopub.execute_input":"2023-09-19T15:33:30.732045Z","iopub.status.idle":"2023-09-19T15:33:42.091703Z","shell.execute_reply.started":"2023-09-19T15:33:30.732006Z","shell.execute_reply":"2023-09-19T15:33:42.090475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-09-19T15:33:47.717786Z","iopub.execute_input":"2023-09-19T15:33:47.718538Z","iopub.status.idle":"2023-09-19T15:34:07.016610Z","shell.execute_reply.started":"2023-09-19T15:33:47.718497Z","shell.execute_reply":"2023-09-19T15:34:07.015442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport py7zr\n\n# Define the paths to the 7z files and the extraction directory\ntrain_7z_file = '/kaggle/input/cifar-10/train.7z'\ntest_7z_file = '/kaggle/input/cifar-10/test.7z'\nextraction_dir = '/kaggle/working/cifar-10'\n\n\n# Extract the train dataset\nwith py7zr.SevenZipFile(train_7z_file, mode='r') as z:\n    z.extractall(extraction_dir)\n\n# Extract the test dataset\nwith py7zr.SevenZipFile(test_7z_file, mode='r') as z:\n    z.extractall(extraction_dir)\n\n# Check the extracted files\nextracted_files = os.listdir(extraction_dir)\nprint(\"Extracted files:\", extracted_files)","metadata":{"execution":{"iopub.status.busy":"2023-09-19T15:34:11.397791Z","iopub.execute_input":"2023-09-19T15:34:11.398246Z","iopub.status.idle":"2023-09-19T15:58:27.868967Z","shell.execute_reply.started":"2023-09-19T15:34:11.398205Z","shell.execute_reply":"2023-09-19T15:58:27.867662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load and Preprocess the data\n\n\n\n# Define the function to load and preprocess images\ndef load_and_preprocess_images(image_paths, labels=None):\n    images = []\n    for image_path in image_paths:\n        image = tf.io.read_file(image_path)\n        image = tf.image.decode_jpeg(image, channels=3)  # Change to 3 channels for RGB images\n        image = tf.image.resize(image, (32, 32))  # Resize images to 32x32 pixels\n        image = tf.image.convert_image_dtype(image, tf.float32)  # Convert to float32\n        images.append(image)\n    images = tf.stack(images)\n    \n    if labels is not None:\n        labels = tf.keras.utils.to_categorical(labels, num_classes=10)  # Convert labels to one-hot encoded vectors\n        return images, labels\n    else:\n        return images\n\n\n\n\n\n# Load the train labels\ntrain_labels = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\n\n# Create a label mapping dictionary\nlabel_mapping = {label: index for index, label in enumerate(train_labels['label'].unique())}\n\n# Map the string labels to numerical labels\ntrain_labels['label'] = train_labels['label'].map(label_mapping)\n\n# Define paths for train and test images\ntrain_image_paths = ['/kaggle/working/cifar-10/train/' + str(img_name) + '.png' for img_name in train_labels['id']]\ntest_image_paths = ['/kaggle/working/cifar-10/test/' + str(img_name) for img_name in os.listdir('/kaggle/working/cifar-10/test/')]\n\n# Load and preprocess train and test images\nX_train, y_train = load_and_preprocess_images(train_image_paths, labels=train_labels['label'].values)\nX_test = load_and_preprocess_images(test_image_paths)","metadata":{"execution":{"iopub.status.busy":"2023-09-19T16:03:14.425104Z","iopub.execute_input":"2023-09-19T16:03:14.425597Z","iopub.status.idle":"2023-09-19T16:09:25.512667Z","shell.execute_reply.started":"2023-09-19T16:03:14.425559Z","shell.execute_reply":"2023-09-19T16:09:25.511416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Split the training data into training and validation sets\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\n\n# Convert X_train and y_train to NumPy arrays\nX_train = np.array(X_train)\ny_train = np.array(y_train)\n\n# Split the training data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T16:10:06.931972Z","iopub.execute_input":"2023-09-19T16:10:06.932468Z","iopub.status.idle":"2023-09-19T16:10:07.585298Z","shell.execute_reply.started":"2023-09-19T16:10:06.932429Z","shell.execute_reply":"2023-09-19T16:10:07.584260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Build the ConvNet model\n\nmodel = models.Sequential([\n    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.Flatten(),\n    layers.Dense(64, activation='relu'),\n    layers.Dense(10, activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer='adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Display model summary\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T16:10:46.195224Z","iopub.execute_input":"2023-09-19T16:10:46.195667Z","iopub.status.idle":"2023-09-19T16:10:46.458747Z","shell.execute_reply.started":"2023-09-19T16:10:46.195633Z","shell.execute_reply":"2023-09-19T16:10:46.456659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nplot_model(model)","metadata":{"execution":{"iopub.status.busy":"2023-09-19T16:10:51.937645Z","iopub.execute_input":"2023-09-19T16:10:51.938106Z","iopub.status.idle":"2023-09-19T16:10:52.260139Z","shell.execute_reply.started":"2023-09-19T16:10:51.938067Z","shell.execute_reply":"2023-09-19T16:10:52.259297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Train the model on two GPUs:\n\n# Define a strategy for multi-GPU training\nstrategy = tf.distribute.MirroredStrategy(devices=[\"/gpu:0\", \"/gpu:1\"])\n\n# Create a mirrored model inside the strategy\nwith strategy.scope():\n    model = models.Sequential([\n        layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),\n        layers.MaxPooling2D((2, 2)),\n        layers.Conv2D(64, (3, 3), activation='relu'),\n        layers.MaxPooling2D((2, 2)),\n        layers.Conv2D(64, (3, 3), activation='relu'),\n        layers.Flatten(),\n        layers.Dense(64, activation='relu'),\n        layers.Dense(10, activation='softmax')\n    ])\n\n    # Compile the model\n    model.compile(optimizer='adam',\n                  loss='categorical_crossentropy',\n                  metrics=['accuracy'])\n\n# Define data augmentation\ndatagen = tf.keras.preprocessing.image.ImageDataGenerator(\n    rotation_range=40,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True,\n    fill_mode='nearest'\n)\n\n# Fit the model with data augmentation\ndatagen.fit(X_train)\n\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=64),\n                    steps_per_epoch=len(X_train) // 64, epochs=10,\n                    validation_data=(X_val, y_val))\n","metadata":{"execution":{"iopub.status.busy":"2023-09-19T16:10:55.001884Z","iopub.execute_input":"2023-09-19T16:10:55.003096Z","iopub.status.idle":"2023-09-19T16:22:59.231046Z","shell.execute_reply.started":"2023-09-19T16:10:55.003056Z","shell.execute_reply":"2023-09-19T16:22:59.229792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# After training, save the model architecture to a JSON file\nmodel_json = model.to_json()\nwith open(\"cifar10_model.json\", \"w\") as json_file:\n    json_file.write(model_json)\n\n# Save the model weights to an HDF5 file\nmodel.save_weights(\"cifar10_model_weights.h5\")\n\nprint(\"Model saved to disk.\")","metadata":{"execution":{"iopub.status.busy":"2023-09-19T16:26:45.802829Z","iopub.execute_input":"2023-09-19T16:26:45.804049Z","iopub.status.idle":"2023-09-19T16:26:45.836988Z","shell.execute_reply.started":"2023-09-19T16:26:45.804009Z","shell.execute_reply":"2023-09-19T16:26:45.835576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot training history\n\n\n# Plot training & validation accuracy values\nplt.figure(figsize=(12, 4))\nplt.subplot(1, 2, 1)\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')\n\n# Plot training & validation loss values\nplt.subplot(1, 2, 2)\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')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-09-19T16:27:01.393446Z","iopub.execute_input":"2023-09-19T16:27:01.393837Z","iopub.status.idle":"2023-09-19T16:27:02.097067Z","shell.execute_reply.started":"2023-09-19T16:27:01.393805Z","shell.execute_reply":"2023-09-19T16:27:02.095867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use the loaded model to make predictions on the test data\npredictions = model.predict(X_test)\n\n# Convert the predictions to class labels (assuming one-hot encoding was used)\npredicted_labels = np.argmax(predictions, axis=1)\n\n# Create a DataFrame for the submission\nsubmission_df = pd.DataFrame({\n    'id': [i for i in range(1, len(predicted_labels) + 1)],  # Assuming 1-based indexing for IDs\n    'label': predicted_labels  # Predicted class labels\n})\n\n# Save the DataFrame as a CSV file\nsubmission_df.to_csv('submission.csv', index=False)\n\n# Display the first few rows of the submission DataFrame\nprint(submission_df.head())\n\n# Check the generated CSV file\nprint(\"Generated submission CSV file:\")\nprint(submission_df.shape)","metadata":{"execution":{"iopub.status.busy":"2023-09-19T16:50:16.209321Z","iopub.execute_input":"2023-09-19T16:50:16.210008Z","iopub.status.idle":"2023-09-19T16:51:56.214756Z","shell.execute_reply.started":"2023-09-19T16:50:16.209932Z","shell.execute_reply":"2023-09-19T16:51:56.213487Z"},"trusted":true},"execution_count":null,"outputs":[]}]}