{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","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"},{"sourceId":5196,"sourceType":"datasetVersion","datasetId":3147}],"dockerImageVersionId":30301,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Import Data","metadata":{}},{"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)\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-01T18:35:29.162268Z","iopub.execute_input":"2023-12-01T18:35:29.162764Z","iopub.status.idle":"2023-12-01T18:35:29.169967Z","shell.execute_reply.started":"2023-12-01T18:35:29.162714Z","shell.execute_reply":"2023-12-01T18:35:29.168034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"../input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:35:29.620516Z","iopub.execute_input":"2023-12-01T18:35:29.62148Z","iopub.status.idle":"2023-12-01T18:35:34.274175Z","shell.execute_reply.started":"2023-12-01T18:35:29.62143Z","shell.execute_reply":"2023-12-01T18:35:34.272648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:35:34.276567Z","iopub.execute_input":"2023-12-01T18:35:34.276953Z","iopub.status.idle":"2023-12-01T18:35:34.303079Z","shell.execute_reply.started":"2023-12-01T18:35:34.276919Z","shell.execute_reply":"2023-12-01T18:35:34.301113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:35:34.305567Z","iopub.execute_input":"2023-12-01T18:35:34.306413Z","iopub.status.idle":"2023-12-01T18:35:34.335912Z","shell.execute_reply.started":"2023-12-01T18:35:34.306294Z","shell.execute_reply":"2023-12-01T18:35:34.334883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train.drop(columns=['label']).to_numpy()\ntrain_y = train['label'].to_numpy()\nprint(train_x.shape)\nprint(train_y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:35:36.410701Z","iopub.execute_input":"2023-12-01T18:35:36.411472Z","iopub.status.idle":"2023-12-01T18:35:36.555655Z","shell.execute_reply.started":"2023-12-01T18:35:36.411431Z","shell.execute_reply":"2023-12-01T18:35:36.554347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_1 = train_x[0].reshape(28, 28)\nplt.imshow(sample_1,cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:35:36.945722Z","iopub.execute_input":"2023-12-01T18:35:36.946791Z","iopub.status.idle":"2023-12-01T18:35:37.235615Z","shell.execute_reply.started":"2023-12-01T18:35:36.94674Z","shell.execute_reply":"2023-12-01T18:35:37.233973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_1_y = train_y[0]\nprint(sample_1_y)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:35:37.495708Z","iopub.execute_input":"2023-12-01T18:35:37.496169Z","iopub.status.idle":"2023-12-01T18:35:37.50531Z","shell.execute_reply.started":"2023-12-01T18:35:37.496132Z","shell.execute_reply":"2023-12-01T18:35:37.503822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_100 = train_x[99].reshape(28, 28)\nplt.imshow(sample_100,cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:35:38.016057Z","iopub.execute_input":"2023-12-01T18:35:38.016905Z","iopub.status.idle":"2023-12-01T18:35:38.244127Z","shell.execute_reply.started":"2023-12-01T18:35:38.016847Z","shell.execute_reply":"2023-12-01T18:35:38.242111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_100_y = train_y[99]\nprint(sample_100_y)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:35:38.568313Z","iopub.execute_input":"2023-12-01T18:35:38.568799Z","iopub.status.idle":"2023-12-01T18:35:38.575925Z","shell.execute_reply.started":"2023-12-01T18:35:38.568752Z","shell.execute_reply":"2023-12-01T18:35:38.574224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:35:39.140018Z","iopub.execute_input":"2023-12-01T18:35:39.141343Z","iopub.status.idle":"2023-12-01T18:35:39.147347Z","shell.execute_reply.started":"2023-12-01T18:35:39.141278Z","shell.execute_reply":"2023-12-01T18:35:39.145995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_1 = test[0].reshape(28, 28)\nplt.imshow(test_1,cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:35:39.694303Z","iopub.execute_input":"2023-12-01T18:35:39.695499Z","iopub.status.idle":"2023-12-01T18:35:39.933591Z","shell.execute_reply.started":"2023-12-01T18:35:39.695431Z","shell.execute_reply":"2023-12-01T18:35:39.931908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_2 = test[1].reshape(28, 28)\nplt.imshow(test_2,cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:35:41.918023Z","iopub.execute_input":"2023-12-01T18:35:41.918579Z","iopub.status.idle":"2023-12-01T18:35:42.176518Z","shell.execute_reply.started":"2023-12-01T18:35:41.918505Z","shell.execute_reply":"2023-12-01T18:35:42.173759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train the model","metadata":{}},{"cell_type":"code","source":"# It's a good idea to ensure you're running the latest version of any libraries you need.\n# `!pip install -Uqq <libraries>` upgrades to the latest version of <libraries>\n# NB: You can safely ignore any warnings or errors pip spits out about running as root or incompatibilities\nimport os\niskaggle = os.environ.get('KAGGLE_KERNEL_RUN_TYPE', '')\n\nif iskaggle:\n    !pip3 install -Uqq fastai duckduckgo_search\n    \nfrom fastcore.all import *\nfrom fastai.vision.all import *\nfrom fastai.imports import *\nfrom fastai.metrics import *\nfrom torchvision.utils import save_image","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:04:01.108366Z","iopub.execute_input":"2023-12-01T18:04:01.108879Z","iopub.status.idle":"2023-12-01T18:04:40.457961Z","shell.execute_reply.started":"2023-12-01T18:04:01.108839Z","shell.execute_reply":"2023-12-01T18:04:40.456042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# add path\npath = Path('/kaggle/input/digit-recognizer')\nPath.BASE_PATH = path","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:04:40.461284Z","iopub.execute_input":"2023-12-01T18:04:40.462178Z","iopub.status.idle":"2023-12-01T18:04:40.468353Z","shell.execute_reply.started":"2023-12-01T18:04:40.462118Z","shell.execute_reply":"2023-12-01T18:04:40.466557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path.ls()","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:04:40.470471Z","iopub.execute_input":"2023-12-01T18:04:40.471892Z","iopub.status.idle":"2023-12-01T18:04:40.495932Z","shell.execute_reply.started":"2023-12-01T18:04:40.471825Z","shell.execute_reply":"2023-12-01T18:04:40.494274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#convert training data to images\npath = Path('train')\nif not path.exists():\n    path.mkdir()\nfor i in range(train_x.shape[0]):\n    image_matrix = tensor(train_x[i]/255).reshape((28,28)) #divided by rgb max value 255 for normalization\n    dest = (path/train_y[i].astype(str))\n    dest.mkdir(exist_ok=True)\n    save_image(image_matrix,f'{dest}/{i}.png')","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:04:40.500301Z","iopub.execute_input":"2023-12-01T18:04:40.501023Z","iopub.status.idle":"2023-12-01T18:05:11.413577Z","shell.execute_reply.started":"2023-12-01T18:04:40.500965Z","shell.execute_reply":"2023-12-01T18:05:11.411678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path.ls()","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:05:11.415883Z","iopub.execute_input":"2023-12-01T18:05:11.416392Z","iopub.status.idle":"2023-12-01T18:05:11.427103Z","shell.execute_reply.started":"2023-12-01T18:05:11.416352Z","shell.execute_reply":"2023-12-01T18:05:11.425721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('train')\ndls = DataBlock(\n    blocks=(ImageBlock, CategoryBlock), \n    get_items=get_image_files, \n    splitter=RandomSplitter(valid_pct=0.2, seed=42),\n    get_y=parent_label,\n    #item_tfms=[Resize(192, method='squish')]\n).dataloaders(path, bs=32)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:05:11.42913Z","iopub.execute_input":"2023-12-01T18:05:11.429635Z","iopub.status.idle":"2023-12-01T18:05:15.325249Z","shell.execute_reply.started":"2023-12-01T18:05:11.42955Z","shell.execute_reply":"2023-12-01T18:05:15.323824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=error_rate)\nlearn.fine_tune(3,base_lr=1e-2)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:05:15.327109Z","iopub.execute_input":"2023-12-01T18:05:15.327933Z","iopub.status.idle":"2023-12-01T18:29:50.535178Z","shell.execute_reply.started":"2023-12-01T18:05:15.327888Z","shell.execute_reply":"2023-12-01T18:29:50.532901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=error_rate)\nlearn.fine_tune(3)","metadata":{"execution":{"iopub.status.busy":"2023-11-17T18:54:09.105535Z","iopub.execute_input":"2023-11-17T18:54:09.10603Z","iopub.status.idle":"2023-11-17T19:11:56.376844Z","shell.execute_reply.started":"2023-11-17T18:54:09.105986Z","shell.execute_reply":"2023-11-17T19:11:56.375707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Use Model","metadata":{}},{"cell_type":"code","source":"test = pd.DataFrame(test)\ntest['id'] = range(len(test))","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:41:16.093345Z","iopub.execute_input":"2023-12-01T18:41:16.094Z","iopub.status.idle":"2023-12-01T18:41:16.106056Z","shell.execute_reply.started":"2023-12-01T18:41:16.093949Z","shell.execute_reply":"2023-12-01T18:41:16.104638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#convert test data to images\npath = Path('test')\nif not path.exists():\n    path.mkdir()\n\nfor index, row in test.iterrows():\n    img_id = row['id']  # 获取当前行的id\n    img_data = tensor(row.drop('id').values / 255).reshape((28,28))  # 假设除了id外，其他列是图像数据\n    save_image(img_data, f'{path}/{img_id}.png')","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:43:30.235266Z","iopub.execute_input":"2023-12-01T18:43:30.236804Z","iopub.status.idle":"2023-12-01T18:44:14.48914Z","shell.execute_reply.started":"2023-12-01T18:43:30.236735Z","shell.execute_reply":"2023-12-01T18:44:14.487914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prediction on test set\npath = Path('test')\ntest_files = get_image_files(path)\n\n# 按数字顺序排序文件\ntest_files = sorted(test_files, key=lambda x: int(x.stem))\n\ntest_dl = dls.test_dl(test_files)\ntest_dl.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:46:19.59707Z","iopub.execute_input":"2023-12-01T18:46:19.59757Z","iopub.status.idle":"2023-12-01T18:46:20.401968Z","shell.execute_reply.started":"2023-12-01T18:46:19.59752Z","shell.execute_reply":"2023-12-01T18:46:20.40072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score, _, pred_classes = learn.get_preds(dl=test_dl, with_decoded=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:46:28.934476Z","iopub.execute_input":"2023-12-01T18:46:28.935081Z","iopub.status.idle":"2023-12-01T18:47:36.354383Z","shell.execute_reply.started":"2023-12-01T18:46:28.935032Z","shell.execute_reply":"2023-12-01T18:47:36.352368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(class_score)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:47:49.445286Z","iopub.execute_input":"2023-12-01T18:47:49.445825Z","iopub.status.idle":"2023-12-01T18:47:49.456964Z","shell.execute_reply.started":"2023-12-01T18:47:49.445785Z","shell.execute_reply":"2023-12-01T18:47:49.454416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_score.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:47:51.457549Z","iopub.execute_input":"2023-12-01T18:47:51.458112Z","iopub.status.idle":"2023-12-01T18:47:51.471258Z","shell.execute_reply.started":"2023-12-01T18:47:51.458069Z","shell.execute_reply":"2023-12-01T18:47:51.46895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(pred_classes)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:47:52.497095Z","iopub.execute_input":"2023-12-01T18:47:52.497511Z","iopub.status.idle":"2023-12-01T18:47:52.50459Z","shell.execute_reply.started":"2023-12-01T18:47:52.497479Z","shell.execute_reply":"2023-12-01T18:47:52.503332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_classes.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:47:53.798718Z","iopub.execute_input":"2023-12-01T18:47:53.799139Z","iopub.status.idle":"2023-12-01T18:47:53.809616Z","shell.execute_reply.started":"2023-12-01T18:47:53.799107Z","shell.execute_reply":"2023-12-01T18:47:53.807485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission =  pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")\nsample_submission.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:47:55.631774Z","iopub.execute_input":"2023-12-01T18:47:55.63231Z","iopub.status.idle":"2023-12-01T18:47:55.658054Z","shell.execute_reply.started":"2023-12-01T18:47:55.632258Z","shell.execute_reply":"2023-12-01T18:47:55.656323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:48:02.841352Z","iopub.execute_input":"2023-12-01T18:48:02.843029Z","iopub.status.idle":"2023-12-01T18:48:02.853422Z","shell.execute_reply.started":"2023-12-01T18:48:02.842952Z","shell.execute_reply":"2023-12-01T18:48:02.851104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageId = []\na = 1\nfor i in range(test.shape[0]):\n    idid = a + i\n    ImageId.append(idid)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:48:04.35264Z","iopub.execute_input":"2023-12-01T18:48:04.353892Z","iopub.status.idle":"2023-12-01T18:48:04.366602Z","shell.execute_reply.started":"2023-12-01T18:48:04.353836Z","shell.execute_reply":"2023-12-01T18:48:04.365569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission  = pd.DataFrame({\n    \"ImageId\": ImageId,\n    \"Label\": pred_classes\n})","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:48:09.278223Z","iopub.execute_input":"2023-12-01T18:48:09.279314Z","iopub.status.idle":"2023-12-01T18:48:09.293497Z","shell.execute_reply.started":"2023-12-01T18:48:09.279274Z","shell.execute_reply":"2023-12-01T18:48:09.292115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(submission)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:48:11.359104Z","iopub.execute_input":"2023-12-01T18:48:11.360405Z","iopub.status.idle":"2023-12-01T18:48:11.371179Z","shell.execute_reply.started":"2023-12-01T18:48:11.360355Z","shell.execute_reply":"2023-12-01T18:48:11.36863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-01T18:49:01.554858Z","iopub.execute_input":"2023-12-01T18:49:01.555422Z","iopub.status.idle":"2023-12-01T18:49:01.604576Z","shell.execute_reply.started":"2023-12-01T18:49:01.555377Z","shell.execute_reply":"2023-12-01T18:49:01.602756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}