Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/FaceMaskOverlay/lib/datasets/cofw.py
3443 views
1
# ------------------------------------------------------------------------------
2
# Copyright (c) Microsoft
3
# Licensed under the MIT License.
4
# Created by Tianheng Cheng([email protected]), Yang Zhao
5
# ------------------------------------------------------------------------------
6
7
import math
8
import random
9
10
import torch
11
import torch.utils.data as data
12
import numpy as np
13
14
from hdf5storage import loadmat
15
from ..utils.transforms import fliplr_joints, crop, generate_target, transform_pixel
16
17
18
class COFW(data.Dataset):
19
20
def __init__(self, cfg, is_train=True, transform=None):
21
# specify annotation file for dataset
22
if is_train:
23
self.mat_file = cfg.DATASET.TRAINSET
24
else:
25
self.mat_file = cfg.DATASET.TESTSET
26
27
self.is_train = is_train
28
self.transform = transform
29
self.data_root = cfg.DATASET.ROOT
30
self.input_size = cfg.MODEL.IMAGE_SIZE
31
self.output_size = cfg.MODEL.HEATMAP_SIZE
32
self.sigma = cfg.MODEL.SIGMA
33
self.scale_factor = cfg.DATASET.SCALE_FACTOR
34
self.rot_factor = cfg.DATASET.ROT_FACTOR
35
self.label_type = cfg.MODEL.TARGET_TYPE
36
self.flip = cfg.DATASET.FLIP
37
38
# load annotations
39
self.mat = loadmat(self.mat_file)
40
if is_train:
41
self.images = self.mat['IsTr']
42
self.pts = self.mat['phisTr']
43
else:
44
self.images = self.mat['IsT']
45
self.pts = self.mat['phisT']
46
47
self.mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
48
self.std = np.array([0.229, 0.224, 0.225], dtype=np.float32)
49
50
def __len__(self):
51
return len(self.images)
52
53
def __getitem__(self, idx):
54
55
img = self.images[idx][0]
56
57
if len(img.shape) == 2:
58
img = img.reshape(img.shape[0], img.shape[1], 1)
59
img = np.repeat(img, 3, axis=2)
60
61
pts = self.pts[idx][0:58].reshape(2, -1).transpose()
62
63
xmin = np.min(pts[:, 0])
64
xmax = np.max(pts[:, 0])
65
ymin = np.min(pts[:, 1])
66
ymax = np.max(pts[:, 1])
67
68
center_w = (math.floor(xmin) + math.ceil(xmax)) / 2.0
69
center_h = (math.floor(ymin) + math.ceil(ymax)) / 2.0
70
71
scale = max(math.ceil(xmax) - math.floor(xmin), math.ceil(ymax) - math.floor(ymin)) / 200.0
72
center = torch.Tensor([center_w, center_h])
73
74
scale *= 1.25
75
nparts = pts.shape[0]
76
77
r = 0
78
if self.is_train:
79
scale = scale * (random.uniform(1 - self.scale_factor,
80
1 + self.scale_factor))
81
r = random.uniform(-self.rot_factor, self.rot_factor) \
82
if random.random() <= 0.6 else 0
83
84
if random.random() <= 0.5 and self.flip:
85
img = np.fliplr(img)
86
pts = fliplr_joints(pts, width=img.shape[1], dataset='COFW')
87
center[0] = img.shape[1] - center[0]
88
89
img = crop(img, center, scale, self.input_size, rot=r)
90
91
target = np.zeros((nparts, self.output_size[0], self.output_size[1]))
92
tpts = pts.copy()
93
94
for i in range(nparts):
95
if tpts[i, 1] > 0:
96
tpts[i, 0:2] = transform_pixel(tpts[i, 0:2]+1, center,
97
scale, self.output_size, rot=r)
98
target[i] = generate_target(target[i], tpts[i]-1, self.sigma,
99
label_type=self.label_type)
100
img = img.astype(np.float32)
101
img = (img/255 - self.mean) / self.std
102
img = img.transpose([2, 0, 1])
103
target = torch.Tensor(target)
104
tpts = torch.Tensor(tpts)
105
center = torch.Tensor(center)
106
107
meta = {'index': idx, 'center': center, 'scale': scale,
108
'pts': torch.Tensor(pts), 'tpts': tpts}
109
110
return img, target, meta
111
112
113
if __name__ == '__main__':
114
115
pass
116
117