{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"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\n\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn.model_selection import train_test_split\n\n# ----------------------------------------------------------------------\n# Config\n# ----------------------------------------------------------------------\nDATA_DIR = \"/kaggle/input/competitions/digit-recognizer-challenge\" if os.path.exists(\"/kaggle/input/competitions/digit-recognizer-challenge\") else \".\"\nOUT_DIR = \"/kaggle/working\" if os.path.exists(\"/kaggle/working\") else \".\"\nBATCH_SIZE = 128\nEPOCHS = 15\nLR = 1e-3\nVAL_FRACTION = 0.1\nSEED = 42\n\ntorch.manual_seed(SEED)\nnp.random.seed(SEED)\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(f\"Using device: {device}\")\n\n\n# ----------------------------------------------------------------------\n# Data\n# ----------------------------------------------------------------------\nclass MNISTDataset(Dataset):\n    \"\"\"Wraps pixel arrays (N, 784) + optional labels into 1x28x28 tensors.\"\"\"\n\n    def __init__(self, pixels, labels=None, train=False):\n        self.pixels = pixels.astype(np.float32) / 255.0\n        self.labels = labels\n        self.train = train\n\n    def __len__(self):\n        return len(self.pixels)\n\n    def __getitem__(self, idx):\n        img = self.pixels[idx].reshape(28, 28)\n\n        if self.train:\n            # light augmentation: random shift up to 2px, keeps digits legible\n            shift_x, shift_y = np.random.randint(-2, 3, size=2)\n            img = np.roll(img, shift_x, axis=1)\n            img = np.roll(img, shift_y, axis=0)\n\n        img = torch.tensor(img, dtype=torch.float32).unsqueeze(0)  # 1x28x28\n        img = (img - 0.1307) / 0.3081  # MNIST mean/std normalization\n\n        if self.labels is not None:\n            return img, torch.tensor(self.labels[idx], dtype=torch.long)\n        return img\n\n\ndef load_data():\n    train_path = os.path.join(DATA_DIR, \"train.csv\")\n    test_path = os.path.join(DATA_DIR, \"test.csv\")\n\n    if not os.path.exists(train_path):\n        raise FileNotFoundError(\n            f\"Couldn't find {train_path}. On Kaggle, make sure this notebook \"\n            \"is attached to the Digit Recognizer competition data. Locally, \"\n            \"place train.csv/test.csv next to this script or edit DATA_DIR.\"\n        )\n\n    train_df = pd.read_csv(train_path)\n    test_df = pd.read_csv(test_path)\n\n    y = train_df[\"label\"].values\n    X = train_df.drop(columns=[\"label\"]).values\n    X_test = test_df.values\n\n    X_train, X_val, y_train, y_val = train_test_split(\n        X, y, test_size=VAL_FRACTION, random_state=SEED, stratify=y\n    )\n    return X_train, y_train, X_val, y_val, X_test\n\n\n# ----------------------------------------------------------------------\n# Model: small but effective CNN\n# ----------------------------------------------------------------------\nclass DigitCNN(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv2d(1, 32, 3, padding=1)\n        self.bn1 = nn.BatchNorm2d(32)\n        self.conv2 = nn.Conv2d(32, 32, 3, padding=1)\n        self.bn2 = nn.BatchNorm2d(32)\n\n        self.conv3 = nn.Conv2d(32, 64, 3, padding=1)\n        self.bn3 = nn.BatchNorm2d(64)\n        self.conv4 = nn.Conv2d(64, 64, 3, padding=1)\n        self.bn4 = nn.BatchNorm2d(64)\n\n        self.pool = nn.MaxPool2d(2, 2)\n        self.dropout1 = nn.Dropout(0.25)\n        self.dropout2 = nn.Dropout(0.5)\n\n        self.fc1 = nn.Linear(64 * 7 * 7, 256)\n        self.fc2 = nn.Linear(256, 10)\n\n    def forward(self, x):\n        x = F.relu(self.bn1(self.conv1(x)))\n        x = F.relu(self.bn2(self.conv2(x)))\n        x = self.pool(x)\n        x = self.dropout1(x)\n\n        x = F.relu(self.bn3(self.conv3(x)))\n        x = F.relu(self.bn4(self.conv4(x)))\n        x = self.pool(x)\n        x = self.dropout1(x)\n\n        x = x.view(x.size(0), -1)\n        x = F.relu(self.fc1(x))\n        x = self.dropout2(x)\n        x = self.fc2(x)\n        return x\n\n\n# ----------------------------------------------------------------------\n# Train / eval loops\n# ----------------------------------------------------------------------\ndef run_epoch(model, loader, optimizer=None, criterion=None):\n    is_train = optimizer is not None\n    model.train() if is_train else model.eval()\n\n    total_loss, correct, total = 0.0, 0, 0\n    context = torch.enable_grad() if is_train else torch.no_grad()\n\n    with context:\n        for imgs, labels in loader:\n            imgs, labels = imgs.to(device), labels.to(device)\n\n            if is_train:\n                optimizer.zero_grad()\n\n            outputs = model(imgs)\n            loss = criterion(outputs, labels)\n\n            if is_train:\n                loss.backward()\n                optimizer.step()\n\n            total_loss += loss.item() * imgs.size(0)\n            correct += (outputs.argmax(1) == labels).sum().item()\n            total += imgs.size(0)\n\n    return total_loss / total, correct / total\n\n\ndef predict(model, loader):\n    model.eval()\n    preds = []\n    with torch.no_grad():\n        for imgs in loader:\n            imgs = imgs.to(device)\n            outputs = model(imgs)\n            preds.append(outputs.argmax(1).cpu().numpy())\n    return np.concatenate(preds)\n\n\n# ----------------------------------------------------------------------\n# Main\n# ----------------------------------------------------------------------\ndef main():\n    X_train, y_train, X_val, y_val, X_test = load_data()\n    print(f\"Train: {X_train.shape}, Val: {X_val.shape}, Test: {X_test.shape}\")\n\n    train_ds = MNISTDataset(X_train, y_train, train=True)\n    val_ds = MNISTDataset(X_val, y_val, train=False)\n    test_ds = MNISTDataset(X_test, train=False)\n\n    train_loader = DataLoader(train_ds, batch_size=BATCH_SIZE, shuffle=True, num_workers=2)\n    val_loader = DataLoader(val_ds, batch_size=BATCH_SIZE, shuffle=False, num_workers=2)\n    test_loader = DataLoader(test_ds, batch_size=BATCH_SIZE, shuffle=False, num_workers=2)\n\n    model = DigitCNN().to(device)\n    criterion = nn.CrossEntropyLoss()\n    optimizer = torch.optim.Adam(model.parameters(), lr=LR)\n    scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(\n        optimizer, mode=\"max\", factor=0.5, patience=2\n    )\n\n    best_val_acc = 0.0\n    best_state = None\n\n    for epoch in range(1, EPOCHS + 1):\n        train_loss, train_acc = run_epoch(model, train_loader, optimizer, criterion)\n        val_loss, val_acc = run_epoch(model, val_loader, criterion=criterion)\n        scheduler.step(val_acc)\n\n        print(\n            f\"Epoch {epoch:2d}/{EPOCHS} | \"\n            f\"train_loss={train_loss:.4f} train_acc={train_acc:.4f} | \"\n            f\"val_loss={val_loss:.4f} val_acc={val_acc:.4f}\"\n        )\n\n        if val_acc > best_val_acc:\n            best_val_acc = val_acc\n            best_state = {k: v.cpu().clone() for k, v in model.state_dict().items()}\n\n    print(f\"\\nBest validation accuracy: {best_val_acc:.4f}\")\n    model.load_state_dict(best_state)\n\n    # Save model weights\n    model_path = os.path.join(OUT_DIR, \"digit_cnn.pt\")\n    torch.save(model.state_dict(), model_path)\n    print(f\"Saved model weights to {model_path}\")\n\n    # Predict on test set and write submission\n    test_preds = predict(model, test_loader)\n    submission = pd.DataFrame({\n        \"ImageId\": np.arange(1, len(test_preds) + 1),\n        \"Label\": test_preds,\n    })\n    sub_path = os.path.join(OUT_DIR, \"submission.csv\")\n    submission.to_csv(sub_path, index=False)\n    print(f\"Saved submission to {sub_path}\")\n\n\nif __name__ == \"__main__\":\n    main()","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}