{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-05T10:24:38.481860Z","iopub.execute_input":"2022-04-05T10:24:38.482312Z","iopub.status.idle":"2022-04-05T10:24:38.514689Z","shell.execute_reply.started":"2022-04-05T10:24:38.482173Z","shell.execute_reply":"2022-04-05T10:24:38.513812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/ultra-mnist/train.csv\")\nprint(train[\"digit_sum\"].nunique())\ntrain[\"id\"] = \"train_img/\" + train[\"id\"] + \".jpeg\"\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:43:05.575342Z","iopub.execute_input":"2022-04-05T10:43:05.575658Z","iopub.status.idle":"2022-04-05T10:43:05.619074Z","shell.execute_reply.started":"2022-04-05T10:43:05.575613Z","shell.execute_reply":"2022-04-05T10:43:05.618018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dict = {k:v for k,v in zip(train.id.values,train.digit_sum.values)}","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:43:21.604877Z","iopub.execute_input":"2022-04-05T10:43:21.605266Z","iopub.status.idle":"2022-04-05T10:43:21.626101Z","shell.execute_reply.started":"2022-04-05T10:43:21.605232Z","shell.execute_reply":"2022-04-05T10:43:21.625172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport matplotlib.pyplot as plt\nimport cv2 as cv\nfrom skimage import morphology\nfrom scipy.ndimage.morphology import binary_fill_holes\n%matplotlib inline\n\nrnd = 100\nPATH = \"../input/ultra-mnist/train/\" + train.id.values[rnd] + \".jpeg\"\n\nimg = cv.imread(PATH,0)\nthresh_image = (255 - cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2))\nrmnoise_image = morphology.remove_small_objects(thresh_image.astype(bool), min_size=30)\nfillcap_image = morphology.binary_closing(rmnoise_image, selem=morphology.disk(2))\nplt.imshow(fillcap_image)\nplt.title(train.digit_sum.values[rnd])","metadata":{"execution":{"iopub.status.busy":"2022-04-05T09:06:38.551445Z","iopub.execute_input":"2022-04-05T09:06:38.552136Z","iopub.status.idle":"2022-04-05T09:06:42.71524Z","shell.execute_reply.started":"2022-04-05T09:06:38.5521Z","shell.execute_reply":"2022-04-05T09:06:42.714599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import fastai","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:24:53.063416Z","iopub.execute_input":"2022-04-05T10:24:53.063685Z","iopub.status.idle":"2022-04-05T10:24:53.072176Z","shell.execute_reply.started":"2022-04-05T10:24:53.063656Z","shell.execute_reply":"2022-04-05T10:24:53.071060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(fastai.__version__)","metadata":{"execution":{"iopub.status.busy":"2022-04-05T09:48:59.553383Z","iopub.execute_input":"2022-04-05T09:48:59.553694Z","iopub.status.idle":"2022-04-05T09:48:59.558899Z","shell.execute_reply.started":"2022-04-05T09:48:59.553664Z","shell.execute_reply":"2022-04-05T09:48:59.557825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.data.all import *\nfrom fastai.vision.all import *","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:24:56.182365Z","iopub.execute_input":"2022-04-05T10:24:56.182680Z","iopub.status.idle":"2022-04-05T10:24:59.313950Z","shell.execute_reply.started":"2022-04-05T10:24:56.182650Z","shell.execute_reply":"2022-04-05T10:24:59.313011Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_files(_):\n    return train.id.values\n\ndef get_x(id):\n    x = f\"../input/ultramnist-resized-256/{id}\"\n    #x = ARRAY[id,:,:]\n    return x\n\ndef get_y(id):\n    y = train_dict[id]\n    return y","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:45:23.537184Z","iopub.execute_input":"2022-04-05T10:45:23.537510Z","iopub.status.idle":"2022-04-05T10:45:23.544975Z","shell.execute_reply.started":"2022-04-05T10:45:23.537463Z","shell.execute_reply":"2022-04-05T10:45:23.543513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dblock = DataBlock(blocks    = (ImageBlock, CategoryBlock),\n                   get_items = get_files,\n                   get_x = get_x,\n                   get_y     = get_y,\n                   splitter  = RandomSplitter(),\n                   )","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:43:29.972388Z","iopub.execute_input":"2022-04-05T10:43:29.973234Z","iopub.status.idle":"2022-04-05T10:43:29.982167Z","shell.execute_reply.started":"2022-04-05T10:43:29.973196Z","shell.execute_reply":"2022-04-05T10:43:29.980877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = dblock.dataloaders(\"\",bs=64)\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:43:33.414172Z","iopub.execute_input":"2022-04-05T10:43:33.414465Z","iopub.status.idle":"2022-04-05T10:43:46.682026Z","shell.execute_reply.started":"2022-04-05T10:43:33.414435Z","shell.execute_reply":"2022-04-05T10:43:46.680929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, resnet34, metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:45:33.700498Z","iopub.execute_input":"2022-04-05T10:45:33.700896Z","iopub.status.idle":"2022-04-05T10:45:37.931129Z","shell.execute_reply.started":"2022-04-05T10:45:33.700847Z","shell.execute_reply":"2022-04-05T10:45:37.930020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fine_tune(3)","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:45:40.402762Z","iopub.execute_input":"2022-04-05T10:45:40.403613Z","iopub.status.idle":"2022-04-05T10:52:55.542090Z","shell.execute_reply.started":"2022-04-05T10:45:40.403575Z","shell.execute_reply":"2022-04-05T10:52:55.541121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"../input/ultra-mnist/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:53:25.331755Z","iopub.execute_input":"2022-04-05T10:53:25.332409Z","iopub.status.idle":"2022-04-05T10:53:25.364904Z","shell.execute_reply.started":"2022-04-05T10:53:25.332361Z","shell.execute_reply":"2022-04-05T10:53:25.363908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[\"id\"] = \"test_img/\" + test_df[\"id\"] + \".jpeg\"\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:53:29.422085Z","iopub.execute_input":"2022-04-05T10:53:29.422395Z","iopub.status.idle":"2022-04-05T10:53:29.448886Z","shell.execute_reply.started":"2022-04-05T10:53:29.422365Z","shell.execute_reply":"2022-04-05T10:53:29.448002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tdl = dls.test_dl(test_df.id.values)","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:53:32.672364Z","iopub.execute_input":"2022-04-05T10:53:32.672664Z","iopub.status.idle":"2022-04-05T10:53:32.684171Z","shell.execute_reply.started":"2022-04-05T10:53:32.672631Z","shell.execute_reply":"2022-04-05T10:53:32.683138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds,_ = learn.get_preds(dl=tdl)","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:53:36.575570Z","iopub.execute_input":"2022-04-05T10:53:36.576189Z","iopub.status.idle":"2022-04-05T10:56:28.303422Z","shell.execute_reply.started":"2022-04-05T10:53:36.576144Z","shell.execute_reply":"2022-04-05T10:56:28.302381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = preds.numpy()","metadata":{"execution":{"iopub.status.busy":"2022-04-05T10:59:44.725010Z","iopub.execute_input":"2022-04-05T10:59:44.725805Z","iopub.status.idle":"2022-04-05T10:59:44.731137Z","shell.execute_reply.started":"2022-04-05T10:59:44.725763Z","shell.execute_reply":"2022-04-05T10:59:44.729950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss = pd.read_csv(\"../input/ultra-mnist/sample_submission.csv\")\nss[\"digit_sum\"] = preds.argmax(axis=1)\nss.to_csv(\"sub.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-05T11:01:47.488323Z","iopub.execute_input":"2022-04-05T11:01:47.488843Z","iopub.status.idle":"2022-04-05T11:01:47.571759Z","shell.execute_reply.started":"2022-04-05T11:01:47.488808Z","shell.execute_reply":"2022-04-05T11:01:47.570602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}