{"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":"! pip install timm -q","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:22:38.246277Z","iopub.execute_input":"2022-08-08T08:22:38.246808Z","iopub.status.idle":"2022-08-08T08:22:52.034709Z","shell.execute_reply.started":"2022-08-08T08:22:38.246683Z","shell.execute_reply":"2022-08-08T08:22:52.033506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%reload_ext autoreload\n%autoreload 2\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:22:52.037438Z","iopub.execute_input":"2022-08-08T08:22:52.038130Z","iopub.status.idle":"2022-08-08T08:22:52.085048Z","shell.execute_reply.started":"2022-08-08T08:22:52.038089Z","shell.execute_reply":"2022-08-08T08:22:52.084163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport torch\nfrom fastai import *\nfrom fastai.basics import *\nfrom fastai.callback.all import *\nfrom fastai.vision import *\nfrom fastai.vision.all import *\nfrom fastai.metrics import error_rate, accuracy\nimport timm\nimport numpy as np\nimport pandas as pd\nimport matplotlib as plt\nimport random\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:22:52.088024Z","iopub.execute_input":"2022-08-08T08:22:52.088303Z","iopub.status.idle":"2022-08-08T08:22:55.710703Z","shell.execute_reply.started":"2022-08-08T08:22:52.088277Z","shell.execute_reply":"2022-08-08T08:22:55.709576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:22:55.713964Z","iopub.execute_input":"2022-08-08T08:22:55.715125Z","iopub.status.idle":"2022-08-08T08:22:55.763214Z","shell.execute_reply.started":"2022-08-08T08:22:55.715086Z","shell.execute_reply":"2022-08-08T08:22:55.762077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT = Path(\"../input/digit-recognizer\")\nos.listdir(INPUT)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:22:55.764743Z","iopub.execute_input":"2022-08-08T08:22:55.765506Z","iopub.status.idle":"2022-08-08T08:22:55.816399Z","shell.execute_reply.started":"2022-08-08T08:22:55.765467Z","shell.execute_reply":"2022-08-08T08:22:55.815346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(INPUT/\"train.csv\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:22:55.817909Z","iopub.execute_input":"2022-08-08T08:22:55.818837Z","iopub.status.idle":"2022-08-08T08:22:58.582530Z","shell.execute_reply.started":"2022-08-08T08:22:55.818800Z","shell.execute_reply":"2022-08-08T08:22:58.581433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(INPUT/\"test.csv\")\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:22:58.584341Z","iopub.execute_input":"2022-08-08T08:22:58.585206Z","iopub.status.idle":"2022-08-08T08:23:00.234264Z","shell.execute_reply.started":"2022-08-08T08:22:58.585145Z","shell.execute_reply":"2022-08-08T08:23:00.233281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN = Path(\"../train\")\nTEST = Path(\"../test\")","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:00.235647Z","iopub.execute_input":"2022-08-08T08:23:00.236714Z","iopub.status.idle":"2022-08-08T08:23:00.279400Z","shell.execute_reply.started":"2022-08-08T08:23:00.236667Z","shell.execute_reply":"2022-08-08T08:23:00.278277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(10):    \n    try:         \n        os.makedirs(TRAIN/str(i))       \n    except:\n        pass","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:00.280942Z","iopub.execute_input":"2022-08-08T08:23:00.281376Z","iopub.status.idle":"2022-08-08T08:23:00.331034Z","shell.execute_reply.started":"2022-08-08T08:23:00.281323Z","shell.execute_reply":"2022-08-08T08:23:00.330082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sorted(os.listdir(TRAIN))","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:00.335536Z","iopub.execute_input":"2022-08-08T08:23:00.335811Z","iopub.status.idle":"2022-08-08T08:23:00.381278Z","shell.execute_reply.started":"2022-08-08T08:23:00.335774Z","shell.execute_reply":"2022-08-08T08:23:00.379851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    os.makedirs(TEST)\nexcept:\n    pass","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:00.383572Z","iopub.execute_input":"2022-08-08T08:23:00.384230Z","iopub.status.idle":"2022-08-08T08:23:00.426310Z","shell.execute_reply.started":"2022-08-08T08:23:00.384176Z","shell.execute_reply":"2022-08-08T08:23:00.425371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(TEST)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:00.429818Z","iopub.execute_input":"2022-08-08T08:23:00.430168Z","iopub.status.idle":"2022-08-08T08:23:00.475225Z","shell.execute_reply.started":"2022-08-08T08:23:00.430143Z","shell.execute_reply":"2022-08-08T08:23:00.474325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if os.path.isdir(TRAIN):\n    print('Train directory has been created')\nelse:\n    print('Train directory creation failed.')\n\nif os.path.isdir(TEST):\n    print('Test directory has been created')\nelse:\n    print('Test directory creation failed.')","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:00.476683Z","iopub.execute_input":"2022-08-08T08:23:00.477693Z","iopub.status.idle":"2022-08-08T08:23:00.521149Z","shell.execute_reply.started":"2022-08-08T08:23:00.477659Z","shell.execute_reply":"2022-08-08T08:23:00.520006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pix2img(pix_data, filepath):\n    img_mat = pix_data.reshape(28,28)\n    img_mat = img_mat.astype(np.uint8())\n    \n    img_dat = Image.fromarray(img_mat)\n    img_dat.save(filepath)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:00.522923Z","iopub.execute_input":"2022-08-08T08:23:00.523300Z","iopub.status.idle":"2022-08-08T08:23:00.566900Z","shell.execute_reply.started":"2022-08-08T08:23:00.523265Z","shell.execute_reply":"2022-08-08T08:23:00.565782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx, data in train_df.iterrows():\n    \n    label, data = data[0], data[1:]\n    folder = TRAIN/str(label)\n    \n    fname = f\"{idx}.jpg\"\n    filepath = folder/fname\n    \n    img_data = data.values\n    \n    pix2img(img_data,filepath)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:00.569506Z","iopub.execute_input":"2022-08-08T08:23:00.570165Z","iopub.status.idle":"2022-08-08T08:23:17.587450Z","shell.execute_reply.started":"2022-08-08T08:23:00.570121Z","shell.execute_reply":"2022-08-08T08:23:17.586438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx, data in test_df.iterrows():\n    \n    folder = TEST\n    \n    fname = f\"{idx}.jpg\"\n    filepath = folder/fname\n    \n    img_data = data.values\n    \n    pix2img(img_data,filepath)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:17.589213Z","iopub.execute_input":"2022-08-08T08:23:17.589637Z","iopub.status.idle":"2022-08-08T08:23:26.009126Z","shell.execute_reply.started":"2022-08-08T08:23:17.589600Z","shell.execute_reply":"2022-08-08T08:23:26.008187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotTrainImage():\n    \n    fig = plt.figure(figsize= (5,10))\n    \n    for rowIdx in range(1,10):\n        \n        foldNum = str(rowIdx)\n        path = TRAIN/foldNum\n        \n        images = os.listdir(path)\n        \n        for sampleIdx in range(1,6):\n            \n            randNum = random.randint(0, len(images)-1)\n            image = Image.open(path/images[randNum])\n            ax = fig.add_subplot(10, 5, 5*rowIdx + sampleIdx)\n            ax.axis(\"off\")\n            \n            plt.imshow(image, cmap='gray')\n            \n    plt.show()   ","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:26.010702Z","iopub.execute_input":"2022-08-08T08:23:26.011055Z","iopub.status.idle":"2022-08-08T08:23:26.061273Z","shell.execute_reply.started":"2022-08-08T08:23:26.011022Z","shell.execute_reply":"2022-08-08T08:23:26.056418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotTestImage():\n    \n    fig = plt.figure(figsize=(5, 10))    \n    paths = os.listdir(TEST)    \n        \n    for i in range(1, 51):\n        randomNumber = random.randint(0, len(paths)-1)\n        image = Image.open(TEST/paths[randomNumber])\n        \n        ax = fig.add_subplot(10, 5, i)\n        ax.axis(\"off\")\n        \n        plt.imshow(image, cmap='gray')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:26.066840Z","iopub.execute_input":"2022-08-08T08:23:26.069060Z","iopub.status.idle":"2022-08-08T08:23:26.123115Z","shell.execute_reply.started":"2022-08-08T08:23:26.068844Z","shell.execute_reply":"2022-08-08T08:23:26.122125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bs = 1024","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:26.124685Z","iopub.execute_input":"2022-08-08T08:23:26.125678Z","iopub.status.idle":"2022-08-08T08:23:26.168486Z","shell.execute_reply.started":"2022-08-08T08:23:26.125641Z","shell.execute_reply":"2022-08-08T08:23:26.167485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = ('../train')\ntest =('../test')","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:26.169944Z","iopub.execute_input":"2022-08-08T08:23:26.170291Z","iopub.status.idle":"2022-08-08T08:23:26.214458Z","shell.execute_reply.started":"2022-08-08T08:23:26.170255Z","shell.execute_reply":"2022-08-08T08:23:26.213291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = ImageDataLoaders.from_folder(path, test=test, valid_pct=0.2, bs=bs)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:26.219125Z","iopub.execute_input":"2022-08-08T08:23:26.219518Z","iopub.status.idle":"2022-08-08T08:23:32.717332Z","shell.execute_reply.started":"2022-08-08T08:23:26.219488Z","shell.execute_reply":"2022-08-08T08:23:32.716305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.show_batch()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:32.718802Z","iopub.execute_input":"2022-08-08T08:23:32.719158Z","iopub.status.idle":"2022-08-08T08:23:34.250519Z","shell.execute_reply.started":"2022-08-08T08:23:32.719124Z","shell.execute_reply":"2022-08-08T08:23:34.249347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(data, resnet18, model_dir=\"/tmp/models\", metrics=accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:34.252222Z","iopub.execute_input":"2022-08-08T08:23:34.252627Z","iopub.status.idle":"2022-08-08T08:23:35.583293Z","shell.execute_reply.started":"2022-08-08T08:23:34.252586Z","shell.execute_reply":"2022-08-08T08:23:35.582059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:23:42.702813Z","iopub.execute_input":"2022-08-08T08:23:42.703327Z","iopub.status.idle":"2022-08-08T08:25:25.577429Z","shell.execute_reply.started":"2022-08-08T08:23:42.703279Z","shell.execute_reply":"2022-08-08T08:25:25.576339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(10, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:25:25.579612Z","iopub.execute_input":"2022-08-08T08:25:25.580583Z","iopub.status.idle":"2022-08-08T08:32:24.615567Z","shell.execute_reply.started":"2022-08-08T08:25:25.580537Z","shell.execute_reply":"2022-08-08T08:32:24.614401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.unfreeze()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:32:24.620907Z","iopub.execute_input":"2022-08-08T08:32:24.623227Z","iopub.status.idle":"2022-08-08T08:32:24.696051Z","shell.execute_reply.started":"2022-08-08T08:32:24.623185Z","shell.execute_reply":"2022-08-08T08:32:24.694699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.fit_one_cycle(8, 3e-3)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:32:24.702352Z","iopub.execute_input":"2022-08-08T08:32:24.705430Z","iopub.status.idle":"2022-08-08T08:39:59.818956Z","shell.execute_reply.started":"2022-08-08T08:32:24.705387Z","shell.execute_reply":"2022-08-08T08:39:59.817807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.show_results()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:51:05.538481Z","iopub.execute_input":"2022-08-08T08:51:05.539115Z","iopub.status.idle":"2022-08-08T08:51:08.403673Z","shell.execute_reply.started":"2022-08-08T08:51:05.539069Z","shell.execute_reply":"2022-08-08T08:51:08.402726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:51:08.426420Z","iopub.execute_input":"2022-08-08T08:51:08.426767Z","iopub.status.idle":"2022-08-08T08:51:18.923314Z","shell.execute_reply.started":"2022-08-08T08:51:08.426734Z","shell.execute_reply":"2022-08-08T08:51:18.922182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:51:18.928869Z","iopub.execute_input":"2022-08-08T08:51:18.931640Z","iopub.status.idle":"2022-08-08T08:51:30.528749Z","shell.execute_reply.started":"2022-08-08T08:51:18.931586Z","shell.execute_reply":"2022-08-08T08:51:30.527408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SAMPLE_SUBMISSION = pd.read_csv(INPUT/'sample_submission.csv')\nSAMPLE_SUBMISSION.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:51:30.530519Z","iopub.execute_input":"2022-08-08T08:51:30.530912Z","iopub.status.idle":"2022-08-08T08:51:30.704873Z","shell.execute_reply.started":"2022-08-08T08:51:30.530873Z","shell.execute_reply":"2022-08-08T08:51:30.703926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = list(Path(r'../test').glob('**/*'))\ntest_dl = data.test_dl(test)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:51:30.710513Z","iopub.execute_input":"2022-08-08T08:51:30.715092Z","iopub.status.idle":"2022-08-08T08:51:31.292519Z","shell.execute_reply.started":"2022-08-08T08:51:30.715053Z","shell.execute_reply":"2022-08-08T08:51:31.291395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = learn.get_preds(dl=test_dl, with_decoded=True)\npreds = preds[2].tolist()","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:51:31.297599Z","iopub.execute_input":"2022-08-08T08:51:31.299929Z","iopub.status.idle":"2022-08-08T08:51:56.544778Z","shell.execute_reply.started":"2022-08-08T08:51:31.299890Z","shell.execute_reply":"2022-08-08T08:51:56.543604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ids = [int(img.stem) + 1 for img in test]","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:51:56.546877Z","iopub.execute_input":"2022-08-08T08:51:56.547613Z","iopub.status.idle":"2022-08-08T08:51:56.673266Z","shell.execute_reply.started":"2022-08-08T08:51:56.547573Z","shell.execute_reply":"2022-08-08T08:51:56.672028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(ids, columns=['ImageId'])","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:51:56.675162Z","iopub.execute_input":"2022-08-08T08:51:56.675957Z","iopub.status.idle":"2022-08-08T08:51:56.751115Z","shell.execute_reply.started":"2022-08-08T08:51:56.675913Z","shell.execute_reply":"2022-08-08T08:51:56.749925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['Label'] = preds","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:51:56.752864Z","iopub.execute_input":"2022-08-08T08:51:56.753562Z","iopub.status.idle":"2022-08-08T08:51:56.827760Z","shell.execute_reply.started":"2022-08-08T08:51:56.753524Z","shell.execute_reply":"2022-08-08T08:51:56.826822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:51:56.829348Z","iopub.execute_input":"2022-08-08T08:51:56.830021Z","iopub.status.idle":"2022-08-08T08:51:56.899537Z","shell.execute_reply.started":"2022-08-08T08:51:56.829980Z","shell.execute_reply":"2022-08-08T08:51:56.898415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"/kaggle/working/submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-08T08:51:56.906016Z","iopub.execute_input":"2022-08-08T08:51:56.908431Z","iopub.status.idle":"2022-08-08T08:51:57.017577Z","shell.execute_reply.started":"2022-08-08T08:51:56.908392Z","shell.execute_reply":"2022-08-08T08:51:57.016619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}