1
0
Fork 0
mirror of https://github.com/deepfakes/faceswap synced 2025-06-08 03:26:47 -04:00
faceswap/lib/gui/display_command.py
torzdf d8557c1970
Faceswap 2.0 (#1045)
* Core Updates
    - Remove lib.utils.keras_backend_quiet and replace with get_backend() where relevant
    - Document lib.gpu_stats and lib.sys_info
    - Remove call to GPUStats.is_plaidml from convert and replace with get_backend()
    - lib.gui.menu - typofix

* Update Dependencies
Bump Tensorflow Version Check

* Port extraction to tf2

* Add custom import finder for loading Keras or tf.keras depending on backend

* Add `tensorflow` to KerasFinder search path

* Basic TF2 training running

* model.initializers - docstring fix

* Fix and pass tests for tf2

* Replace Keras backend tests with faceswap backend tests

* Initial optimizers update

* Monkey patch tf.keras optimizer

* Remove custom Adam Optimizers and Memory Saving Gradients

* Remove multi-gpu option. Add Distribution to cli

* plugins.train.model._base: Add Mirror, Central and Default distribution strategies

* Update tensorboard kwargs for tf2

* Penalized Loss - Fix for TF2 and AMD

* Fix syntax for tf2.1

* requirements typo fix

* Explicit None for clipnorm if using a distribution strategy

* Fix penalized loss for distribution strategies

* Update Dlight

* typo fix

* Pin to TF2.2

* setup.py - Install tensorflow from pip if not available in Conda

* Add reduction options and set default for mirrored distribution strategy

* Explicitly use default strategy rather than nullcontext

* lib.model.backup_restore documentation

* Remove mirrored strategy reduction method and default based on OS

* Initial restructure - training

* Remove PingPong
Start model.base refactor

* Model saving and resuming enabled

* More tidying up of model.base

* Enable backup and snapshotting

* Re-enable state file
Remove loss names from state file
Fix print loss function
Set snapshot iterations correctly

* Revert original model to Keras Model structure rather than custom layer
Output full model and sub model summary
Change NNBlocks to callables rather than custom keras layers

* Apply custom Conv2D layer

* Finalize NNBlock restructure
Update Dfaker blocks

* Fix reloading model under a different distribution strategy

* Pass command line arguments through to trainer

* Remove training_opts from model and reference params directly

* Tidy up model __init__

* Re-enable tensorboard logging
Suppress "Model Not Compiled" warning

* Fix timelapse

* lib.model.nnblocks - Bugfix residual block
Port dfaker
bugfix original

* dfl-h128 ported

* DFL SAE ported

* IAE Ported

* dlight ported

* port lightweight

* realface ported

* unbalanced ported

* villain ported

* lib.cli.args - Update Batchsize + move allow_growth to config

* Remove output shape definition
Get image sizes per side rather than globally

* Strip mask input from encoder

* Fix learn mask and output learned mask to preview

* Trigger Allow Growth prior to setting strategy

* Fix GUI Graphing

* GUI - Display batchsize correctly + fix training graphs

* Fix penalized loss

* Enable mixed precision training

* Update analysis displayed batch to match input

* Penalized Loss - Multi-GPU Fix

* Fix all losses for TF2

* Fix Reflect Padding

* Allow different input size for each side of the model

* Fix conv-aware initialization on reload

* Switch allow_growth order

* Move mixed_precision to cli

* Remove distrubution strategies

* Compile penalized loss sub-function into LossContainer

* Bump default save interval to 250
Generate preview on first iteration but don't save
Fix iterations to start at 1 instead of 0
Remove training deprecation warnings
Bump some scripts.train loglevels

* Add ability to refresh preview on demand on pop-up window

* Enable refresh of training preview from GUI

* Fix Convert
Debug logging in Initializers

* Fix Preview Tool

* Update Legacy TF1 weights to TF2
Catch stats error on loading stats with missing logs

* lib.gui.popup_configure - Make more responsive + document

* Multiple Outputs supported in trainer
Original Model - Mask output bugfix

* Make universal inference model for convert
Remove scaling from penalized mask loss (now handled at input to y_true)

* Fix inference model to work properly with all models

* Fix multi-scale output for convert

* Fix clipnorm issue with distribution strategies
Edit error message on OOM

* Update plaidml losses

* Add missing file

* Disable gmsd loss for plaidnl

* PlaidML - Basic training working

* clipnorm rewriting for mixed-precision

* Inference model creation bugfixes

* Remove debug code

* Bugfix: Default clipnorm to 1.0

* Remove all mask inputs from training code

* Remove mask inputs from convert

* GUI - Analysis Tab - Docstrings

* Fix rate in totals row

* lib.gui - Only update display pages if they have focus

* Save the model on first iteration

* plaidml - Fix SSIM loss with penalized loss

* tools.alignments - Remove manual and fix jobs

* GUI - Remove case formatting on help text

* gui MultiSelect custom widget - Set default values on init

* vgg_face2 - Move to plugins.extract.recognition and use plugins._base base class
cli - Add global GPU Exclude Option
tools.sort - Use global GPU Exlude option for backend
lib.model.session - Exclude all GPUs when running in CPU mode
lib.cli.launcher - Set backend to CPU mode when all GPUs excluded

* Cascade excluded devices to GPU Stats

* Explicit GPU selection for Train and Convert

* Reduce Tensorflow Min GPU Multiprocessor Count to 4

* remove compat.v1 code from extract

* Force TF to skip mixed precision compatibility check if GPUs have been filtered

* Add notes to config for non-working AMD losses

* Rasie error if forcing extract to CPU mode

* Fix loading of legace dfl-sae weights + dfl-sae typo fix

* Remove unused requirements
Update sphinx requirements
Fix broken rst file locations

* docs: lib.gui.display

* clipnorm amd condition check

* documentation - gui.display_analysis

* Documentation - gui.popup_configure

* Documentation - lib.logger

* Documentation - lib.model.initializers

* Documentation - lib.model.layers

* Documentation - lib.model.losses

* Documentation - lib.model.nn_blocks

* Documetation - lib.model.normalization

* Documentation - lib.model.session

* Documentation - lib.plaidml_stats

* Documentation: lib.training_data

* Documentation: lib.utils

* Documentation: plugins.train.model._base

* GUI Stats: prevent stats from using GPU

* Documentation - Original Model

* Documentation: plugins.model.trainer._base

* linting

* unit tests: initializers + losses

* unit tests: nn_blocks

* bugfix - Exclude gpu devices in train, not include

* Enable Exclude-Gpus in Extract

* Enable exclude gpus in tools

* Disallow multiple plugin types in a single model folder

* Automatically add exclude_gpus argument in for cpu backends

* Cpu backend fixes

* Relax optimizer test threshold

* Default Train settings - Set mask to Extended

* Update Extractor cli help text
Update to Python 3.8

* Fix FAN to run on CPU

* lib.plaidml_tools - typofix

* Linux installer - check for curl

* linux installer - typo fix
2020-08-12 10:36:41 +01:00

322 lines
13 KiB
Python

#!/usr/bin python3
""" Command specific tabs of Display Frame of the Faceswap GUI """
import datetime
import logging
import os
import tkinter as tk
from tkinter import ttk
from .display_graph import TrainingGraph
from .display_page import DisplayOptionalPage
from .custom_widgets import Tooltip
from .stats import Calculations
from .control_helper import set_slider_rounding
from .utils import FileHandler, get_config, get_images, preview_trigger
logger = logging.getLogger(__name__) # pylint: disable=invalid-name
class PreviewExtract(DisplayOptionalPage): # pylint: disable=too-many-ancestors
""" Tab to display output preview images for extract and convert """
def display_item_set(self):
""" Load the latest preview if available """
logger.trace("Loading latest preview")
size = 256 if self.command == "convert" else 128
get_images().load_latest_preview(thumbnail_size=int(size * get_config().scaling_factor),
frame_dims=(self.winfo_width(), self.winfo_height()))
self.display_item = get_images().previewoutput
def display_item_process(self):
""" Display the preview """
logger.trace("Displaying preview")
if not self.subnotebook.children:
self.add_child()
else:
self.update_child()
def add_child(self):
""" Add the preview label child """
logger.debug("Adding child")
preview = self.subnotebook_add_page(self.tabname, widget=None)
lblpreview = ttk.Label(preview, image=get_images().previewoutput[1])
lblpreview.pack(side=tk.TOP, anchor=tk.NW)
Tooltip(lblpreview, text=self.helptext, wraplength=200)
def update_child(self):
""" Update the preview image on the label """
logger.trace("Updating preview")
for widget in self.subnotebook_get_widgets():
widget.configure(image=get_images().previewoutput[1])
def save_items(self):
""" Open save dialogue and save preview """
location = FileHandler("dir", None).retfile
if not location:
return
filename = "extract_convert_preview"
now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = os.path.join(location,
"{}_{}.{}".format(filename,
now,
"png"))
get_images().previewoutput[0].save(filename)
logger.debug("Saved preview to %s", filename)
print("Saved preview to {}".format(filename))
class PreviewTrain(DisplayOptionalPage): # pylint: disable=too-many-ancestors
""" Training preview image(s) """
def __init__(self, *args, **kwargs):
self.update_preview = get_config().tk_vars["updatepreview"]
super().__init__(*args, **kwargs)
def add_options(self):
""" Add the additional options """
self.add_option_refresh()
super().add_options()
def add_option_refresh(self):
""" Add refresh button to refresh preview immediately """
logger.debug("Adding refresh option")
btnrefresh = ttk.Button(self.optsframe,
image=get_images().icons["reload"],
command=preview_trigger().set)
btnrefresh.pack(padx=2, side=tk.RIGHT)
Tooltip(btnrefresh,
text="Preview updates at every model save. Click to refresh now.",
wraplength=200)
logger.debug("Added refresh option")
def display_item_set(self):
""" Load the latest preview if available """
logger.trace("Loading latest preview")
if not self.update_preview.get():
logger.trace("Preview not updated")
return
get_images().load_training_preview()
self.display_item = get_images().previewtrain
def display_item_process(self):
""" Display the preview(s) resized as appropriate """
logger.trace("Displaying preview")
sortednames = sorted(list(get_images().previewtrain.keys()))
existing = self.subnotebook_get_titles_ids()
should_update = self.update_preview.get()
for name in sortednames:
if name not in existing.keys():
self.add_child(name)
elif should_update:
tab_id = existing[name]
self.update_child(tab_id, name)
if should_update:
self.update_preview.set(False)
def add_child(self, name):
""" Add the preview canvas child """
logger.debug("Adding child")
preview = PreviewTrainCanvas(self.subnotebook, name)
preview = self.subnotebook_add_page(name, widget=preview)
Tooltip(preview, text=self.helptext, wraplength=200)
self.vars["modified"].set(get_images().previewtrain[name][2])
def update_child(self, tab_id, name):
""" Update the preview canvas """
logger.debug("Updating preview")
if self.vars["modified"].get() != get_images().previewtrain[name][2]:
self.vars["modified"].set(get_images().previewtrain[name][2])
widget = self.subnotebook_page_from_id(tab_id)
widget.reload()
def save_items(self):
""" Open save dialogue and save preview """
location = FileHandler("dir", None).retfile
if not location:
return
for preview in self.subnotebook.children.values():
preview.save_preview(location)
class PreviewTrainCanvas(ttk.Frame): # pylint: disable=too-many-ancestors
""" Canvas to hold a training preview image """
def __init__(self, parent, previewname):
logger.debug("Initializing %s: (previewname: '%s')", self.__class__.__name__, previewname)
ttk.Frame.__init__(self, parent)
self.name = previewname
get_images().resize_image(self.name, None)
self.previewimage = get_images().previewtrain[self.name][1]
self.canvas = tk.Canvas(self, bd=0, highlightthickness=0)
self.canvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.imgcanvas = self.canvas.create_image(0,
0,
image=self.previewimage,
anchor=tk.NW)
self.bind("<Configure>", self.resize)
logger.debug("Initialized %s:", self.__class__.__name__)
def resize(self, event):
""" Resize the image to fit the frame, maintaining aspect ratio """
logger.trace("Resizing preview image")
framesize = (event.width, event.height)
# Sometimes image is resized before frame is drawn
framesize = None if framesize == (1, 1) else framesize
get_images().resize_image(self.name, framesize)
self.reload()
def reload(self):
""" Reload the preview image """
logger.trace("Reloading preview image")
self.previewimage = get_images().previewtrain[self.name][1]
self.canvas.itemconfig(self.imgcanvas, image=self.previewimage)
def save_preview(self, location):
""" Save the figure to file """
filename = self.name
now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
filename = os.path.join(location,
"{}_{}.{}".format(filename,
now,
"png"))
get_images().previewtrain[self.name][0].save(filename)
logger.debug("Saved preview to %s", filename)
print("Saved preview to {}".format(filename))
class GraphDisplay(DisplayOptionalPage): # pylint: disable=too-many-ancestors
""" The Graph Tab of the Display section """
def __init__(self, parent, tab_name, helptext, waittime, command=None):
self.trace_var = None
super().__init__(parent, tab_name, helptext, waittime, command)
def add_options(self):
""" Add the additional options """
self.add_option_refresh()
super().add_options()
self.add_option_smoothing()
def add_option_refresh(self):
""" Add refresh button to refresh graph immediately """
logger.debug("Adding refresh option")
tk_var = get_config().tk_vars["refreshgraph"]
btnrefresh = ttk.Button(self.optsframe,
image=get_images().icons["reload"],
command=lambda: tk_var.set(True))
btnrefresh.pack(padx=2, side=tk.RIGHT)
Tooltip(btnrefresh,
text="Graph updates at every model save. Click to refresh now.",
wraplength=200)
logger.debug("Added refresh option")
def add_option_smoothing(self):
""" Add refresh button to refresh graph immediately """
logger.debug("Adding Smoothing Slider")
tk_var = get_config().tk_vars["smoothgraph"]
min_max = (0, 0.99)
hlp = "Set the smoothing amount. 0 is no smoothing, 0.99 is maximum smoothing."
ctl_frame = ttk.Frame(self.optsframe)
ctl_frame.pack(padx=2, side=tk.RIGHT)
lbl = ttk.Label(ctl_frame, text="Smoothing Amount:", anchor=tk.W)
lbl.pack(pady=5, side=tk.LEFT, anchor=tk.N, expand=True)
tbox = ttk.Entry(ctl_frame, width=6, textvariable=tk_var, justify=tk.RIGHT)
tbox.pack(padx=(0, 5), side=tk.RIGHT)
ctl = ttk.Scale(
ctl_frame,
variable=tk_var,
command=lambda val, var=tk_var, dt=float, rn=2, mm=(0, 0.99):
set_slider_rounding(val, var, dt, rn, mm))
ctl["from_"] = min_max[0]
ctl["to"] = min_max[1]
ctl.pack(padx=5, pady=5, fill=tk.X, expand=True)
for item in (tbox, ctl):
Tooltip(item,
text=hlp,
wraplength=200)
logger.debug("Added Smoothing Slider")
def display_item_set(self):
""" Load the graph(s) if available """
session = get_config().session
smooth_amount_var = get_config().tk_vars["smoothgraph"]
if session.initialized and session.logging_disabled:
logger.trace("Logs disabled. Hiding graph")
self.set_info("Graph is disabled as 'no-logs' has been selected")
self.display_item = None
if self.trace_var is not None:
smooth_amount_var.trace_vdelete("w", self.trace_var)
self.trace_var = None
elif session.initialized:
logger.trace("Loading graph")
self.display_item = session
if self.trace_var is None:
self.trace_var = smooth_amount_var.trace("w", self.smooth_amount_callback)
else:
self.display_item = None
if self.trace_var is not None:
smooth_amount_var.trace_vdelete("w", self.trace_var)
self.trace_var = None
def display_item_process(self):
""" Add a single graph to the graph window """
logger.debug("Adding graph")
existing = list(self.subnotebook_get_titles_ids().keys())
loss_keys = [key for key in self.display_item.loss_keys if key != "total"]
display_tabs = sorted(set(key[:-1].rstrip("_") for key in loss_keys))
for loss_key in display_tabs:
tabname = loss_key.replace("_", " ").title()
if tabname in existing:
continue
display_keys = [key for key in loss_keys if key.startswith(loss_key)]
data = Calculations(session=get_config().session,
display="loss",
loss_keys=display_keys,
selections=["raw", "smoothed"],
smooth_amount=get_config().tk_vars["smoothgraph"].get())
self.add_child(tabname, data)
def smooth_amount_callback(self, *args):
""" Update each graph's smooth amount on variable change """
smooth_amount = get_config().tk_vars["smoothgraph"].get()
logger.debug("Updating graph smooth_amount: (new_value: %s, args: %s)",
smooth_amount, args)
for graph in self.subnotebook.children.values():
graph.calcs.args["smooth_amount"] = smooth_amount
def add_child(self, name, data):
""" Add the graph for the selected keys """
logger.debug("Adding child: %s", name)
graph = TrainingGraph(self.subnotebook, data, "Loss")
graph.build()
graph = self.subnotebook_add_page(name, widget=graph)
Tooltip(graph, text=self.helptext, wraplength=200)
def save_items(self):
""" Open save dialogue and save graphs """
graphlocation = FileHandler("dir", None).retfile
if not graphlocation:
return
for graph in self.subnotebook.children.values():
graph.save_fig(graphlocation)
def close(self):
""" Clear the plots from RAM """
if self.trace_var is not None:
get_config().tk_vars["smoothgraph"].trace_vdelete("w", self.trace_var)
self.trace_var = None
if self.subnotebook is None:
logger.debug("No graphs to clear. Returning")
return
for name, graph in self.subnotebook.children.items():
logger.debug("Clearing: %s", name)
graph.clear()
super().close()