From ca0af39086f926e9fd9d4302d980ce6710ed743e Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Sat, 4 Nov 2023 11:34:18 -0400 Subject: [PATCH] attempt to autofix nan values --- javascript/progressBar.js | 1 + modules/processing.py | 29 ++++++++++++----------------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/javascript/progressBar.js b/javascript/progressBar.js index 5d8e65323..0133d3c1f 100644 --- a/javascript/progressBar.js +++ b/javascript/progressBar.js @@ -111,6 +111,7 @@ function requestProgress(id_task, progressEl, galleryEl, atEnd = null, onProgres }; const start = (id_task, id_live_preview) => { // eslint-disable-line no-shadow + if (!opts.live_previews_enable || opts.live_preview_refresh_period === 0 || opts.show_progress_every_n_steps === 0) return; request('./internal/progress', { id_task, id_live_preview }, (res) => { lastState = res; const elapsedFromStart = (new Date() - dateStart) / 1000; diff --git a/modules/processing.py b/modules/processing.py index 3a5a4b435..12a590b30 100644 --- a/modules/processing.py +++ b/modules/processing.py @@ -4,6 +4,7 @@ import math import time import hashlib import random +import warnings from contextlib import nullcontext from typing import Any, Dict, List import torch @@ -726,23 +727,17 @@ def process_images(p: StableDiffusionProcessing) -> Processed: def validate_sample(sample): - ok = True - try: - sample = sample.astype(np.uint8) - return sample - except (Exception, Warning, RuntimeWarning) as e: - shared.log.error(f'Failed to validate sample values: {e}') - ok = False - if not ok: - try: - sample = np.nan_to_num(sample, nan=0, posinf=255, neginf=0) - sample = sample.astype(np.uint8) - shared.log.debug('Corrected sample values') - except (Exception, Warning, RuntimeWarning) as e: - shared.log.error(f'Failed to correct sample values: {e}') - sample = np.zeros_like(sample) - sample = sample.astype(np.uint8) - return sample + sample[0][0][0] = np.nan + with warnings.catch_warnings(record=True) as w: + cast = sample.astype(np.uint8) + if len(w) > 0: + nans = np.isnan(sample).sum() + shared.log.error(f'Failed to validate samples: sample={sample.shape} invalid={nans}') + cast = np.nan_to_num(sample) + minimum, maximum, mean = np.min(cast), np.max(cast), np.mean(cast) + cast = cast.astype(np.uint8) + shared.log.warning(f'Attempted to correct samples: min={minimum:.2f} max={maximum:.2f} mean={mean:.2f}') + return cast def process_images_inner(p: StableDiffusionProcessing) -> Processed: