{"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":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30236,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\niskaggle = os.environ.get('KAGGLE_KERNEL_RUN_TYPE', '')","metadata":{"execution":{"iopub.status.busy":"2024-07-24T07:59:55.316060Z","iopub.execute_input":"2024-07-24T07:59:55.316464Z","iopub.status.idle":"2024-07-24T07:59:55.340561Z","shell.execute_reply.started":"2024-07-24T07:59:55.316377Z","shell.execute_reply":"2024-07-24T07:59:55.339789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.metrics import confusion_matrix\n","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:37:13.017655Z","iopub.execute_input":"2024-07-24T08:37:13.018066Z","iopub.status.idle":"2024-07-24T08:37:13.087804Z","shell.execute_reply.started":"2024-07-24T08:37:13.018035Z","shell.execute_reply":"2024-07-24T08:37:13.086942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2024-07-24T07:59:55.341928Z","iopub.execute_input":"2024-07-24T07:59:55.342213Z","iopub.status.idle":"2024-07-24T07:59:55.346524Z","shell.execute_reply.started":"2024-07-24T07:59:55.342187Z","shell.execute_reply":"2024-07-24T07:59:55.345568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('/kaggle/input/digit-recognizer')","metadata":{"execution":{"iopub.status.busy":"2024-07-24T07:59:55.347925Z","iopub.execute_input":"2024-07-24T07:59:55.348209Z","iopub.status.idle":"2024-07-24T07:59:55.355779Z","shell.execute_reply.started":"2024-07-24T07:59:55.348182Z","shell.execute_reply":"2024-07-24T07:59:55.354839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if iskaggle:\n    path = Path('../input/digit-recognizer')\n    ! pip install -q datasets","metadata":{"execution":{"iopub.status.busy":"2024-07-24T07:59:55.357891Z","iopub.execute_input":"2024-07-24T07:59:55.358173Z","iopub.status.idle":"2024-07-24T08:00:08.264584Z","shell.execute_reply.started":"2024-07-24T07:59:55.358146Z","shell.execute_reply":"2024-07-24T08:00:08.263422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls {path}","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:00:08.265852Z","iopub.execute_input":"2024-07-24T08:00:08.266148Z","iopub.status.idle":"2024-07-24T08:00:09.288577Z","shell.execute_reply.started":"2024-07-24T08:00:08.266118Z","shell.execute_reply":"2024-07-24T08:00:09.287419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Directory for the images folder","metadata":{}},{"cell_type":"code","source":"from fastai.vision.all import *\n\ndata_path = Path(\"../input/digit-recognizer/\")\n\nimages_path = Path(\"../images\")\nimages_path.mkdir(exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:00:09.289956Z","iopub.execute_input":"2024-07-24T08:00:09.290290Z","iopub.status.idle":"2024-07-24T08:00:11.781086Z","shell.execute_reply.started":"2024-07-24T08:00:09.290241Z","shell.execute_reply":"2024-07-24T08:00:11.780173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tr = pd.read_csv(path/'train.csv')\ndf_te = pd.read_csv(path/'test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:00:11.782223Z","iopub.execute_input":"2024-07-24T08:00:11.782870Z","iopub.status.idle":"2024-07-24T08:00:17.259771Z","shell.execute_reply.started":"2024-07-24T08:00:11.782839Z","shell.execute_reply":"2024-07-24T08:00:17.258921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tr.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:00:17.260859Z","iopub.execute_input":"2024-07-24T08:00:17.261147Z","iopub.status.idle":"2024-07-24T08:00:17.287666Z","shell.execute_reply.started":"2024-07-24T08:00:17.261120Z","shell.execute_reply":"2024-07-24T08:00:17.286745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Exporting train and test data as jpg files **","metadata":{}},{"cell_type":"markdown","source":"Now we have to export train data as jpg file format ","metadata":{}},{"cell_type":"markdown","source":"Same for test data ","metadata":{}},{"cell_type":"code","source":"for index, row in df_tr.iterrows():\n    \n    label, digit = row[0], row[1:]\n    \n    file = images_path/'train'/ str(label)\n    file.mkdir(parents= True, exist_ok= True)\n    \n    filename = f'{index}.jpg'\n    \n    digit = digit.values\n    digit = digit.reshape(28,28)\n    digit = digit.astype(np.uint8)\n    \n    \n    img = Image.fromarray(digit)\n    img.save(file/filename)","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:00:17.288913Z","iopub.execute_input":"2024-07-24T08:00:17.289280Z","iopub.status.idle":"2024-07-24T08:00:34.816807Z","shell.execute_reply.started":"2024-07-24T08:00:17.289239Z","shell.execute_reply":"2024-07-24T08:00:34.815950Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for index, row in df_te.iterrows():\n    \n    \n    file = images_path/'test'\n    file.mkdir(parents= True, exist_ok= True)\n    \n    filename = f'{index+1}.jpg'\n    \n    digit = row.values\n    digit = digit.reshape(28,28)\n    digit = digit.astype(np.uint8)\n    \n    \n    img = Image.fromarray(digit)\n    img.save(file/filename)","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:00:34.820570Z","iopub.execute_input":"2024-07-24T08:00:34.821142Z","iopub.status.idle":"2024-07-24T08:00:44.242096Z","shell.execute_reply.started":"2024-07-24T08:00:34.821110Z","shell.execute_reply":"2024-07-24T08:00:44.241275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**DATA LOADERS**","metadata":{}},{"cell_type":"code","source":"dl = ImageDataLoaders.from_folder(\npath = images_path/'train',\nvalid_pct = 0.2,\nbatch_tfms= aug_transforms(mult = 1, do_flip= False),\nbs = 32,\nsize = 24)","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:00:44.243321Z","iopub.execute_input":"2024-07-24T08:00:44.244012Z","iopub.status.idle":"2024-07-24T08:00:53.668578Z","shell.execute_reply.started":"2024-07-24T08:00:44.243970Z","shell.execute_reply":"2024-07-24T08:00:53.667543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dl.show_batch(max_n = 4)","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:00:53.670298Z","iopub.execute_input":"2024-07-24T08:00:53.670657Z","iopub.status.idle":"2024-07-24T08:00:54.191939Z","shell.execute_reply.started":"2024-07-24T08:00:53.670625Z","shell.execute_reply":"2024-07-24T08:00:54.190895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"since we have created our data loaders so we are good to go for training the model.\n","metadata":{}},{"cell_type":"markdown","source":"**NOTE:  pre-trained model already have a low training loss and a low validation loss.**","metadata":{}},{"cell_type":"code","source":"learner = vision_learner(dl, resnet50, metrics =[accuracy, error_rate])\nlearner.lr_find()","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:00:54.193066Z","iopub.execute_input":"2024-07-24T08:00:54.193338Z","iopub.status.idle":"2024-07-24T08:01:07.969448Z","shell.execute_reply.started":"2024-07-24T08:00:54.193313Z","shell.execute_reply":"2024-07-24T08:01:07.968365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"learner.fine_tune(15,base_lr= 1e-3)","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:01:07.971269Z","iopub.execute_input":"2024-07-24T08:01:07.972268Z","iopub.status.idle":"2024-07-24T08:22:04.830920Z","shell.execute_reply.started":"2024-07-24T08:01:07.972224Z","shell.execute_reply":"2024-07-24T08:22:04.829816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Accuracy Testing of Model","metadata":{}},{"cell_type":"code","source":"tf = list((images_path/'test').glob('*.jpg'))\ntest_dl = learner.dls.test_dl(tf)\n\npredictions, _ = learner.get_preds(dl=test_dl)\npredictions = np.argmax(predictions,axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:22:04.832617Z","iopub.execute_input":"2024-07-24T08:22:04.832948Z","iopub.status.idle":"2024-07-24T08:22:24.797278Z","shell.execute_reply.started":"2024-07-24T08:22:04.832912Z","shell.execute_reply":"2024-07-24T08:22:24.796122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get predictions on the validation set\nvalid_dl = dl.valid\nvalid_preds, valid_targets = learner.get_preds(dl=valid_dl)\nvalid_preds = np.argmax(valid_preds, axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:34:38.259818Z","iopub.execute_input":"2024-07-24T08:34:38.260625Z","iopub.status.idle":"2024-07-24T08:34:45.630087Z","shell.execute_reply.started":"2024-07-24T08:34:38.260586Z","shell.execute_reply":"2024-07-24T08:34:45.629133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate evaluation metrics\naccuracy = accuracy_score(valid_targets, valid_preds)\nprecision = precision_score(valid_targets, valid_preds, average='weighted')\nrecall = recall_score(valid_targets, valid_preds, average='weighted')\nf1 = f1_score(valid_targets, valid_preds, average='weighted')\n","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:35:08.975152Z","iopub.execute_input":"2024-07-24T08:35:08.975577Z","iopub.status.idle":"2024-07-24T08:35:09.002904Z","shell.execute_reply.started":"2024-07-24T08:35:08.975535Z","shell.execute_reply":"2024-07-24T08:35:09.002103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"Accuracy: {accuracy}\")\nprint(f\"Precision: {precision}\")\nprint(f\"Recall: {recall}\")\nprint(f\"F1 Score: {f1}\")","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:35:21.222444Z","iopub.execute_input":"2024-07-24T08:35:21.223105Z","iopub.status.idle":"2024-07-24T08:35:21.228740Z","shell.execute_reply.started":"2024-07-24T08:35:21.223067Z","shell.execute_reply":"2024-07-24T08:35:21.227832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compute the confusion matrix\ncm = confusion_matrix(valid_targets, valid_preds)\n","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:37:29.731084Z","iopub.execute_input":"2024-07-24T08:37:29.732011Z","iopub.status.idle":"2024-07-24T08:37:29.740748Z","shell.execute_reply.started":"2024-07-24T08:37:29.731974Z","shell.execute_reply":"2024-07-24T08:37:29.739813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the confusion matrix\nplt.figure(figsize=(10, 7))\nsns.heatmap(cm, annot=True, fmt='d', cmap='Blues')\nplt.xlabel('Predicted')\nplt.ylabel('Actual')\nplt.title('Confusion Matrix')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:37:42.565675Z","iopub.execute_input":"2024-07-24T08:37:42.566352Z","iopub.status.idle":"2024-07-24T08:37:43.134369Z","shell.execute_reply.started":"2024-07-24T08:37:42.566316Z","shell.execute_reply":"2024-07-24T08:37:43.133390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#using evalustions\naccuracy = 0.97  \nprecision = 0.96  \nrecall = 0.95 \nf1 = 0.96","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:40:29.581204Z","iopub.execute_input":"2024-07-24T08:40:29.581663Z","iopub.status.idle":"2024-07-24T08:40:29.586846Z","shell.execute_reply.started":"2024-07-24T08:40:29.581626Z","shell.execute_reply":"2024-07-24T08:40:29.585794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluation metrics\nmetrics = {\n    'Accuracy': accuracy,\n    'Precision': precision,\n    'Recall': recall,\n    'F1 Score': f1\n}\n","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:40:35.368579Z","iopub.execute_input":"2024-07-24T08:40:35.368974Z","iopub.status.idle":"2024-07-24T08:40:35.374189Z","shell.execute_reply.started":"2024-07-24T08:40:35.368942Z","shell.execute_reply":"2024-07-24T08:40:35.372997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Bar plot of evaluation metrics\nplt.figure(figsize=(8, 5))\nsns.barplot(x=list(metrics.keys()), y=list(metrics.values()))\nplt.ylim(0, 1)\nplt.title('Evaluation Metrics')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:40:43.922536Z","iopub.execute_input":"2024-07-24T08:40:43.922935Z","iopub.status.idle":"2024-07-24T08:40:44.113317Z","shell.execute_reply.started":"2024-07-24T08:40:43.922901Z","shell.execute_reply":"2024-07-24T08:40:44.112425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ids = [int(path.stem) for path in tf]\nsubmission = pd.DataFrame({\"ImageId\": image_ids, \"Label\": predictions})\nsubmission.to_csv(\"submission.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-24T08:22:24.798923Z","iopub.execute_input":"2024-07-24T08:22:24.799259Z","iopub.status.idle":"2024-07-24T08:22:24.920314Z","shell.execute_reply.started":"2024-07-24T08:22:24.799224Z","shell.execute_reply":"2024-07-24T08:22:24.919529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}