-
Notifications
You must be signed in to change notification settings - Fork 1
/
plaque.py
76 lines (65 loc) · 2.41 KB
/
plaque.py
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
import collections
import os.path as osp
import numpy as np
import PIL.Image
import scipy.io
import torch
from torch.utils import data
import torchvision
class Plaqueseg(data.Dataset):
class_names=np.array(['background','plaque'])
#mean_bgr = np.array([104.00698793, 116.66876762, 122.67891434])
def __init__(self, root, split='train', transform=False):
self.root = root
self.split = split
self._transform = transform
dataset_dir = osp.join(self.root, 'unet_xinxueguan')
self.files = collections.defaultdict(list)
for split in ['train', 'val']:
imgsets_file = osp.join(
dataset_dir, 'Segmentation/%s.txt' % split)
for did in open(imgsets_file):
did = did.strip()
img_file = osp.join(dataset_dir, 'IMAGES/%s' % did)
lbl_file = osp.join(
dataset_dir, 'SegmentationClass/%s' % did)
self.files[split].append({
'img': img_file,
'lbl': lbl_file,
})
def __len__(self):
return len(self.files[self.split])
def __getitem__(self, index):
data_file = self.files[self.split][index]
# load image
img_file = data_file['img']
img = PIL.Image.open(img_file)
img = np.array(img, dtype=np.uint8)
# load label
lbl_file = data_file['lbl']
lbl = PIL.Image.open(lbl_file)
lbl = np.array(lbl, dtype=np.int32)
lbl[lbl == 255] = -1
if self._transform:
return self.transform(img, lbl)
else:
return img, lbl
def transform(self, img, lbl):
img = img[:, :, ::-1] # RGB -> BGR
img = img.astype(np.float64)
#img -= self.mean_bgr
dt_trans=torchvision.transforms.Compose([
torchvision.transforms.Normalize(mean=[102.9801,115.9465,122.7717],std=[1.,1.,1.])])
img = img.transpose(2, 0, 1)
img = torch.from_numpy(img).float()
lbl = torch.from_numpy(lbl).float()
img=dt_trans(img)
return img, lbl
def untransform(self, img, lbl):
#img = img.numpy()
#img = img.transpose(1, 2, 0)
#img += self.mean_bgr
#img = img.astype(np.uint8)
#img = img[:, :, ::-1]
lbl = lbl.numpy().astype(int)
return img, lbl