{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":3004,"databundleVersionId":861823,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":275870916,"sourceType":"kernelVersion"}],"dockerImageVersionId":31089,"isInternetEnabled":false,"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","trusted":true,"execution":{"iopub.status.busy":"2025-11-04T20:44:27.341554Z","iopub.execute_input":"2025-11-04T20:44:27.341784Z","iopub.status.idle":"2025-11-04T20:44:29.369393Z","shell.execute_reply.started":"2025-11-04T20:44:27.341757Z","shell.execute_reply":"2025-11-04T20:44:29.368749Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch \nimport torch.nn as nn \nimport matplotlib.pyplot as plt # visualization\nfrom torch.utils.data import DataLoader, TensorDataset # load the data\nfrom sklearn.model_selection import StratifiedKFold\nfrom torch.optim import SGD, Adam\nfrom tqdm import tqdm # track the progress \nimport timm","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T20:45:07.372428Z","iopub.execute_input":"2025-11-04T20:45:07.372714Z","iopub.status.idle":"2025-11-04T20:45:15.104275Z","shell.execute_reply.started":"2025-11-04T20:45:07.372693Z","shell.execute_reply":"2025-11-04T20:45:15.103652Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_data=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest_data.head()\nlen(test_data)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T20:44:34.059439Z","iopub.execute_input":"2025-11-04T20:44:34.059761Z","iopub.status.idle":"2025-11-04T20:44:38.710782Z","shell.execute_reply.started":"2025-11-04T20:44:34.059744Z","shell.execute_reply":"2025-11-04T20:44:38.710030Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_data","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T20:44:38.711553Z","iopub.execute_input":"2025-11-04T20:44:38.711753Z","iopub.status.idle":"2025-11-04T20:44:38.746996Z","shell.execute_reply.started":"2025-11-04T20:44:38.711737Z","shell.execute_reply":"2025-11-04T20:44:38.746119Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CFG:\n    seed = 0\n    batch_size = 64\n    lr = 1e-3\n    num_epochs = 10\n    n_folds = 5\n    folds_to_run = [0]\n    ckpt_path = \"/kaggle/input/fork-of-digit-recognizer-efficientnet-lr-schedul/last_epoch-04.bin\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T20:44:38.748984Z","iopub.execute_input":"2025-11-04T20:44:38.749211Z","iopub.status.idle":"2025-11-04T20:44:38.753504Z","shell.execute_reply.started":"2025-11-04T20:44:38.749195Z","shell.execute_reply":"2025-11-04T20:44:38.752673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"state_dict = torch.load(CFG.ckpt_path, weights_only=False)\nmodel = timm.create_model('tf_efficientnetv2_s.in21k_ft_in1k', pretrained=False, in_chans=1,\n            num_classes=10)\nmodel.load_state_dict(state_dict)\n#model.eval()\n#optimizer = SGD(model.parameters(), lr=CFG.lr)\n#criterion = nn.CrossEntropyLoss()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T20:49:48.360590Z","iopub.execute_input":"2025-11-04T20:49:48.361135Z","iopub.status.idle":"2025-11-04T20:49:49.192659Z","shell.execute_reply.started":"2025-11-04T20:49:48.361115Z","shell.execute_reply":"2025-11-04T20:49:49.191977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def prepare_loaders(fold, batch_size):\n    X = test_data.iloc[:,:].values / 255\n    X = X.reshape(X.shape[0],1,28,28)\n    y = np.zeros(len(test_data), dtype=int)\n    test_dataset = TensorDataset(torch.tensor(X), torch.tensor(y))\n    test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, pin_memory=True, drop_last=False)\n    return test_loader","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T20:49:52.431696Z","iopub.execute_input":"2025-11-04T20:49:52.432407Z","iopub.status.idle":"2025-11-04T20:49:52.436568Z","shell.execute_reply.started":"2025-11-04T20:49:52.432386Z","shell.execute_reply":"2025-11-04T20:49:52.435945Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_loader = prepare_loaders(fold=0, batch_size=CFG.batch_size)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T20:49:56.369090Z","iopub.execute_input":"2025-11-04T20:49:56.369394Z","iopub.status.idle":"2025-11-04T20:49:56.516339Z","shell.execute_reply.started":"2025-11-04T20:49:56.369373Z","shell.execute_reply":"2025-11-04T20:49:56.515705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred = []\nmodel.eval()\nwith torch.no_grad():\n    for data in tqdm(test_loader):\n        feature, target = data\n        prediction = model(feature.float())\n        pred += [prediction]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T20:49:58.512959Z","iopub.execute_input":"2025-11-04T20:49:58.513519Z","iopub.status.idle":"2025-11-04T20:50:13.709956Z","shell.execute_reply.started":"2025-11-04T20:49:58.513497Z","shell.execute_reply":"2025-11-04T20:50:13.708845Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_pred = torch.cat(pred)\ntest_pred.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T20:44:40.105285Z","iopub.status.idle":"2025-11-04T20:44:40.105734Z","shell.execute_reply.started":"2025-11-04T20:44:40.105526Z","shell.execute_reply":"2025-11-04T20:44:40.105539Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsub['Label'] = np.argmax(test_pred, 1)\n\nprint(sub.shape)\nsub.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T20:44:40.106623Z","iopub.status.idle":"2025-11-04T20:44:40.106828Z","shell.execute_reply.started":"2025-11-04T20:44:40.106729Z","shell.execute_reply":"2025-11-04T20:44:40.106737Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred_df = pd.DataFrame(sub)\npred_df.to_csv('submission.csv',index=False)\ndisplay(pred_df.head(5))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-04T20:44:40.107356Z","iopub.status.idle":"2025-11-04T20:44:40.107608Z","shell.execute_reply.started":"2025-11-04T20:44:40.107503Z","shell.execute_reply":"2025-11-04T20:44:40.107513Z"}},"outputs":[],"execution_count":null}]}