This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
289 lines (239 loc) · 8.7 KB
/
train.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
import os
import tensorflow as tf
import tensorflow_addons as tfa
import heteroscedastic
from DeepDanbooru.deepdanbooru import deepdanbooru as dd
from robust_loss.adaptive import AdaptiveLossFunction
with open("./tags.txt", encoding="utf8") as istrm:
tags = istrm.read().split()
def jensen_shannon():
@tf.function
def loss(y, p):
d = tf.keras.losses.kl_divergence
m = 0.5 * y + 0.5 * p
return 0.5 * d(y, m) + 0.5 * d(p, m)
return loss
def focal_loss(
alpha=0.25,
gamma=2.00,
epsilon=1e-7,
from_logits=False,
inner=tf.keras.losses.binary_crossentropy,
):
@tf.function
def loss(y, p):
y = tf.math.maximum(tf.cast(y, tf.float32), epsilon)
p = tf.math.maximum(tf.cast(p, tf.float32), epsilon)
if from_logits:
p = tf.nn.sigmoid(p)
base = inner(y, p)[:, None]
negative = p * y + (1 - p) * (1 - y)
positive = alpha * y + (1 - alpha) * (1 - y)
return base * positive * ((1 - negative) ** gamma)
return loss
def make_adaptive(inner, num_channels=len(tags)):
adapt = AdaptiveLossFunction(num_channels=num_channels, float_dtype=tf.float32)
@tf.function
def loss(y, p):
u = inner(y, p)
with tf.variable_creator_scope("adaloss"):
return adapt(u)
return loss
def preprocessor(image_shape, num_tags=len(tags)):
def preprocess(x, y):
x = tf.io.decode_image(tf.squeeze(x), expand_animations=False)
x = tf.cast(x, tf.float32)
x = x[..., : image_shape[-1]]
if tf.shape(x)[-1] < 3:
x = tf.reduce_mean(x, axis=-1)
x = tf.expand_dims(x, axis=-1)
x = tf.image.grayscale_to_rgb(x)
x = tf.image.resize(x, image_shape[:-1])
return x, y[:num_tags]
return preprocess
def record_parser():
feature_desc = dict(
tag_indxs=tf.io.VarLenFeature(tf.int64),
tag_names=tf.io.FixedLenFeature((), tf.string),
image_str=tf.io.FixedLenFeature((), tf.string),
post_id=tf.io.FixedLenFeature((), tf.int64),
)
@tf.function
def parse(record):
return tf.io.parse_single_example(record, feature_desc)
return parse
def record_deserializer(num_tags=len(tags)):
@tf.function
def decode(record):
x = record["image_str"]
ids = tf.sparse.to_dense(record["tag_indxs"])
y = tf.SparseTensor(ids[:, None], tf.ones_like(ids), [len(tags)])
y = tf.sparse.to_dense(y)[:num_tags]
y = tf.cast(y, tf.float32)
return x, y
return decode
def shard_names(root):
return tf.io.gfile.glob(f"{root}/*.tfrecords")
def read_records(shards):
return (
tf.data.Dataset.from_tensor_slices(shards)
.shuffle(len(shards))
.repeat()
.interleave(
lambda shard: (
tf.data.TFRecordDataset(shard).map(
record_parser(),
num_parallel_calls=1,
)
),
cycle_length=1,
block_length=256,
num_parallel_calls=1,
deterministic=False,
)
.prefetch(tf.data.AUTOTUNE)
)
def make_dataset(shards, image_shape, num_tags=len(tags)):
return (
read_records(shards)
.map(record_deserializer(num_tags))
.map(
preprocessor(image_shape, num_tags=num_tags),
num_parallel_calls=4,
deterministic=False,
)
.apply(tf.data.experimental.ignore_errors())
)
def fwd_attention_head(x, out_units=len(tags)):
x = tf.keras.layers.MultiHeadAttention(2, 2)(x, x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.LocallyConnected2D(*x.shape[-2:][::-1])(x)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.layers.Flatten()(x)
x = heteroscedastic.MCSigmoidDenseFA(
out_units,
logits_only=True,
name="denoising",
)(x)
return tf.keras.layers.Activation(tf.nn.sigmoid, name="enc_tags")(x)
def res_attention_head(x, enc_units=512, out_units=len(tags)):
dsampled = tf.keras.layers.BatchNormalization()(x)
attended = tf.keras.layers.MultiHeadAttention(2, 2)(dsampled, dsampled)
attended = tf.keras.layers.LocallyConnected2D(*attended.shape[-2:][::-1])(attended)
squeezed = tf.keras.layers.LocallyConnected2D(*dsampled.shape[-2:][::-1])(dsampled)
squeezed = tf.keras.layers.Dense(enc_units)(squeezed)
squeezed = tf.keras.layers.Activation(tf.nn.silu)(squeezed)
squeezed = tf.keras.layers.Dense(attended.shape[-1])(squeezed)
outputs = tf.keras.layers.Add()([squeezed, attended])
outputs = tf.keras.layers.LayerNormalization()(outputs)
outputs = tf.keras.layers.Flatten()(outputs)
outputs = heteroscedastic.MCSigmoidDenseFA(
out_units,
logits_only=True,
name="denoising",
)(outputs)
return tf.keras.layers.Activation(tf.nn.sigmoid, name="enc_tags")(outputs)
def build_model(num_tags=len(tags)):
image_shape = (224, 224, 3)
preprocess = tf.keras.Sequential(
[
tf.keras.layers.experimental.preprocessing.RandomFlip(),
tf.keras.layers.experimental.preprocessing.RandomContrast(0.25),
tf.keras.layers.experimental.preprocessing.RandomRotation(0.25),
tf.keras.layers.experimental.preprocessing.RandomZoom(-0.25),
tf.keras.layers.experimental.preprocessing.Resizing(*image_shape[:-1]),
tf.keras.layers.Lambda(tf.keras.applications.resnet.preprocess_input),
],
name="preprocess",
)
inputs = tf.keras.layers.Input(shape=image_shape, name="images")
outputs = preprocess(inputs)
outputs = dd.model.create_resnet_custom_v4(outputs, output_dim=num_tags * 2)
outputs = tf.keras.Model(inputs, outputs).layers[-3].output
# outputs = tf.keras.applications.EfficientNetB0(
# weights=None, include_top=False, input_shape=image_shape
# )(outputs)
outputs = res_attention_head(outputs)
return tf.keras.Model(inputs, outputs)
class CycleSchedule(tf.optimizers.schedules.LearningRateSchedule):
def __init__(self, min_lr, max_lr, total_steps, epsilon=1e-7, cycles=1, **kwargs):
super().__init__(**kwargs)
self.min_lr = min_lr
self.max_lr = max_lr
self.total_steps = total_steps
self.epsilon = epsilon
self.cycles = cycles
def __call__(self, step):
q = tf.clip_by_value(
(step * self.cycles % self.total_steps) / self.total_steps,
self.epsilon,
1 - self.epsilon,
)
lr = self.max_lr - 2 * self.max_lr * tf.math.abs(q - 0.5)
return tf.clip_by_value(lr, self.min_lr, self.max_lr)
def get_config(self):
return {
"min_lr": self.min_lr,
"max_lr": self.max_lr,
"total_steps": self.total_steps,
"epsilon": self.epsilon,
"cycles": self.cycles,
}
if __name__ == "__main__":
model = build_model()
image_shape = model.inputs[0].shape[1:]
n_logs = len(tf.io.gfile.glob("./logs/*"))
callbacks = [
tf.keras.callbacks.ModelCheckpoint(
"./train/e6tag_sm-{epoch:05}-{auc:.2f}.h5",
monitor="auc",
mode="max",
save_best_only=False,
save_weights_only=False,
save_freq=1024,
),
tf.keras.callbacks.TensorBoard(
f"./logs/{n_logs+1}",
update_freq=1,
write_graph=False,
write_steps_per_second=True,
),
]
initial_epoch = 0
ckpts = sorted(tf.io.gfile.glob("./train/e6tag_sm-*.h5"))
if ckpts:
best = ckpts[-1]
model.load_weights(best)
initial_epoch, _ = os.path.splitext(best.split("-")[1])
initial_epoch = int(initial_epoch) - 1
# test_ds, train_ds = (
# dataset.take(1024).cache().repeat().batch(4),
# dataset.batch(1),
# )
shards = shard_names("D:/yiff") # [::2][:64]
batch_size = 12
dataset = make_dataset(shards, image_shape).batch(batch_size)
metrics = [
tf.keras.metrics.AUC(name="auc"),
]
shard_size = 256
total_epochs = 4
steps_per_epoch = int(len(shards) * shard_size / batch_size / total_epochs)
lr_schedule = CycleSchedule(0.1, 4.0, steps_per_epoch * total_epochs, cycles=1)
train_config = dict(
x=dataset,
verbose=1,
epochs=total_epochs,
initial_epoch=initial_epoch,
callbacks=callbacks,
steps_per_epoch=steps_per_epoch,
shuffle=False,
)
model.compile(
optimizer=tf.keras.optimizers.SGD(learning_rate=lr_schedule, momentum=0.99),
loss=make_adaptive(focal_loss()),
metrics=metrics,
)
model.fit(**train_config)