{"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-12T04:14:34.760929Z","iopub.execute_input":"2021-07-12T04:14:34.761330Z","iopub.status.idle":"2021-07-12T04:14:34.767128Z","shell.execute_reply.started":"2021-07-12T04:14:34.761277Z","shell.execute_reply":"2021-07-12T04:14:34.766065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/digit-recognizer-as-png/data/train/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:34.769172Z","iopub.execute_input":"2021-07-12T04:14:34.769968Z","iopub.status.idle":"2021-07-12T04:14:34.834631Z","shell.execute_reply.started":"2021-07-12T04:14:34.769929Z","shell.execute_reply":"2021-07-12T04:14:34.833397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:34.839646Z","iopub.execute_input":"2021-07-12T04:14:34.840104Z","iopub.status.idle":"2021-07-12T04:14:34.858001Z","shell.execute_reply.started":"2021-07-12T04:14:34.840066Z","shell.execute_reply":"2021-07-12T04:14:34.856909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('Unnamed: 0',axis = 1)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:34.863032Z","iopub.execute_input":"2021-07-12T04:14:34.864971Z","iopub.status.idle":"2021-07-12T04:14:34.885088Z","shell.execute_reply.started":"2021-07-12T04:14:34.864927Z","shell.execute_reply":"2021-07-12T04:14:34.884330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:34.886658Z","iopub.execute_input":"2021-07-12T04:14:34.888339Z","iopub.status.idle":"2021-07-12T04:14:34.898004Z","shell.execute_reply.started":"2021-07-12T04:14:34.888278Z","shell.execute_reply":"2021-07-12T04:14:34.897248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = pd.DataFrame({'path': ['/kaggle/input/digit-recognizer-as-png/data/train/'+str(ii) for ii in df['path'].str.split('/', expand = True)[5]]})\ntmp","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:34.899333Z","iopub.execute_input":"2021-07-12T04:14:34.901026Z","iopub.status.idle":"2021-07-12T04:14:35.113549Z","shell.execute_reply.started":"2021-07-12T04:14:34.900988Z","shell.execute_reply":"2021-07-12T04:14:35.112447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['path'] = tmp['path']\ndf","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:35.117493Z","iopub.execute_input":"2021-07-12T04:14:35.117836Z","iopub.status.idle":"2021-07-12T04:14:35.139484Z","shell.execute_reply.started":"2021-07-12T04:14:35.117801Z","shell.execute_reply":"2021-07-12T04:14:35.138588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# im = df.iloc[0,1:]","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:35.174892Z","iopub.status.idle":"2021-07-12T04:14:35.177332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# im = np.array(im).reshape((28,28))","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:35.180964Z","iopub.status.idle":"2021-07-12T04:14:35.183314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# im.max()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:35.186854Z","iopub.status.idle":"2021-07-12T04:14:35.189165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n# plt.imshow(im)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:35.191916Z","iopub.status.idle":"2021-07-12T04:14:35.193568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import Image\n# from matplotlib import cm\n# im = Image.fromarray(np.uint8(im))\n# im.save('test.png')","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:35.197515Z","iopub.status.idle":"2021-07-12T04:14:35.198261Z"},"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-12T04:14:35.199406Z","iopub.status.idle":"2021-07-12T04:14:35.200178Z"},"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-12T04:14:35.207984Z","iopub.status.idle":"2021-07-12T04:14:35.208745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# path = '/kaggle/working/data/train/'\n# train = pd.DataFrame(columns = ['class','path'])\n# for 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-12T04:14:35.209860Z","iopub.status.idle":"2021-07-12T04:14:35.210619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train.to_csv(path+'train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:35.211702Z","iopub.status.idle":"2021-07-12T04:14:35.212439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = df","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:42.096659Z","iopub.execute_input":"2021-07-12T04:14:42.096994Z","iopub.status.idle":"2021-07-12T04:14:42.101050Z","shell.execute_reply.started":"2021-07-12T04:14:42.096966Z","shell.execute_reply":"2021-07-12T04:14:42.099849Z"},"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-12T04:14:43.727519Z","iopub.execute_input":"2021-07-12T04:14:43.727838Z","iopub.status.idle":"2021-07-12T04:14:43.733265Z","shell.execute_reply.started":"2021-07-12T04:14:43.727809Z","shell.execute_reply":"2021-07-12T04:14:43.731188Z"},"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-12T04:14:46.088811Z","iopub.execute_input":"2021-07-12T04:14:46.089150Z","iopub.status.idle":"2021-07-12T04:14:56.127062Z","shell.execute_reply.started":"2021-07-12T04:14:46.089118Z","shell.execute_reply":"2021-07-12T04:14:56.126001Z"},"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-12T04:14:58.120034Z","iopub.execute_input":"2021-07-12T04:14:58.120379Z","iopub.status.idle":"2021-07-12T04:14:58.917974Z","shell.execute_reply.started":"2021-07-12T04:14:58.120348Z","shell.execute_reply":"2021-07-12T04:14:58.917026Z"},"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-12T04:15:05.587527Z","iopub.execute_input":"2021-07-12T04:15:05.587857Z","iopub.status.idle":"2021-07-12T04:15:06.542280Z","shell.execute_reply.started":"2021-07-12T04:15:05.587828Z","shell.execute_reply":"2021-07-12T04:15:06.541344Z"},"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-12T04:15:11.544992Z","iopub.execute_input":"2021-07-12T04:15:11.545345Z","iopub.status.idle":"2021-07-12T04:15:14.877823Z","shell.execute_reply.started":"2021-07-12T04:15:11.545293Z","shell.execute_reply":"2021-07-12T04:15:14.876963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_min, lr_steep = learn.lr_find()\n","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:31:09.451968Z","iopub.execute_input":"2021-07-12T04:31:09.452322Z","iopub.status.idle":"2021-07-12T04:31:25.219576Z","shell.execute_reply.started":"2021-07-12T04:31:09.452275Z","shell.execute_reply":"2021-07-12T04:31:25.218612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#finetune \nlearn.fine_tune(epochs=20,\n          base_lr=lr_min, #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-12T04:37:11.630793Z","iopub.execute_input":"2021-07-12T04:37:11.631159Z","iopub.status.idle":"2021-07-12T04:41:32.656505Z","shell.execute_reply.started":"2021-07-12T04:37:11.631121Z","shell.execute_reply":"2021-07-12T04:41:32.655549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learn.export()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:35.247156Z","iopub.status.idle":"2021-07-12T04:14:35.247807Z"},"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-12T04:41:43.224347Z","iopub.execute_input":"2021-07-12T04:41:43.224711Z","iopub.status.idle":"2021-07-12T04:41:55.878524Z","shell.execute_reply.started":"2021-07-12T04:41:43.224673Z","shell.execute_reply":"2021-07-12T04:41:55.877496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"interp.plot_top_losses(9, nrows=3)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:44:38.725706Z","iopub.execute_input":"2021-07-12T04:44:38.726029Z","iopub.status.idle":"2021-07-12T04:44:39.540863Z","shell.execute_reply.started":"2021-07-12T04:44:38.725999Z","shell.execute_reply":"2021-07-12T04:44:39.540018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/digit-recognizer-as-png/data/test/test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:42:04.508073Z","iopub.execute_input":"2021-07-12T04:42:04.508422Z","iopub.status.idle":"2021-07-12T04:42:04.551134Z","shell.execute_reply.started":"2021-07-12T04:42:04.508387Z","shell.execute_reply":"2021-07-12T04:42:04.550351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.drop('Unnamed: 0',axis = 1)\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:42:06.948189Z","iopub.execute_input":"2021-07-12T04:42:06.948545Z","iopub.status.idle":"2021-07-12T04:42:06.960973Z","shell.execute_reply.started":"2021-07-12T04:42:06.948513Z","shell.execute_reply":"2021-07-12T04:42:06.959972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df)","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:42:12.437539Z","iopub.execute_input":"2021-07-12T04:42:12.437876Z","iopub.status.idle":"2021-07-12T04:42:12.445143Z","shell.execute_reply.started":"2021-07-12T04:42:12.437844Z","shell.execute_reply":"2021-07-12T04:42:12.442926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tmp = pd.DataFrame({'path': ['/kaggle/input/digit-recognizer-as-png/data/test/'+str(ii) for ii in df['path'].str.split('/', expand = True)[5]]})\ntmp","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:42:15.434219Z","iopub.execute_input":"2021-07-12T04:42:15.434578Z","iopub.status.idle":"2021-07-12T04:42:15.809892Z","shell.execute_reply.started":"2021-07-12T04:42:15.434546Z","shell.execute_reply":"2021-07-12T04:42:15.808766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = tmp\ntest","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:42:53.049670Z","iopub.execute_input":"2021-07-12T04:42:53.050003Z","iopub.status.idle":"2021-07-12T04:42:53.062389Z","shell.execute_reply.started":"2021-07-12T04:42:53.049973Z","shell.execute_reply":"2021-07-12T04:42:53.061319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# path = '/kaggle/working/data/test/'\n# test = pd.DataFrame(columns = ['class','path'])\n# for 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-12T04:14:35.256639Z","iopub.status.idle":"2021-07-12T04:14:35.257246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test.to_csv(path+'test.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:14:35.258410Z","iopub.status.idle":"2021-07-12T04:14:35.258951Z"},"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-12T04:42:59.959222Z","iopub.execute_input":"2021-07-12T04:42:59.959636Z","iopub.status.idle":"2021-07-12T04:43:00.672966Z","shell.execute_reply.started":"2021-07-12T04:42:59.959600Z","shell.execute_reply":"2021-07-12T04:43:00.672199Z"},"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-12T04:43:03.482804Z","iopub.execute_input":"2021-07-12T04:43:03.483122Z","iopub.status.idle":"2021-07-12T04:44:11.375785Z","shell.execute_reply.started":"2021-07-12T04:43:03.483092Z","shell.execute_reply":"2021-07-12T04:44:11.374928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#put in dataframe\npred = pd.DataFrame({'fname':test_fnames,'Label':pred_labs})\npred['ImageId'] = pred.fname.map(lambda x: int(str(x)[-9:-4])-10000)\npred = pred[['ImageId','Label']].sort_values('ImageId').reset_index(drop=True)\npred.tail()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:44:18.984305Z","iopub.execute_input":"2021-07-12T04:44:18.984650Z","iopub.status.idle":"2021-07-12T04:44:19.044814Z","shell.execute_reply.started":"2021-07-12T04:44:18.984617Z","shell.execute_reply":"2021-07-12T04:44:19.043845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred.to_csv('/kaggle/working/predict.csv',index=False)\npred.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-12T04:44:22.942178Z","iopub.execute_input":"2021-07-12T04:44:22.942542Z","iopub.status.idle":"2021-07-12T04:44:22.998037Z","shell.execute_reply.started":"2021-07-12T04:44:22.942510Z","shell.execute_reply":"2021-07-12T04:44:22.997102Z"},"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-12T04:14:35.267717Z","iopub.status.idle":"2021-07-12T04:14:35.268346Z"},"trusted":true},"execution_count":null,"outputs":[]}]}