{"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":"markdown","source":"### Step 0: Install autogluon via pip and import necessary packages \n\nMake sure you select the GPU accelerator","metadata":{}},{"cell_type":"code","source":"%%capture\n!pip install autogluon.vision --pre\n!pip install mxnet-cu110","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:54:21.5798Z","iopub.execute_input":"2021-06-22T20:54:21.580117Z","iopub.status.idle":"2021-06-22T20:54:56.044132Z","shell.execute_reply.started":"2021-06-22T20:54:21.580087Z","shell.execute_reply":"2021-06-22T20:54:56.043133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport logging\nlogging.basicConfig(level=logging.INFO)\n# attach logging to console\nlogging.getLogger().addHandler(logging.StreamHandler())","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-06-22T20:54:56.047596Z","iopub.execute_input":"2021-06-22T20:54:56.047874Z","iopub.status.idle":"2021-06-22T20:54:56.05312Z","shell.execute_reply.started":"2021-06-22T20:54:56.047845Z","shell.execute_reply":"2021-06-22T20:54:56.052368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Enter the competition!\n### Step 1: import autogluon","metadata":{}},{"cell_type":"code","source":"import autogluon.core as ag\nfrom autogluon.vision import ImagePredictor","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:54:56.055332Z","iopub.execute_input":"2021-06-22T20:54:56.055885Z","iopub.status.idle":"2021-06-22T20:55:00.336234Z","shell.execute_reply.started":"2021-06-22T20:54:56.055848Z","shell.execute_reply":"2021-06-22T20:55:00.33537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Step 2: Apply customization to competition data","metadata":{}},{"cell_type":"code","source":"def fix_root(df, root='/kaggle/input/classify-leaves'):\n    df['image'] = df['image'].apply(lambda x: os.path.join(root, x))\n    return df\n\n# load train data\ntrain_data = fix_root(pd.read_csv('/kaggle/input/classify-leaves/train.csv'))","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:55:00.337685Z","iopub.execute_input":"2021-06-22T20:55:00.33819Z","iopub.status.idle":"2021-06-22T20:55:00.41087Z","shell.execute_reply.started":"2021-06-22T20:55:00.33815Z","shell.execute_reply":"2021-06-22T20:55:00.4101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictor = ImagePredictor(verbosity=1).fit(train_data, time_limit=3600*3, presets='medium_quality_faster_train', hyperparameters={'epochs': 30, 'model': 'resnest50', 'lr': ag.Categorical(0.1, 0.15)}) \nprint(predictor.fit_summary())","metadata":{"execution":{"iopub.status.busy":"2021-06-22T20:55:29.40598Z","iopub.execute_input":"2021-06-22T20:55:29.406324Z","iopub.status.idle":"2021-06-22T23:16:47.655857Z","shell.execute_reply.started":"2021-06-22T20:55:29.406293Z","shell.execute_reply":"2021-06-22T23:16:47.655013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Step 3: Test","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('/kaggle/input/classify-leaves/test.csv')\ntest","metadata":{"execution":{"iopub.status.busy":"2021-06-22T23:16:47.659588Z","iopub.execute_input":"2021-06-22T23:16:47.661339Z","iopub.status.idle":"2021-06-22T23:16:47.700051Z","shell.execute_reply.started":"2021-06-22T23:16:47.6613Z","shell.execute_reply":"2021-06-22T23:16:47.699358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_test = predictor.predict(fix_root(test.copy()))\n# save prediction with image id\ny = pd.concat([test, pred_test.to_frame(name='label')], axis=1)\ny.to_csv('submission_test.csv', index=False)\ny","metadata":{"execution":{"iopub.status.busy":"2021-06-22T23:16:47.703652Z","iopub.execute_input":"2021-06-22T23:16:47.71555Z","iopub.status.idle":"2021-06-22T23:18:26.66612Z","shell.execute_reply.started":"2021-06-22T23:16:47.715518Z","shell.execute_reply":"2021-06-22T23:18:26.665182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Last step: save model and submission file","metadata":{}},{"cell_type":"code","source":"predictor.save('predictor.ag')","metadata":{"execution":{"iopub.status.busy":"2021-06-22T23:40:19.881489Z","iopub.execute_input":"2021-06-22T23:40:19.881995Z","iopub.status.idle":"2021-06-22T23:40:20.914303Z","shell.execute_reply.started":"2021-06-22T23:40:19.881954Z","shell.execute_reply":"2021-06-22T23:40:20.913387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}