google-research

Форк
0
210 строк · 8.4 Кб
1
# coding=utf-8
2
# Copyright 2024 The Google Research Authors.
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15

16
import os
17
import numpy as np
18
import tensorflow as tf
19
# pylint: skip-file
20

21
ATT_ID = {'5_o_Clock_Shadow': 0, 'Arched_Eyebrows': 1, 'Attractive': 2,
22
          'Bags_Under_Eyes': 3, 'Bald': 4, 'Bangs': 5, 'Big_Lips': 6,
23
          'Big_Nose': 7, 'Black_Hair': 8, 'Blond_Hair': 9, 'Blurry': 10,
24
          'Brown_Hair': 11, 'Bushy_Eyebrows': 12, 'Chubby': 13,
25
          'Double_Chin': 14, 'Eyeglasses': 15, 'Goatee': 16,
26
          'Gray_Hair': 17, 'Heavy_Makeup': 18, 'High_Cheekbones': 19,
27
          'Male': 20, 'Mouth_Slightly_Open': 21, 'Mustache': 22,
28
          'Narrow_Eyes': 23, 'No_Beard': 24, 'Oval_Face': 25,
29
          'Pale_Skin': 26, 'Pointy_Nose': 27, 'Receding_Hairline': 28,
30
          'Rosy_Cheeks': 29, 'Sideburns': 30, 'Smiling': 31,
31
          'Straight_Hair': 32, 'Wavy_Hair': 33, 'Wearing_Earrings': 34,
32
          'Wearing_Hat': 35, 'Wearing_Lipstick': 36,
33
          'Wearing_Necklace': 37, 'Wearing_Necktie': 38, 'Young': 39}
34
ID_ATT = {v: k for k, v in ATT_ID.items()}
35
NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 162770
36
CENTRAL_FRACTION = 0.89
37
LOAD_SIZE = 142 #286
38
CROP_SIZE = 128 #256
39

40
def cal_eo(a, y_label, y_pred):
41
  a = np.array(a)
42
  y_label = np.array(y_label)
43
  y_pred = np.array(y_pred)
44

45
  idx00 = np.logical_and(a==0,y_label==0)
46
  idx01 = np.logical_and(a==0,y_label==1)
47
  idx10 = np.logical_and(a==1,y_label==0)
48
  idx11 = np.logical_and(a==1,y_label==1)
49

50
  d00 = 1 - np.sum(y_pred[idx00])/y_pred[idx00].shape[0]
51
  d01 = np.sum(y_pred[idx01])/y_pred[idx01].shape[0]
52
  d10 = 1 - np.sum(y_pred[idx10])/y_pred[idx10].shape[0]
53
  d11 = np.sum(y_pred[idx11])/y_pred[idx11].shape[0]
54

55
  eo = np.abs(d00-d10)+np.abs(d01-d11)
56
  return (d00,d01,d10,d11,eo)
57

58
def reorg(label_path,af,bf):
59
  img_names = np.genfromtxt(label_path, dtype=str, usecols=0)
60
  labels = np.genfromtxt(label_path, dtype=int, usecols=range(1, 41))
61
  entry = np.concatenate((img_names[:, np.newaxis], labels), axis=1)
62
  a = np.asarray((labels[:,ATT_ID[af]]+1)//2)
63
  b = np.asarray((labels[:,ATT_ID[bf]]+1)//2)
64
  d00 = []
65
  d01 = []
66
  d10 = []
67
  d11 = []
68
  for i in range(labels.shape[0]):
69
      if a[i]==0:
70
          if b[i]==0: d00.append(entry[i])
71
          elif b[i]==1: d01.append(entry[i])
72
      elif a[i]==1:
73
          if b[i]==0: d10.append(entry[i])
74
          elif b[i]==1: d11.append(entry[i])
75
  min_leng = np.min([len(d00),len(d01),len(d10),len(d11)])
76
  new_list = d00[:min_leng]+d01[:3*min_leng]+d10[:3*min_leng]+d11[:min_leng]
77
  return np.array(new_list)
78

79
def reorg_fake(label_path,af,bf):
80
  img_names = np.genfromtxt(label_path, dtype=str, usecols=0)
81
  labels = np.genfromtxt(label_path, dtype=int, usecols=range(1, 41))
82
  entry = np.concatenate((img_names[:, np.newaxis], labels), axis=1)
83
  a = np.asarray((labels[:,ATT_ID[af]]+1)//2)
84
  b = np.asarray((labels[:,ATT_ID[bf]]+1)//2)
85
  d00 = []
86
  d01 = []
87
  d10 = []
88
  d11 = []
89
  for i in range(labels.shape[0]):
90
      if a[i]==0:
91
          if b[i]==0: d00.append(entry[i])
92
          elif b[i]==1: d01.append(entry[i])
93
      elif a[i]==1:
94
          if b[i]==0: d10.append(entry[i])
95
          elif b[i]==1: d11.append(entry[i])
96
  min_leng = np.min([len(d00),len(d01),len(d10),len(d11)])
97
  new_list = d00[:min_leng]+d01[:3*min_leng]+d10[:3*min_leng]+d11[:min_leng]
98
  return np.array(new_list)
99

100
def load_train(image_path, label, att):
101
  image = tf.io.read_file(image_path)
102
  image = tf.image.decode_jpeg(image)
103
  image = tf.image.resize(image, [LOAD_SIZE, LOAD_SIZE])
104
  image = tf.image.random_flip_left_right(image)
105
  image = tf.image.random_crop(image, [CROP_SIZE, CROP_SIZE, 3])
106
  image = tf.clip_by_value(image, 0, 255) / 127.5 - 1
107
  label = (label + 1) // 2
108
  att = (att + 1) // 2
109
  image = tf.cast(image, tf.float32)
110
  label = tf.cast(label, tf.int32)
111
  att = tf.cast(att, tf.int32)
112
  return (image, label, att)
113

114
def load_test(image_path, label, att):
115
  image = tf.io.read_file(image_path)
116
  image = tf.image.decode_jpeg(image)
117
  image = tf.image.resize(image, [LOAD_SIZE, LOAD_SIZE])
118
  image = tf.image.central_crop(image, CENTRAL_FRACTION)
119
  image = tf.clip_by_value(image, 0, 255) / 127.5 - 1
120
  label = (label + 1) // 2
121
  att = (att + 1) // 2
122
  image = tf.cast(image, tf.float32)
123
  label = tf.cast(label, tf.int32)
124
  att = tf.cast(att, tf.int32)
125
  return (image, label, att)
126

127
# load balanced training dataset
128
def data_train(image_path, label_path, batch_size):
129
  a = 'Male'
130
  b = 'Arched_Eyebrows'
131
  new_entry = reorg(label_path,a,b)
132
  n_examples = new_entry.shape[0]
133
  img_names = new_entry[:,0]
134
  img_paths = np.array([os.path.join(image_path, img_name) for img_name in img_names])
135
  img_labels = new_entry[:,1:]
136
  labels = img_labels[:,ATT_ID['Arched_Eyebrows']].astype(int)
137
  att = img_labels[:,ATT_ID['Male']].astype(int)
138

139
  train_dataset = tf.data.Dataset.from_tensor_slices((img_paths, labels, att))
140
  train_dataset = train_dataset.map(load_train, num_parallel_calls=tf.data.experimental.AUTOTUNE)
141
  train_dataset = train_dataset.shuffle(n_examples)
142
  train_dataset = train_dataset.batch(batch_size, drop_remainder=True)
143
  train_dataset = train_dataset.repeat().prefetch(1)
144

145
  train_iter = train_dataset.make_one_shot_iterator()
146
  batch = train_iter.get_next()
147

148
  return batch, int(np.ceil(n_examples/batch_size))
149

150
def data_fake(image_path, label_path, batch_size):
151
  a = 'Male'
152
  b = 'Arched_Eyebrows'
153
  new_entry = reorg_fake(label_path,a,b)
154
  n_examples = new_entry.shape[0]
155
  img_names = new_entry[:,0]
156
  img_paths = np.array([os.path.join(image_path, img_name) for img_name in img_names])
157
  img_labels = new_entry[:,1:]
158
  labels = img_labels[:,ATT_ID['Arched_Eyebrows']].astype(int)
159
  att = img_labels[:,ATT_ID['Male']].astype(int)
160

161
  train_dataset = tf.data.Dataset.from_tensor_slices((img_paths, labels, att))
162
  train_dataset = train_dataset.map(load_train, num_parallel_calls=tf.data.experimental.AUTOTUNE)
163
  train_dataset = train_dataset.shuffle(n_examples)
164
  train_dataset = train_dataset.batch(batch_size, drop_remainder=True)
165
  train_dataset = train_dataset.repeat().prefetch(1)
166

167
  train_iter = train_dataset.make_one_shot_iterator()
168
  batch = train_iter.get_next()
169

170
  return batch, int(np.ceil(n_examples/batch_size))
171

172
# load entire training dataset
173
# def data_train(image_path, label_path, batch_size):
174
#     img_names = np.genfromtxt(label_path, dtype=str, usecols=0)
175
#     img_paths = np.array([os.path.join(image_path, img_name) for img_name in img_names])
176
#     labels = np.genfromtxt(label_path, dtype=int, usecols=range(1, 41))
177
#     n_examples = img_names.shape[0]
178
#     # labels = labels[:,ATT_ID['Male']]
179
#     labels = labels[:,ATT_ID['Smiling']]
180
#     # labels = labels[:,ATT_ID['Arched_Eyebrows']]
181

182

183
#     train_dataset = tf.data.Dataset.from_tensor_slices((img_paths, labels))
184
#     train_dataset = train_dataset.map(load_train, num_parallel_calls=tf.data.experimental.AUTOTUNE)
185
#     train_dataset = train_dataset.shuffle(NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN, seed=0)
186
#     train_dataset = train_dataset.batch(batch_size, drop_remainder=True)
187
#     train_dataset = train_dataset.repeat().prefetch(1)
188

189
#     train_iter = train_dataset.make_one_shot_iterator()
190
#     batch = train_iter.get_next()
191

192
#     return batch, int(np.ceil(n_examples/batch_size))
193

194
def data_test(image_path, label_path, batch_size):
195
  img_names = np.genfromtxt(label_path, dtype=str, usecols=0)
196
  img_paths = np.array([os.path.join(image_path, img_name) for img_name in img_names])
197
  img_labels = np.genfromtxt(label_path, dtype=int, usecols=range(1, 41))
198
  n_examples = img_names.shape[0]
199
  labels = img_labels[:,ATT_ID['Arched_Eyebrows']]
200
  att = img_labels[:,ATT_ID['Male']]
201

202
  test_dataset = tf.data.Dataset.from_tensor_slices((img_paths, labels, att))
203
  test_dataset = test_dataset.map(load_test, num_parallel_calls=tf.data.experimental.AUTOTUNE)
204
  test_dataset = test_dataset.batch(batch_size, drop_remainder=False)
205
  test_dataset = test_dataset.repeat().prefetch(1)
206

207
  test_iter = test_dataset.make_one_shot_iterator()
208
  batch = test_iter.get_next()
209

210
  return batch, int(np.ceil(n_examples/batch_size))
211

212

213

214

215

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.