HistoCancer

Histopathologic Cancer Detection Identify metastatic tissue in histopathologic scans of lymph node sections

https://www.kaggle.com/c/histopathologic-cancer-detection

In [1]:
%reload_ext autoreload
%autoreload 2
%matplotlib inline
In [2]:
from fastai.vision import *
from fastai.metrics import accuracy
In [3]:
PATH = "/home/katey/DeepLearning/Data/HistoCancer/"
In [4]:
sz = 96
bs = 128
In [5]:
tfms = get_transforms(flip_vert = True)
In [6]:
data = ImageDataBunch.from_csv(path = PATH, folder = 'train', valid_pct = 0.2, 
                               csv_labels = f'{PATH}train_labels.csv',
                               suffix='.tif',ds_tfms=tfms, size=sz).normalize(imagenet_stats)
In [18]:
data.show_batch(rows=5, figsize=(12,10))
In [8]:
learn = create_cnn(data, models.resnet34, metrics=accuracy)

Tune the head of the network

In [9]:
learn.lr_find()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [10]:
learn.recorder.plot()
In [10]:
lr = 1e-3
In [11]:
learn.fit_one_cycle(5, slice(lr))
Total time: 09:43

epoch train_loss valid_loss accuracy
1 0.356286 0.321215 0.864743
2 0.291884 0.247546 0.900261
3 0.247712 0.222897 0.910919
4 0.243428 0.211053 0.916555
5 0.259109 0.207304 0.918487

Tune the entire network

In [12]:
learn.unfreeze()
In [13]:
learn.lr_find()
learn.recorder.plot()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [14]:
learn.fit_one_cycle(5, slice(1e-5, 1e-4))
Total time: 13:59

epoch train_loss valid_loss accuracy
1 0.216757 0.176541 0.932576
2 0.181029 0.143088 0.946665
3 0.176699 0.132459 0.951778
4 0.148086 0.119252 0.956869
5 0.142231 0.115753 0.957709
In [15]:
learn.fit_one_cycle(10, slice(1e-5, 1e-4/2))
Total time: 28:18

epoch train_loss valid_loss accuracy
1 0.131185 0.116179 0.957868
2 0.131398 0.116980 0.958232
3 0.129987 0.114514 0.958255
4 0.117303 0.117869 0.956846
5 0.114479 0.104771 0.963095
6 0.107468 0.106613 0.962981
7 0.102098 0.101647 0.964413
8 0.093999 0.094393 0.966913
9 0.089301 0.092189 0.967685
10 0.081598 0.092440 0.968004
In [19]:
learn.lr_find()
learn.recorder.plot()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [20]:
learn.fit_one_cycle(5, slice(1e-4))
Total time: 14:07

epoch train_loss valid_loss accuracy
1 0.099719 0.098563 0.965140
2 0.100742 0.095237 0.967526
3 0.080233 0.093085 0.968004
4 0.085672 0.089695 0.969231
5 0.079084 0.088749 0.969481
In [21]:
learn.save('mod-resnet34')

Try a larger architecture - Resnet 50

In [22]:
learn = create_cnn(data, models.resnet50, metrics=accuracy)
In [23]:
learn.lr_find()
learn.recorder.plot()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [24]:
lr = 1e-2
In [25]:
learn.fit_one_cycle(5, slice(lr))
Total time: 17:58

epoch train_loss valid_loss accuracy
1 0.262684 0.210577 0.918032
2 0.214478 0.182743 0.938507
3 0.184052 0.154144 0.941802
4 0.169342 0.144003 0.946279
5 0.140668 0.134642 0.950278
In [26]:
learn.unfreeze()
In [27]:
learn.lr_find()
learn.recorder.plot()
LR Finder is complete, type {learner_name}.recorder.plot() to see the graph.
In [28]:
learn.fit_one_cycle(5, slice(1e-5, 1e-4))
Total time: 25:19

epoch train_loss valid_loss accuracy
1 0.143936 0.146590 0.946529
2 0.159401 0.126735 0.953596
3 0.133350 0.114746 0.958005
4 0.114838 0.098947 0.965936
5 0.102861 0.100709 0.964390
In [29]:
learn.save('mod-resnet50')
In [30]:
learn.fit_one_cycle(10, slice(1e-5, 1e-4/2))
Total time: 50:49

epoch train_loss valid_loss accuracy
1 0.106281 0.104538 0.962550
2 0.110408 0.100640 0.964481
3 0.120076 0.111544 0.959596
4 0.108414 0.094156 0.967026
5 0.101646 0.095099 0.967117
6 0.101098 0.089746 0.969208
7 0.075929 0.087267 0.969572
8 0.075743 0.083529 0.971662
9 0.078420 0.081847 0.972140
10 0.079067 0.082096 0.972026
In [31]:
learn.save('mod-resnet50')
In [32]:
interp = ClassificationInterpretation.from_learner(learn)

losses,idxs = interp.top_losses()

len(data.valid_ds)==len(losses)==len(idxs)
Out[32]:
True
In [33]:
interp.plot_top_losses(16, figsize=(12,10))
In [34]:
interp.plot_confusion_matrix(figsize=(3,3), dpi=100)
In [ ]: