-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.py
40 lines (30 loc) · 1.06 KB
/
loader.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
import os
import pickle
import numpy as np
from PIL import Image
import torch
import torchvision.transforms as transforms
class PatchDataset(torch.utils.data.Dataset):
def __init__(self, file_path, transform):
self.transform = transform
with open(file_path, 'rb') as f:
data = pickle.load(f)
self.data_dir = data['base_dir']
self.image_list = data['list']
def __getitem__(self, index):
img_path = os.path.join(self.data_dir, self.image_list[index][0])
label = self.image_list[index][1]
img = Image.open(img_path).convert('RGB')
if self.transform!=None:
img = self.transform(img)
return img, label
def __len__(self):
return len(self.image_list)
def get_weights(self):
num = self.__len__()
labels = np.zeros((num,), np.int)
for s_ind, sample in enumerate(self.image_list):
labels[s_ind] = sample[1]
tmp = np.bincount(labels)
weights = 1.0 / np.asarray(tmp[labels], np.float)
return weights