-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdist_loader_pkl_multi.py
executable file
·78 lines (66 loc) · 2.25 KB
/
cdist_loader_pkl_multi.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
import torch.utils.data as data
from PIL import Image
import os
import os.path
# from __future__ import print_function, division
# import os
# import torch
# import pandas as pd
# from skimage import io, transform
# import numpy as np
# import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
import pickle as pkl
import numpy as np
# from torchvision import transforms, utils
# # Ignore warnings
# import warnings
# warnings.filterwarnings("ignore")
def pil_loader(path):
# open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
with open(path, 'rb') as f:
with Image.open(f) as img:
return img.convert('RGB')
# def accimage_loader(path):
# import accimage
# try:
# return accimage.Image(path)
# except IOError:
# # Potentially a decoding problem, fall back to PIL.Image
# return pil_loader(path)
def default_loader(path):
# from torchvision import get_image_backend
# if get_image_backend() == 'accimage':
# return accimage_loader(path)
# else:
return pil_loader(path)
class CDiscountDatasetMy(Dataset):
"""Face Landmarks dataset."""
def __init__(self, root, pkl_name, transform=None, loader=default_loader):
with open(pkl_name,'rb') as f_pkl:
self.imgs=pkl.load(f_pkl)
self.root = root
#self.classes = range(num_classes)
self.transform = transform
self.loader = loader
self.idx2cls=pkl.load(open('/home/dereyly/data_raw/multi_idx2cls.pkl','rb'))
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is class_index of the target class.
"""
fname, target = self.imgs[index]
# target=int(target)
path=self.root+'/' +fname
img = self.loader(path)
if self.transform is not None:
img = self.transform(img)
#maybe is better to not convert into numpy
target_multi=[target[0],target[0]]
for k in range(self.idx2cls.shape[0]):
target_multi.append(self.idx2cls[k][target[0]])
return img, target_multi #np.array(target,int)
def __len__(self):
return len(self.imgs)