forked from huanglianghua/siamfc-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpairwise.py
132 lines (108 loc) · 4.57 KB
/
pairwise.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
from __future__ import absolute_import, division
import numpy as np
from collections import namedtuple
from torch.utils.data import Dataset
from torchvision.transforms import Compose, CenterCrop, RandomCrop, ToTensor
from PIL import Image, ImageStat, ImageOps
class RandomStretch(object):
def __init__(self, max_stretch=0.05, interpolation='bilinear'):
assert interpolation in ['bilinear', 'bicubic']
self.max_stretch = max_stretch
self.interpolation = interpolation
def __call__(self, img):
scale = 1.0 + np.random.uniform(
-self.max_stretch, self.max_stretch)
size = np.round(np.array(img.size, float) * scale).astype(int)
if self.interpolation == 'bilinear':
method = Image.BILINEAR
elif self.interpolation == 'bicubic':
method = Image.BICUBIC
return img.resize(tuple(size), method)
class Pairwise(Dataset):
def __init__(self, seq_dataset, **kargs):
super(Pairwise, self).__init__()
self.cfg = self.parse_args(**kargs)
self.seq_dataset = seq_dataset
self.indices = np.random.permutation(len(seq_dataset))
# augmentation for exemplar and instance images
self.transform_z = Compose([
RandomStretch(max_stretch=0.05),
CenterCrop(self.cfg.instance_sz - 8),
RandomCrop(self.cfg.instance_sz - 2 * 8),
CenterCrop(self.cfg.exemplar_sz),
ToTensor()])
self.transform_x = Compose([
RandomStretch(max_stretch=0.05),
CenterCrop(self.cfg.instance_sz - 8),
RandomCrop(self.cfg.instance_sz - 2 * 8),
ToTensor()])
def parse_args(self, **kargs):
# default parameters
cfg = {
'pairs_per_seq': 10,
'max_dist': 100,
'exemplar_sz': 127,
'instance_sz': 255,
'context': 0.5}
for key, val in kargs.items():
if key in cfg:
cfg.update({key: val})
return namedtuple('GenericDict', cfg.keys())(**cfg)
def __getitem__(self, index):
index = self.indices[index % len(self.seq_dataset)]
img_files, anno = self.seq_dataset[index]
rand_z, rand_x = self._sample_pair(len(img_files))
exemplar_image = Image.open(img_files[rand_z])
instance_image = Image.open(img_files[rand_x])
exemplar_image = self._crop_and_resize(exemplar_image, anno[rand_z])
instance_image = self._crop_and_resize(instance_image, anno[rand_x])
exemplar_image = 255.0 * self.transform_z(exemplar_image)
instance_image = 255.0 * self.transform_x(instance_image)
return exemplar_image, instance_image
def __len__(self):
return self.cfg.pairs_per_seq * len(self.seq_dataset)
def _sample_pair(self, n):
assert n > 0
if n == 1:
return 0, 0
elif n == 2:
return 0, 1
else:
max_dist = min(n - 1, self.cfg.max_dist)
rand_dist = np.random.choice(max_dist) + 1
rand_z = np.random.choice(n - rand_dist)
rand_x = rand_z + rand_dist
return rand_z, rand_x
def _crop_and_resize(self, image, box):
# convert box to 0-indexed and center based
box = np.array([
box[0] - 1 + (box[2] - 1) / 2,
box[1] - 1 + (box[3] - 1) / 2,
box[2], box[3]], dtype=np.float32)
center, target_sz = box[:2], box[2:]
# exemplar and search sizes
context = self.cfg.context * np.sum(target_sz)
z_sz = np.sqrt(np.prod(target_sz + context))
x_sz = z_sz * self.cfg.instance_sz / self.cfg.exemplar_sz
# convert box to corners (0-indexed)
size = round(x_sz)
corners = np.concatenate((
np.round(center - (size - 1) / 2),
np.round(center - (size - 1) / 2) + size))
corners = np.round(corners).astype(int)
# pad image if necessary
pads = np.concatenate((
-corners[:2], corners[2:] - image.size))
npad = max(0, int(pads.max()))
if npad > 0:
avg_color = ImageStat.Stat(image).mean
# PIL doesn't support float RGB image
avg_color = tuple(int(round(c)) for c in avg_color)
image = ImageOps.expand(image, border=npad, fill=avg_color)
# crop image patch
corners = tuple((corners + npad).astype(int))
patch = image.crop(corners)
# resize to instance_sz
out_size = (self.cfg.instance_sz, self.cfg.instance_sz)
patch = patch.resize(out_size, Image.BILINEAR)
return patch