網(wǎng)站微信認證費用多少錢八上數(shù)學優(yōu)化設計答案
目錄
ControlNet 介紹
ControlNet 14種模型分別是用來做什么的
ControlNet 運行環(huán)境搭建
用到的相關模型地址
ControlNet 介紹
ControlNet 是一種用于控制擴散模型的神經(jīng)網(wǎng)絡結構,可以通過添加額外的條件來實現(xiàn)對圖像生成的控制12。它通過將神經(jīng)網(wǎng)絡塊的權重復制到一個“鎖定”的副本和一個“可訓練”的副本來實現(xiàn)這一點。 “可訓練”的副本學習你的條件,而“鎖定”的副本保留你的模型。這樣,使用小規(guī)模的圖像對數(shù)據(jù)集進行訓練不會破壞生產(chǎn)就緒的擴散模型。
ControlNet 的創(chuàng)新之處在于它解決了空間一致性的問題。以前,沒有有效的方法可以告訴 AI 模型保留輸入圖像的哪些部分,而 ControlNet 通過引入一種方法來實現(xiàn)這一點,使得穩(wěn)定擴散模型能夠使用額外的輸入條件來指導模型的行為。
ControlNet 可以通過重復上述簡單的結構 14 次來控制穩(wěn)定擴散。這樣,ControlNet 就可以重用 SD 編碼器作為一個深層、強大、穩(wěn)健和強大的骨干網(wǎng)絡,來學習多樣化的控制1。
ControlNet 可以使用各種技術來對輸入圖像和提示進行條件化,例如姿態(tài)、邊緣檢測、深度圖等。它可以讓我們通過不同的方式來控制最終的圖像生成,例如涂鴉、交互式分割、內容混合等。
ControlNet 14種模型分別是用來做什么的
- Canny: 用于生成邊緣檢測圖像,可以用于AI繪畫或者風格遷移。
- Depth: 用于生成深度圖像,可以用于3D重建或者虛擬現(xiàn)實。
- Openpose: 用于生成人體姿態(tài)估計圖像,可以用于動作識別或者動畫制作。
- Style: 用于生成不同風格的圖像,可以用于藝術創(chuàng)作或者濾鏡效果。
- MLSD: 用于生成直線檢測圖像,可以用于幾何變換或者透視校正。
- Normal: 用于生成法線圖像,可以用于光照模擬或者材質編輯。
- Seg: 用于生成分割圖像,可以用于物體識別或者背景替換。
- Inpaint: 用于生成修復圖像,可以用于去除水印或者填補空缺。
- Lineart: 用于生成線稿圖像,可以用于漫畫制作或者素描練習。
- Lineart_anime: 用于生成動漫風格的線稿圖像,可以用于二次元創(chuàng)作或者上色。
- Scribble: 用于生成涂鴉圖像,可以用于草圖設計或者兒童游戲。
- Softedge: 用于生成軟邊緣圖像,可以用于模糊效果或者邊緣檢測。
- Shuffle: 用于生成隨機排列的圖像,可以用于拼圖游戲或者視覺混亂。
- IP2P: 用于生成圖片到圖片的轉換,可以用于風格遷移或者內容變換。
ControlNet 運行環(huán)境搭建
- 克隆項目
git clone --recursive https:\\github.com\lllyasviel/ControlNet-v1-1-nightly
- 創(chuàng)建虛擬環(huán)境
cd ControlNet-v1-1-nightly
conda env create -f=environment.yml
- 試運行depth模型
首先去huggingface下載,control_v11flp_sd15_depth.pth模型和v1-5-pruned.ckpt兩個模型放置在models目錄下,運行代碼
python gradio_depth.py
瀏覽器打開http://127.0.0.1:7860/
- 使用視頻生成連續(xù)的幀并合成視頻文件
生成得太慢了,這里只生成100幀,模型用的原生stablediffusion的模型,對人物的繪制不太好,用經(jīng)過lora訓練的模型生成的視頻更好
from share import *
import configimport cv2
import einops
import gradio as gr
import sys
import numpy as np
import torch
import randomfrom pytorch_lightning import seed_everything
from annotator.util import resize_image, HWC3
from annotator.openpose import OpenposeDetector
from cldm.model import create_model, load_state_dict
from cldm.ddim_hacked import DDIMSamplerpreprocessor = Nonemodel_name = 'control_v11p_sd15_openpose'
model = create_model(f'./models/{model_name}.yaml').cpu()
model.load_state_dict(load_state_dict('./models/v1-5-pruned.ckpt', location='cuda'), strict=False)
model.load_state_dict(load_state_dict(f'./models/{model_name}.pth', location='cuda'), strict=False)
model = model.cuda()
ddim_sampler = DDIMSampler(model)# def process(det, input_image, prompt, a_prompt, n_prompt, num_samples, image_resolution, detect_resolution, ddim_steps, guess_mode, strength, scale, seed, eta):
def process(input_image, prompt, det="Openpose_Full", seed=1, num_samples=1, detect_resolution=512, image_resolution=512, guess_mode=False, a_prompt="best quality", n_prompt="lowres, bad anatomy, bad hands, cropped, worst quality", strength=1.0, ddim_steps=20, eta=1.0, scale=9.0):global preprocessorif 'Openpose' in det:if not isinstance(preprocessor, OpenposeDetector):preprocessor = OpenposeDetector()with torch.no_grad():input_image = HWC3(input_image)if det == 'None':detected_map = input_image.copy()else:detected_map = preprocessor(resize_image(input_image, detect_resolution), hand_and_face='Full' in det)detected_map = HWC3(detected_map)img = resize_image(input_image, image_resolution)H, W, C = img.shapedetected_map = cv2.resize(detected_map, (W, H), interpolation=cv2.INTER_LINEAR)control = torch.from_numpy(detected_map.copy()).float().cuda() / 255.0control = torch.stack([control for _ in range(num_samples)], dim=0)control = einops.rearrange(control, 'b h w c -> b c h w').clone()if seed == -1:seed = random.randint(0, 65535)seed_everything(seed)if config.save_memory:model.low_vram_shift(is_diffusing=False)cond = {"c_concat": [control], "c_crossattn": [model.get_learned_conditioning([prompt + ', ' + a_prompt] * num_samples)]}un_cond = {"c_concat": None if guess_mode else [control], "c_crossattn": [model.get_learned_conditioning([n_prompt] * num_samples)]}shape = (4, H // 8, W // 8)if config.save_memory:model.low_vram_shift(is_diffusing=True)model.control_scales = [strength * (0.825 ** float(12 - i)) for i in range(13)] if guess_mode else ([strength] * 13)# Magic number. IDK why. Perhaps because 0.825**12<0.01 but 0.826**12>0.01samples, intermediates = ddim_sampler.sample(ddim_steps, num_samples,shape, cond, verbose=False, eta=eta,unconditional_guidance_scale=scale,unconditional_conditioning=un_cond)if config.save_memory:model.low_vram_shift(is_diffusing=False)x_samples = model.decode_first_stage(samples)x_samples = (einops.rearrange(x_samples, 'b c h w -> b h w c') * 127.5 + 127.5).cpu().numpy().clip(0, 255).astype(np.uint8)results = [x_samples[i] for i in range(num_samples)]return [detected_map] + resultsif __name__=='__main__':video_capture = cv2.VideoCapture(sys.argv[1])fps = int(video_capture.get(cv2.CAP_PROP_FPS))frame_size = (int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)),int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))count = video_capture.get(cv2.CAP_PROP_FRAME_COUNT)output_video = Noneinit = Falsewhile True:ret, frame = video_capture.read()if not ret:breakoimage = process(input_image=np.uint8(frame), prompt="photorealistic,long_hair,realistic,solo,long_hair,(photorealistic:1.4),best quality,ultra high res,teeth,Long sleeve,""Blue dress,full body,big breasts, 3girls,Grin,graffiti (medium),ok sign,sexy,""smile,stand,1girl,full body,beautiful,masterpiece,best quality,extremely detailed face,perfect lighting,1girl,solo""best quality,ultra high res,(photorealistic:1.4),parted lipslipstick,ultra detailed,Peach buttock,looking at viewer,masterpiece,best quality")print(oimage[1].shape)if init is False:output_video = cv2.VideoWriter("output_video.mp4", cv2.VideoWriter_fourcc(*'mp4v'), fps, (oimage[1].shape[1], oimage[1].shape[0]))init = Trueoutput_video.write(oimage[1])print("==> frame:%d count:%d" % (video_capture.get(cv2.CAP_PROP_POS_FRAMES), count))if video_capture.get(cv2.CAP_PROP_POS_FRAMES) == 100:break;video_capture.release()output_video.release()cv2.destroyAllWindows()
- 運行
python ovc_openpose.py exp.mp4
用到的相關模型地址
Annotators
StableDiffusion
ControlNet
?