-
Notifications
You must be signed in to change notification settings - Fork 7
/
dataset.py
127 lines (96 loc) · 4.32 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
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
120
121
122
123
124
125
126
127
import os
import numpy as np
from PIL import Image
import torch
import torchvision
import torchvision.datasets
from torchvision import transforms
from torch.utils.data import Dataset
import config as cfg
class LT_Dataset(Dataset):
num_classes = cfg.num_classes
def __init__(self, root, txt, transform=None):
self.img_path = []
self.targets = []
self.transform = transform
with open(root+txt) as f:
for line in f:
self.img_path.append(os.path.join(root, line.split()[0]))
self.targets.append(int(line.split()[1]))
cls_num_list_old = [np.sum(np.array(self.targets) == i) for i in range(self.num_classes)]
# generate class_map: class index sort by num (descending)
sorted_classes = np.argsort(-np.array(cls_num_list_old))
self.class_map = [0 for i in range(self.num_classes)]
for i in range(self.num_classes):
self.class_map[sorted_classes[i]] = i
self.targets = np.array(self.class_map)[self.targets].tolist()
self.class_data = [[] for i in range(self.num_classes)]
for i in range(len(self.targets)):
j = self.targets[i]
self.class_data[j].append(i)
self.cls_num_list = [np.sum(np.array(self.targets)==i) for i in range(self.num_classes)]
def __len__(self):
return len(self.targets)
def __getitem__(self, index):
path = self.img_path[index]
target = self.targets[index]
with open(path, 'rb') as f:
sample = Image.open(f).convert('RGB')
if self.transform is not None:
sample = self.transform(sample)
return sample, target
class LT_Dataset_Eval(Dataset):
num_classes = cfg.num_classes
def __init__(self, root, txt, class_map, transform=None):
self.img_path = []
self.targets = []
self.transform = transform
self.class_map = class_map
with open(root+txt) as f:
for line in f:
self.img_path.append(os.path.join(root, line.split()[0]))
self.targets.append(int(line.split()[1]))
self.targets = np.array(self.class_map)[self.targets].tolist()
self.cls_num_list = [np.sum(np.array(self.targets)==i) for i in range(self.num_classes)]
def __len__(self):
return len(self.targets)
def __getitem__(self, index):
path = self.img_path[index]
target = self.targets[index]
with open(path, 'rb') as f:
sample = Image.open(f).convert('RGB')
if self.transform is not None:
sample = self.transform(sample)
return sample, target
class Food_LT(object):
def __init__(self, distributed, root="", batch_size=60, num_works=40):
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
transform_train = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(brightness=0.4, contrast=0.4, saturation=0.4, hue=0),
transforms.ToTensor(),
normalize
])
transform_test = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize
])
''' change to your index path '''
train_txt = "./data/food/train.txt"
eval_txt = "./data/food/val.txt"
train_dataset = LT_Dataset(root, train_txt, transform=transform_train)
eval_dataset = LT_Dataset_Eval(root, eval_txt, transform=transform_test, class_map=train_dataset.class_map)
self.cls_num_list = train_dataset.cls_num_list
self.val_num_list = eval_dataset.cls_num_list
self.dist_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset) if distributed else None
self.train_instance = torch.utils.data.DataLoader(
train_dataset,
batch_size=batch_size, shuffle=True,
num_workers=num_works, pin_memory=True, sampler=self.dist_sampler)
self.eval = torch.utils.data.DataLoader(
eval_dataset,
batch_size=batch_size, shuffle=False,
num_workers=num_works, pin_memory=True)