-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyDataset.py
58 lines (47 loc) · 2.04 KB
/
MyDataset.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
from torch.utils.data import DataLoader, Dataset
from PIL import Image
import pandas as pd
import os
root_info = '/home/xutengfei/meisai2021'
root_img = '/home/xutengfei/meisai2021/imgs'
def default_loader(path):
# ANTIALIAS:high quality
return Image.open(path).resize((224, 224), Image.ANTIALIAS).convert('RGB')
class MyDataset(Dataset):
"""MyDataset for read garbage_cls data.
"""
def __init__(self, root_info=root_info, root_img=root_img, transform=None, target_transform=None,
loader=default_loader, negative_num=7):
self.root_info = root_info
self.root_img = root_img
self.transform = transform
self.target_transform = target_transform
self.loader = loader
imgs = []
df_filename_status = pd.read_csv(os.path.join(root_info, 'info.csv'))
df_filename_status.sample(frac=1) # shuffle the dataframe ,so every time we get different negatives
nid = 0
for index, row in df_filename_status.iterrows():
if df_filename_status.loc[index, 'Lab Status'] == 'Positive ID':
imgs.append((df_filename_status.loc[index, 'FileName'], 1))
elif df_filename_status.loc[index, 'Lab Status'] == 'Negative ID' and df_filename_status.loc[index, 'FileName'].endswith(('.jpg', '.png')) and nid < negative_num:
imgs.append((df_filename_status.loc[index, 'FileName'], 0))
nid = nid+1
self.imgs = imgs
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is class_index of the target class.
"""
path, target = self.imgs[index]
# the returned "path" is a relative one
img = self.loader(os.path.join(root_img, path))
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
return len(self.imgs)