{"cells":[{"metadata":{},"cell_type":"markdown","source":"### If it was helpful please upvote "},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","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":{"trusted":true},"cell_type":"code","source":"path = \"/kaggle/input/breast-cancer-image-data/project/\"","execution_count":null,"outputs":[]},{"metadata":{"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)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.show_batch(rows=4)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Total classes, length of train, validation and test set"},{"metadata":{"trusted":true},"cell_type":"code","source":"len(data.classes)","execution_count":null,"outputs":[]},{"metadata":{},"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":{"trusted":true},"cell_type":"code","source":"fb = FBeta()\nfb.average='macro'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"arch = models.resnet50","execution_count":null,"outputs":[]},{"metadata":{"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":{},"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":{"trusted":true},"cell_type":"code","source":"try:\n    learn = cnn_learner(data, arch, metrics = [fb],model_dir='/kaggle/working').to_fp16()\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_fp16()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.lr_find()\nlearn.recorder.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Model Summary"},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"lr = 1e-2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.fit_one_cycle(6,lr,moms=(0.9,0.8))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Classification Interpretation"},{"metadata":{"trusted":true},"cell_type":"code","source":"interp = ClassificationInterpretation.from_learner(learn)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.plot_top_losses(12,figsize=(20,8))","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Most Confused"},{"metadata":{"trusted":true},"cell_type":"code","source":"interp.most_confused(min_val=3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.save('model1')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"learn.export('/kaggle/working/breast.pkl')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = open_image('/kaggle/input/breast-cancer-image-data/project/benin/mdb002.jpg')\nprint(learn.predict(img)[0])\nimg","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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":1}