-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_dataset.py
140 lines (119 loc) · 4.88 KB
/
my_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
import numpy as np
from PIL import Image
import torch
from torch.utils.data import Dataset
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
class FS2K_DataSet(Dataset):
"""自定义数据集"""
def __init__(self, attrs, transform=None):
self.images_path = attrs['image_name']
self.hair = attrs['hair']
self.hair_color = attrs['hair_color'] # sketch不用hair_color
self.gender = attrs['gender']
self.earring = attrs['earring']
self.smile = attrs['smile']
self.frontal_face = attrs['frontal_face']
self.style = attrs['style'] # photo不用style
self.transform = transform
def __len__(self):
return len(self.images_path)
def __getitem__(self, item):
# 图像路径
img_path = self.images_path[item]
# 读取图像
# img = Image.open(img_path).convert('L')
img = Image.open(img_path).convert('RGB')
# 图像增强
if self.transform:
img = self.transform(img)
# 返回图像和所有相关标签
dict_data = {
'img': img,
'labels': {
'hair': self.hair[item],
'hair_color': self.hair_color[item],
'gender': self.gender[item],
'earring': self.earring[item],
'smile': self.smile[item],
'frontal_face': self.frontal_face[item],
'style': self.style[item]
}
}
return dict_data
def get_parsing(seg_parsing):
seg_parsing = np.array(seg_parsing)
seg_parsing = seg_parsing.mean(axis=2)
seg_parsing[seg_parsing >= 150] = 255
seg_parsing[seg_parsing < 150] = 0
seg_parsing = seg_parsing // 255
seg_parsing = seg_parsing.reshape(7, 32, 7, 32)
# seg_earring = seg_earring.reshape(14, 16, 14, 16)
seg_parsing = seg_parsing.mean(axis=(1, 3))
seg_parsing[seg_parsing >= 0.031] = 1
seg_parsing = seg_parsing.flatten()
return seg_parsing
def seg_to_img(seg_path):
seg_img = Image.open(seg_path)
seg_img = np.array(seg_img)
seg_img = np.expand_dims(seg_img, axis=0)
seg_img = np.repeat(seg_img, 3, axis=0)
seg_img = seg_img.transpose((1, 2, 0))
seg_img = Image.fromarray(seg_img)
return seg_img
class FS2K_DataSet_With_Seg(Dataset):
"""自定义数据集"""
def __init__(self, attrs, transform=None, t2=None):
self.images_path = attrs['image_name']
self.hair = attrs['hair']
self.hair_color = attrs['hair_color'] # sketch不用hair_color
self.gender = attrs['gender']
self.earring = attrs['earring']
self.smile = attrs['smile']
self.frontal_face = attrs['frontal_face']
self.style = attrs['style'] # photo不用style
self.transform = transform
self.t2 = t2
def __len__(self):
return len(self.images_path)
def __getitem__(self, item):
# 图像路径
img_path = self.images_path[item]
img_name = img_path.split('/')[-1]
style_label, index = img_name.split('_')
seg_earring_path = f'/data2/yuhao/FS2K/FS2K/seg_earring/photo{style_label}/{index}'
seg_earring = seg_to_img(seg_earring_path)
seg_smile_path = f'/data2/yuhao/FS2K/FS2K/seg_face/photo{style_label}/{index}'
seg_smile = seg_to_img(seg_smile_path)
seg_hair_path = f'/data2/yuhao/FS2K/FS2K/seg_hair/photo{style_label}/{index}'
seg_hair = seg_to_img(seg_hair_path)
seg_total_path = f'/data2/yuhao/FS2K/FS2K/seg_ex_background/photo{style_label}/{index}'
seg_total = seg_to_img(seg_total_path)
# 读取图像
img = Image.open(img_path).convert('RGB')
# 图像增强
if self.transform:
img, seg_earring, seg_smile, seg_hair, seg_total = self.transform(img, seg_earring, seg_smile, seg_hair, seg_total)
img = self.t2(img)
seg_smile = get_parsing(seg_smile)
seg_earring = get_parsing(seg_earring)
seg_hair = get_parsing(seg_hair)
seg_total = get_parsing(seg_total)
# 返回图像和所有相关标签
dict_data = {
'img': img,
'labels': {
'hair': self.hair[item],
'hair_color': self.hair_color[item],
'gender': self.gender[item],
'earring': self.earring[item],
'smile': self.smile[item],
'frontal_face': self.frontal_face[item],
'style': self.style[item],
'seg_earring': seg_earring.astype(np.float32),
'seg_smile': seg_smile.astype(np.float32),
'seg_hair': seg_hair.astype(np.float32),
'seg_total': seg_total.astype(np.float32)
}
}
return dict_data