Advertisement
Dvadch

Untitled

Aug 23rd, 2022 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.67 KB | None | 0 0
  1. import PIL
  2. import gradio as gr
  3. import argparse, os, sys, glob
  4. import torch
  5. import torch.nn as nn
  6. import numpy as np
  7. from omegaconf import OmegaConf
  8. from PIL import Image
  9. from tqdm import tqdm, trange
  10. from itertools import islice
  11. from einops import rearrange, repeat
  12. from torchvision.utils import make_grid
  13. import time
  14. from pytorch_lightning import seed_everything
  15. from torch import autocast
  16. from contextlib import contextmanager, nullcontext
  17. import accelerate
  18. import mimetypes
  19. import gc
  20. from basicsr.utils import imwrite
  21. import cv2
  22. from gfpgan import GFPGANer
  23.  
  24. mimetypes.init()
  25. mimetypes.add_type('application/javascript', '.js')
  26.  
  27.  
  28. import k_diffusion as K
  29. from ldm.util import instantiate_from_config
  30. from ldm.models.diffusion.ddim import DDIMSampler
  31. from ldm.models.diffusion.plms import PLMSSampler
  32.  
  33. def FACE_RESTORATION(image, bg_upsampling, upscale):
  34. from basicsr.archs.rrdbnet_arch import RRDBNet
  35. from realesrgan import RealESRGANer
  36. model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
  37. bg_upsampler = RealESRGANer(
  38. scale=2,
  39. model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
  40. model=model,
  41. tile=400,
  42. tile_pad=10,
  43. pre_pad=0,
  44. half=True)
  45. arch = 'clean'
  46. channel_multiplier = 2
  47. model_name = 'GFPGANv1.3'
  48. model_path = os.path.join('/content/GFPGAN/experiments/pretrained_models/GFPGANv1.3.pth')
  49. restorer = GFPGANer(
  50. model_path=model_path,
  51. upscale=upscale,
  52. arch=arch,
  53. channel_multiplier=channel_multiplier,
  54. bg_upsampler=bg_upsampler
  55. )
  56.  
  57. image=np.array(image)
  58. input_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  59.  
  60. cropped_faces, restored_faces, restored_img = restorer.enhance(
  61. input_img, has_aligned=False, only_center_face=False, paste_back=True)
  62.  
  63. for idx, (cropped_face, restored_face) in enumerate(zip(cropped_faces, restored_faces)):
  64. image = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
  65. return image
  66.  
  67. def chunk(it, size):
  68. it = iter(it)
  69. return iter(lambda: tuple(islice(it, size)), ())
  70.  
  71.  
  72. def load_model_from_config(config, ckpt, verbose=False):
  73. print(f"Loading model from {ckpt}")
  74. pl_sd = torch.load(ckpt, map_location="cpu")
  75. if "global_step" in pl_sd:
  76. print(f"Global Step: {pl_sd['global_step']}")
  77. sd = pl_sd["state_dict"]
  78. model = instantiate_from_config(config.model)
  79. m, u = model.load_state_dict(sd, strict=False)
  80. if len(m) > 0 and verbose:
  81. print("missing keys:")
  82. print(m)
  83. if len(u) > 0 and verbose:
  84. print("unexpected keys:")
  85. print(u)
  86.  
  87. model.cuda()
  88. model.eval()
  89. return model
  90.  
  91. def load_img_pil(img_pil):
  92. image = img_pil.convert("RGB")
  93. w, h = image.size
  94. print(f"loaded input image of size ({w}, {h})")
  95. w, h = map(lambda x: x - x % 64, (w, h)) # resize to integer multiple of 64
  96. image = image.resize((w, h), resample=PIL.Image.LANCZOS)
  97. print(f"cropped image to size ({w}, {h})")
  98. image = np.array(image).astype(np.float32) / 255.0
  99. image = image[None].transpose(0, 3, 1, 2)
  100. image = torch.from_numpy(image)
  101. return 2.*image - 1.
  102.  
  103. def load_img(path):
  104. return load_img_pil(Image.open(path))
  105.  
  106.  
  107. class CFGDenoiser(nn.Module):
  108. def __init__(self, model):
  109. super().__init__()
  110. self.inner_model = model
  111.  
  112. def forward(self, x, sigma, uncond, cond, cond_scale):
  113. x_in = torch.cat([x] * 2)
  114. sigma_in = torch.cat([sigma] * 2)
  115. cond_in = torch.cat([uncond, cond])
  116. uncond, cond = self.inner_model(x_in, sigma_in, cond=cond_in).chunk(2)
  117. return uncond + (cond - uncond) * cond_scale
  118.  
  119.  
  120. config = OmegaConf.load("configs/stable-diffusion/v1-inference.yaml")
  121. model = load_model_from_config(config, "/gdrive/My Drive/model.ckpt")
  122.  
  123. device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
  124. model = model.half().to(device)
  125.  
  126. def dream(prompt: str, ddim_steps: int, plms: bool, fixed_code: bool, ddim_eta: float, n_iter: int, n_samples: int, cfg_scales: str, seed: int, height: int, width: int, same_seed: bool, GFPGAN: bool, bg_upsampling: bool, upscale: int):
  127. torch.cuda.empty_cache()
  128. parser = argparse.ArgumentParser()
  129.  
  130. parser.add_argument(
  131. "--outdir",
  132. type=str,
  133. nargs="?",
  134. help="dir to write results to",
  135. default="outputs/txt2img-samples"
  136. )
  137. parser.add_argument(
  138. "--skip_grid",
  139. action='store_true',
  140. help="do not save a grid, only individual samples. Helpful when evaluating lots of samples",
  141. )
  142. parser.add_argument(
  143. "--skip_save",
  144. action='store_true',
  145. help="do not save individual samples. For speed measurements.",
  146. )
  147. parser.add_argument(
  148. "--laion400m",
  149. action='store_true',
  150. help="uses the LAION400M model",
  151. )
  152. parser.add_argument(
  153. "--H",
  154. type=int,
  155. default=height,
  156. help="image height, in pixel space",
  157. )
  158. parser.add_argument(
  159. "--W",
  160. type=int,
  161. default=width,
  162. help="image width, in pixel space",
  163. )
  164. parser.add_argument(
  165. "--C",
  166. type=int,
  167. default=4,
  168. help="latent channels",
  169. )
  170. parser.add_argument(
  171. "--f",
  172. type=int,
  173. default=8,
  174. help="downsampling factor",
  175. )
  176. parser.add_argument(
  177. "--n_rows",
  178. type=int,
  179. default=0,
  180. help="rows in the grid (default: n_samples)",
  181. )
  182. parser.add_argument(
  183. "--from-file",
  184. type=str,
  185. help="if specified, load prompts from this file",
  186. )
  187. parser.add_argument(
  188. "--config",
  189. type=str,
  190. default="configs/stable-diffusion/v1-inference.yaml",
  191. help="path to config which constructs model",
  192. )
  193. parser.add_argument(
  194. "--ckpt",
  195. type=str,
  196. default="/gdrive/My Drive/model.ckpt\model.ckpt",
  197. help="path to checkpoint of model",
  198. )
  199. parser.add_argument(
  200. "--precision",
  201. type=str,
  202. help="evaluate at this precision",
  203. choices=["full", "autocast"],
  204. default="autocast"
  205. )
  206. opt = parser.parse_args()
  207.  
  208. if opt.laion400m:
  209. print("Falling back to LAION 400M model...")
  210. opt.config = "configs/latent-diffusion/txt2img-1p4B-eval.yaml"
  211. opt.ckpt = "models/ldm/text2img-large/model.ckpt"
  212. opt.outdir = "outputs/txt2img-samples-laion400m"
  213.  
  214. accelerator = accelerate.Accelerator()
  215. device = accelerator.device
  216. rng_seed = seed_everything(seed)
  217. seeds = torch.randint(-2 ** 63, 2 ** 63 - 1, [accelerator.num_processes])
  218. torch.manual_seed(seeds[accelerator.process_index].item())
  219.  
  220. if plms:
  221. sampler = PLMSSampler(model)
  222. else:
  223. sampler = DDIMSampler(model)
  224.  
  225.  
  226. model_wrap = K.external.CompVisDenoiser(model)
  227. sigma_min, sigma_max = model_wrap.sigmas[0].item(), model_wrap.sigmas[-1].item()
  228.  
  229. os.makedirs(opt.outdir, exist_ok=True)
  230. outpath = opt.outdir
  231.  
  232. batch_size = n_samples
  233. n_rows = opt.n_rows if opt.n_rows > 0 else batch_size
  234. if not opt.from_file:
  235. assert prompt is not None
  236. data = [batch_size * [prompt]]
  237.  
  238. else:
  239. print(f"reading prompts from {opt.from_file}")
  240. with open(opt.from_file, "r") as f:
  241. data = f.read().splitlines()
  242. data = list(chunk(data, batch_size))
  243.  
  244. sample_path = os.path.join(outpath, "samples")
  245. os.makedirs(sample_path, exist_ok=True)
  246. base_count = len(os.listdir(sample_path))
  247. grid_count = len(os.listdir(outpath)) - 1
  248. seedit = 0
  249.  
  250. start_code = None
  251. if fixed_code:
  252. start_code = torch.randn([n_samples, opt.C, opt.H // opt.f, opt.W // opt.f], device=device)
  253.  
  254. precision_scope = autocast if opt.precision=="autocast" else nullcontext
  255. if len(cfg_scales) > 1: cfg_scales = list(map(float, cfg_scales.split(' ')))
  256. output_images = []
  257. with torch.no_grad():
  258. gc.collect()
  259. torch.cuda.empty_cache()
  260. with precision_scope("cuda"):
  261. with model.ema_scope():
  262. tic = time.time()
  263. all_samples = list()
  264. for n in trange(n_iter, desc="Sampling", disable =not accelerator.is_main_process):
  265. for prompts in tqdm(data, desc="data", disable=not accelerator.is_main_process):
  266. if n_iter > 1: seedit += 1
  267. for cfg in tqdm(cfg_scales, desc="cfg_scales", disable=not accelerator.is_main_process):
  268. cfg_scale = cfg
  269. uc = None
  270. if cfg_scale != 1.0:
  271. uc = model.get_learned_conditioning(batch_size * [""])
  272. if isinstance(prompts, tuple):
  273. prompts = list(prompts)
  274. c = model.get_learned_conditioning(prompts)
  275. shape = [opt.C, opt.H // opt.f, opt.W // opt.f]
  276. # samples_ddim, _ = sampler.sample(S=opt.ddim_steps,
  277. # conditioning=c,
  278. # batch_size=opt.n_samples,
  279. # shape=shape,
  280. # verbose=False,
  281. # unconditional_guidance_scale=opt.scale,
  282. # unconditional_conditioning=uc,
  283. # eta=opt.ddim_eta,
  284. # x_T=start_code)
  285.  
  286. sigmas = model_wrap.get_sigmas(ddim_steps)
  287. torch.manual_seed(rng_seed + seedit) # changes manual seeding procedure
  288. # sigmas = K.sampling.get_sigmas_karras(opt.ddim_steps, sigma_min, sigma_max, device=device)
  289. x = torch.randn([n_samples, *shape], device=device) * sigmas[0] # for GPU draw
  290. # x = torch.randn([opt.n_samples, *shape]).to(device) * sigmas[0] # for CPU draw
  291. model_wrap_cfg = CFGDenoiser(model_wrap)
  292. extra_args = {'cond': c, 'uncond': uc, 'cond_scale': cfg_scale}
  293. samples_ddim = K.sampling.sample_lms(model_wrap_cfg, x, sigmas, extra_args=extra_args, disable=not accelerator.is_main_process)
  294. x_samples_ddim = model.decode_first_stage(samples_ddim)
  295. x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
  296. x_samples_ddim = accelerator.gather(x_samples_ddim)
  297.  
  298. if accelerator.is_main_process and not opt.skip_save:
  299. for x_sample in x_samples_ddim:
  300. x_sample = 255. * rearrange(x_sample.cpu().numpy(), 'c h w -> h w c')
  301. Image.fromarray(x_sample.astype(np.uint8)).save(
  302. os.path.join(sample_path, f"{base_count:05}-{rng_seed + seedit}-{cfg_scale}_{prompt.replace(' ', '_')[:128]}.png"))
  303. output_images.append(Image.fromarray(x_sample.astype(np.uint8)))
  304.  
  305. ########
  306. base_count += 1
  307. if not same_seed: seedit += 1
  308.  
  309. if accelerator.is_main_process and not opt.skip_grid:
  310. all_samples.append(x_samples_ddim)
  311.  
  312. if accelerator.is_main_process and not opt.skip_grid:
  313. # additionally, save as grid
  314. grid = torch.stack(all_samples, 0)
  315. grid = rearrange(grid, 'n b c h w -> (n b) c h w')
  316. grid = make_grid(grid, nrow=n_rows)
  317.  
  318. # to image
  319. grid = 255. * rearrange(grid, 'c h w -> h w c').cpu().numpy()
  320. Image.fromarray(grid.astype(np.uint8)).save(os.path.join(outpath, f'grid-{grid_count:04}.png'))
  321. grid_count += 1
  322.  
  323.  
  324. toc = time.time()
  325. gc.collect()
  326. torch.cuda.empty_cache()
  327. del sampler
  328. for i in range(len(output_images)):
  329. if GFPGAN:
  330. output_images[i] = Image.fromarray(FACE_RESTORATION(output_images[i], bg_upsampling, upscale).astype(np.uint8))
  331. output_images[i].save(os.path.join(sample_path, f"{base_count:05}-{rng_seed + seedit}-{cfg_scale}_{prompt.replace(' ', '_')[:128]}.png"))
  332.  
  333. return output_images, rng_seed
  334.  
  335. def translation(prompt: str, init_img, ddim_steps: int, ddim_eta: float, n_iter: int, n_samples: int, cfg_scales: str, denoising_strength: float, seed: int, height: int, width: int, same_seed=bool):
  336. torch.cuda.empty_cache()
  337. parser = argparse.ArgumentParser()
  338.  
  339. parser.add_argument(
  340. "--outdir",
  341. type=str,
  342. nargs="?",
  343. help="dir to write results to",
  344. default="outputs/img2img-samples"
  345. )
  346.  
  347. parser.add_argument(
  348. "--skip_grid",
  349. action='store_true',
  350. help="do not save a grid, only individual samples. Helpful when evaluating lots of samples",
  351. )
  352.  
  353. parser.add_argument(
  354. "--skip_save",
  355. action='store_true',
  356. help="do not save indiviual samples. For speed measurements.",
  357. )
  358. parser.add_argument(
  359. "--C",
  360. type=int,
  361. default=4,
  362. help="latent channels",
  363. )
  364. parser.add_argument(
  365. "--f",
  366. type=int,
  367. default=8,
  368. help="downsampling factor, most often 8 or 16",
  369. )
  370. parser.add_argument(
  371. "--n_rows",
  372. type=int,
  373. default=0,
  374. help="rows in the grid (default: n_samples)",
  375. )
  376. parser.add_argument(
  377. "--from-file",
  378. type=str,
  379. help="if specified, load prompts from this file",
  380. )
  381. parser.add_argument(
  382. "--config",
  383. type=str,
  384. default="configs/stable-diffusion/v1-inference.yaml",
  385. help="path to config which constructs model",
  386. )
  387. parser.add_argument(
  388. "--ckpt",
  389. type=str,
  390. default="/gdrive/My Drive/model.ckpt\model.ckpt",
  391. help="path to checkpoint of model",
  392. )
  393. parser.add_argument(
  394. "--precision",
  395. type=str,
  396. help="evaluate at this precision",
  397. choices=["full", "autocast"],
  398. default="autocast"
  399. )
  400.  
  401. opt = parser.parse_args()
  402.  
  403. accelerator = accelerate.Accelerator()
  404. device = accelerator.device
  405. rng_seed = seed_everything(seed)
  406. seeds = torch.randint(-2 ** 63, 2 ** 63 - 1, [accelerator.num_processes])
  407. torch.manual_seed(seeds[accelerator.process_index].item())
  408.  
  409. sampler = DDIMSampler(model)
  410.  
  411. model_wrap = K.external.CompVisDenoiser(model)
  412. sigma_min, sigma_max = model_wrap.sigmas[0].item(), model_wrap.sigmas[-1].item()
  413.  
  414. os.makedirs(opt.outdir, exist_ok=True)
  415. outpath = opt.outdir
  416.  
  417. batch_size = n_samples
  418. n_rows = opt.n_rows if opt.n_rows > 0 else batch_size
  419. if not opt.from_file:
  420. prompt = prompt
  421. assert prompt is not None
  422. data = [batch_size * [prompt]]
  423. else:
  424. print(f"reading prompts from {opt.from_file}")
  425. with open(opt.from_file, "r") as f:
  426. data = f.read().splitlines()
  427. data = list(chunk(data, batch_size))
  428.  
  429. sample_path = os.path.join(outpath, "samples")
  430. os.makedirs(sample_path, exist_ok=True)
  431. base_count = len(os.listdir(sample_path))
  432. grid_count = len(os.listdir(outpath)) - 1
  433. seedit = 0
  434.  
  435. image = init_img.convert("RGB")
  436. w, h = image.size
  437. print(f"loaded input image of size ({w}, {h})")
  438. w, h = map(lambda x: x - x % 32, (width, height)) # resize to integer multiple of 32
  439. image = image.resize((w, h), resample=PIL.Image.LANCZOS)
  440. print(f"cropped image to size ({w}, {h})")
  441. image = np.array(image).astype(np.float32) / 255.0
  442. image = image[None].transpose(0, 3, 1, 2)
  443. image = torch.from_numpy(image)
  444. if len(cfg_scales) > 1: cfg_scales = list(map(float, cfg_scales.split(' ')))
  445. output_images = []
  446. precision_scope = autocast if opt.precision == "autocast" else nullcontext
  447. with torch.no_grad():
  448. gc.collect()
  449. torch.cuda.empty_cache()
  450. with precision_scope("cuda"):
  451. init_image = 2.*image - 1.
  452. init_image = init_image.to(device)
  453. init_image = repeat(init_image, '1 ... -> b ...', b=batch_size)
  454. init_latent = model.get_first_stage_encoding(model.encode_first_stage(init_image)) # move to latent space
  455. x0 = init_latent
  456.  
  457. sampler.make_schedule(ddim_num_steps=ddim_steps, ddim_eta=ddim_eta, verbose=False)
  458.  
  459. assert 0. <= denoising_strength <= 1., 'can only work with strength in [0.0, 1.0]'
  460. t_enc = int(denoising_strength * ddim_steps)
  461. print(f"target t_enc is {t_enc} steps")
  462. with model.ema_scope():
  463. tic = time.time()
  464. all_samples = list()
  465. for n in trange(n_iter, desc="Sampling", disable=not accelerator.is_main_process):
  466. for prompts in tqdm(data, desc="data", disable=not accelerator.is_main_process):
  467. for cfg in tqdm(cfg_scales, desc="cfg_scales", disable=not accelerator.is_main_process):
  468. cfg_scale = cfg
  469. uc = None
  470. if cfg_scale != 1.0:
  471. uc = model.get_learned_conditioning(batch_size * [""])
  472. if isinstance(prompts, tuple):
  473. prompts = list(prompts)
  474. c = model.get_learned_conditioning(prompts)
  475.  
  476. sigmas = model_wrap.get_sigmas(ddim_steps)
  477. torch.manual_seed(rng_seed + seedit) # changes manual seeding procedure
  478. # sigmas = K.sampling.get_sigmas_karras(ddim_steps, sigma_min, sigma_max, device=device)
  479. noise = torch.randn_like(x0) * sigmas[ddim_steps - t_enc - 1] # for GPU draw
  480. xi = x0 + noise
  481. sigma_sched = sigmas[ddim_steps - t_enc - 1:]
  482. # x = torch.randn([n_samples, *shape]).to(device) * sigmas[0] # for CPU draw
  483. model_wrap_cfg = CFGDenoiser(model_wrap)
  484. extra_args = {'cond': c, 'uncond': uc, 'cond_scale': cfg_scale}
  485. samples_ddim = K.sampling.sample_lms(model_wrap_cfg, xi, sigma_sched, extra_args=extra_args, disable=not accelerator.is_main_process)
  486. x_samples_ddim = model.decode_first_stage(samples_ddim)
  487. x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
  488. x_samples_ddim = accelerator.gather(x_samples_ddim)
  489.  
  490. if accelerator.is_main_process and not opt.skip_save:
  491. for x_sample in x_samples_ddim:
  492. x_sample = 255. * rearrange(x_sample.cpu().numpy(), 'c h w -> h w c')
  493. Image.fromarray(x_sample.astype(np.uint8)).save(
  494. os.path.join(sample_path, f"{base_count:05}-{rng_seed + seedit}-{cfg_scale}_{prompt.replace(' ', '_')[:128]}.png"))
  495. output_images.append(Image.fromarray(x_sample.astype(np.uint8)))
  496. base_count += 1
  497. if not same_seed: seedit += 1
  498.  
  499. if accelerator.is_main_process and not opt.skip_grid:
  500. all_samples.append(x_samples_ddim)
  501.  
  502. if not opt.skip_grid:
  503. # additionally, save as grid
  504. grid = torch.stack(all_samples, 0)
  505. grid = rearrange(grid, 'n b c h w -> (n b) c h w')
  506. grid = make_grid(grid, nrow=n_rows)
  507.  
  508. # to image
  509. grid = 255. * rearrange(grid, 'c h w -> h w c').cpu().numpy()
  510. Image.fromarray(grid.astype(np.uint8)).save(os.path.join(outpath, f'grid-{grid_count:04}.png'))
  511. Image.fromarray(grid.astype(np.uint8))
  512. grid_count += 1
  513.  
  514. toc = time.time()
  515. gc.collect()
  516. torch.cuda.empty_cache()
  517. del sampler
  518. return output_images, rng_seed
  519.  
  520. dream_interface = gr.Interface(
  521. dream,
  522. inputs=[
  523. gr.Textbox(placeholder="A corgi wearing a top hat as an oil painting.", lines=1),
  524. gr.Slider(minimum=1, maximum=200, step=1, label="Шаги диффузии, идеал - 100.", value=50),
  525. gr.Checkbox(label='Включить PLMS ', value=False),
  526. gr.Checkbox(label='Сэмплинг с одной точки', value=False),
  527. gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="DDIM ETA", value=0.0, visible=False),
  528. gr.Slider(minimum=1, maximum=50, step=1, label='Сколько раз сгенерировать по запросу (последовательно)', value=1),
  529. gr.Slider(minimum=1, maximum=8, step=1, label='Сколько картинок за раз (одновременно). ЖРЕТ МНОГО ПАМЯТИ', value=1),
  530. gr.Textbox(placeholder="7.0", label='Classifier Free Guidance Scales, через пробел либо только одна. Если больше одной, сэмплит один и тот же запрос с разными cfg. Обязательно число с точкой, типа 7.0 или 15.0', lines=1, value=9.0),
  531. gr.Number(label='Сид', value=-1),
  532. gr.Slider(minimum=64, maximum=2048, step=64, label="Высота", value=512),
  533. gr.Slider(minimum=64, maximum=2048, step=64, label="Ширина", value=512),
  534. gr.Checkbox(label='Один и тот же сид каждый раз. Для того чтобы генерировать одно и тоже с одинаковым запросом.', value=False),
  535. gr.Checkbox(label='GFPGAN, восстанавливает лица, может апскейлить. Все настройки ниже к нему.', value=True),
  536. gr.Checkbox(label='Улучшение фона', value=True),
  537. gr.Slider(minimum=1, maximum=8, step=1, label="Апскейлинг. 1 значит не используется.", value=1)
  538. ],
  539. outputs=[
  540. gr.Gallery(),
  541. gr.Number(label='Сид')
  542. ],
  543. title="Stable Diffusion 1.4 текст в картинку",
  544. description="создай картинку из текста, анон, K-LMS используется по умолчанию",
  545. )
  546.  
  547. # prompt, init_img, ddim_steps, plms, ddim_eta, n_iter, n_samples, cfg_scale, denoising_strength, seed
  548.  
  549. img2img_interface = gr.Interface(
  550. translation,
  551. inputs=[
  552. gr.Textbox(placeholder="A fantasy landscape, trending on artstation.", lines=1),
  553. gr.Image(value="https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg", source="upload", interactive=True, type="pil"),
  554. gr.Slider(minimum=1, maximum=200, step=1, label="Шаги диффузии, идеал - 100.", value=50),
  555. gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="DDIM ETA", value=0.0, visible=False),
  556. gr.Slider(minimum=1, maximum=50, step=1, label='Сколько раз генерировать по запросу (последовательно)', value=1),
  557. gr.Slider(minimum=1, maximum=8, step=1, label='Сколько картинок за раз (одновременно) ЖРЕТ МНОГО ПАМЯТИ', value=1),
  558. gr.Textbox(placeholder="7.0", label='Classifier Free Guidance Scales, через пробел либо только одна. Если больше одной, сэмплит один и тот же запрос с разнами cfg. Обязательно число с точкой, типа 7.0 или 15.0', lines=1, value=7.0),
  559. gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Процент шагов, указанных выше чтобы пройтись по картинке. Моюно считать "силой"', value=0.75),
  560. gr.Number(label='Сид', value=-1),
  561. gr.Slider(minimum=64, maximum=2048, step=64, label="Resize Height", value=512),
  562. gr.Slider(minimum=64, maximum=2048, step=64, label="Resize Width", value=512),
  563. gr.Checkbox(label='Один и тот же сид каждый раз. Для того чтобы генерировать одно и тоже с одинаковым запросом.', value=False)
  564. ],
  565. outputs=[
  566. gr.Gallery(),
  567. gr.Number(label='Seed')
  568. ],
  569. title="Stable Diffusion Image-to-Image",
  570. description="генерация изображения из изображения",
  571. )
  572.  
  573. demo = gr.TabbedInterface(interface_list=[dream_interface, img2img_interface], tab_names=["Dream", "Image Translation"])
  574.  
  575. demo.launch(share=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement