johnfelipe2012 commited on
Commit
94f4aa2
1 Parent(s): 090adfd

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import torch
3
+ import torch.nn as nn
4
+ import torchio as tio
5
+ import numpy as np
6
+ from tqdm.notebook import tqdm
7
+ import gradio as gr
8
+ from matplotlib import pyplot as plt
9
+
10
+ torch.set_grad_enabled(False);
11
+ # Download an example image
12
+ import urllib
13
+ url, filename = ("https://github.com/mateuszbuda/brain-segmentation-pytorch/raw/master/assets/TCGA_CS_4944.png", "TCGA_CS_4944.png")
14
+ try: urllib.URLopener().retrieve(url, filename)
15
+ except: urllib.request.urlretrieve(url, filename)
16
+ def inference(img):
17
+ path = img.name
18
+ slices = [tio.ScalarImage(path).data]
19
+ tensor = torch.cat(slices, dim=-1)
20
+ guessed_affine = np.diag([-1, -1, 9, 1])
21
+ subject = tio.Subject(mri=tio.ScalarImage(tensor=tensor, affine=guessed_affine))
22
+ subject_preprocessed = tio.ZNormalization()(subject)
23
+ subject_preprocessed.plot()
24
+ subject_preprocessed.mri
25
+ patch_overlap = 0
26
+ patch_size = 256, 256, 1
27
+ grid_sampler = tio.inference.GridSampler(
28
+ subject_preprocessed,
29
+ patch_size,
30
+ patch_overlap,
31
+ )
32
+ patch_loader = torch.utils.data.DataLoader(grid_sampler, batch_size=8)
33
+ aggregator = tio.inference.GridAggregator(grid_sampler)
34
+ model = torch.hub.load(
35
+ 'mateuszbuda/brain-segmentation-pytorch',
36
+ 'unet',
37
+ in_channels=3,
38
+ out_channels=1,
39
+ init_features=32,
40
+ pretrained=True,
41
+ )
42
+ for patches_batch in tqdm(patch_loader):
43
+ input_tensor = patches_batch['mri'][tio.DATA][..., 0]
44
+ locations = patches_batch[tio.LOCATION]
45
+ probs = model(input_tensor)[..., np.newaxis]
46
+ aggregator.add_batch(probs, locations)
47
+ output_tensor = aggregator.get_output_tensor()
48
+ output_subject = tio.Subject(prediction=tio.ScalarImage(tensor=output_tensor, affine=guessed_affine))
49
+ images = subject_preprocessed.mri.tensor.detach().numpy().reshape((3, 256, 256))
50
+ mask = output_subject.prediction.tensor.detach().numpy().reshape((256, 256))
51
+ images = np.moveaxis(np.moveaxis(images, 0, 2), 0, 1)
52
+ mask = np.moveaxis(mask, 0, 1)
53
+
54
+ f, ax = plt.subplots(1, 2)
55
+ ax[0].set_axis_off()
56
+ ax[1].set_axis_off()
57
+ ax[0].imshow(images)
58
+ ax[1].imshow(mask, cmap='gray')
59
+ return f
60
+
61
+ title = "U-NET FOR BRAIN MRI"
62
+ description = "Gradio demo for u-net for brain mri, U-Net with batch normalization for biomedical image segmentation with pretrained weights for abnormality segmentation in brain MRI. To use it, simply add your image, or click one of the examples to load them. Read more at the links below."
63
+ article = "<p style='text-align: center'><a href='https://mateuszbuda.github.io/2017/12/01/brainseg.html'>Segmentation of brain tumor in magnetic resonance images</a> | <a href='https://github.com/mateuszbuda/brain-segmentation-pytorch'>Github Repo</a></p>"
64
+ examples = [
65
+ ['TCGA_CS_4944.png']
66
+ ]
67
+ gr.Interface(inference, gr.inputs.Image(label="input image", type='file'), gr.outputs.Image(type='plot'), description=description, article=article, title=title, examples=examples, analytics_enabled=False).launch()