Repository navigation
Expand file tree
/
Copy pathimage_pixel.dgl
More file actions
64 lines (61 loc) · 2.47 KB
/
Copy pathimage_pixel.dgl
File metadata and controls
64 lines (61 loc) · 2.47 KB
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
# version: 0.0.0
var {
{{variables}}
}
{% include "image_helpers" %}
fn main({{inputs}}) {
base_model = {{model_loader}}(model_path{{model_options}})
base_text = load_text_encoder(text_path{{text_options}})
vae = {{vae_loader}}(vae_path{{vae_options}})
{{loras}}
model = {{patched_model}}
text = {{patched_text}}
encode_rng = make_rng(rng_type, seed = seed)
{% if flags.has_init %}
source = image::to_f32(fit_canvas(input_image, width, height, mode = "nearest"), channels = {{channels}})
base = tensor::affine(source, scale = 2, offset = -1)
{% else %}
base = tensor::zeros([width, height, {{channels}}, 1])
{% endif %}
{{references}}
{% if flags.has_mask %}
mask_pixels = tensor::threshold(image::to_f32(fit_canvas(tensor::grayscale(input_mask, axis = 0), width, height, mode = "nearest_exact")), threshold = 0.5)
mask_latent = mask_pixels
{% endif %}
positive_text = encode_prompt(text, prompt, width = width, height = height{{prompt_options}})
{% if flags.minit2i %}
negative_text = zero_out(positive_text, fields = ["vector"])
{% else %}
{% if flags.encode_negative %}
negative_text = encode_prompt(text, {{negative_prompt_input}}, width = width, height = height{{negative_prompt_options}})
{% else %}
negative_text = positive_text
{% endif %}
{% endif %}
pos = predictor(model, positive_text{{predictor_options}})
neg = predictor(model, negative_text{{negative_predictor_options}})
guided = {{guidance_function}}(pos, neg, scale = cfg_scale{{guidance_options}})
{% if flags.custom_sigmas %}
full_sigmas = tensor::vector(custom_sigmas, dtype = "f32")
{% else %}
full_sigmas = make_sigmas({{schedule_predictor}}, latent = base, steps = steps, scheduler = scheduler{{schedule_options}})
{% endif %}
{% if flags.has_init %}
sigmas = slice_sigmas_by_strength(full_sigmas, strength)
{% else %}
sigmas = full_sigmas
{% endif %}
rng = make_rng(rng_type, seed = seed)
{% if flags.shared_rng %}
sampler_rng = rng
{% else %}
sampler_rng = make_rng(sampler_rng_type, seed = seed)
{% endif %}
start = add_noise(guided, base, tensor::randn_like(base, rng = rng), sigma = sigmas[0])
latent = sample(start, sigmas, predict = guided, method = method, rng = sampler_rng, eta = eta{{sample_options}})
{% if flags.hires %}
{% include "image_hires" %}
{% endif %}
pixels = tensor::clamp(tensor::affine({{result_latent}}, scale = 0.5, offset = 0.5), 0, 1)
return {image = image::to_u8(pixels), latent = {{result_latent}}}
}