from fastai.vision.all import *
from fastai.medical.imaging import *
import torchvision
import torchvision.transforms as t
Pneumothorax demo
= untar_data(URLs.SIIM_SMALL) pneumothorax_source
= get_dicom_files(pneumothorax_source/f"train/") items
= pd.read_csv(pneumothorax_source/f"labels.csv")
df df.head()
file | label | |
---|---|---|
0 | train/No Pneumothorax/000000.dcm | No Pneumothorax |
1 | train/Pneumothorax/000001.dcm | Pneumothorax |
2 | train/No Pneumothorax/000002.dcm | No Pneumothorax |
3 | train/Pneumothorax/000003.dcm | Pneumothorax |
4 | train/Pneumothorax/000004.dcm | Pneumothorax |
= df[df['label']=='Pneumothorax'] df_pneumo
= DataBlock(blocks=(ImageBlock(cls=PILDicom), MaskBlock(codes=['bg','pneumo'])),
pneumothorax =lambda x:pneumothorax_source/f"{x[0]}",
get_x=lambda x:'example/siim/'+Path(x[0]).stem+'.png',
get_y=aug_transforms(do_flip=False,p_affine=0,p_lighting=0,size=224))
batch_tfms
= pneumothorax.dataloaders(df_pneumo.values, num_workers=0, bs=16) dls
=16, vmin=0) dls.show_batch(max_n
# just for testing - you can skip this when running interactively
= unet_learner(dls, resnet34) learn
1) learn.fine_tune(
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 1.382017 | 0.309487 | 01:04 |
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 0.167802 | 0.111701 | 00:51 |
# this is skipped when testing, instead a single epoch is done (not sufficient for overfitting)
= unet_learner(dls, resnet34)
learn 8) learn.fine_tune(
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 2.200016 | 0.416453 | 01:05 |
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 0.195540 | 0.105748 | 00:51 |
1 | 0.148728 | 0.141623 | 00:51 |
2 | 0.126741 | 0.176495 | 00:54 |
3 | 0.116757 | 0.106115 | 00:57 |
4 | 0.106740 | 0.093054 | 02:16 |
5 | 0.098731 | 0.140762 | 01:49 |
6 | 0.092059 | 0.120493 | 02:27 |
7 | 0.086470 | 0.116142 | 01:48 |
=0,max_n=9,vmin=0) learn.show_results(ds_idx
= learn.get_preds(ds_idx=0) preds
1][2]) plt.imshow(preds[
<matplotlib.image.AxesImage>
0].argmax(dim=1).sum()
preds[#plt.imshow(preds[0].argmax(dim=1)[0])
TensorBase(0)
8) learn.fine_tune(
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 0.061920 | 2.473048 | 01:03 |
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 0.314660 | 0.080977 | 01:54 |
1 | 0.208264 | 0.088037 | 00:55 |
2 | 0.160613 | 0.078349 | 00:54 |
3 | 0.134872 | 0.086172 | 00:52 |
4 | 0.113790 | 0.082046 | 00:52 |
5 | 0.100933 | 0.077185 | 00:53 |
6 | 0.090635 | 0.089535 | 01:10 |
7 | 0.083492 | 0.089540 | 01:10 |
learn.recorder.plot_loss()
= learn.get_preds(ds_idx=0)
preds 0].argmax(dim=1).sum() preds[
TensorBase(0)
= plt.subplots(5,2,figsize=(8,16))
fig, axs for i in range(5):
0].imshow(preds[1][i])
axs[i,1].imshow(preds[0][i][1])
axs[i,print(preds[0][i][1].max())
TensorBase(0.3102)
TensorBase(0.3205)
TensorBase(0.3076)
TensorBase(0.2694)
TensorBase(0.0728)
8) learn.fit_one_cycle(
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 0.043738 | 0.085497 | 01:06 |
1 | 0.041399 | 0.126955 | 00:53 |
2 | 0.039557 | 0.166106 | 00:52 |
3 | 0.038748 | 0.160928 | 00:55 |
4 | 0.036368 | 0.123756 | 00:52 |
5 | 0.034367 | 0.111612 | 00:52 |
6 | 0.032606 | 0.130679 | 00:52 |
7 | 0.030831 | 0.122198 | 00:53 |
= learn.get_preds(ds_idx=0)
preds 0].argmax(dim=1).sum() preds[
TensorBase(14126)
20) learn.fit_one_cycle(
epoch | train_loss | valid_loss | time |
---|---|---|---|
0 | 0.023082 | 0.114249 | 00:56 |
1 | 0.022736 | 0.110707 | 00:53 |
2 | 0.021745 | 0.209588 | 00:52 |
3 | 0.022693 | 0.105819 | 01:18 |
4 | 0.024324 | 0.151829 | 00:49 |
5 | 0.024436 | 0.093544 | 00:49 |
6 | 0.024203 | 0.123490 | 00:48 |
7 | 0.023524 | 0.166477 | 00:50 |
8 | 0.022349 | 0.262088 | 00:55 |
9 | 0.021310 | 0.254958 | 01:41 |
10 | 0.020904 | 0.199529 | 02:09 |
11 | 0.020301 | 0.199600 | 00:58 |
12 | 0.019917 | 0.186603 | 01:07 |
13 | 0.019124 | 0.135860 | 00:52 |
14 | 0.018472 | 0.152205 | 00:49 |
15 | 0.017754 | 0.178598 | 00:52 |
16 | 0.017116 | 0.187399 | 00:56 |
17 | 0.016518 | 0.185505 | 00:56 |
18 | 0.015890 | 0.184788 | 00:57 |
19 | 0.015337 | 0.181746 | 00:57 |
= learn.get_preds(ds_idx=0)
preds 0].argmax(dim=1).sum() preds[
TensorBase(41177)
= plt.subplots(5,3,figsize=(12,16))
fig, axs for i in range(5):
0].imshow(preds[1][i], interpolation="nearest")
axs[i,1].imshow(preds[0][i][1])
axs[i,2].imshow(preds[0].argmax(dim=1)[i], interpolation="nearest") axs[i,
'pneumothorx') learn.save(
Path('models/pneumothorx.pth')
We managed to overfit
'pneumothorx') learn.load(
<fastai.learner.Learner>
from misas.core import *
from fastai.vision.all import * #open_mask, Image, ImageSegment
import pydicom
def read_dcm(file):
= pydicom.dcmread(file)
ds = Tensor(ds.pixel_array.astype(np.int16))
img = img/img.max()
img = t.ToPILImage()
transform = transform (torch.stack([img, img, img]))
img return img
1])[0].show(vmin=0) learn.predict(learn.dls.train_ds.items[
<AxesSubplot:>
= df_pneumo.iloc[3]['file']
fname = lambda: read_dcm(pneumothorax_source/fname)
img = lambda: Image.open('example/siim/'+Path(fname).stem+'.png') #open_mask('example/siim/'+Path(fname).stem+'.png') trueMask
= img() dcm
dcm#to_image(dcm.data)
= learn.predict(PILDicom(dcm))[1]
pred plt.imshow(pred)
<matplotlib.image.AxesImage>
class Fastai2_wrapper:
def __init__(self, model):
self.model = model
self.model.cbs = L([])
def imageToPILDicom(self, image):
return PILDicom(image)
def prepareSize(self, imageOrMask):
return imageOrMask.resize((224,224))
def predict(self, image):
= self.imageToPILDicom(image)
image = self.model.predict(image)[0]#(in_image)
pred return Image.fromarray(np.array(pred).astype(np.uint8))
= Fastai2_wrapper(learn) model
plt.imshow(model.predict(img()))
<matplotlib.image.AxesImage>
plot_series(get_rotation_series(img(), model))
= eval_rotation_series(img(), trueMask(), model, start=-180, end=180, components=['bg','pneumo']) result
'deg'],result['pneumo']) plt.plot(result[