loopback.py 5.1 KB
Newer Older
J
James Railton 已提交
1
import math
2 3

import gradio as gr
J
James Railton 已提交
4 5
import modules.scripts as scripts
from modules import deepbooru, images, processing, shared
6
from modules.processing import Processed
J
James Railton 已提交
7
from modules.shared import opts, state
M
me 已提交
8

9 10 11 12 13 14 15 16

class Script(scripts.Script):
    def title(self):
        return "Loopback"

    def show(self, is_img2img):
        return is_img2img

M
me 已提交
17 18
    def ui(self, is_img2img):        
        loops = gr.Slider(minimum=1, maximum=32, step=1, label='Loops', value=4, elem_id=self.elem_id("loops"))
J
James Railton 已提交
19 20
        final_denoising_strength = gr.Slider(minimum=0, maximum=1, step=0.01, label='Final denoising strength', value=0.5, elem_id=self.elem_id("final_denoising_strength"))
        denoising_curve = gr.Dropdown(label="Denoising strength curve", choices=["Aggressive", "Linear", "Lazy"], value="Linear")
21
        append_interrogation = gr.Dropdown(label="Append interrogated prompt at each iteration", choices=["None", "CLIP", "DeepBooru"], value="None")
22

J
James Railton 已提交
23
        return [loops, final_denoising_strength, denoising_curve, append_interrogation]
24

J
James Railton 已提交
25
    def run(self, p, loops, final_denoising_strength, denoising_curve, append_interrogation):
26 27 28
        processing.fix_seed(p)
        batch_count = p.n_iter
        p.extra_generation_params = {
J
James Railton 已提交
29 30
            "Final denoising strength": final_denoising_strength,
            "Denoising curve": denoising_curve
31 32 33 34 35
        }

        p.batch_size = 1
        p.n_iter = 1

J
James Railton 已提交
36
        info = None
37 38
        initial_seed = None
        initial_info = None
J
James Railton 已提交
39
        initial_denoising_strength = p.denoising_strength
40 41 42

        grids = []
        all_images = []
43
        original_init_image = p.init_images
44
        original_prompt = p.prompt
45 46
        state.job_count = loops * batch_count

47
        initial_color_corrections = [processing.setup_color_correction(p.init_images[0])]
48

J
James Railton 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        def calculate_denoising_strength(loop):
            strength = initial_denoising_strength

            if loops == 1:
                return strength

            progress = loop / (loops - 1)
            match denoising_curve:
                case "Aggressive":
                    strength = math.sin((progress) * math.pi * 0.5)

                case "Lazy":
                    strength = 1 - math.cos((progress) * math.pi * 0.5)

                case _:
                    strength = progress

            change = (final_denoising_strength - initial_denoising_strength) * strength
            return initial_denoising_strength + change
68

J
James Railton 已提交
69 70 71
        history = []

        for n in range(batch_count):
72 73 74
            # Reset to original init image at the start of each batch
            p.init_images = original_init_image

J
James Railton 已提交
75 76 77 78 79
            # Reset to original denoising strength
            p.denoising_strength = initial_denoising_strength

            last_image = None

80 81 82 83
            for i in range(loops):
                p.n_iter = 1
                p.batch_size = 1
                p.do_not_save_grid = True
84 85 86

                if opts.img2img_color_correction:
                    p.color_corrections = initial_color_corrections
87

88 89 90 91 92 93 94
                if append_interrogation != "None":
                    p.prompt = original_prompt + ", " if original_prompt != "" else ""
                    if append_interrogation == "CLIP":
                        p.prompt += shared.interrogator.interrogate(p.init_images[0])
                    elif append_interrogation == "DeepBooru":
                        p.prompt += deepbooru.model.tag(p.init_images[0])

95 96 97 98
                state.job = f"Iteration {i + 1}/{loops}, batch {n + 1}/{batch_count}"

                processed = processing.process_images(p)

J
James Railton 已提交
99 100 101 102
                # Generation cancelled.
                if state.interrupted:
                    break

103 104 105 106 107
                if initial_seed is None:
                    initial_seed = processed.seed
                    initial_info = processed.info

                p.seed = processed.seed + 1
J
James Railton 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
                p.denoising_strength = calculate_denoising_strength(i + 1)
                
                if state.skipped:
                    break

                last_image = processed.images[0]
                p.init_images = [last_image]

                if batch_count == 1:
                    history.append(last_image)
                    all_images.append(last_image)

            if batch_count > 1 and not state.skipped and not state.interrupted:
                history.append(last_image)
                all_images.append(last_image)
                
            if state.interrupted:
                    break

        if len(history) > 1:
128 129 130 131
            grid = images.image_grid(history, rows=1)
            if opts.grid_save:
                images.save_image(grid, p.outpath_grids, "grid", initial_seed, p.prompt, opts.grid_format, info=info, short_filename=not opts.grid_extended_filename, grid=True, p=p)

J
James Railton 已提交
132 133 134 135
            if opts.return_grid:
                grids.append(grid)
                
        all_images = grids + all_images
136 137 138 139

        processed = Processed(p, all_images, initial_seed, initial_info)

        return processed