{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-12T02:43:26.717013Z","iopub.execute_input":"2021-07-12T02:43:26.717385Z","iopub.status.idle":"2021-07-12T02:43:26.721192Z","shell.execute_reply.started":"2021-07-12T02:43:26.717351Z","shell.execute_reply":"2021-07-12T02:43:26.720257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:43:28.246514Z","iopub.execute_input":"2021-07-12T02:43:28.246855Z","iopub.status.idle":"2021-07-12T02:43:31.439933Z","shell.execute_reply.started":"2021-07-12T02:43:28.246824Z","shell.execute_reply":"2021-07-12T02:43:31.439072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:43:31.44224Z","iopub.execute_input":"2021-07-12T02:43:31.442743Z","iopub.status.idle":"2021-07-12T02:43:31.473004Z","shell.execute_reply.started":"2021-07-12T02:43:31.442703Z","shell.execute_reply":"2021-07-12T02:43:31.472073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:43:36.64643Z","iopub.execute_input":"2021-07-12T02:43:36.646761Z","iopub.status.idle":"2021-07-12T02:43:36.652358Z","shell.execute_reply.started":"2021-07-12T02:43:36.646729Z","shell.execute_reply":"2021-07-12T02:43:36.651337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = df.iloc[0,1:]","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:43:38.500779Z","iopub.execute_input":"2021-07-12T02:43:38.50121Z","iopub.status.idle":"2021-07-12T02:43:38.509648Z","shell.execute_reply.started":"2021-07-12T02:43:38.501171Z","shell.execute_reply":"2021-07-12T02:43:38.508629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = np.array(im).reshape((28,28))","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:43:40.422028Z","iopub.execute_input":"2021-07-12T02:43:40.422384Z","iopub.status.idle":"2021-07-12T02:43:40.426251Z","shell.execute_reply.started":"2021-07-12T02:43:40.422352Z","shell.execute_reply":"2021-07-12T02:43:40.425426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im.max()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:43:53.49203Z","iopub.execute_input":"2021-07-12T02:43:53.492424Z","iopub.status.idle":"2021-07-12T02:43:53.500673Z","shell.execute_reply.started":"2021-07-12T02:43:53.492389Z","shell.execute_reply":"2021-07-12T02:43:53.499846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:43:43.175382Z","iopub.execute_input":"2021-07-12T02:43:43.175724Z","iopub.status.idle":"2021-07-12T02:43:43.327865Z","shell.execute_reply.started":"2021-07-12T02:43:43.175694Z","shell.execute_reply":"2021-07-12T02:43:43.327089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nfrom matplotlib import cm\nim = Image.fromarray(np.uint8(im))\nim.save('test.png')","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:44:06.957594Z","iopub.execute_input":"2021-07-12T02:44:06.957922Z","iopub.status.idle":"2021-07-12T02:44:06.963209Z","shell.execute_reply.started":"2021-07-12T02:44:06.957892Z","shell.execute_reply":"2021-07-12T02:44:06.962066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image.fromarray(np.uint8(np.array(df.iloc[0,1:]).reshape((28,28))*255)).save('test2.png')","metadata":{"execution":{"iopub.status.busy":"2021-07-11T20:06:41.461299Z","iopub.execute_input":"2021-07-11T20:06:41.462068Z","iopub.status.idle":"2021-07-11T20:06:41.473162Z","shell.execute_reply.started":"2021-07-11T20:06:41.462023Z","shell.execute_reply":"2021-07-11T20:06:41.472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir data\n!mkdir data/train\n!mkdir data/test","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:44:21.685534Z","iopub.execute_input":"2021-07-12T02:44:21.68585Z","iopub.status.idle":"2021-07-12T02:44:23.5809Z","shell.execute_reply.started":"2021-07-12T02:44:21.68582Z","shell.execute_reply":"2021-07-12T02:44:23.579874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/working/data/train/'\ntrain = pd.DataFrame(columns = ['class','path'])\nfor ii in range(len(df)):\n    Image.fromarray(np.uint8(np.array(df.iloc[ii,1:]).reshape((28,28)))).save(path+str(10000+ii)+'.png')\n    train = train.append({'class':df.iloc[ii,0],'path':path+str(10000+ii)+'.png'},ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:45:11.224539Z","iopub.execute_input":"2021-07-12T02:45:11.224851Z","iopub.status.idle":"2021-07-12T02:47:45.003065Z","shell.execute_reply.started":"2021-07-12T02:45:11.224823Z","shell.execute_reply":"2021-07-12T02:47:45.002238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.to_csv(path+'train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:51:52.859716Z","iopub.execute_input":"2021-07-12T02:51:52.860038Z","iopub.status.idle":"2021-07-12T02:51:53.008533Z","shell.execute_reply.started":"2021-07-12T02:51:52.86001Z","shell.execute_reply":"2021-07-12T02:51:53.007727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nfrom fastai.callback.tracker import SaveModelCallback","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:51:57.726903Z","iopub.execute_input":"2021-07-12T02:51:57.727232Z","iopub.status.idle":"2021-07-12T02:52:00.301378Z","shell.execute_reply.started":"2021-07-12T02:51:57.7272Z","shell.execute_reply":"2021-07-12T02:52:00.300539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"splitter=RandomSplitter(valid_pct=0.2, seed=42)\ndblock = DataBlock(\n    blocks=(ImageBlock, CategoryBlock), #x - image; y - single class\n    get_x=ColReader(1), #x is stored in column 2\n    get_y=ColReader(0), #y is stored in column 1\n    splitter=splitter, #use parent folder as train-valid split\n    batch_tfms=aug_transforms(size=28,do_flip=False,),\n)\ndls = dblock.dataloaders(train, path='.', bs=64)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:54:50.550725Z","iopub.execute_input":"2021-07-12T02:54:50.551059Z","iopub.status.idle":"2021-07-12T02:54:59.120311Z","shell.execute_reply.started":"2021-07-12T02:54:50.551029Z","shell.execute_reply":"2021-07-12T02:54:59.119436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.train.show_batch(max_n=10, nrows=2)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:54:59.121853Z","iopub.execute_input":"2021-07-12T02:54:59.122224Z","iopub.status.idle":"2021-07-12T02:54:59.923444Z","shell.execute_reply.started":"2021-07-12T02:54:59.122163Z","shell.execute_reply":"2021-07-12T02:54:59.922614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls.train.show_batch(max_n=16, nrows=8, unique=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:55:04.00849Z","iopub.execute_input":"2021-07-12T02:55:04.008827Z","iopub.status.idle":"2021-07-12T02:55:04.889257Z","shell.execute_reply.started":"2021-07-12T02:55:04.008795Z","shell.execute_reply":"2021-07-12T02:55:04.888303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn = cnn_learner(dls, \n                    resnet34, \n                    metrics=[accuracy,]).to_fp16()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:55:27.656013Z","iopub.execute_input":"2021-07-12T02:55:27.656407Z","iopub.status.idle":"2021-07-12T02:55:33.284536Z","shell.execute_reply.started":"2021-07-12T02:55:27.656378Z","shell.execute_reply":"2021-07-12T02:55:33.283669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.lr_find()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:22:39.987972Z","iopub.execute_input":"2021-07-12T03:22:39.988335Z","iopub.status.idle":"2021-07-12T03:22:40.790077Z","shell.execute_reply.started":"2021-07-12T03:22:39.988302Z","shell.execute_reply":"2021-07-12T03:22:40.78423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#finetune \nlearn.fine_tune(epochs=5,\n          base_lr=1e-2, #max lr; when unfrozen base_lr/2\n          freeze_epochs=1, #how many epochs to train frozen\n          lr_mult=100, #train feature extractor with max lr at base_lr/lr_mult\n          pct_start=0.2, #start decreasing lr at \n          div=5.0, #start at base_lr (max lr) / div\n          cbs=[SaveModelCallback(monitor='accuracy'),]\n#               WandbCallback(), #track to wandb] #monitor accuracy and save best model\n          )","metadata":{"execution":{"iopub.status.busy":"2021-07-12T02:57:09.845489Z","iopub.execute_input":"2021-07-12T02:57:09.845823Z","iopub.status.idle":"2021-07-12T03:04:01.735724Z","shell.execute_reply.started":"2021-07-12T02:57:09.84579Z","shell.execute_reply":"2021-07-12T03:04:01.734815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:04:26.271067Z","iopub.execute_input":"2021-07-12T03:04:26.271435Z","iopub.status.idle":"2021-07-12T03:04:26.566496Z","shell.execute_reply.started":"2021-07-12T03:04:26.271401Z","shell.execute_reply":"2021-07-12T03:04:26.565655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#see validation performance\ninterp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:04:28.919026Z","iopub.execute_input":"2021-07-12T03:04:28.919381Z","iopub.status.idle":"2021-07-12T03:04:38.568441Z","shell.execute_reply.started":"2021-07-12T03:04:28.91935Z","shell.execute_reply":"2021-07-12T03:04:38.567591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(9, nrows=3)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:05:07.358114Z","iopub.execute_input":"2021-07-12T03:05:07.358527Z","iopub.status.idle":"2021-07-12T03:05:07.983791Z","shell.execute_reply.started":"2021-07-12T03:05:07.358466Z","shell.execute_reply":"2021-07-12T03:05:07.982992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:05:30.719583Z","iopub.execute_input":"2021-07-12T03:05:30.719946Z","iopub.status.idle":"2021-07-12T03:05:32.845062Z","shell.execute_reply.started":"2021-07-12T03:05:30.719913Z","shell.execute_reply":"2021-07-12T03:05:32.844233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:05:55.142945Z","iopub.execute_input":"2021-07-12T03:05:55.143281Z","iopub.status.idle":"2021-07-12T03:05:55.160072Z","shell.execute_reply.started":"2021-07-12T03:05:55.14325Z","shell.execute_reply":"2021-07-12T03:05:55.159305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '/kaggle/working/data/test/'\ntest = pd.DataFrame(columns = ['class','path'])\nfor ii in range(len(df)):\n    Image.fromarray(np.uint8(np.array(df.iloc[ii]).reshape((28,28)))).save(path+str(10000+ii)+'.png')\n    test = test.append({'class':df.iloc[ii,0],'path':path+str(10000+ii)+'.png'},ignore_index=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:06:30.662092Z","iopub.execute_input":"2021-07-12T03:06:30.662456Z","iopub.status.idle":"2021-07-12T03:08:06.684423Z","shell.execute_reply.started":"2021-07-12T03:06:30.662424Z","shell.execute_reply":"2021-07-12T03:08:06.683589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.to_csv(path+'test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:09:09.877968Z","iopub.execute_input":"2021-07-12T03:09:09.878337Z","iopub.status.idle":"2021-07-12T03:09:09.980096Z","shell.execute_reply.started":"2021-07-12T03:09:09.878304Z","shell.execute_reply":"2021-07-12T03:09:09.979309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_fnames = get_image_files(\"/kaggle/input/emg-png/data/test/\")\ntest_fnames = test['path']\nlen(test_fnames)\n#create test dataloader\ntst_dl = dls.test_dl(test_fnames,shuffle=False)\ntst_dl.show_batch(max_n=9)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:09:13.36672Z","iopub.execute_input":"2021-07-12T03:09:13.367051Z","iopub.status.idle":"2021-07-12T03:09:13.80778Z","shell.execute_reply.started":"2021-07-12T03:09:13.367019Z","shell.execute_reply":"2021-07-12T03:09:13.806955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = learn.get_preds(dl=tst_dl) # get prediction logits\npred_idxs = preds[0].argmax(1) # get index with highest logit\npred_labs = [learn.dls.train_ds.vocab[i] for i in pred_idxs] #convert index to labels\npred_labs[:10]","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:09:21.156253Z","iopub.execute_input":"2021-07-12T03:09:21.156595Z","iopub.status.idle":"2021-07-12T03:09:40.810643Z","shell.execute_reply.started":"2021-07-12T03:09:21.156566Z","shell.execute_reply":"2021-07-12T03:09:40.809792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#put in dataframe\npred = pd.DataFrame({'fname':test_fnames,'Predicted':pred_labs})\npred['filename'] = pred.fname.map(lambda x: str(x)[-13:-4])\npred = pred[['filename','Predicted']].sort_values('filename').reset_index(drop=True)\npred.tail()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:09:44.066008Z","iopub.execute_input":"2021-07-12T03:09:44.066373Z","iopub.status.idle":"2021-07-12T03:09:44.131529Z","shell.execute_reply.started":"2021-07-12T03:09:44.066338Z","shell.execute_reply":"2021-07-12T03:09:44.130573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred.to_csv('/kaggle/working/data/predict.csv',index=False)\npred.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:09:52.008851Z","iopub.execute_input":"2021-07-12T03:09:52.009215Z","iopub.status.idle":"2021-07-12T03:09:52.067389Z","shell.execute_reply.started":"2021-07-12T03:09:52.009178Z","shell.execute_reply":"2021-07-12T03:09:52.066427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!zip -r -q data.zip data/\n!rm -rf  data/*","metadata":{"execution":{"iopub.status.busy":"2021-07-12T03:10:08.595691Z","iopub.execute_input":"2021-07-12T03:10:08.596034Z","iopub.status.idle":"2021-07-12T03:10:15.827358Z","shell.execute_reply.started":"2021-07-12T03:10:08.596002Z","shell.execute_reply":"2021-07-12T03:10:15.826274Z"},"trusted":true},"execution_count":null,"outputs":[]}]}