{"cells":[{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nfrom torchvision.models import * \n\nimport os\nimport pathlib\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7c9e4ace51c50e13af6533168054a21656d860d1"},"cell_type":"code","source":"#data = pd.read_csv('/kaggle/input/train_labels.csv')\n#train_path = '/kaggle/input/train/'\n#test_path = '/kaggle/input/test/'\n# quick look at the label stats\n#data['label'].value_counts()\n\npath = Path(\"../input\")\nlabels = pd.read_csv(path/\"train_labels.csv\")\nlabels.head()\n\n#path_w =Path(\"../directory\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7ed10abc6ebe9cda27665c393801051e62f21629"},"cell_type":"code","source":"ls","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9e7e2a57b6285d946ce2b1d4351c140221bcf1cf"},"cell_type":"code","source":"print(labels[\"label\"].nunique()); classes = list(set(labels[\"label\"])); classes\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c756c55a199a0ee9a31f95778d59a299269ded90"},"cell_type":"code","source":"for i in classes:\n    print(\"Number of items in class {} is {}\".format(i,len(labels[labels[\"label\"] == i])))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c48d7fe76ffb7d81a8ef95eae900de64531d9bc"},"cell_type":"code","source":"tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=.0, max_zoom=.1,\n                      max_lighting=0.05, max_warp=0.)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f926985f1f9299f5ff70b3883f8113216ace140a"},"cell_type":"code","source":"np.random.seed(123)\nsz = 32\ndata = ImageDataBunch.from_csv(path, folder = 'train', csv_labels = \"train_labels.csv\",\n                               test = 'test',suffix=\".tif\", size = sz,bs = 128,\n                               ds_tfms = tfms)\n#data.path = pathlib.Path('.')\ndata.normalize(imagenet_stats)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fb6d9e595eaa0734ba279ed817c10ce94aa4a10c"},"cell_type":"code","source":"print(data.classes); data.c","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2650ab6b49fc3b1592a9015bb7d1d48de67211e7"},"cell_type":"code","source":"data.show_batch(rows=3, figsize=(5,5))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"303687730592375c9c3e354b6d2b0e4fc2c78c40"},"cell_type":"markdown","source":"Model"},{"metadata":{"trusted":true,"_uuid":"d18793e8c88ef04a3fdcc9794121494051d1f592"},"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\n\ndef auc_score(y_pred,y_true,tens=True):\n    score = roc_auc_score(y_true,torch.sigmoid(y_pred)[:,1])\n    if tens:\n        score = tensor(score)\n    return score","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fc13b50b1fdb408bdaecd88aa23076900077de44"},"cell_type":"code","source":"arch = models.densenet169\nlearn = create_cnn(data,arch,pretrained = True,ps = 0.5,metrics = [auc_score,accuracy], model_dir='/tmp/models')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a8d593e9ff600b5ba6c1c346cc35091a1a3cd120"},"cell_type":"code","source":"# learn.save('stage-0')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"05cc30faafd611f01b4970b7bf46972207db5815"},"cell_type":"code","source":"# #learn.lr_find()\n# learn.lr_find()\n# #lr_find??","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5cd7b6e608b232cc2d7865e86fe493929862ace1"},"cell_type":"code","source":"# learn.recorder.plot(learn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1ab03370393952239e993ab95d1a31c0202d010c"},"cell_type":"code","source":"# lr = 0.001\n# learn.fit_one_cycle(1, slice(lr))\n# learn.save('stage-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c476a281abbd16a329dcacb431f74100fe293098"},"cell_type":"code","source":"# learn.load('stage-1')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"856e8655f0aad5bcda19fd256ee1914d8317ab65"},"cell_type":"code","source":"# learn.unfreeze()\n# learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3c04b764ff1121b487c5fffad456bb1b696e3dca"},"cell_type":"code","source":"# learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cd58de94e295d2c3ca52b4002d936028ce02f10a"},"cell_type":"code","source":"# learn.fit_one_cycle(3,max_lr = slice(5e-2,1e-3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cfcee4541b19358fd8c08129e2e456cf26cb3267"},"cell_type":"code","source":"# interp = ClassificationInterpretation.from_learner(learn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1f128cdee087aab179d48963c23171536f5c8791"},"cell_type":"code","source":"# interp.plot_top_losses(9)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c6fee0d531fadb515ceee68914d9edfa26bf23f5"},"cell_type":"code","source":"# interp.plot_confusion_matrix()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8ea897911423a56a9460d9d0f716f06d5309da88"},"cell_type":"markdown","source":"Transform learning"},{"metadata":{"trusted":true,"_uuid":"9bee71e27ffb37374519fb9089d7205f3c2b37a5"},"cell_type":"code","source":"# newTfms = get_transforms(do_flip = True,flip_vert = True,max_zoom = 1.5)\n# newSz = 96\n# newData = ImageDataBunch.from_csv(path, folder = 'train', csv_labels = \"train_labels.csv\",\n#                                test = 'test',suffix=\".tif\", size = newSz, ds_tfms = newTfms)\n# newData.path = pathlib.Path('.')\n# newData.normalize(imagenet_stats)\n# learn.data = newData","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a515ea1b9631753339f86b3ccad32cccbebad1a6"},"cell_type":"code","source":"# learn.freeze()\n# learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a00c9f39688bcde9ca70e98b645e03b7a09473d0"},"cell_type":"code","source":"# learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2e9926169ea48962b4b1076cfa15710a6ebe01e1"},"cell_type":"code","source":"#learn.fit_one_cycle(5,max_lr = slice(5e-3,5e-5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ae09cb7d615fdb61928809111cbe58f30504b212"},"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}