{"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":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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-05-28T19:31:35.179604Z","iopub.execute_input":"2024-05-28T19:31:35.180047Z","iopub.status.idle":"2024-05-28T19:31:35.189563Z","shell.execute_reply.started":"2024-05-28T19:31:35.180013Z","shell.execute_reply":"2024-05-28T19:31:35.188333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\ntrain_csv_file=\"/kaggle/input/digit-recognizer/train.csv\"\ntrain_df=pd.read_csv(train_csv_file,index_col=None)\ntest_csv_file=\"/kaggle/input/digit-recognizer/test.csv\"\ntest_df=pd.read_csv(test_csv_file,index_col=None)\n\nprint(train_df.shape)\ntrain_df\n","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:31:35.210954Z","iopub.execute_input":"2024-05-28T19:31:35.211365Z","iopub.status.idle":"2024-05-28T19:31:40.694880Z","shell.execute_reply.started":"2024-05-28T19:31:35.211336Z","shell.execute_reply":"2024-05-28T19:31:40.693706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images=train_df.iloc[:,1:].to_numpy()\ntrain_labels=train_df.iloc[:,0].to_numpy()\ntest_images=test_df.to_numpy()\n\nprint(test_images.shape)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:31:40.697007Z","iopub.execute_input":"2024-05-28T19:31:40.697999Z","iopub.status.idle":"2024-05-28T19:31:40.705530Z","shell.execute_reply.started":"2024-05-28T19:31:40.697958Z","shell.execute_reply":"2024-05-28T19:31:40.704414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images=train_images.reshape(-1,1,28,28)\ntest_images=test_images.reshape(-1,1,28,28)\nprint(test_images.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:31:40.706784Z","iopub.execute_input":"2024-05-28T19:31:40.707099Z","iopub.status.idle":"2024-05-28T19:31:40.724327Z","shell.execute_reply.started":"2024-05-28T19:31:40.707073Z","shell.execute_reply":"2024-05-28T19:31:40.723002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch \n\ntrain_labels = torch.tensor(train_labels,dtype=torch.long)\ntrain_images=torch.tensor(train_images,dtype=torch.float32)/255.0\ntest_images=torch.tensor(test_images,dtype=torch.float32)/255.0\n\nprint(f'test images tensor shape: {test_images.shape}')","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:31:40.726796Z","iopub.execute_input":"2024-05-28T19:31:40.727842Z","iopub.status.idle":"2024-05-28T19:31:40.957394Z","shell.execute_reply.started":"2024-05-28T19:31:40.727807Z","shell.execute_reply":"2024-05-28T19:31:40.956600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset, DataLoader\n\nclass ImageDataset(Dataset):\n    def __init__(self,images,labels):\n        self.images=images\n        self.labels=labels\n       \n    def __len__(self):\n        return len(self.images)\n    \n    def __getitem__(self,idx):\n        image= self.images[idx]\n        label=self.labels[idx]\n        return image,label\n        \n        \nclass ImageDatasetTest(Dataset):\n    def __init__(self,images):\n        self.images=images\n        \n       \n    def __len__(self):\n        return len(self.images)\n    \n    def __getitem__(self,idx):\n        image= self.images[idx]\n        return image\n        \n        \ntrain_dataset=ImageDataset(train_images,train_labels)\ntest_dataset=ImageDatasetTest(test_images)\ntrain_dataloader= DataLoader(train_dataset,batch_size=32,shuffle=True)\ntest_dataloader=DataLoader(test_dataset,batch_size=32,shuffle=False)\nfor images,labels in train_dataloader:\n    print(images.shape)\n    print(labels.shape)\n    break","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:31:40.958610Z","iopub.execute_input":"2024-05-28T19:31:40.959536Z","iopub.status.idle":"2024-05-28T19:31:40.992753Z","shell.execute_reply.started":"2024-05-28T19:31:40.959505Z","shell.execute_reply":"2024-05-28T19:31:40.991625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef show_image(image_tensor):\n    image=image_tensor.squeeze()\n    plt.imshow(image,cmap='gray')\n    plt.show()\nfor batch_images,batch_labels in train_dataloader:\n    show_image(batch_images[0])\n    print(f'Label:{batch_labels[0]}')\n    \n    break","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:31:40.994119Z","iopub.execute_input":"2024-05-28T19:31:40.994561Z","iopub.status.idle":"2024-05-28T19:31:41.183492Z","shell.execute_reply.started":"2024-05-28T19:31:40.994532Z","shell.execute_reply":"2024-05-28T19:31:41.182340Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F\n\nclass SimpleCNN(nn.Module):\n    def __init__(self):\n        super(SimpleCNN, self).__init__()\n        # Define a convolutional layer with 1 input channel, 16 output channels, and a kernel size of 3\n        self.conv1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=3, padding=1)\n        # Define a second convolutional layer with 16 input channels and 32 output channels\n        self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1)\n        # Define a max pooling layer with a kernel size of 2x2\n        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)\n        # Define a fully connected layer with an input size of 32*7*7 and output size of 128\n        self.fc1 = nn.Linear(in_features=32*7*7, out_features=128)\n        # Define a second fully connected layer with an output size of 10 (assuming 10 classes)\n        self.fc2 = nn.Linear(in_features=128, out_features=10)\n\n    def forward(self, x):\n        # Apply the first convolutional layer followed by a ReLU activation function and max pooling\n        x = self.pool(F.relu(self.conv1(x)))\n        # Apply the second convolutional layer followed by a ReLU activation function and max pooling\n        x = self.pool(F.relu(self.conv2(x)))\n        # Flatten the tensor before the fully connected layers\n        x = x.view(-1, 32*7*7)\n        # Apply the first fully connected layer followed by a ReLU activation function\n        x = F.relu(self.fc1(x))\n        # Apply the second fully connected layer (output layer)\n        x = self.fc2(x)\n        return x\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:31:41.185048Z","iopub.execute_input":"2024-05-28T19:31:41.185677Z","iopub.status.idle":"2024-05-28T19:31:41.196855Z","shell.execute_reply.started":"2024-05-28T19:31:41.185644Z","shell.execute_reply":"2024-05-28T19:31:41.195692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\ndef plot_metrics(train_losses, train_accuracies, num_epochs):\n    plt.figure(figsize=(12, 5))\n\n    plt.subplot(1, 2, 1)\n    plt.plot(range(1, num_epochs + 1), train_losses, label='Training Loss')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.title('Loss Curve')\n    plt.legend()\n\n    plt.subplot(1, 2, 2)\n    plt.plot(range(1, num_epochs + 1), train_accuracies, label='Training Accuracy')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.title('Accuracy Curve')\n    plt.legend()\n\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:31:41.198751Z","iopub.execute_input":"2024-05-28T19:31:41.199431Z","iopub.status.idle":"2024-05-28T19:31:41.212326Z","shell.execute_reply.started":"2024-05-28T19:31:41.199401Z","shell.execute_reply":"2024-05-28T19:31:41.211202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\n\n# Instantiate the model\nmodel = SimpleCNN()\n\n# Check if GPU is available and move the model to GPU if available\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nmodel.to(device)\n\n# Define a loss function and optimizer\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# Function to calculate accuracy\ndef calculate_accuracy(outputs, labels):\n    _, predicted = torch.max(outputs, 1)\n    total = labels.size(0)\n    correct = (predicted == labels).sum().item()\n    return correct / total\n\n# Training loop with accuracy tracking\nnum_epochs = 5\ntrain_losses = []\ntrain_accuracies = []\n\nfor epoch in range(num_epochs):\n    epoch_loss = 0\n    epoch_accuracy = 0\n    for images, labels in train_dataloader:\n        # Move data to the GPU if available\n        images, labels = images.to(device), labels.to(device)\n        \n        # Zero the parameter gradients\n        optimizer.zero_grad()\n        \n        # Forward pass\n        outputs = model(images)\n        \n        # Compute the loss\n        loss = criterion(outputs, labels)\n        \n        # Backward pass and optimize\n        loss.backward()\n        optimizer.step()\n        \n        # Accumulate loss and accuracy\n        epoch_loss += loss.item()\n        epoch_accuracy += calculate_accuracy(outputs, labels)\n    \n    # Average loss and accuracy for the epoch\n    epoch_loss /= len(train_dataloader)\n    epoch_accuracy /= len(train_dataloader)\n    \n    # Save the loss and accuracy\n    train_losses.append(epoch_loss)\n    train_accuracies.append(epoch_accuracy)\n    \n    # Print loss and accuracy for every epoch\n    print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {epoch_loss:.4f}, Accuracy: {epoch_accuracy:.4f}')\n\n# Save the trained model\ntorch.save(model.state_dict(), 'simple_cnn.pth')\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:31:41.213662Z","iopub.execute_input":"2024-05-28T19:31:41.214239Z","iopub.status.idle":"2024-05-28T19:33:02.987543Z","shell.execute_reply.started":"2024-05-28T19:31:41.214209Z","shell.execute_reply":"2024-05-28T19:33:02.986478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting the loss and accuracy curves using the function\n\nplot_metrics(train_losses, train_accuracies, num_epochs)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:33:02.990890Z","iopub.execute_input":"2024-05-28T19:33:02.991253Z","iopub.status.idle":"2024-05-28T19:33:03.503831Z","shell.execute_reply.started":"2024-05-28T19:33:02.991223Z","shell.execute_reply":"2024-05-28T19:33:03.502793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\n\ndef plot_confusion_matrix(true_labels, predicted_labels, classes):\n    # Calculate the confusion matrix\n    conf_matrix = confusion_matrix(true_labels, predicted_labels)\n\n    # Plot the confusion matrix\n    plt.figure(figsize=(10, 8))\n    plt.imshow(conf_matrix, interpolation='nearest', cmap=plt.cm.Blues)\n    plt.title('Confusion Matrix')\n    plt.colorbar()\n\n    # Set tick marks\n    tick_marks = np.arange(len(classes))\n    plt.xticks(tick_marks, classes, rotation=45)\n    plt.yticks(tick_marks, classes)\n\n    # Display the values of the confusion matrix\n    thresh = conf_matrix.max() / 2.\n    for i in range(conf_matrix.shape[0]):\n        for j in range(conf_matrix.shape[1]):\n            plt.text(j, i, format(conf_matrix[i, j], 'd'),\n                     horizontalalignment=\"center\",\n                     color=\"white\" if conf_matrix[i, j] > thresh else \"black\")\n\n    plt.tight_layout()\n    plt.ylabel('True label')\n    plt.xlabel('Predicted label')\n    plt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:33:03.504861Z","iopub.execute_input":"2024-05-28T19:33:03.505192Z","iopub.status.idle":"2024-05-28T19:33:04.285422Z","shell.execute_reply.started":"2024-05-28T19:33:03.505163Z","shell.execute_reply":"2024-05-28T19:33:04.283963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\n\n# Set the model to evaluation mode\nmodel.eval()\n\n# Make predictions on the training dataset\ntrain_predictions = []\ntrue_labels = []\n\nwith torch.no_grad():  # Disable gradient computation for inference\n    for images, labels in train_dataloader:\n        images, labels = images.to(device), labels.to(device)\n        outputs = model(images)\n        _, predicted = torch.max(outputs, 1)\n        train_predictions.extend(predicted.cpu().numpy())\n        true_labels.extend(labels.cpu().numpy())\n\n# Convert lists to numpy arrays\ntrain_predictions = np.array(train_predictions)\ntrue_labels = np.array(true_labels)\n\nunique_labels = np.unique(true_labels)  # Assuming 'true_labels' is your true label array\nclasses = [str(label) for label in unique_labels]\n\n# Usage:\nplot_confusion_matrix(true_labels, train_predictions, classes)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:33:04.288355Z","iopub.execute_input":"2024-05-28T19:33:04.288815Z","iopub.status.idle":"2024-05-28T19:33:11.655748Z","shell.execute_reply.started":"2024-05-28T19:33:04.288774Z","shell.execute_reply":"2024-05-28T19:33:11.654403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the trained model (if saved earlier)\nmodel = SimpleCNN()\nmodel.load_state_dict(torch.load('simple_cnn.pth'))\nmodel.to(device)  # Move model to the GPU if available\nmodel.eval()  # Set the model to evaluation mode\n\n# Collect all predictions\nall_predictions = []\n\nwith torch.no_grad():  # Disable gradient computation for inference\n    for batch_images in test_dataloader:\n        batch_images = batch_images.to(device)  # Move data to the GPU if available\n        outputs = model(batch_images)\n        _, predicted = torch.max(outputs, 1)\n        all_predictions.extend(predicted.cpu().numpy())\n\n# Convert the list of predictions to a numpy array\nall_predictions = np.array(all_predictions)\n\n# Print the shape of predictions\nprint(f'Predictions shape: {all_predictions.shape}')\n","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:33:11.657517Z","iopub.execute_input":"2024-05-28T19:33:11.657878Z","iopub.status.idle":"2024-05-28T19:33:15.475921Z","shell.execute_reply.started":"2024-05-28T19:33:11.657850Z","shell.execute_reply":"2024-05-28T19:33:15.475094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\n\n# Assuming you have already made predictions and stored them in 'all_predictions'\n\n# Create a DataFrame with ImageId and Prediction columns\nimage_ids = [i for i in range(1, len(all_predictions) + 1)]\ndf_predictions = pd.DataFrame({'ImageId': image_ids, 'Label': all_predictions})\n\n# Save the DataFrame to a CSV file\ndf_predictions.to_csv('test_predictions.csv', index=False)\n\n# Display the first few rows of the DataFrame\nprint(df_predictions.head())\n","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:45:34.395122Z","iopub.execute_input":"2024-05-28T19:45:34.395684Z","iopub.status.idle":"2024-05-28T19:45:34.463119Z","shell.execute_reply.started":"2024-05-28T19:45:34.395645Z","shell.execute_reply":"2024-05-28T19:45:34.462192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"!kaggle competitions submit -c {digit-recognizer} -f {test_predictions.csv} -m \"First Submission.\"","metadata":{"execution":{"iopub.status.busy":"2024-05-28T19:42:37.633283Z","iopub.execute_input":"2024-05-28T19:42:37.633947Z","iopub.status.idle":"2024-05-28T19:42:39.092743Z","shell.execute_reply.started":"2024-05-28T19:42:37.633905Z","shell.execute_reply":"2024-05-28T19:42:39.091733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}