-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathdataset.py
51 lines (43 loc) · 1.46 KB
/
dataset.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
from torchvision import transforms
from torch.utils.data import Dataset
from PIL import Image
import numpy as np
class MDataset(Dataset):
def __init__(self, data):
self.transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.244, 0.225]),
])
self.paths = []
self.labels = []
for x, y in data:
self.paths.append(x)
self.labels.append(y)
def __len__(self):
return len(self.paths)
def labelcount(self, minlength=0):
return np.bincount(self.labels, minlength=minlength)
def undersample(self):
labelcount = self.labelcount()
count = [0 for i in labelcount]
new_paths = []
new_labels = []
for i in range(len(self)):
path = self.paths[i]
label = self.labels[i]
if count[label] < labelcount.min():
new_labels.append(label)
new_paths.append(path)
count[label] += 1
self.paths = new_paths
self.labels = new_labels
def __getitem__(self, idx):
path = self.paths[idx]
try:
img = Image.open(path).convert('RGB')
except BaseException:
img = Image.new('RGB', (256, 256))
tensor_img = self.transform(img)
label = self.labels[idx]
return tensor_img, label, idx