{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install git+https://github.com/fastai/fastai.git\n!pip install efficientnet-pytorch`","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nfrom efficientnet_pytorch import EfficientNet\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"path = '/kaggle/working/cassava-leaf-disease-classification/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nfrom time import sleep\nimport pandas as pd\nfrom fastai.vision.all import *\nfrom fastai.callback.tracker import SaveModelCallback\ndf = pd.read_csv(path + 'train.csv')\ndf_train = df.sample(int(0.7*len(df)))\ndf_test = df[~df.image_id.isin(df_train.image_id)] \ncaminho = path + \"train_images/\"\ndls = ImageDataLoaders.from_df(df, path=caminho, item_tfms=Resize(224))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%cd /kaggle/working","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir /kaggle/working/models\n\n!cp ../input/cassava-leaf-disease-models/* /kaggle/working/models/\n!cp -R ../input/cassava-leaf-disease-classification/ /kaggle/working/cassava-leaf-disease-classification\n!ls /kaggle/working/models/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pwd\n\n!ls","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = EfficientNet.from_name('efficientnet-b0')\nlearn = Learner(dls, model, metrics=[error_rate,accuracy],path='./')\n#learn.load('EfNetB0_275_16')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr = 0.02089296132326126\nlearn.fine_tune(2,lr)\nlearn.unfreeze()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('efficientnet')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Testando sequencia\n* sem unfreeze\n* one cycle\n* lr_find\n* unfreeze\n* one cycle"},{"metadata":{"trusted":true},"cell_type":"code","source":"model = EfficientNet.from_name('efficientnet-b0')\nlearn = Learner(dls, model, metrics=[error_rate,accuracy],path='./')\nlearn.fit_one_cycle(2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('modelefficient')\nlearn.unfreeze()\nlearn.fit_one_cycle(20, slice(6.30957365501672e-06,1.5848931980144698e-06))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('modelefficient')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Testando sequencia\n* sem unfreeze\n* fit"},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.callback.tracker import SaveModelCallback","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"save_callback = SaveModelCallback(fname='best_model_eff', with_opt=True, monitor='valid_loss', reset_on_fit=False) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = EfficientNet.from_name('efficientnet-b0')\nlearn = Learner(dls, model, metrics=[error_rate,accuracy],path='./')\nlearn.fit_one_cycle(2,cbs=save_callback)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.load('best_model_eff')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = EfficientNet.from_name('efficientnet-b0')\nlearn = Learner(dls, model, metrics=[error_rate,accuracy],path='./')\nlearn.fit_one_cycle(18,cbs=save_callback)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.load('best_model_eff')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(20,cbs=save_callback)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Procurando um melhor learning rate**"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Ajustando o learning rate mínimo e máximo e treinando com fine_tune"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(4,slice(1.0964782268274575e-05,1.0964781722577754e-06),cbs=save_callback)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(16,slice(1.0964782268274575e-05,1.0964781722577754e-06),cbs=save_callback)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('best_model_eff3')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Exportação"},{"metadata":{"trusted":true},"cell_type":"code","source":"img = dls.train_ds[0][0]\nlearn.predict(img)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission_df = pd.read_csv(path + 'sample_submission.csv')\ncaminho = path + 'test_images'\ntest_data_path = submission_df['image_id'].apply(lambda x: caminho + '/' + x)\ntst_dl = learn.dls.test_dl(test_data_path)\npredictions = learn.tta(dl = tst_dl, n=10)\n\nsubmission_df['label'] = np.argmax(predictions[0],axis=1)\nsubmission_df.to_csv('submission.csv',index=False)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}