thov commited on
Commit
1df7042
1 Parent(s): 194f0a4

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +10 -0
  2. app.py +0 -0
  3. requirements.txt +6 -0
  4. src/medicalDataLoader.py +113 -0
Dockerfile ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:latest
2
+
3
+ WORKDIR /usr/src/app
4
+
5
+ COPY . .
6
+
7
+ RUN /usr/local/bin/python -m pip install --upgrade pip
8
+ RUN pip install -r requirements.txt
9
+
10
+ CMD ["python", "./app.py"]
app.py ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ numpy
2
+ matplotlib
3
+ tqdm
4
+ torch
5
+ torchmetrics
6
+ segmentation_models_pytorch
src/medicalDataLoader.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import print_function, division
2
+ import os
3
+ import torch
4
+ import pandas as pd
5
+ from skimage import io, transform
6
+ import numpy as np
7
+ from torch.utils.data import Dataset, DataLoader
8
+ from torchvision import transforms, utils
9
+ from PIL import Image, ImageOps
10
+ from random import random, randint
11
+
12
+ import warnings
13
+ warnings.filterwarnings("ignore")
14
+
15
+ def make_dataset(root, mode):
16
+ assert mode in ['train','val', 'test']
17
+ items = []
18
+
19
+ if mode == 'train':
20
+ train_img_path = os.path.join(root, 'train', 'Img')
21
+ train_mask_path = os.path.join(root, 'train', 'GT')
22
+
23
+ images = os.listdir(train_img_path)
24
+ labels = os.listdir(train_mask_path)
25
+
26
+ images.sort()
27
+ labels.sort()
28
+
29
+ for it_im, it_gt in zip(images, labels):
30
+ item = (os.path.join(train_img_path, it_im), os.path.join(train_mask_path, it_gt))
31
+ items.append(item)
32
+
33
+
34
+ elif mode == 'val':
35
+ val_img_path = os.path.join(root, 'val', 'Img')
36
+ val_mask_path = os.path.join(root, 'val', 'GT')
37
+
38
+ images = os.listdir(val_img_path)
39
+ labels = os.listdir(val_mask_path)
40
+
41
+ images.sort()
42
+ labels.sort()
43
+
44
+ for it_im, it_gt in zip(images, labels):
45
+ item = (os.path.join(val_img_path, it_im), os.path.join(val_mask_path, it_gt))
46
+ items.append(item)
47
+ else:
48
+ test_img_path = os.path.join(root, 'test', 'Img')
49
+ test_mask_path = os.path.join(root, 'test', 'GT')
50
+
51
+ images = os.listdir(test_img_path)
52
+ labels = os.listdir(test_mask_path)
53
+
54
+ images.sort()
55
+ labels.sort()
56
+
57
+ for it_im, it_gt in zip(images, labels):
58
+ item = (os.path.join(test_img_path, it_im), os.path.join(test_mask_path, it_gt))
59
+ items.append(item)
60
+
61
+ return items
62
+
63
+
64
+ class MedicalImageDataset(Dataset):
65
+ """Face Landmarks dataset."""
66
+
67
+ def __init__(self, mode, root_dir, transform=None, mask_transform=None, augment=False, equalize=False):
68
+ """
69
+ Args:
70
+ root_dir (string): Directory with all the images.
71
+ transform (callable, optional): Optional transform to be applied
72
+ on a sample.
73
+ """
74
+ self.root_dir = root_dir
75
+ self.transform = transform
76
+ self.mask_transform = mask_transform
77
+ self.imgs = make_dataset(root_dir, mode)
78
+ self.augmentation = augment
79
+ self.equalize = equalize
80
+ self.mode = mode
81
+
82
+ def __len__(self):
83
+ return len(self.imgs)
84
+
85
+ def augment(self, img, mask):
86
+ if random() > 0.5:
87
+ img = ImageOps.flip(img)
88
+ mask = ImageOps.flip(mask)
89
+ if random() > 0.5:
90
+ img = ImageOps.mirror(img)
91
+ mask = ImageOps.mirror(mask)
92
+ if random() > 0.5:
93
+ angle = random() * 60 - 30
94
+ img = img.rotate(angle)
95
+ mask = mask.rotate(angle)
96
+ return img, mask
97
+
98
+ def __getitem__(self, index):
99
+ img_path, mask_path = self.imgs[index]
100
+ img = Image.open(img_path)
101
+ mask = Image.open(mask_path).convert('L')
102
+
103
+ if self.equalize:
104
+ img = ImageOps.equalize(img)
105
+
106
+ if self.augmentation:
107
+ img, mask = self.augment(img, mask)
108
+
109
+ if self.transform:
110
+ img = self.transform(img)
111
+ mask = self.mask_transform(mask)
112
+
113
+ return [img, mask, img_path]