{"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# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-22T01:52:36.483708Z","iopub.execute_input":"2023-05-22T01:52:36.484424Z","iopub.status.idle":"2023-05-22T01:52:36.496700Z","shell.execute_reply.started":"2023-05-22T01:52:36.484390Z","shell.execute_reply":"2023-05-22T01:52:36.495829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\n\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\", 'r') as zip_ref:\n    zip_ref.extractall(\"./\")\n\nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\", 'r') as zip_ref:\n    zip_ref.extractall(\"./\")","metadata":{"execution":{"iopub.status.busy":"2023-05-22T01:52:36.498578Z","iopub.execute_input":"2023-05-22T01:52:36.498955Z","iopub.status.idle":"2023-05-22T01:52:52.057632Z","shell.execute_reply.started":"2023-05-22T01:52:36.498903Z","shell.execute_reply":"2023-05-22T01:52:52.056520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import fastai\nfrom fastai.vision.all import *\npath = \"./train\"","metadata":{"execution":{"iopub.status.busy":"2023-05-22T01:52:52.061123Z","iopub.execute_input":"2023-05-22T01:52:52.061875Z","iopub.status.idle":"2023-05-22T01:52:57.797276Z","shell.execute_reply.started":"2023-05-22T01:52:52.061839Z","shell.execute_reply":"2023-05-22T01:52:57.796093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files = get_image_files(path)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T01:52:57.798538Z","iopub.execute_input":"2023-05-22T01:52:57.798888Z","iopub.status.idle":"2023-05-22T01:52:58.209363Z","shell.execute_reply.started":"2023-05-22T01:52:57.798856Z","shell.execute_reply":"2023-05-22T01:52:58.208406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_name_re(\n        path=path,\n        fnames=files,\n        pat=r'(.+)\\.\\d+.jpg$',\n        valid_pct=0.2, \n        seed=13,\n        item_tfms=Resize(224))\n\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T01:52:58.212046Z","iopub.execute_input":"2023-05-22T01:52:58.212495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model  = cnn_learner(dls, resnet34, pretrained = True, metrics=[accuracy])","metadata":{"execution":{"iopub.execute_input":"2023-05-22T01:53:04.923185Z","iopub.status.idle":"2023-05-22T01:53:06.148889Z","shell.execute_reply.started":"2023-05-22T01:53:04.923151Z","shell.execute_reply":"2023-05-22T01:53:06.147964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.fine_tune(2, 1e-3)\nmodel.fit_one_cycle(6, slice(5e-3,5e-2),cbs=[ShowGraphCallback()])","metadata":{"execution":{"iopub.status.busy":"2023-05-22T01:53:06.150393Z","iopub.execute_input":"2023-05-22T01:53:06.150755Z","iopub.status.idle":"2023-05-22T02:07:32.359278Z","shell.execute_reply.started":"2023-05-22T01:53:06.150722Z","shell.execute_reply":"2023-05-22T02:07:32.358141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As you can see, loss (both training and validation) steadily decreased to a very low level","metadata":{}},{"cell_type":"markdown","source":"# MNIST","metadata":{}},{"cell_type":"code","source":"path = \"/kaggle/input/digit-recognizer/\"","metadata":{"execution":{"iopub.status.busy":"2023-05-22T02:07:32.364178Z","iopub.execute_input":"2023-05-22T02:07:32.366919Z","iopub.status.idle":"2023-05-22T02:07:32.374108Z","shell.execute_reply.started":"2023-05-22T02:07:32.366879Z","shell.execute_reply":"2023-05-22T02:07:32.372948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(f\"{path}train.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-05-22T02:07:32.375691Z","iopub.execute_input":"2023-05-22T02:07:32.376820Z","iopub.status.idle":"2023-05-22T02:07:37.246414Z","shell.execute_reply.started":"2023-05-22T02:07:32.376788Z","shell.execute_reply":"2023-05-22T02:07:37.245319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = train_df[\"label\"]\nX_train = train_df.drop(\"label\", axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T02:07:37.251695Z","iopub.execute_input":"2023-05-22T02:07:37.254111Z","iopub.status.idle":"2023-05-22T02:07:37.357861Z","shell.execute_reply.started":"2023-05-22T02:07:37.254071Z","shell.execute_reply":"2023-05-22T02:07:37.356763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28, 28)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T02:07:37.366643Z","iopub.execute_input":"2023-05-22T02:07:37.369211Z","iopub.status.idle":"2023-05-22T02:07:37.376466Z","shell.execute_reply.started":"2023-05-22T02:07:37.369172Z","shell.execute_reply":"2023-05-22T02:07:37.375493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = \"./\"","metadata":{"execution":{"iopub.status.busy":"2023-05-22T02:07:37.382358Z","iopub.execute_input":"2023-05-22T02:07:37.385319Z","iopub.status.idle":"2023-05-22T02:07:37.392059Z","shell.execute_reply.started":"2023-05-22T02:07:37.385283Z","shell.execute_reply":"2023-05-22T02:07:37.390738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices = list(Y_train.index)\nlabel = list(Y_train)\nfor ind, digit, image in list(zip(indices, label, X_train)):\n    folder_dir = Path(path + str(digit))\n    folder_dir.mkdir(parents=True, exist_ok = True)\n    file_name = f'{ind}.png'\n    file_path = folder_dir / file_name\n    plt.imsave(file_path, image)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T02:07:37.397821Z","iopub.execute_input":"2023-05-22T02:07:37.398613Z","iopub.status.idle":"2023-05-22T02:09:03.769953Z","shell.execute_reply.started":"2023-05-22T02:07:37.398578Z","shell.execute_reply":"2023-05-22T02:09:03.768958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nnew_folder = Path(path) / \"MNISTstuff\"\nnew_folder.mkdir(parents = True, exist_ok = True)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T02:09:03.774888Z","iopub.execute_input":"2023-05-22T02:09:03.777439Z","iopub.status.idle":"2023-05-22T02:09:03.784525Z","shell.execute_reply.started":"2023-05-22T02:09:03.777402Z","shell.execute_reply":"2023-05-22T02:09:03.783478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for n in range(10):\n    source_folder = Path(path) / str(n)\n    shutil.move(source_folder, new_folder)","metadata":{"execution":{"iopub.status.busy":"2023-05-22T02:09:03.789669Z","iopub.execute_input":"2023-05-22T02:09:03.792376Z","iopub.status.idle":"2023-05-22T02:09:03.800274Z","shell.execute_reply.started":"2023-05-22T02:09:03.792343Z","shell.execute_reply":"2023-05-22T02:09:03.799239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnistTrainingPath = Path(path) / \"MNISTstuff\"","metadata":{"execution":{"iopub.status.busy":"2023-05-22T02:09:03.805510Z","iopub.execute_input":"2023-05-22T02:09:03.808134Z","iopub.status.idle":"2023-05-22T02:09:03.814149Z","shell.execute_reply.started":"2023-05-22T02:09:03.808101Z","shell.execute_reply":"2023-05-22T02:09:03.813220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dls = ImageDataLoaders.from_folder(mnistTrainingPath, valid_pct = 0.2, bs = 128)\ndls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2023-05-22T02:09:03.817464Z","iopub.execute_input":"2023-05-22T02:09:03.819198Z","iopub.status.idle":"2023-05-22T02:09:09.870716Z","shell.execute_reply.started":"2023-05-22T02:09:03.819162Z","shell.execute_reply":"2023-05-22T02:09:09.869706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = cnn_learner(dls, resnet34, metrics = [accuracy], loss_func = CrossEntropyLossFlat())","metadata":{"execution":{"iopub.status.busy":"2023-05-22T02:09:09.875288Z","iopub.execute_input":"2023-05-22T02:09:09.877923Z","iopub.status.idle":"2023-05-22T02:09:10.571180Z","shell.execute_reply.started":"2023-05-22T02:09:09.877886Z","shell.execute_reply":"2023-05-22T02:09:10.570172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit_one_cycle(10, slice(1e-2,1e-1),cbs=[ShowGraphCallback()])","metadata":{"execution":{"iopub.status.busy":"2023-05-22T02:09:10.576108Z","iopub.execute_input":"2023-05-22T02:09:10.578667Z","iopub.status.idle":"2023-05-22T02:17:20.298585Z","shell.execute_reply.started":"2023-05-22T02:09:10.578620Z","shell.execute_reply":"2023-05-22T02:17:20.297415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}