MCPcopy Create free account
hub / github.com/Sygil-Dev/sygil-webui / sample_iteration_callback

Function sample_iteration_callback

scripts/webui.py:1657–1743  ·  view source on GitHub ↗

Called from the sampler every iteration

(image_sample: torch.Tensor, iter_num: int)

Source from the content-addressed store, hash-verified

1655 step_preview_model = model
1656
1657 def sample_iteration_callback(image_sample: torch.Tensor, iter_num: int):
1658 """Called from the sampler every iteration"""
1659 if job_info:
1660 job_info.active_iteration_cnt = iter_num
1661 record_periodic_image = job_info.rec_steps_enabled and (
1662 0 == iter_num % job_info.rec_steps_intrvl
1663 )
1664 if (
1665 record_periodic_image
1666 or job_info.refresh_active_image_requested.is_set()
1667 ):
1668 preview_start_time = time.time()
1669 if opt.optimized:
1670 step_preview_model.to(device)
1671
1672 decoded_batch: List[torch.Tensor] = []
1673 # Break up batch to save VRAM
1674 for sample in image_sample:
1675 sample = sample[
1676 None, :
1677 ] # expands the tensor as if it still had a batch dimension
1678 decoded_sample = step_preview_model.decode_first_stage(
1679 sample
1680 )[0]
1681 decoded_sample = torch.clamp(
1682 (decoded_sample + 1.0) / 2.0, min=0.0, max=1.0
1683 )
1684 decoded_sample = decoded_sample.cpu()
1685 decoded_batch.append(decoded_sample)
1686
1687 batch_size = len(decoded_batch)
1688
1689 if opt.optimized:
1690 step_preview_model.cpu()
1691
1692 images: List[Image.Image] = []
1693 # Convert tensor to image (copied from code below)
1694 for ddim in decoded_batch:
1695 x_sample = 255.0 * rearrange(ddim.numpy(), "c h w -> h w c")
1696 x_sample = x_sample.astype(np.uint8)
1697 image = Image.fromarray(x_sample)
1698 images.append(image)
1699
1700 caption = f"Iter {iter_num}"
1701 grid = image_grid(
1702 images,
1703 len(images),
1704 force_n_rows=1,
1705 captions=[caption] * len(images),
1706 )
1707
1708 # Save the images if recording steps, and append existing saved steps
1709 if job_info.rec_steps_enabled:
1710 gallery_img_size = tuple(
1711 int(0.25 * dim) for dim in images[0].size
1712 )
1713 job_info.rec_steps_imgs.append(
1714 grid.resize(gallery_img_size)

Callers

nothing calls this directly

Calls 2

image_gridFunction · 0.70
decode_first_stageMethod · 0.45

Tested by

no test coverage detected