{"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":"none","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Project Implementation Guide\n\n### Step 1: Set Up Your Kaggle Notebook\n- Create a new notebook on Kaggle.\n- Ensure the installation of necessary libraries: TensorFlow/Keras, NumPy, Matplotlib, etc.\n\n### Step 2: Download the CIFAR-10 Dataset\n- Access the dataset through the Kaggle competitions page. Ensure competition rules acceptance.\n- Download the dataset using the Kaggle API:\n  ```python\n  !kaggle competitions download -c cifar-10\n  ```\n\n### Step 3: Load and Preprocess the Data\n- Unzip the downloaded data and load it using appropriate libraries.\n- Normalize image data and convert labels to one-hot encoded vectors if needed.\n\n### Step 4: Build and Train the CNN Model\n- Design your CNN in Keras. Start with simple layers and increment complexity as required.\n- Compile the model with an optimizer, loss function, and metrics such as accuracy.\n- Train the model with training and validation data:\n  ```python\n  model.fit(x_train, y_train, epochs=10, validation_data=(x_val, y_val))\n  ```\n\n### Step 5: Evaluate Model Performance\n- Use the test set to evaluate the model and calculate final performance metrics.\n- Implement model checkpointing and early stopping to save the best model during training.\n\n### Step 6: Model Selection for Inference\n- Select the best-performing model for inference.\n- Predict the classes of 10 selected images, using:\n  ```python\n  predictions = model.predict(x_test)\n  ```\n\n### Step 7: Create a Confusion Matrix\n- Construct a confusion matrix with the predictions to visualize model performance:\n  ```python\n  from sklearn.metrics import confusion_matrix\n  import seaborn as sns\n\n  cm = confusion_matrix(true_labels, predictions)\n  sns.heatmap(cm, annot=True, fmt='d')\n  ```\n\n### Step 8: Document Your Findings\n- Detail methodology, code, and findings within the notebook.\n- Include graphs for training/validation loss and accuracy, and the confusion matrix.\n\n### Step 9: Finalize and Share Your Notebook\n- Ensure your notebook is clear, well-documented, and reproducible.\n- Share your work in the Kaggle community for feedback.\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"markdown","source":"### Step 1: Set Up Your Kaggle Notebook\n- Create a new notebook on Kaggle.\n- Ensure the installation of necessary libraries: TensorFlow/Keras, NumPy, Matplotlib, etc.","metadata":{}},{"cell_type":"code","source":"!pip install tensorflow numpy matplotlib","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:06:30.148108Z","iopub.execute_input":"2024-04-26T00:06:30.148502Z","iopub.status.idle":"2024-04-26T00:06:44.959175Z","shell.execute_reply.started":"2024-04-26T00:06:30.14847Z","shell.execute_reply":"2024-04-26T00:06:44.957682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom keras.datasets import cifar10\nfrom keras.utils import to_categorical\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom keras.optimizers import Adam\nfrom keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom sklearn.metrics import accuracy_score, classification_report, confusion_matrix\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:06:52.341658Z","iopub.execute_input":"2024-04-26T00:06:52.342181Z","iopub.status.idle":"2024-04-26T00:06:52.352241Z","shell.execute_reply.started":"2024-04-26T00:06:52.342124Z","shell.execute_reply":"2024-04-26T00:06:52.350958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\n\n(x_train, y_train), (x_test, y_test) = cifar10.load_data()# Load the CIFAR-10 data\n\nx_train = x_train.astype('float32') / 255.0 # Normalize the image data\nx_test = x_test.astype('float32') / 255.0\n\ny_train = to_categorical(y_train, 10) # Convert class vectors to binary class matrices (one-hot encoding)\ny_test = to_categorical(y_test, 10)","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:07:28.407608Z","iopub.execute_input":"2024-04-26T00:07:28.408315Z","iopub.status.idle":"2024-04-26T00:07:29.671663Z","shell.execute_reply.started":"2024-04-26T00:07:28.408282Z","shell.execute_reply":"2024-04-26T00:07:29.670323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout\n\n# Build the CNN model\nmodel = Sequential([\n    Conv2D(32, (3, 3), padding='same', input_shape=x_train.shape[1:], activation='relu'),\n    Conv2D(32, (3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n\n    Conv2D(64, (3, 3), padding='same', activation='relu'),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n\n    Flatten(),\n    Dense(512, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')])\n\n# Compile the model\nmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit(x_train, y_train, batch_size=32, epochs=20, validation_data=(x_test, y_test), verbose=1)","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:07:39.349544Z","iopub.execute_input":"2024-04-26T00:07:39.349951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model on the test data\nscores = model.evaluate(x_test, y_test, verbose=0)\nprint('Test loss:', scores[0])\nprint('Test accuracy:', scores[1])","metadata":{"execution":{"iopub.status.busy":"2024-04-23T20:38:53.745504Z","iopub.execute_input":"2024-04-23T20:38:53.745856Z","iopub.status.idle":"2024-04-23T20:38:59.721435Z","shell.execute_reply.started":"2024-04-23T20:38:53.745826Z","shell.execute_reply":"2024-04-23T20:38:59.72025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict classes with the model\npredicted_classes = model.predict(x_test)\npredicted_classes = np.argmax(predicted_classes, axis=1)\ntrue_classes = np.argmax(y_test, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-04-23T20:39:04.401007Z","iopub.execute_input":"2024-04-23T20:39:04.401417Z","iopub.status.idle":"2024-04-23T20:39:10.729652Z","shell.execute_reply.started":"2024-04-23T20:39:04.401384Z","shell.execute_reply":"2024-04-23T20:39:10.728485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Change number of convolutional layers","metadata":{}},{"cell_type":"code","source":"from keras.layers import Input, Conv2D, MaxPooling2D, Flatten, Dense, Dropout\nfrom keras.models import Model\nfrom keras.optimizers import Adam\n\ndef build_model(n_layers):\n    input_layer = Input(shape=x_train.shape[1:])\n    x = input_layer\n    for _ in range(n_layers):\n        x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)\n        x = Conv2D(32, (3, 3), activation='relu')(x)\n        x = MaxPooling2D(pool_size=(2, 2))(x)\n        x = Dropout(0.25)(x)\n\n    x = Flatten()(x)\n    x = Dense(512, activation='relu')(x)\n    x = Dropout(0.5)(x)\n    output_layer = Dense(10, activation='softmax')(x)\n\n    model = Model(inputs=input_layer, outputs=output_layer)\n    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n    return model\n\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-23T20:43:22.411941Z","iopub.execute_input":"2024-04-23T20:43:22.413127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Testing different numbers of convolutional layers\nlayer_options = [2, 3, 4]\nhistories = []\nfor n_layers in layer_options:\n    print(f\"Training model with {n_layers} convolutional layers...\")\n    model = build_model(n_layers)\n    history = model.fit(x_train, y_train, batch_size=32, epochs=10, validation_data=(x_test, y_test), verbose=1)\n    histories.append(history)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(10, 8))\nfor i, history in enumerate(histories):\n    plt.plot(history.history['val_accuracy'], label=f'{layer_options[i]} Layers')\nplt.title('Model Accuracy by Number of Convolutional Layers')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-23T21:01:35.160234Z","iopub.execute_input":"2024-04-23T21:01:35.160687Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Change the learning rate","metadata":{}},{"cell_type":"code","source":"def train_with_different_learning_rates(learning_rates):\n    histories_lr = []\n    for lr in learning_rates:\n        print(f\"Training model with learning rate: {lr}\")\n        model = build_model(3)  # Using 3 layers as an example\n        optimizer = Adam(learning_rate=lr)\n        model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\n        history = model.fit(x_train, y_train, batch_size=32, epochs=10, validation_data=(x_test, y_test), verbose=1)\n        histories_lr.append(history)\n    return histories_lr\n\nlearning_rates = [0.001, 0.01, 0.1]\nhistories_lr = train_with_different_learning_rates(learning_rates)\n\n# Plotting\nplt.figure(figsize=(10, 8))\nfor i, history in enumerate(histories_lr):\n    plt.plot(history.history['val_accuracy'], label=f'LR={learning_rates[i]}')\nplt.title('Model Accuracy by Learning Rate')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend()\nplt.show()","metadata":{},"execution_count":null,"outputs":[]}]}