{"cells":[{"metadata":{"_uuid":"328f68be1973bce5ef39bf9214306f42d5eff842"},"cell_type":"markdown","source":" > **<h1> Metastatic Cancer cells in lymph node samples **\n \n **<h2> Contents : **\n   **1. Understanding the problem **\n   \n   **2. Strategy we're gonna take **\n \n    \n   **3. Create the model**"},{"metadata":{"_uuid":"821df8331af12d939ba309e17e94008bc4fe54de"},"cell_type":"markdown","source":"**<h1>Understanding the problem**\n          \n**<h2> Describe the problem**\n       \n  The problem requires us to detect whether or not lymph\n  node sections contain metastatic cancer tissue\n  \n**<h2>What are lymph nodes ?**\n  \n   To understand what lymph nodes are, we need to unerstand what\n   the lymph system is.\n\n **<h3>What is the lymph system ?**\n    \n  ![](https://www.cancer.org/cancer/cancer-basics/lymph-nodes-and-cancer/_jcr_content/par/textimage/image.img.gif/149877$) \n\n  Our bodies have a network of lymph vessels and lymph nodes. (Lymph is pronounced limf.)\n  This network is a part of the body’s immune system.\n  It collects fluid, waste material, and other things (like viruses and bacteria) that are in the body tissues, outside the bloodstream.\n\n  Lymph vessels are a lot like the veins that collect and carry blood through the body.\n  But instead of carrying blood, these vessels carry the clear watery fluid called lymph.\n\n  Lymph fluid flows out from capillary walls to bathe the body’s tissue cells.\n  It carries oxygen and other nutrients to the cells,\n  and carries away waste products like carbon dioxide (CO2) that flow out of the cells.\n  Lymph fluid also contains white blood cells, which help fight infections.\n \n**<h3>What are lymph nodes and what they do ?**\n  \n  Lymph vessels route lymph fluid through nodes throughout the body.\n  Lymph nodes are small structures that work as filters for harmful substances.\n  They contain immune cells that can help fight infection by attacking and destroying germs that are carried in through the \n  lymph fluid."},{"metadata":{"_uuid":"17c7e541aa158e522ae64a935052883f24640ef2"},"cell_type":"markdown","source":"**<h2> What's Metastatic Cancer ?**\n![](https://www.cancer.gov/PublishedContent/Images/images/cancer-types/metastasis-article.__v30025841.jpg)\n\n In metastasis, cancer cells break away from where they first formed (primary cancer),\n travel through the blood or lymph system, and form new tumors (metastatic tumors) in other parts of the body.\n The metastatic tumor is the same type of cancer as the primary tumor.\n\n To Learn more about Metastatic Cancer refer to : [This Link](https://www.cancer.gov/types/metastatic-cancer)\n \n **<h2>How is cancer in lymph nodes found ?**\n \n Normal lymph nodes are tiny and can be hard to find, but when there’s infection, inflammation, or cancer, the nodes can get larger.\n Those near the body’s surface often get big enough to feel with your fingers, and some can even be seen.\n But if there are only a few cancer cells in a lymph node, it may look and feel normal.\n In that case, the doctor must check for cancer by removing all or part of the lymph node.\n \n Doctors take samples of one or more nodes using needles. Usually, this is done on lymph nodes that are enlarged.\n This is called a needle biopsy. The tissue that’s removed is looked at under the microscope by a pathologist (a doctor who diagnoses illness using tissue samples) \n to find out if there are cancer cells in it, but this is where we come in to make models that could classify and help the pathologist with his job."},{"metadata":{"_uuid":"647806496b1ebfc60aec4dd4e53675bf45789caa"},"cell_type":"markdown","source":"Please refer to those references to learn more and more abt this topic\n\nReferences : https://www.cancer.gov/types/metastatic-cancer\n                       <br>   https://www.cancer.org/cancer/cancer-basics/lymph-nodes-and-cancer.html\n"},{"metadata":{"_uuid":"d4e9613fa8f27f87a31f9a38d8c76c33227832c8"},"cell_type":"markdown","source":" **<h1> Strategy we're gonna take**\n\n1. Try Densenet169 w/ 48px imgs\n\n2. Create a Databunch and look at the data\n\n3. Train and finetune \n  \n4. Resize to original size(96px) and create another databunch  \n  \n5. Use the previous pretrained model to train and fine tune again "},{"metadata":{"_uuid":"cffdd19f42b0c31da7c1953d242c4a15933c8320"},"cell_type":"markdown","source":"**<h1>Create the model**"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"#Import Libs\nimport numpy as np \nimport pandas as pd \nfrom fastai import *\nfrom fastai.vision import *\nfrom torchvision.models import *\nimport os \nimport path","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f565bd7990b775da6430eec0dac75e96a3e2f19"},"cell_type":"markdown","source":"1. declare the specification of our model(arch, bs, size,...)\n2. create a dataframe and look at how ur labels are formated \n3. set your transforms \n4. create a databunch (with the declared size but we're gonna increase it and tune the model more) \n5. look at your data and understand it \n6. create a learner \n7. train for a while. after performing lr_find \n8. unfreeze and retrain and once ur happy with the model export it  \n9. increase size \n10. do 4->7->8 again "},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# print(fastai.__version__)\nimgs = os.listdir('../input/train')[:5]\narch = resnet50 \nbs = 64\nsz = 48 #gonna start out with 48x48 px and then gonna move up ro the original size 96x96 px \npath = Path('../input')\nopen_image('../input/train/'+imgs[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1fa706e4b0d0b5a8274d5e41b56277d429878768"},"cell_type":"code","source":"# look at the csv\ndf = pd.read_csv(path/'train_labels.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"00f911c4f143dc7ce17495494089fff1be06e51e"},"cell_type":"code","source":"# Use data_block API to define the default specs for each databunch we create... \ntfms = get_transforms()\nsrc = (ImageItemList.from_csv(path, 'train_labels.csv', folder = 'train', suffix = '.tif')\n      .random_split_by_pct()\n      .label_from_df()\n      .add_test_folder('test'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5d23f74becbf71a8f4b2ea5e9f1a104d300f15de"},"cell_type":"code","source":"np.random.seed(50)\ndata = (src.transform(tfms, size = sz)\n       .databunch().normalize(imagenet_stats)) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"204f0392e4adc5025aa256a4542fb672dd3be93d"},"cell_type":"code","source":"data.show_batch(rows = 3, figsize=(11,12))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fd83cf2212d93e4cd561a6560919537a090feba3"},"cell_type":"code","source":"#thanks to Khoi Nguyen's kernel :https://www.kaggle.com/suicaokhoailang/wip-densenet121-baseline-with-fastai\nfrom 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":"565fb5788aa25512fa38979e5579199a495506ee"},"cell_type":"code","source":"learn = create_cnn(data, arch, metrics = [accuracy, auc_score], model_dir = '/tmp/models/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"187d4bf431703e27b5696d9e9cb0fe3b97cd3c85"},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ae6677b262f2cbec291119a953442767889a391b"},"cell_type":"code","source":"lr = 1e-02 \nlearn.fit_one_cycle(5, slice(lr))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"58cfe382cc802df13df93e68c5ae3141e6b32efa"},"cell_type":"code","source":"learn.save('model-1-rn50')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0720c53a627b04c26ea3a85d8ec471d161be0d83"},"cell_type":"code","source":"learn.unfreeze()\nlearn.lr_find() \nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"08df8f273c72ebdbd3edad4c41af1a97c16958de"},"cell_type":"code","source":"learn.fit_one_cycle(5, slice(1e-05, lr/5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8df42d2c097c20cce265cd91eb216874305a80cd"},"cell_type":"code","source":"learn.save('model-2-rn50')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c95c5c40ef33e873f611cdcc24aad470b249ada"},"cell_type":"code","source":"#increase the size and replace the old data w/ the new one and do transfer learning \ndata = (src.transform(tfms, size = 96)\n       .databunch().normalize(imagenet_stats))\nlearn.data = data \ndata.train_ds[0][0].shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f9bf4f9664cdc40231fce2181559bf56327174a9"},"cell_type":"code","source":"learn.freeze()\nlearn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"scrolled":true,"_uuid":"2dea31918b2512ae18589fee70c4d045bbb61c87"},"cell_type":"code","source":"lr = 1e-03 \nlearn.fit_one_cycle(5, slice(lr))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"39f8e31956a2908a37bcbc4e493da6da4a1b1ac7"},"cell_type":"code","source":"learn.save('model-big-1-rn50')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"631c1c203d0b94a28f24d61219776556b276505e"},"cell_type":"code","source":"learn.unfreeze()\nlearn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"46c8a1e0b4a5e0ca618f61d75cc6983638a3bb69"},"cell_type":"code","source":"learn.fit_one_cycle(5, slice(3e-06, lr/5))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2c0572a9d0e84abf8682976cf5f8106c937e29cd"},"cell_type":"code","source":"learn.save('model-big-2-rn50')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6160d98be99f857da55a4b5b1b373457b4b66e75"},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\nlosses,idxs = interp.top_losses()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74ed3ee40317681d4f19dd0b612e3ed0a0cca69d"},"cell_type":"code","source":"losses,idxs = interp.top_losses()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dfaa3a0adcd1791d8726ae2da3e2bca095c70734"},"cell_type":"code","source":"interp.plot_confusion_matrix(figsize=(12,12), dpi=60)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"96a4f37308cb73e1a94084950e70396529451260"},"cell_type":"code","source":"preds,y = learn.TTA()\nacc = accuracy(preds, y)\nprint('The validation accuracy is {} %.'.format(acc * 100))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9d41b92b8d1402157c6ac1aada2d266afcbe2060"},"cell_type":"code","source":"pred_score = auc_score(preds,y).item()\nprint('The validation AUC is {}.'.format(pred_score))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"84402b43d0fc2650df89e3693efc72ab9630de0a"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a0b57392533084dbcb9a3c0db3664647468f3c7e"},"cell_type":"code","source":"df1 = pd.read_csv(path/'sample_submission.csv')\nid_list = list(df1.id)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"90d73c5dd1d0deb4394c14614a5995f8fe3c718b"},"cell_type":"code","source":"preds,y = learn.TTA(ds_type=DatasetType.Test)\npred_list = list(preds[:,1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9c6cc9981c472783635be7589ce9773c5dfe5dd2"},"cell_type":"code","source":"pred_dict = dict((key, value.item()) for (key, value) in zip(learn.data.test_ds.items,pred_list))\npred_ordered = [pred_dict[Path('../input/test/' + id + '.tif')] for id in id_list]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cdb61cb46a225e2bd385da15d9d0269f479ee113"},"cell_type":"code","source":"submissions = pd.DataFrame({'id':id_list,'label':pred_ordered})\nsubmissions.to_csv(\"submission_{}.csv\".format(pred_score),index = False)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d7ca10b5b17f636a1f0276c3a79db9ca5e3d4705"},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"296c469d06e70aa0615b6c444f4b54e365501281"},"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}