-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCellsDataset.py
121 lines (99 loc) · 3.61 KB
/
CellsDataset.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
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
from __future__ import print_function, division
import os
import torch
import csv
import pandas as pd
from skimage import io, transform
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from torch.utils.data import Dataset, DataLoader, sampler
from torchvision import transforms, utils
from ImageProcessing import conv2grayFrB, createHeatMap,visualizeNpImage
np.set_printoptions(threshold=np.nan)
class CellsDataset(Dataset):
def __init__(self, root_dir, shuffle=True, transform=None, new = False):
if new:
if shuffle:
csv_file = 'cells_n_landmarks_rand.csv'
else:
csv_file = 'cells_n_landmarks.csv'
else:
if shuffle:
csv_file = 'cells_landmarks_rand.csv'
else:
csv_file = 'cells_landmarks.csv'
self.landmarks_frame = pd.read_csv(csv_file, error_bad_lines=False)
self.root_dir = root_dir
self.transform = transform
self.rowCount = self.landmarks_frame.apply(lambda x: x.count(), axis=1)
self.new = new
def __len__(self):
return len(self.landmarks_frame)
def __getitem__(self, idx):
img_path = os.path.join(self.root_dir, self.landmarks_frame.iloc[idx, 0])
image = io.imread(img_path)
image = conv2grayFrB(image)
if self.new:
dots_path = os.path.join(self.root_dir, convertFileName_n(self.landmarks_frame.iloc[idx, 0]))
else:
dots_path = os.path.join(self.root_dir, convertFileName(self.landmarks_frame.iloc[idx, 0]))
landmarks = io.imread(dots_path)
landmarks = createHeatMap(landmarks, (64,64))
sample = {'image': image, 'landmarks': landmarks}
if self.transform:
sample = self.transform(sample)
return sample
class ToTensor(object):
"""Convert ndarrays in sample to Tensors."""
def __call__(self, sample):
image, landmarks = sample['image'], sample['landmarks']
# swap color axis because
# numpy image: H x W x C
# torch image: C X H X W
image = image.transpose((2, 0, 1))
landmarks = landmarks.transpose ((2,0,1))
newImg = torch.from_numpy(image)
newLmks = torch.from_numpy(landmarks)
return {'image': newImg.float(), 'landmarks': newLmks.float()}
def convertFileName(string1):
string = list(string1)
string[3] = 'd'
string[4] = 'o'
string[5] = 't'
string[6] = 's'
newString = "".join(string)
return newString
def convertFileName_n(string1):
string = list(string1)
string[4] = 'd'
string[5] = 'o'
string[6] = 't'
string[7] = 's'
newString = "".join(string)
return newString
class IndexSampler(sampler.Sampler):
"""Samples elements sequentially from some offset.
Arguments:
num_samples: # of desired datapoints
start: offset where we should start selecting from
"""
def __init__(self, indexes):
self._indexes = indexes
def __iter__(self):
return iter(np.random.permutation(self._indexes))
def __len__(self):
return len(self._indexes)
class ChunkSampler(sampler.Sampler):
"""Samples elements sequentially from some offset.
Arguments:
num_samples: # of desired datapoints
start: offset where we should start selecting from
"""
def __init__(self, num_samples, start=0):
self.num_samples = num_samples
self.start = start
def __iter__(self):
return iter(np.random.permutation(range(self.start, self.start + self.num_samples)))
def __len__(self):
return self.num_samples