{"cells":[{"metadata":{"_uuid":"e6c94bbe-d265-4b8d-8ce7-fd0b08626f5b","_cell_guid":"960dbc7d-3328-4c79-bf5c-27f8d7f2228f","trusted":true},"cell_type":"markdown","source":"Breast Cancer Detection\n"},{"metadata":{"_uuid":"975a080d-f563-4e0f-8e3d-ba7b0e60b501","_cell_guid":"e3d37583-d264-40a4-9fdb-3a0c7f3b00a0","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n\nimport os\nfrom fastai.vision import *\nfrom fastai import *\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom functools import partial\nfrom tqdm.notebook import tqdm\nimport gc\nfrom pylab import imread,subplot,imshow,show\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1713271e-7937-4486-8d83-80d5d74ff4b9","_cell_guid":"ccd187a2-0507-424d-ae2b-570561d2a081","trusted":true},"cell_type":"code","source":"path = \"/kaggle/input/breast-cancer-image-data/project/\"","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d3b29578-cdb1-4a4f-b6ee-cc862e3f7fd0","_cell_guid":"f6654e55-7b67-40b1-b8d1-7d4fd3627155","trusted":true},"cell_type":"code","source":"size = 224\nbs = 64\ndata = ImageDataBunch.from_folder(path, \n                                  ds_tfms=get_transforms(max_rotate=0.1,max_lighting=0.15),\n                                  valid_pct=0.2, \n                                  size=size, \n                                  bs=bs)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b616044c-24ae-4473-885e-e9ecab3fed7d","_cell_guid":"e4b05a36-f3e5-4960-96c6-e8057293933d","trusted":true},"cell_type":"code","source":"data.show_batch(rows=4)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"53c8eaab-b327-4788-8503-7b07334de3e4","_cell_guid":"819ea26b-9ea5-4af5-8846-4b43f72cfedb","trusted":true},"cell_type":"markdown","source":"### Total classes, length of train, validation and test set"},{"metadata":{"_uuid":"7da599cd-2855-459a-ac5e-594be85f88a8","_cell_guid":"47cc004b-aa24-49c7-98f9-825b91060892","trusted":true},"cell_type":"code","source":"len(data.classes)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4836d6ad-69a9-41ae-a83a-a8a48214d9f3","_cell_guid":"9e0ef864-1a60-468e-b141-74ad6d50bf63","trusted":true},"cell_type":"markdown","source":"# Using a pretrained ResNet50 model\n\n## with metrics = f1_score \n>average = macro \n\n\n* because there is class imbalance in the data set"},{"metadata":{"_uuid":"dabcf9eb-bd8e-4acc-8408-8ac41509ee9b","_cell_guid":"30783042-fdf6-4a20-a8c5-ee8ae459f159","trusted":true},"cell_type":"code","source":"fb = FBeta()\nfb.average='macro'","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8757c32d-9f22-48c9-8a2a-836e429bdd54","_cell_guid":"689a9ba4-4e93-4f3f-b606-7416938379ab","trusted":true},"cell_type":"code","source":"arch = models.resnet50","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"84cb0d10-089e-45a2-b428-c45bb52bc0c9","_cell_guid":"3f4c6bb6-54de-42e2-9a0e-8bd80966316a","trusted":true},"cell_type":"code","source":"#!mkdir -p /tmp/.cache/torch/checkpoints/\n#!cp /kaggle/input/resnet50/resnet50.pth  /root/.cache/torch/checkpoints/resnet50-19c8e357.pth","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"58e04883-f64b-4ff7-8fd0-37bbd63f0519","_cell_guid":"63035e8e-0bff-42f9-ab95-e6b7223c558d","trusted":true},"cell_type":"markdown","source":"# Using mixed precision training\n\n* Mixed precision training utilizes half-precision to speed up training, achieving the same accuracy in some cases as single-precision training using the same hyper-parameters. \n* Memory requirements are also reduced, allowing larger models and minibatches"},{"metadata":{"_uuid":"e6d3c721-2744-4672-b3c1-4e131961b26e","_cell_guid":"60c2bed6-d56d-4957-b9c8-fb7b17d53d7b","trusted":true},"cell_type":"code","source":"try:\n    learn = cnn_learner(data, arch, metrics = [fb],model_dir='/kaggle/working').to_fp32()\nexcept:\n    !mkdir -p /tmp/.cache/torch/checkpoints/\n    !cp /kaggle/input/resnet50/resnet50.pth  /root/.cache/torch/checkpoints/resnet50-19c8e357.pth\n    \n    learn = cnn_learner(data, arch, metrics = [fb],model_dir='/kaggle/working').to_fp32()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d2da64ac-1c97-4f02-a950-60e88b7e3c56","_cell_guid":"0a2c6c5e-a032-493d-b1df-16579b60660a","trusted":true},"cell_type":"code","source":"learn.lr_find()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"04d3cc91-23a6-4865-8bd4-c630f548b5f9","_cell_guid":"0a23b1f0-65d3-4a94-9034-21dd7c6e8e70","trusted":true},"cell_type":"code","source":"learn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4177be87-e52a-4033-a0b7-ff5119b8654d","_cell_guid":"869ce110-204f-4e24-a8e7-94c49eaee5dd","trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7a2b69c4-b03c-46fa-bac3-4d7c44291f96","_cell_guid":"bb2aecca-1200-4078-b3e9-a0873cb38dcd","trusted":true},"cell_type":"markdown","source":"# Model Summary"},{"metadata":{"_uuid":"b3beb051-0655-4203-8a31-2f56f174063d","_cell_guid":"6fe8b253-16d5-48d8-9711-235cf6dcf34e","trusted":true},"cell_type":"code","source":"learn.summary()\n","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1bdb9c44-82f8-4a42-a60b-560c55164cbe","_cell_guid":"a0cffdf7-7fc9-4e1d-b1de-24ac4dd1ce86","trusted":true},"cell_type":"code","source":"lr = 1e-2","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ac57f507-fbe2-4377-b12c-a04a1c949619","_cell_guid":"66773ce9-7c82-46c7-b608-2584526e558f","trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f900909c-ec60-42b3-8241-18fbf04e88ca","_cell_guid":"907268d5-9f37-4e8a-9cac-7f1c81bd352f","trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(6,lr,moms=(0.9,0.8))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"37bbca51-5861-4f62-8bd5-915596f2fc18","_cell_guid":"9834aaba-ec27-4fe0-900b-5423cdcf8c44","trusted":true},"cell_type":"markdown","source":"# Classification Interpretation"},{"metadata":{"_uuid":"b7884a20-5f15-41bd-be11-7ec5465e1ea0","_cell_guid":"bcf9c3a0-f154-42ea-bfcd-f5e6b75fa234","trusted":true},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)\ninterp.plot_confusion_matrix(title='Confusion matrix')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"322d0a25-ca11-4dc9-8213-b9daec987763","_cell_guid":"23f7cba3-0ec0-4662-98ad-60b5dd824208","trusted":true},"cell_type":"code","source":"interp.plot_top_losses(12,figsize=(20,8))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"194c20a2-4b3f-43b3-905c-84e32c7eb498","_cell_guid":"b2ec55ae-bbd6-44c3-a83d-9d5c139af826","trusted":true},"cell_type":"markdown","source":"# Most Confused"},{"metadata":{"_uuid":"63ca46ba-5189-4ce1-8875-c4cdd68d3dfb","_cell_guid":"6d53f399-44df-4647-bf90-2d033214895b","trusted":true},"cell_type":"code","source":"interp.most_confused(min_val=3)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c32cc8aa-eda3-4525-abfd-c4d2b32928fb","_cell_guid":"d76fbb33-e861-4c82-af3a-55c34acaa937","trusted":true},"cell_type":"code","source":"learn.save('model1')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"e9a5e794-618d-408d-9a34-006da3d8a875","_cell_guid":"e17560b1-ae1b-4e2d-89a2-144f776ea364","trusted":true},"cell_type":"code","source":"learn.export('/kaggle/working/breast.pkl')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7bd26807-06ea-4a13-8a1c-cbfe27fb6856","_cell_guid":"512ade04-bb6e-4d58-8880-80e2205b571f","trusted":true},"cell_type":"code","source":"img = image.open('/kaggle/input/breast-cancer-image-data/project/malignant/mdb023.jpg')\nprint(learn.predict(img)[0])\n\nimg","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"01551855-8850-49dd-bcd3-c03dd25900bc","_cell_guid":"d81063e0-fbe4-4bbe-b9d0-810f45d03cdf","trusted":true},"cell_type":"code","source":"learn.show_results()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c52e71da-01b0-4429-836d-8fbdfc9a7414","_cell_guid":"f1eda7aa-35b1-4d5d-9ef5-3a7c0a8e024d","trusted":true},"cell_type":"code","source":"idx=0\nx,y=data.valid_ds[idx]\nx.show()\ny","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"965333e7-3126-49dd-9f22-cc3cc2602bf4","_cell_guid":"a3caf5c2-f99c-46e0-ac19-5114bff16b57","trusted":true},"cell_type":"code","source":"preds,y = learn.TTA()\n#preds,y, loss = learn.get_preds(with_loss=True)\n# get accuracy\nacc = accuracy(preds, y)\nprint('The accuracy is {0} %.'.format(acc))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#probs = np.mean(np.exp(log_preds),axis=0);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#out_arr = np.arange(3)\n#probs = np.mean(log_preds,axis=0,out = out_arr)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"log_preds,y","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"4192a111-6024-4b1e-b131-6c6f89afa6d9","_cell_guid":"f9fb4c0c-09c0-4e26-96bf-122553474c9d","trusted":true},"cell_type":"code","source":"accuracy_np(probs, y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_confusion_matrix(title='Confusion matrix')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}