-
-
Notifications
You must be signed in to change notification settings - Fork 13k
/
train.py
616 lines (532 loc) · 23.8 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/usr/bin python3
""" Main entry point to the training process of FaceSwap """
from __future__ import annotations
import logging
import os
import sys
import typing as T
from time import sleep
from threading import Event
import cv2
import numpy as np
from lib.gui.utils.image import TRAININGPREVIEW
from lib.image import read_image_meta
from lib.keypress import KBHit
from lib.multithreading import MultiThread, FSThread
from lib.training import Preview, PreviewBuffer, TriggerType
from lib.utils import (get_folder, get_image_paths, handle_deprecated_cliopts,
FaceswapError, IMAGE_EXTENSIONS)
from plugins.plugin_loader import PluginLoader
if T.TYPE_CHECKING:
import argparse
from collections.abc import Callable
from plugins.train.model._base import ModelBase
from plugins.train.trainer._base import TrainerBase
logger = logging.getLogger(__name__)
class Train():
""" The Faceswap Training Process.
The training process is responsible for training a model on a set of source faces and a set of
destination faces.
The training process is self contained and should not be referenced by any other scripts, so it
contains no public properties.
Parameters
----------
arguments: argparse.Namespace
The arguments to be passed to the training process as generated from Faceswap's command
line arguments
"""
def __init__(self, arguments: argparse.Namespace) -> None:
logger.debug("Initializing %s: (args: %s", self.__class__.__name__, arguments)
self._args = handle_deprecated_cliopts(arguments)
if self._args.summary:
# If just outputting summary we don't need to initialize everything
return
self._images = self._get_images()
self._timelapse = self._set_timelapse()
gui_cache = os.path.join(
os.path.realpath(os.path.dirname(sys.argv[0])), "lib", "gui", ".cache")
self._gui_triggers: dict[T.Literal["mask", "refresh"], str] = {
"mask": os.path.join(gui_cache, ".preview_mask_toggle"),
"refresh": os.path.join(gui_cache, ".preview_trigger")}
self._stop: bool = False
self._save_now: bool = False
self._preview = PreviewInterface(self._args.preview)
logger.debug("Initialized %s", self.__class__.__name__)
def _get_images(self) -> dict[T.Literal["a", "b"], list[str]]:
""" Check the image folders exist and contains valid extracted faces. Obtain image paths.
Returns
-------
dict
The image paths for each side. The key is the side, the value is the list of paths
for that side.
"""
logger.debug("Getting image paths")
images = {}
for side in ("a", "b"):
side = T.cast(T.Literal["a", "b"], side)
image_dir = getattr(self._args, f"input_{side}")
if not os.path.isdir(image_dir):
logger.error("Error: '%s' does not exist", image_dir)
sys.exit(1)
images[side] = get_image_paths(image_dir, ".png")
if not images[side]:
logger.error("Error: '%s' contains no images", image_dir)
sys.exit(1)
# Validate the first image is a detected face
test_image = next(img for img in images[side])
meta = read_image_meta(test_image)
logger.debug("Test file: (filename: %s, metadata: %s)", test_image, meta)
if "itxt" not in meta or "alignments" not in meta["itxt"]:
logger.error("The input folder '%s' contains images that are not extracted faces.",
image_dir)
logger.error("You can only train a model on faces generated from Faceswap's "
"extract process. Please check your sources and try again.")
sys.exit(1)
logger.info("Model %s Directory: '%s' (%s images)",
side.upper(), image_dir, len(images[side]))
logger.debug("Got image paths: %s", [(key, str(len(val)) + " images")
for key, val in images.items()])
self._validate_image_counts(images)
return images
@classmethod
def _validate_image_counts(cls, images: dict[T.Literal["a", "b"], list[str]]) -> None:
""" Validate that there are sufficient images to commence training without raising an
error.
Confirms that there are at least 24 images in each folder. Whilst this is not enough images
to train a Neural Network to any successful degree, it should allow the process to train
without raising errors when generating previews.
A warning is raised if there are fewer than 250 images on any side.
Parameters
----------
images: dict
The image paths for each side. The key is the side, the value is the list of paths
for that side.
"""
counts = {side: len(paths) for side, paths in images.items()}
msg = ("You need to provide a significant number of images to successfully train a Neural "
"Network. Aim for between 500 - 5000 images per side.")
if any(count < 25 for count in counts.values()):
logger.error("At least one of your input folders contains fewer than 25 images.")
logger.error(msg)
sys.exit(1)
if any(count < 250 for count in counts.values()):
logger.warning("At least one of your input folders contains fewer than 250 images. "
"Results are likely to be poor.")
logger.warning(msg)
def _set_timelapse(self) -> dict[T.Literal["input_a", "input_b", "output"], str]:
""" Set time-lapse paths if requested.
Returns
-------
dict
The time-lapse keyword arguments for passing to the trainer
"""
if (not self._args.timelapse_input_a and
not self._args.timelapse_input_b and
not self._args.timelapse_output):
return {}
if (not self._args.timelapse_input_a or
not self._args.timelapse_input_b or
not self._args.timelapse_output):
raise FaceswapError("To enable the timelapse, you have to supply all the parameters "
"(--timelapse-input-A, --timelapse-input-B and "
"--timelapse-output).")
timelapse_output = get_folder(self._args.timelapse_output)
for side in ("a", "b"):
side = T.cast(T.Literal["a", "b"], side)
folder = getattr(self._args, f"timelapse_input_{side}")
if folder is not None and not os.path.isdir(folder):
raise FaceswapError(f"The Timelapse path '{folder}' does not exist")
training_folder = getattr(self._args, f"input_{side}")
if folder == training_folder:
continue # Time-lapse folder is training folder
filenames = [fname for fname in os.listdir(folder)
if os.path.splitext(fname)[-1].lower() in IMAGE_EXTENSIONS]
if not filenames:
raise FaceswapError(f"The Timelapse path '{folder}' does not contain any valid "
"images")
# Time-lapse images must appear in the training set, as we need access to alignment and
# mask info. Check filenames are there to save failing much later in the process.
training_images = [os.path.basename(img) for img in self._images[side]]
if not all(img in training_images for img in filenames):
raise FaceswapError(f"All images in the Timelapse folder '{folder}' must exist in "
f"the training folder '{training_folder}'")
TKey = T.Literal["input_a", "input_b", "output"]
kwargs = {T.cast(TKey, "input_a"): self._args.timelapse_input_a,
T.cast(TKey, "input_b"): self._args.timelapse_input_b,
T.cast(TKey, "output"): timelapse_output}
logger.debug("Timelapse enabled: %s", kwargs)
return kwargs
def process(self) -> None:
""" The entry point for triggering the Training Process.
Should only be called from :class:`lib.cli.launcher.ScriptExecutor`
"""
if self._args.summary:
self._load_model()
return
logger.debug("Starting Training Process")
logger.info("Training data directory: %s", self._args.model_dir)
thread = self._start_thread()
# from lib.queue_manager import queue_manager; queue_manager.debug_monitor(1)
err = self._monitor(thread)
self._end_thread(thread, err)
logger.debug("Completed Training Process")
def _start_thread(self) -> MultiThread:
""" Put the :func:`_training` into a background thread so we can keep control.
Returns
-------
:class:`lib.multithreading.MultiThread`
The background thread for running training
"""
logger.debug("Launching Trainer thread")
thread = MultiThread(target=self._training)
thread.start()
logger.debug("Launched Trainer thread")
return thread
def _end_thread(self, thread: MultiThread, err: bool) -> None:
""" Output message and join thread back to main on termination.
Parameters
----------
thread: :class:`lib.multithreading.MultiThread`
The background training thread
err: bool
Whether an error has been detected in :func:`_monitor`
"""
logger.debug("Ending Training thread")
if err:
msg = "Error caught! Exiting..."
log = logger.critical
else:
msg = ("Exit requested! The trainer will complete its current cycle, "
"save the models and quit (This can take a couple of minutes "
"depending on your training speed).")
if not self._args.redirect_gui:
msg += " If you want to kill it now, press Ctrl + c"
log = logger.info
log(msg)
self._stop = True
thread.join()
sys.stdout.flush()
logger.debug("Ended training thread")
def _training(self) -> None:
""" The training process to be run inside a thread. """
try:
sleep(0.5) # Let preview instructions flush out to logger
logger.debug("Commencing Training")
logger.info("Loading data, this may take a while...")
model = self._load_model()
trainer = self._load_trainer(model)
if trainer.exit_early:
self._stop = True
return
self._run_training_cycle(model, trainer)
except KeyboardInterrupt:
try:
logger.debug("Keyboard Interrupt Caught. Saving Weights and exiting")
model.io.save(is_exit=True)
trainer.clear_tensorboard()
except KeyboardInterrupt:
logger.info("Saving model weights has been cancelled!")
sys.exit(0)
except Exception as err:
raise err
def _load_model(self) -> ModelBase:
""" Load the model requested for training.
Returns
-------
:file:`plugins.train.model` plugin
The requested model plugin
"""
logger.debug("Loading Model")
model_dir = get_folder(self._args.model_dir)
model: ModelBase = PluginLoader.get_model(self._args.trainer)(
model_dir,
self._args,
predict=False)
model.build()
logger.debug("Loaded Model")
return model
def _load_trainer(self, model: ModelBase) -> TrainerBase:
""" Load the trainer requested for training.
Parameters
----------
model: :file:`plugins.train.model` plugin
The requested model plugin
Returns
-------
:file:`plugins.train.trainer` plugin
The requested model trainer plugin
"""
logger.debug("Loading Trainer")
base = PluginLoader.get_trainer(model.trainer)
trainer: TrainerBase = base(model,
self._images,
self._args.batch_size,
self._args.configfile)
logger.debug("Loaded Trainer")
return trainer
def _run_training_cycle(self, model: ModelBase, trainer: TrainerBase) -> None:
""" Perform the training cycle.
Handles the background training, updating previews/time-lapse on each save interval,
and saving the model.
Parameters
----------
model: :file:`plugins.train.model` plugin
The requested model plugin
trainer: :file:`plugins.train.trainer` plugin
The requested model trainer plugin
"""
logger.debug("Running Training Cycle")
update_preview_images = False
if self._args.write_image or self._args.redirect_gui or self._args.preview:
display_func: Callable | None = self._show
else:
display_func = None
for iteration in range(1, self._args.iterations + 1):
logger.trace("Training iteration: %s", iteration) # type:ignore
save_iteration = iteration % self._args.save_interval == 0 or iteration == 1
gui_triggers = self._process_gui_triggers()
if self._preview.should_toggle_mask or gui_triggers["mask"]:
trainer.toggle_mask()
update_preview_images = True
if self._preview.should_refresh or gui_triggers["refresh"] or update_preview_images:
viewer = display_func
update_preview_images = False
else:
viewer = None
timelapse = self._timelapse if save_iteration else {}
trainer.train_one_step(viewer, timelapse)
if viewer is not None and not save_iteration:
# Spammy but required by GUI to know to update window
print("")
logger.info("[Preview Updated]")
if self._stop:
logger.debug("Stop received. Terminating")
break
if save_iteration or self._save_now:
logger.debug("Saving (save_iterations: %s, save_now: %s) Iteration: "
"(iteration: %s)", save_iteration, self._save_now, iteration)
model.io.save(is_exit=False)
self._save_now = False
update_preview_images = True
logger.debug("Training cycle complete")
model.io.save(is_exit=True)
trainer.clear_tensorboard()
self._stop = True
def _output_startup_info(self) -> None:
""" Print the startup information to the console. """
logger.debug("Launching Monitor")
logger.info("===================================================")
logger.info(" Starting")
if self._args.preview:
logger.info(" Using live preview")
if sys.stdout.isatty():
logger.info(" Press '%s' to save and quit",
"Stop" if self._args.redirect_gui else "ENTER")
if not self._args.redirect_gui and sys.stdout.isatty():
logger.info(" Press 'S' to save model weights immediately")
logger.info("===================================================")
def _check_keypress(self, keypress: KBHit) -> bool:
""" Check if a keypress has been detected.
Parameters
----------
keypress: :class:`lib.keypress.KBHit`
The keypress monitor
Returns
-------
bool
``True`` if an exit keypress has been detected otherwise ``False``
"""
retval = False
if keypress.kbhit():
console_key = keypress.getch()
if console_key in ("\n", "\r"):
logger.debug("Exit requested")
retval = True
if console_key in ("s", "S"):
logger.info("Save requested")
self._save_now = True
return retval
def _process_gui_triggers(self) -> dict[T.Literal["mask", "refresh"], bool]:
""" Check whether a file drop has occurred from the GUI to manually update the preview.
Returns
-------
dict
The trigger name as key and boolean as value
"""
retval: dict[T.Literal["mask", "refresh"], bool] = {key: False
for key in self._gui_triggers}
if not self._args.redirect_gui:
return retval
for trigger, filename in self._gui_triggers.items():
if os.path.isfile(filename):
logger.debug("GUI Trigger received for: '%s'", trigger)
retval[trigger] = True
logger.debug("Removing gui trigger file: %s", filename)
os.remove(filename)
if trigger == "refresh":
print("") # Let log print on different line from loss output
logger.info("Refresh preview requested...")
return retval
def _monitor(self, thread: MultiThread) -> bool:
""" Monitor the background :func:`_training` thread for key presses and errors.
Parameters
----------
thread: :class:~`lib.multithreading.MultiThread`
The thread containing the training loop
Returns
-------
bool
``True`` if there has been an error in the background thread otherwise ``False``
"""
self._output_startup_info()
keypress = KBHit(is_gui=self._args.redirect_gui)
err = False
while True:
try:
if thread.has_error:
logger.debug("Thread error detected")
err = True
break
if self._stop:
logger.debug("Stop received")
break
# Preview Monitor
if self._preview.should_quit:
break
if self._preview.should_save:
self._save_now = True
# Console Monitor
if self._check_keypress(keypress):
break # Exit requested
sleep(1)
except KeyboardInterrupt:
logger.debug("Keyboard Interrupt received")
break
self._preview.shutdown()
keypress.set_normal_term()
logger.debug("Closed Monitor")
return err
def _show(self, image: np.ndarray, name: str = "") -> None:
""" Generate the preview and write preview file output.
Handles the output and display of preview images.
Parameters
----------
image: :class:`numpy.ndarray`
The preview image to be displayed and/or written out
name: str, optional
The name of the image for saving or display purposes. If an empty string is passed
then it will automatically be named. Default: ""
"""
logger.debug("Updating preview: (name: %s)", name)
try:
scriptpath = os.path.realpath(os.path.dirname(sys.argv[0]))
if self._args.write_image:
logger.debug("Saving preview to disk")
img = "training_preview.png"
imgfile = os.path.join(scriptpath, img)
cv2.imwrite(imgfile, image) # pylint:disable=no-member
logger.debug("Saved preview to: '%s'", img)
if self._args.redirect_gui:
logger.debug("Generating preview for GUI")
img = TRAININGPREVIEW
imgfile = os.path.join(scriptpath, "lib", "gui", ".cache", "preview", img)
cv2.imwrite(imgfile, image) # pylint:disable=no-member
logger.debug("Generated preview for GUI: '%s'", imgfile)
if self._args.preview:
logger.debug("Generating preview for display: '%s'", name)
self._preview.buffer.add_image(name, image)
logger.debug("Generated preview for display: '%s'", name)
except Exception as err:
logging.error("could not preview sample")
raise err
logger.debug("Updated preview: (name: %s)", name)
class PreviewInterface():
""" Run the preview window in a thread and interface with it
Parameters
----------
use_preview: bool
``True`` if pop-up preview window has been requested otherwise ``False``
"""
def __init__(self, use_preview: bool) -> None:
self._active = use_preview
self._triggers: TriggerType = {"toggle_mask": Event(),
"refresh": Event(),
"save": Event(),
"quit": Event(),
"shutdown": Event()}
self._buffer = PreviewBuffer()
self._thread = self._launch_thread()
@property
def buffer(self) -> PreviewBuffer:
""" :class:`PreviewBuffer`: The thread save preview image object """
return self._buffer
@property
def should_toggle_mask(self) -> bool:
""" bool: Check whether the mask should be toggled and return the value. If ``True`` is
returned then resets mask toggle back to ``False`` """
if not self._active:
return False
retval = self._triggers["toggle_mask"].is_set()
if retval:
logger.debug("Sending toggle mask")
self._triggers["toggle_mask"].clear()
return retval
@property
def should_refresh(self) -> bool:
""" bool: Check whether the preview should be updated and return the value. If ``True`` is
returned then resets the refresh trigger back to ``False`` """
if not self._active:
return False
retval = self._triggers["refresh"].is_set()
if retval:
logger.debug("Sending should refresh")
self._triggers["refresh"].clear()
return retval
@property
def should_save(self) -> bool:
""" bool: Check whether a save request has been made. If ``True`` is returned then save
trigger is set back to ``False`` """
if not self._active:
return False
retval = self._triggers["save"].is_set()
if retval:
logger.debug("Sending should save")
self._triggers["save"].clear()
return retval
@property
def should_quit(self) -> bool:
""" bool: Check whether an exit request has been made. ``True`` if an exit request has
been made otherwise ``False``.
Raises
------
Error
Re-raises any error within the preview thread
"""
if self._thread is None:
return False
self._thread.check_and_raise_error()
retval = self._triggers["quit"].is_set()
if retval:
logger.debug("Sending should stop")
return retval
def _launch_thread(self) -> FSThread | None:
""" Launch the preview viewer in it's own thread if preview has been selected
Returns
-------
:class:`lib.multithreading.FSThread` or ``None``
The thread that holds the preview viewer if preview is selected otherwise ``None``
"""
if not self._active:
return None
thread = FSThread(target=Preview,
name="preview",
args=(self._buffer, ),
kwargs={"triggers": self._triggers})
thread.start()
return thread
def shutdown(self) -> None:
""" Send a signal to shutdown the preview window. """
if not self._active:
return
logger.debug("Sending shutdown to preview viewer")
self._triggers["shutdown"].set()