File size: 15,778 Bytes
3757e50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import numpy as np
import pandas as pd
from PIL import Image
import os

import torch
import utilities
from skimage.transform import resize

import albumentations as A
from albumentations.pytorch import ToTensorV2
import cv2
## -----------------------------------------------------------------------------------------------------------------##
##                                                          CHEST DATASET                                           ##
## -----------------------------------------------------------------------------------------------------------------##
"""
LINK: https://www.kaggle.com/datasets/nikhilpandey360/chest-xray-masks-and-labels

X-ray images in this data set have been acquired from the tuberculosis control program of the Department of Health and Human Services of Montgomery County, MD, USA. 
This set contains 138 posterior-anterior x-rays, of which 80 x-rays are normal and 58 x-rays are abnormal with manifestations of tuberculosis. 
All images are de-identified and available in DICOM format. The set covers a wide range of abnormalities, including effusions and miliary patterns.
"""
class Chest(torch.utils.data.Dataset):

    def __init__(self, prefix, phase, size=(512, 512), num_channels=1, fuse_heatmap=False, sigma=8):
        self.phase = phase
        self.new_size = size
        self.dataset_name = 'Chest'

        self.transforms = self.get_transforms()
        self.num_channels = num_channels
        self.fuse_heatmap = fuse_heatmap
        self.sigma = sigma
        self.num_landmarks = 6
        self.pth_Image = os.path.join(prefix, 'pngs')
        self.pth_Label = os.path.join(prefix, 'labels')

        # file index
        files = [i[:-4] for i in sorted(os.listdir(self.pth_Image))]

        exclude_list = ['CHNCXR_0059_0', 'CHNCXR_0178_0', 'CHNCXR_0228_0', 'CHNCXR_0267_0', 'CHNCXR_0295_0', 'CHNCXR_0310_0', 'CHNCXR_0285_0', 'CHNCXR_0276_0', 'CHNCXR_0303_0']
        if exclude_list is not None:
            st = set(exclude_list)
            files = [f for f in files if f not in st]

        n = len(files)
        train_num = 195  
        val_num = 34 
        test_num = n - train_num - val_num
        if self.phase == 'train':
            self.indexes = files[:train_num]
        elif self.phase == 'validate':
            self.indexes = files[train_num:-test_num]
        elif self.phase == 'test':
            self.indexes = files[-test_num:]
        elif self.phase == 'all':
            self.indexes = files
        else:
            raise Exception("Unknown phase: {phase}".format(phase=phase))

    def __getitem__(self, index):
        name = self.indexes[index]
        ret = {'name': name}

        img, img_size= self.readImage(os.path.join(self.pth_Image, name + '.png'))
        points = self.readLandmark(name)        
        heatmaps = utilities.points_to_heatmap(points, sigma=self.sigma, img_size=self.new_size, fuse=self.fuse_heatmap)

        transformed = self.transforms(image=img, masks=heatmaps)
        
        # img shape: CxHxW | heatmaps is a list of CxHxW: example: [CxHxW, CxHxW, CxHxW, CxHxW, CxHxW, CxHxW]
        img, heatmaps = transformed['image'], transformed['masks']
        
        # Image is a torch tensor [C, H, W]
        ret['image'] = img
        ret['landmarks'] = torch.FloatTensor(points)
        # Convert heatmaps to torch tensor [C, H, W]. Stack to give new dimension and float32 type to avoid error in loss function
        ret['heatmaps'] = torch.stack([hm.float() for hm in heatmaps])
        ret['original_size'] = torch.FloatTensor(img_size)
        ret['resized_size'] = torch.FloatTensor(self.new_size)

        return ret

    def __len__(self):
        return len(self.indexes)

    def readLandmark(self, name):
        path = os.path.join(self.pth_Label, name + '.txt')
        points = []
        with open(path, 'r') as f:
            n = int(f.readline())
            for i in range(n):
                ratios = [float(i) for i in f.readline().split()]
                points.append(ratios)
        return np.array(points)
    
    def readImage(self, path):

        if self.num_channels == 3:
            img = Image.open(path).convert('RGB')
            arr = np.array(img).astype(np.float32)
 
        elif self.num_channels == 1:
            img = Image.open(path).convert('L')
            arr = np.array(img).astype(np.float32)
            arr = np.expand_dims(arr, 2)
        else:
            raise ValueError('Channels must be either 1 or 3')

        # Original size in (width, height)
        origin_size = img.size
        resized_image = resize(arr, (self.new_size[0], self.new_size[1], self.num_channels))

        return resized_image, origin_size

    def get_transforms(self):
        if self.phase == 'train':
            return A.Compose([
            A.ShiftScaleRotate(shift_limit=0.02, scale_limit=0, rotate_limit=2, border_mode=cv2.BORDER_REPLICATE, p=0.5),
            #A.Perspective(scale=(0, 0.02), pad_mode=cv2.BORDER_REPLICATE, p=0.5),
            #A.RandomBrightnessContrast(brightness_limit=(-0.1, 0.1), contrast_limit=(-0.2, 0.2), p=0.5),
            #A.Resize(self.new_size[0], self.new_size[1]),
            A.Normalize(normalization='min_max'),
            A.pytorch.ToTensorV2()
        ])
        elif self.phase == 'validate':
            return A.Compose([
            #A.Resize(self.new_size[0], self.new_size[1]),
            A.Normalize(normalization='min_max'),
            A.pytorch.ToTensorV2()
            ])
        elif self.phase == 'test':
            return A.Compose([
            #A.Resize(self.new_size[0], self.new_size[1]),
            A.Normalize(normalization='min_max'),
            A.pytorch.transforms.ToTensorV2()
            ])
        else:
            raise ValueError('phase must be either "train" or "validate" or "test"')

## -----------------------------------------------------------------------------------------------------------------##
##                                                          HAND DATASET                                            ##
## -----------------------------------------------------------------------------------------------------------------##

"""
LINK: https://ipilab.usc.edu/research/baaweb/
ASI: Asian; BLK: African American; CAU: Caucasian; HIS: Hispanic.
"""

class Hand(torch.utils.data.Dataset):

    def __init__(self, prefix, phase, size=(512, 368), num_channels=1, fuse_heatmap=False, sigma=5):

        self.phase = phase
        self.new_size = size
        self.dataset_name = 'Hand'
        
        self.transforms = self.get_transforms()
        self.num_channels = num_channels
        self.fuse_heatmap = fuse_heatmap
        self.sigma = sigma
        self.num_landmarks = 37

        self.pth_Image = os.path.join(prefix, 'jpg')
        self.labels = pd.read_csv(os.path.join(
            prefix, 'labels/all.csv'), header=None, index_col=0)

        # file index
        index_set = set(self.labels.index) # Set of all the labels
        files = [i[:-4] for i in sorted(os.listdir(self.pth_Image))] # -4 to cut ".jpg" # List of all the images
        files = [i for i in files if int(i) in index_set] # List of filters that has a label

        n = len(files)
        train_num = 550
        val_num = 59
        test_num = n - train_num - val_num

        if phase == 'train':
            self.indexes = files[:train_num]
        elif phase == 'validate':
            self.indexes = files[train_num:-test_num]
        elif phase == 'test':
            self.indexes = files[-test_num:]
        elif phase == 'all':
            self.indexes = files
        else:
            raise Exception("Unknown phase: {phase}".format(phase=phase))

    def __getitem__(self, index):
        name = self.indexes[index]
        ret = {'name': name}

        img, img_size = self.readImage(
            os.path.join(self.pth_Image, name + '.jpg'))

        points = self.readLandmark(name, img_size)
        heatmaps = utilities.points_to_heatmap(points, sigma=self.sigma, img_size=self.new_size, fuse=self.fuse_heatmap)

        transformed = self.transforms(image=img, masks=heatmaps)
        img, heatmaps = transformed['image'], transformed['masks']
        
        ret['image'] = img
        ret['landmarks'] = torch.FloatTensor(points)
        ret['heatmaps'] = torch.stack([hm.float() for hm in heatmaps])
        ret['original_size'] = torch.FloatTensor(img_size)
        ret['resized_size'] = torch.FloatTensor(self.new_size)

        return ret
    

    def __len__(self):
        return len(self.indexes)

    def readLandmark(self, name, origin_size):
        li = list(self.labels.loc[int(name), :])   
        points = []
        for i in range(0, len(li), 2):
            ratios = (li[i] / origin_size[0], li[i + 1] / origin_size[1])
            points.append(ratios)
        return np.array(points)

    def readImage(self, path):
        
        if self.num_channels == 3:
            img = Image.open(path).convert('RGB')
            arr = np.array(img).astype(np.float32)
 
        elif self.num_channels == 1:
            img = Image.open(path).convert('L')
            arr = np.array(img).astype(np.float32)
            arr = np.expand_dims(arr, 2)
        else:
            raise ValueError('Channels must be either 1 or 3')
            
        # Original size in (width, height)
        origin_size = img.size
        resized_image = resize(arr, (self.new_size[0], self.new_size[1], self.num_channels))

        return resized_image, origin_size
    
    def get_transforms(self):
        if self.phase == 'train':
            return A.Compose([
            A.ShiftScaleRotate(shift_limit=0.02, scale_limit=(-0.02, 0.02), rotate_limit=2, border_mode=cv2.BORDER_REPLICATE, p=0.5),
            #A.Perspective(scale=(0, 0.02), pad_mode=cv2.BORDER_REPLICATE, p=0.5),
            #A.RandomBrightnessContrast(brightness_limit=(-0.1, 0.1), contrast_limit=(-0.2, 0.2), p=0.5),
            #A.Resize(self.new_size[0], self.new_size[1]),
            A.Normalize(normalization='min_max'),
            A.pytorch.ToTensorV2()
        ])
        elif self.phase == 'validate':
            return A.Compose([
            #A.Resize(self.new_size[0], self.new_size[1]),
            A.Normalize(normalization='min_max'),
            A.pytorch.ToTensorV2()
            ])
        elif self.phase == 'test':
            return A.Compose([
            #A.Resize(self.new_size[0], self.new_size[1]),
            A.Normalize(normalization='min_max'),
            A.pytorch.transforms.ToTensorV2()
            ])
        else:
            raise ValueError('phase must be either "train" or "validate" or "test"')



## -----------------------------------------------------------------------------------------------------------------##
##                                                          CEPHALOMETRIC DATASET                                            ##
## -----------------------------------------------------------------------------------------------------------------##

"""
LINK: https://www.kaggle.com/datasets/c34a0ef0cd3cfd5c5afbdb30f8541e887171f19f196b1ad63790ca5b28c0ec93
https://figshare.com/s/37ec464af8e81ae6ebbf?file=5466581
"""


class Cephalo(torch.utils.data.Dataset):

    def __init__(self, prefix, phase, size=(512, 416), num_channels=1, fuse_heatmap=False, sigma=5):
        self.phase = phase
        self.new_size = size
        self.dataset_name = 'Cephalo'
        
        self.transforms = self.get_transforms()
        self.num_channels = num_channels
        self.fuse_heatmap = fuse_heatmap
        self.sigma = sigma
        
        self.num_landmarks = 19


        self.pth_Image = os.path.join(prefix, 'jpg')
        self.pth_label_junior = os.path.join(prefix, '400_junior')
        self.pth_label_senior = os.path.join(prefix, '400_senior')

        # file index
        files = [i[:-4] for i in sorted(os.listdir(self.pth_Image))]
        n = len(files)
        
        if phase == 'train':
            self.indexes = files[:130]
        elif phase == 'validate':
            self.indexes = files[130:150]
        elif phase == 'test':
            self.indexes = files[150:400]
        elif phase == 'all':
            self.indexes = files
        else:
            raise Exception("Unknown phase: {phase}".format(phase=phase))


    def __getitem__(self, index):
        name = self.indexes[index]
        ret = {'name': name}

        img, img_size = self.readImage(os.path.join(self.pth_Image, name+'.jpg'))
        points = self.readLandmark(name, img_size)
        heatmaps = utilities.points_to_heatmap(points, sigma=self.sigma, img_size=self.new_size, fuse=self.fuse_heatmap)

        transformed = self.transforms(image=img, masks=heatmaps)        
        img, heatmaps = transformed['image'], transformed['masks']
        
        ret['image'] = img
        ret['landmarks'] = torch.FloatTensor(points)
        ret['heatmaps'] = torch.stack([hm.float() for hm in heatmaps])
        ret['original_size'] = torch.FloatTensor(img_size)
        ret['resized_size'] = torch.FloatTensor(self.new_size)

        return ret

    def __len__(self):
        return len(self.indexes)

    def readLandmark(self, name, origin_size):
        points = []
        with open(os.path.join(self.pth_label_junior, name + '.txt')) as f1:
            with open(os.path.join(self.pth_label_senior, name + '.txt')) as f2:
                for i in range(self.num_landmarks):
                    landmark1 = f1.readline().rstrip('\n').split(',')
                    landmark2 = f2.readline().rstrip('\n').split(',')
                    # Average of junior and senior landmarks
                    landmark = [(float(i) + float(j)) / 2 for i, j in zip(landmark1, landmark2)]
                    #landmark = [float(i) for i in landmark1] 
                    ratios = (landmark[0] / origin_size[0], landmark[1] / origin_size[1])
                    points.append(ratios)
        return np.array(points)

    def readImage(self, path):

        if self.num_channels == 3:
            img = Image.open(path).convert('RGB')
            arr = np.array(img).astype(np.float32)
 
        elif self.num_channels == 1:
            img = Image.open(path).convert('L')
            arr = np.array(img).astype(np.float32)
            arr = np.expand_dims(arr, 2)
        else:
            raise ValueError('Channels must be either 1 or 3')

        # Original size in (width, height)
        origin_size = img.size
        resized_image = resize(arr, (self.new_size[0], self.new_size[1], self.num_channels))
        
        return resized_image, origin_size
    
    def get_transforms(self):
        if self.phase == 'train':
            return A.Compose([
            A.ShiftScaleRotate(shift_limit=0.02, scale_limit=(-0.02, 0.02), rotate_limit=2, border_mode=cv2.BORDER_REPLICATE, p=0.5),
            #A.Perspective(scale=(0, 0.02), pad_mode=cv2.BORDER_REPLICATE, p=0.5),
            #A.RandomBrightnessContrast(brightness_limit=(-0.1, 0.1), contrast_limit=(-0.2, 0.2), p=0.5),
            #A.Resize(self.new_size[0], self.new_size[1]),
            A.Normalize(normalization='min_max'),
            A.pytorch.ToTensorV2()
        ])
        elif self.phase == 'validate':
            return A.Compose([
            #A.Resize(self.new_size[0], self.new_size[1]),
            A.Normalize(normalization='min_max'),
            A.pytorch.ToTensorV2()
            ])
        elif self.phase == 'test':
            return A.Compose([
            #A.Resize(self.new_size[0], self.new_size[1]),
            A.Normalize(normalization='min_max'),
            A.pytorch.transforms.ToTensorV2()
            ])
        else:
            raise ValueError('phase must be either "train" or "validate" or "test"')