{"cells":[{"metadata":{"_uuid":"2cc32ef9bba8300433885bf63496e728e48aabd1"},"cell_type":"markdown","source":"[Reference]\n* https://www.kaggle.com/guntherthepenguin/fastai-v1-densenet169"},{"metadata":{"trusted":true,"_uuid":"eda84cac535b233620a5a521476896b92ab59b43"},"cell_type":"code","source":"!ls ../input","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import fastai\nfrom fastai.imports import *\nfrom fastai.vision import *\nfrom fastai.metrics import *\nfrom fastai.gen_doc.nbdoc import *\nprint('fast.ai version:{}'.format(fastai.__version__))","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"WORK_DIR = os.getcwd()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"444540e3e26dc7d0ee2bf0bd282047073eb9b4da"},"cell_type":"code","source":"show_doc(get_transforms)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bef8b66503a08a277e545a034a90a45b76f0750b"},"cell_type":"code","source":"show_doc(ImageDataBunch.from_csv)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5b985c3c8183d1c2735a80737bdcd10ca3a1d5c8"},"cell_type":"code","source":"param_transforms = {\n    'flip_vert':True,\n    'max_rotate':90,\n    'max_zoom':1,\n    'max_warp':None,\n    'max_lighting':None,\n}\nparams = {'path': Path('../input/histopathologic-cancer-detection'),\n          'csv_labels':'train_labels.csv',\n          'ds_tfms':get_transforms(**param_transforms),\n          'suffix':'.tif',\n          'folder':'train',\n          'test':'test',\n          'size':32,\n          'bs':128,\n          'num_workers':0,\n         }\ndata = ImageDataBunch.from_csv(**params).normalize(imagenet_stats)\ndata","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"460dd818beee7fc33eae9ae654e150dd0d901fc6"},"cell_type":"code","source":"data.show_batch(rows=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ecd9b589132a49afec5db035d637d3fcc0ed98b0"},"cell_type":"code","source":"learn = create_cnn(data, models.resnet101, metrics=accuracy, path='.', model_dir='.')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d84f3997339ab792eca3a3a34509aa816a527a23"},"cell_type":"code","source":"!ls ../input/dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"622841b6c380f5b3ca1367ffad46aa026ffe3fa0"},"cell_type":"code","source":"learn.load('../input/dataset/histopathologic_cancer_detection')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b4bddb0295b756555f09c01998e739a4e6b1cfd4","scrolled":true},"cell_type":"code","source":"learn.unfreeze()\n# learn.fit_one_cycle(16)\nlearn.fit_one_cycle(16,slice(1e-6,1e-4))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"abcb5a83fab1560a86cd879ed6c9981f94508548"},"cell_type":"code","source":"learn.save('histopathologic_cancer_detection')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e25a5c6c1ecd16b7cf2b0a8cc0458e8188e8927"},"cell_type":"code","source":"learn.recorder.plot_losses()\nplt.ylim([0,0.5])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4ade8fc06046c1e54a37d77239f166c1c8845c47"},"cell_type":"code","source":"# Check if DatasetType loaded\n# It's depend on difference between fast.ai version v1.0.39/v1.0.36.\ntry:\n    DatasetType\nexcept:\n    from fastai import DatasetType","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"439616f809f25a1e68ed342b141b4ee677df4f53"},"cell_type":"code","source":"try:\n    # TTA cause error @fast.ai v1.0.39\n    preds, _ = learn.TTA(ds_type=DatasetType.Test)\n    print('Predict w/ test time augument done.')\nexcept:\n    preds, _ = learn.get_preds(DatasetType.Test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"872974503d55f879beed2b59635a20616ed53e84"},"cell_type":"code","source":"preds","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e105b3662c0db331deb25965d258794101578b9"},"cell_type":"code","source":"ids = [file.stem for file in data.test_ds.x.items]\nids[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f045c2749386a30c3002f8996de52d3b7e24e9e4"},"cell_type":"code","source":"data.classes","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"863ad291fd10319fb7f1768be96d3b8efbcdbf6a"},"cell_type":"code","source":"df=pd.read_csv('../input/histopathologic-cancer-detection/sample_submission.csv').set_index('id')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a6ba67643175d8d3f65840a42071f2f996690c04"},"cell_type":"code","source":"df.loc[ids,'label'] = preds[:,1].numpy()\ndf = df.reset_index()\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c7762f5a74949c3fd3fb79f54d00a13b7b43f1ad"},"cell_type":"code","source":"df.to_csv('submission.csv', index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2fe68e7b299795e44b4f2fa30137f863f9974f5e"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}