-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdataset.py
211 lines (158 loc) · 7.74 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import os
import random
import torch
import numpy as np
import h5py
from torch.utils.data import Dataset
from PIL import Image
from torchvision import transforms
from matplotlib import pyplot as plt
import scipy.io as io
import cv2
transform=transforms.Compose([
transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),])
class listDataset(Dataset):
def __init__(self, root, shape=None, shuffle=True, crop=True, scale=True, flip=True, transform=None, train=False, seen=0, batch_size=1, num_workers=4):
if train:
root = root*4
random.shuffle(root)
self.nSamples = len(root)
self.lines = root
self.transform = transform
self.train = train
self.shape = shape
self.seen = seen
self.batch_size = batch_size
self.num_workers = num_workers
self.crop = crop
self.scale = scale
self.flip = flip
self.train = train
def _crop(self, img, count_target, prob_target):
w, h = img.size
crop_size = (384, 384)
if h < crop_size[1]:
img = transforms.ToTensor()(img)
tmp_img = torch.zeros([3,crop_size[1],w])
tmp_img[:,0:h,:] = img
img = transforms.ToPILImage()(tmp_img).convert('RGB')
tmp_target = np.zeros([crop_size[1],w])
tmp_target[0:h,:] = count_target
count_target = tmp_target
tmp_target = np.zeros([crop_size[1],w])
tmp_target[0:h,:] = prob_target
prob_target = tmp_target
h = crop_size[1]
if w < crop_size[0]:
img = transforms.ToTensor()(img)
tmp_img = torch.zeros([3,h,crop_size[0]])
tmp_img[:,:,0:w] = img
img = transforms.ToPILImage()(tmp_img).convert('RGB')
tmp_target = np.zeros([h,crop_size[1]])
tmp_target[:,0:w] = count_target
count_target = tmp_target
tmp_target = np.zeros([h,crop_size[1]])
tmp_target[:,0:w] = prob_target
prob_target = tmp_target
w = crop_size[0]
dx = int(random.random() * (w - crop_size[0]))
dy = int(random.random() * (h - crop_size[1]))
img = img.crop((dx, dy, crop_size[0] + dx, crop_size[1] + dy))
count_target = count_target[dy:crop_size[1] + dy, dx:crop_size[0] + dx]
prob_target = prob_target[dy:crop_size[1] + dy, dx:crop_size[0] + dx]
return img, count_target, prob_target
def _scale(self, img, count_target, prob_target):
if random.random() > 0.5:
scale_factor = 0.75 + 0.15 * random.random()
w, h = img.size
w_new = int(w * scale_factor)
h_new = int(h * scale_factor)
img = img.resize((w_new, h_new),Image.ANTIALIAS)
count_sum = count_target.sum()
count_target = cv2.resize(count_target, (w_new, h_new), interpolation=cv2.INTER_CUBIC)
count_target = count_target * count_sum / float(count_target.sum() + 1e-6)
prob_max = prob_target.max()
prob_target = cv2.resize(prob_target, (w_new, h_new), interpolation=cv2.INTER_CUBIC)
prob_target = prob_target * prob_max / float(prob_target.max() + 1e-6)
return img, count_target, prob_target
def _align_train(self, img, count_target, prob_target):
w, h = img.size
if w != 384 or h != 384:
img = transforms.ToTensor()(img)
tmp_img = torch.zeros([3,384,384])
tmp_img[:,0:h,0:w] = img
img = transforms.ToPILImage()(tmp_img).convert('RGB')
tmp_target = np.zeros([384,384])
tmp_target[0:h,0:w] = count_target
count_target = tmp_target
tmp_target = np.zeros([384,384])
tmp_target[0:h,0:w] = prob_target
prob_target = tmp_target
return img, count_target, prob_target
def _align_test(self, img, count_target, prob_target):
w, h = img.size
if w % 32 != 0 or h % 32 != 0:
rf = 32
h1 = int((h + rf - 1) / rf) * rf
w1 = int((w + rf - 1) / rf) * rf
img = transforms.ToTensor()(img)
tmp_img = torch.zeros([3,h1,w1])
tmp_img[:,0:h,0:w] = img
img = transforms.ToPILImage()(tmp_img).convert('RGB')
tmp_target = np.zeros([h1,w1])
tmp_target[0:h,0:w] = count_target
count_target = tmp_target
tmp_target = np.zeros([h1,w1])
tmp_target[0:h,0:w] = prob_target
prob_target = tmp_target
return img, count_target, prob_target
def _flip(self, img, count_target, prob_target):
if random.random() > 0.8:
count_target = np.fliplr(count_target)
prob_target = np.fliplr(prob_target)
img = img.transpose(Image.FLIP_LEFT_RIGHT)
return img, count_target, prob_target
def __len__(self):
return self.nSamples
def __getitem__(self, index):
assert index <= len(self), 'index range error'
img_path = self.lines[index]
img = Image.open(img_path).convert('RGB')
count_path = img_path.replace('.jpg','.h5')
prob_path = img_path.replace('.jpg','_probmap.h5')
count_file = h5py.File(count_path, 'r')
prob_file = h5py.File(prob_path, 'r')
count_target = np.asarray(count_file['density'])
prob_target = np.asarray(prob_file['density'])
if self.crop == True:
img, count_target, prob_target = self._crop(img, count_target, prob_target)
if self.scale == True:
img, count_target, prob_target = self._scale(img, count_target, prob_target)
if self.train == True:
img, count_target, prob_target = self._align_train(img, count_target, prob_target)
else:
img, count_target, prob_target = self._align_test(img, count_target, prob_target)
if self.flip == True:
img, count_target, prob_target = self._flip(img, count_target, prob_target)
count_target = cv2.resize(count_target, (int(count_target.shape[1]), int(count_target.shape[0])), interpolation=cv2.INTER_CUBIC)
count_target = torch.from_numpy(count_target).type(torch.FloatTensor).unsqueeze(0)
prob_target = cv2.resize(prob_target, (int(prob_target.shape[1]), int(prob_target.shape[0])), interpolation=cv2.INTER_CUBIC)
prob_target = torch.from_numpy(prob_target).type(torch.FloatTensor).unsqueeze(0)
if self.transform is not None:
img = self.transform(img)
if self.train:
return img, count_target, prob_target
else:
dot_path = img_path.replace('.jpg','_dotmap.h5')
dot_file = h5py.File(dot_path, 'r')
dot_target = np.asarray(dot_file['density'])
h,w = dot_target.shape
if w % 32 != 0 or h % 32 != 0:
rf = 32
h1 = int((h + rf - 1) / rf) * rf
w1 = int((w + rf - 1) / rf) * rf
tmp_target = np.zeros([h1,w1])
tmp_target[0:h,0:w] = dot_target
dot_target = tmp_target
return img, count_target, prob_target, dot_target