| 
									
										
										
										
											2023-01-30 10:11:30 +03:00
										 |  |  | from collections import namedtuple | 
					
						
							| 
									
										
										
										
											2022-09-06 23:10:12 +03:00
										 |  |  | import numpy as np | 
					
						
							| 
									
										
										
										
											2022-09-03 12:08:45 +03:00
										 |  |  | import torch | 
					
						
							| 
									
										
										
										
											2022-09-06 23:10:12 +03:00
										 |  |  | from PIL import Image | 
					
						
							| 
									
										
										
										
											2023-01-30 09:51:06 +03:00
										 |  |  | from modules import devices, processing, images, sd_vae_approx | 
					
						
							| 
									
										
										
										
											2022-09-03 12:08:45 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-30 09:51:06 +03:00
										 |  |  | from modules.shared import opts, state | 
					
						
							| 
									
										
										
										
											2022-09-03 12:08:45 +03:00
										 |  |  | import modules.shared as shared | 
					
						
							| 
									
										
										
										
											2022-09-03 17:21:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-06 14:12:52 +03:00
										 |  |  | SamplerData = namedtuple('SamplerData', ['name', 'constructor', 'aliases', 'options']) | 
					
						
							| 
									
										
										
										
											2022-09-03 17:21:15 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-22 20:48:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-19 16:42:56 +03:00
										 |  |  | def setup_img2img_steps(p, steps=None): | 
					
						
							|  |  |  |     if opts.img2img_fix_steps or steps is not None: | 
					
						
							| 
									
										
										
										
											2023-01-04 23:56:43 +03:00
										 |  |  |         requested_steps = (steps or p.steps) | 
					
						
							|  |  |  |         steps = int(requested_steps / min(p.denoising_strength, 0.999)) if p.denoising_strength > 0 else 0 | 
					
						
							|  |  |  |         t_enc = requested_steps - 1 | 
					
						
							| 
									
										
										
										
											2022-09-16 13:38:02 +03:00
										 |  |  |     else: | 
					
						
							|  |  |  |         steps = p.steps | 
					
						
							|  |  |  |         t_enc = int(min(p.denoising_strength, 0.999) * steps) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return steps, t_enc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-24 22:39:00 +03:00
										 |  |  | approximation_indexes = {"Full": 0, "Approx NN": 1, "Approx cheap": 2} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def single_sample_to_image(sample, approximation=None): | 
					
						
							|  |  |  |     if approximation is None: | 
					
						
							|  |  |  |         approximation = approximation_indexes.get(opts.show_progress_type, 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if approximation == 2: | 
					
						
							|  |  |  |         x_sample = sd_vae_approx.cheap_approximation(sample) | 
					
						
							|  |  |  |     elif approximation == 1: | 
					
						
							|  |  |  |         x_sample = sd_vae_approx.model()(sample.to(devices.device, devices.dtype).unsqueeze(0))[0].detach() | 
					
						
							| 
									
										
										
										
											2022-12-24 14:00:17 +03:00
										 |  |  |     else: | 
					
						
							|  |  |  |         x_sample = processing.decode_first_stage(shared.sd_model, sample.unsqueeze(0))[0] | 
					
						
							| 
									
										
										
										
											2022-12-24 22:39:00 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-06 23:10:12 +03:00
										 |  |  |     x_sample = torch.clamp((x_sample + 1.0) / 2.0, min=0.0, max=1.0) | 
					
						
							|  |  |  |     x_sample = 255. * np.moveaxis(x_sample.cpu().numpy(), 0, 2) | 
					
						
							|  |  |  |     x_sample = x_sample.astype(np.uint8) | 
					
						
							|  |  |  |     return Image.fromarray(x_sample) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-22 20:48:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-24 22:39:00 +03:00
										 |  |  | def sample_to_image(samples, index=0, approximation=None): | 
					
						
							| 
									
										
										
										
											2022-12-24 14:00:17 +03:00
										 |  |  |     return single_sample_to_image(samples[index], approximation) | 
					
						
							| 
									
										
										
										
											2022-10-22 20:48:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-02 12:45:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-24 22:39:00 +03:00
										 |  |  | def samples_to_image_grid(samples, approximation=None): | 
					
						
							| 
									
										
										
										
											2022-12-24 14:00:17 +03:00
										 |  |  |     return images.image_grid([single_sample_to_image(sample, approximation) for sample in samples]) | 
					
						
							| 
									
										
										
										
											2022-10-22 20:48:13 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-06 23:10:12 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | def store_latent(decoded): | 
					
						
							|  |  |  |     state.current_latent = decoded | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-14 16:29:23 +03:00
										 |  |  |     if opts.live_previews_enable and opts.show_progress_every_n_steps > 0 and shared.state.sampling_step % opts.show_progress_every_n_steps == 0: | 
					
						
							| 
									
										
										
										
											2022-09-06 23:10:12 +03:00
										 |  |  |         if not shared.parallel_processing_allowed: | 
					
						
							| 
									
										
										
										
											2023-01-15 18:50:56 +03:00
										 |  |  |             shared.state.assign_current_image(sample_to_image(decoded)) | 
					
						
							| 
									
										
										
										
											2022-09-06 23:10:12 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-18 17:23:38 +03:00
										 |  |  | class InterruptedException(BaseException): | 
					
						
							|  |  |  |     pass |