-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdataset.py
170 lines (127 loc) · 4.86 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
import cv2
import numpy as np
from torch.utils.data import Dataset
from PIL import Image
class CUB_200_2011_Train(Dataset):
def __init__(self, path, transform=None, target_transform=None):
self.root = path
self.transform = transform
self.target_transform = target_transform
self.images_path = {}
with open(os.path.join(self.root, 'images.txt')) as f:
for line in f:
image_id, path = line.split()
self.images_path[image_id] = path
self.class_ids = {}
with open(os.path.join(self.root, 'image_class_labels.txt')) as f:
for line in f:
image_id, class_id = line.split()
self.class_ids[image_id] = class_id
self.train_id = []
with open(os.path.join(self.root, 'train_test_split.txt')) as f:
for line in f:
image_id, is_train = line.split()
if int(is_train) and self.class_ids[image_id]=='1':# only use the first class
self.train_id.append(image_id)
def __len__(self):
return len(self.train_id)
def __getitem__(self, index):
"""
Args:
index: index of training dataset
Returns:
image and its corresponding label
"""
image_id = self.train_id[index]
class_id = int(self._get_class_by_id(image_id)) - 1
path = self._get_path_by_id(image_id)
cv2.setNumThreads(0)
cv2.ocl.setUseOpenCL(False)
image = cv2.imread(os.path.join(self.root, 'images', path))
if self.transform:
image = self.transform(image)
if self.target_transform:
class_id = self.target_transform(class_id)
return image, class_id
def _get_path_by_id(self, image_id):
return self.images_path[image_id]
def _get_class_by_id(self, image_id):
return self.class_ids[image_id]
class CUB_200_2011_Test(Dataset):
def __init__(self, path, transform=None, target_transform=None):
self.root = path
self.transform = transform
self.target_transform = target_transform
self.images_path = {}
with open(os.path.join(self.root, 'images.txt')) as f:
for line in f:
image_id, path = line.split()
self.images_path[image_id] = path
self.class_ids = {}
with open(os.path.join(self.root, 'image_class_labels.txt')) as f:
for line in f:
image_id, class_id = line.split()
self.class_ids[image_id] = class_id
self.train_id = []
with open(os.path.join(self.root, 'train_test_split.txt')) as f:
for line in f:
image_id, is_train = line.split()
if not int(is_train) and self.class_ids[image_id]=='1':# only use the first class
self.train_id.append(image_id)
def __len__(self):
return len(self.train_id)
def __getitem__(self, index):
"""
Args:
index: index of training dataset
Returns:
image and its corresponding label
"""
image_id = self.train_id[index]
class_id = int(self._get_class_by_id(image_id)) - 1
cv2.setNumThreads(0)
cv2.ocl.setUseOpenCL(False)
path = self._get_path_by_id(image_id)
image = cv2.imread(os.path.join(self.root, 'images', path))
if self.transform:
image = self.transform(image)
if self.target_transform:
class_id = self.target_transform(class_id)
return image, class_id
def _get_path_by_id(self, image_id):
return self.images_path[image_id]
def _get_class_by_id(self, image_id):
return self.class_ids[image_id]
def compute_mean_and_std(dataset):
"""Compute dataset mean and std, and normalize it
Args:
dataset: instance of CUB_200_2011_Train, CUB_200_2011_Test
Returns:
return: mean and std of this dataset
"""
mean_r = 0
mean_g = 0
mean_b = 0
for img, _ in dataset:
mean_b += np.mean(img[:, :, 0])
mean_g += np.mean(img[:, :, 1])
mean_r += np.mean(img[:, :, 2])
mean_b /= len(dataset)
mean_g /= len(dataset)
mean_r /= len(dataset)
diff_r = 0
diff_g = 0
diff_b = 0
N = 0
for img, _ in dataset:
diff_b += np.sum(np.power(img[:, :, 0] - mean_b, 2))
diff_g += np.sum(np.power(img[:, :, 1] - mean_g, 2))
diff_r += np.sum(np.power(img[:, :, 2] - mean_r, 2))
N += np.prod(img[:, :, 0].shape)
std_b = np.sqrt(diff_b / N)
std_g = np.sqrt(diff_g / N)
std_r = np.sqrt(diff_r / N)
mean = (mean_b.item() / 255.0, mean_g.item() / 255.0, mean_r.item() / 255.0)
std = (std_b.item() / 255.0, std_g.item() / 255.0, std_r.item() / 255.0)
return mean, std