Datawhale X 魔搭 AI夏令营-AIGC文生图方向Task2笔记
学习链接:https://datawhaler.feishu.cn/wiki/UM7awcAuQicI4ukd2qtccT51nug由于不会专业性的术语所以在此依然是简单记录自己的运行和打卡。
1.文生图理论学习
Deepfake技能
Deepfake是一种使用人工智能技能生成的伪造媒体,特别是视频和音频,它们看起来或听起来非常真实,但实际上是由计算机生成的。这种技能通常涉及到深度学习算法,特别是生成对抗网络(GANs),它们能够学习真实数据的特征,并生成新的、逼真的数据。
从零入门CV图像比赛(Deepfake攻防)
AI生图的历史
AI生图模子属于多模态呆板学习模子,通过海量的图库和文本形貌的深度神经网络学习,终极的目标是可以根据输入的指示(不管是文本照旧图片照旧任何)生成符合语义的图片。
最早的AI生图可追溯到20世纪70年代,当时由艺术家哈罗德·科恩(Harold Cohen)发明AARON,可通过机器臂输出作画。
今世的AI生图模子大多基于深度神经网络基础上训练,最早可追溯到2012年吴恩达训练出的能生成“猫脸”的模子。
它使用卷积神经网络(CNN)训练,证明确深度学习模子能够学习到图像的复杂特征。
2015年,谷歌推出了“深梦”(Deep Dream)图像生成工具,类似一个高级滤镜,可以基于给定的图片生成梦幻版图片。
2021 年 1 月 OpenAI 推出DALL-E模子(一个深度学习算法模子,是GPT-3 语言处理模子的一个衍生版本),能直接从文本提示“按需创造”风格多样的图形设计。
通过学习大量画家的作品,AI生图模子 往往可以照猫画虎绘制出类似的画作,在2022年8月,AI生图真正走进了大众的视野,让各个范畴无法忽视。
当时让AI生图破圈的是AI绘画作品《太空歌剧院》,该作品在美国科罗拉多州举行的新兴数字艺术家比赛中获得了角逐“数字艺术/数字修饰照片”种别一等奖,引起了当时“艺术家们 Not Happy”的社会舆论。
Kolors(可图)模子
Kolors是一个基于潜扩散的大规模文本转图像生成模子,由快手Kolors团队开发。颠末数十亿对文本-图像对的训练,Kolors 在视觉质量、复杂语义正确性以及中英文字符的文本渲染方面与开源和专有模子相比都体现出显着上风。别的,Kolors支持中文和英文输入,在明白和生成中文特定内容方面体现出强大的体现。
2.借助AI助手分析代码
使用通义千问AI助手的好处有:帮助我们分析代码的主题框架,甚至可以进行逐行的代码分析。
baseline
https://i-blog.csdnimg.cn/direct/2d93de9fe025425ab5e5878e9ad05b77.png
!pip install simple-aesthetics-predictor
!pip install -v -e data-juicer
!pip uninstall pytorch-lightning -y
!pip install peft lightning pandas torchvision
!pip install -e DiffSynth-Studio
from modelscope.msdatasets import MsDataset
ds = MsDataset.load(
'AI-ModelScope/lowres_anime',
subset_name='default',
split='train',
cache_dir="/mnt/workspace/kolors/data"
)
import json, os
from data_juicer.utils.mm_utils import SpecialTokens
from tqdm import tqdm
os.makedirs("./data/lora_dataset/train", exist_ok=True)
os.makedirs("./data/data-juicer/input", exist_ok=True)
with open("./data/data-juicer/input/metadata.jsonl", "w") as f:
for data_id, data in enumerate(tqdm(ds)):
image = data["image"].convert("RGB")
image.save(f"/mnt/workspace/kolors/data/lora_dataset/train/{data_id}.jpg")
metadata = {"text": "二次元", "image": }
f.write(json.dumps(metadata))
f.write("\n")
data_juicer_config = """
# global parameters
project_name: 'data-process'
dataset_path: './data/data-juicer/input/metadata.jsonl'# path to your dataset directory or file
np: 4# number of subprocess to process your dataset
text_keys: 'text'
image_key: 'image'
image_special_token: '<__dj__image>'
export_path: './data/data-juicer/output/result.jsonl'
# process schedule
# a list of several process operators with their arguments
process:
- image_shape_filter:
min_width: 1024
min_height: 1024
any_or_all: any
- image_aspect_ratio_filter:
min_ratio: 0.5
max_ratio: 2.0
any_or_all: any
"""
with open("data/data-juicer/data_juicer_config.yaml", "w") as file:
file.write(data_juicer_config.strip())
!dj-process --config data/data-juicer/data_juicer_config.yaml
import pandas as pd
import os, json
from PIL import Image
from tqdm import tqdm
texts, file_names = [], []
os.makedirs("./data/data-juicer/output/images", exist_ok=True)
with open("./data/data-juicer/output/result.jsonl", "r") as f:
for line in tqdm(f):
metadata = json.loads(line)
texts.append(metadata["text"])
file_names.append(metadata["image"])
df = pd.DataFrame({"text": texts, "file_name": file_names})
df.to_csv("./data/data-juicer/output/result.csv", index=False)
df
from transformers import CLIPProcessor, CLIPModel
import torch
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
images = ]
inputs = processor(text=df["text"].tolist(), images=images, return_tensors="pt", padding=True)
outputs = model(**inputs)
logits_per_image = outputs.logits_per_image# this is the image-text similarity score
probs = logits_per_image.softmax(dim=1)# we can take the softmax to get the probabilities
probs
from torch.utils.data import Dataset, DataLoader
class CustomDataset(Dataset):
def __init__(self, df, processor):
self.texts = df["text"].tolist()
self.images = ]
self.processor = processor
def __len__(self):
return len(self.texts)
def __getitem__(self, idx):
inputs = self.processor(text=self.texts, images=self.images, return_tensors="pt", padding=True)
return inputs
dataset = CustomDataset(df, processor)
dataloader = DataLoader(dataset, batch_size=8)
for batch in dataloader:
outputs = model(**batch)
logits_per_image = outputs.logits_per_image
probs = logits_per_image.softmax(dim=1)
print(probs)
import torch
from diffusers import StableDiffusionPipeline
torch.manual_seed(1)
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v-1-4", torch_dtype=torch.float16)
pipe = pipe.to("cuda")
prompt = "二次元,一个紫色长发小女孩穿着粉色吊带漏肩连衣裙,在练习室练习唱歌,手持话筒"
negative_prompt = "丑陋、变形、嘈杂、模糊、低对比度"
guidance_scale = 4
num_inference_steps = 50
image = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps,
height=1024,
width=1024,
).images
image.save("example_image.png")
image
from PIL import Image
torch.manual_seed(1)
image = pipe(
prompt="二次元,日系动漫,演唱会的观众席,人山人海,一个紫色短发小女孩穿着粉色吊带漏肩连衣裙坐在演唱会的观众席,舞台上衣着华丽的歌星们在唱歌",
negative_prompt="丑陋、变形、嘈杂、模糊、低对比度",
cfg_scale=4,
num_inference_steps=50, height=1024, width=1024,
)
image.save("1.jpg")
torch.manual_seed(1)
image = pipe(
prompt="二次元,一个紫色短发小女孩穿着粉色吊带漏肩连衣裙坐在演唱会的观众席,露出憧憬的神情",
negative_prompt="丑陋、变形、嘈杂、模糊、低对比度,色情擦边",
cfg_scale=4,
num_inference_steps=50, height=1024, width=1024,
)
image.save("2.jpg")
torch.manual_seed(2)
image = pipe(
prompt="二次元,一个紫色短发小女孩穿着粉色吊带漏肩连衣裙坐在演唱会的观众席,露出憧憬的神情",
negative_prompt="丑陋、变形、嘈杂、模糊、低对比度,色情擦边",
cfg_scale=4,
num_inference_steps=50, height=1024, width=1024,
)
image.save("3.jpg")
torch.manual_seed(5)
image = pipe(
prompt="二次元,一个紫色短发小女孩穿着粉色吊带漏肩连衣裙,对着流星许愿,闭着眼睛,十指交叉,侧面",
negative_prompt="丑陋、变形、嘈杂、模糊、低对比度,扭曲的手指,多余的手指",
cfg_scale=4,
num_inference_steps=50, height=1024, width=1024,
)
image.save("4.jpg")
torch.manual_seed(0)
image = pipe(
prompt="二次元,一个紫色中等长度头发小女孩穿着粉色吊带漏肩连衣裙,在练习室练习唱歌",
negative_prompt="丑陋、变形、嘈杂、模糊、低对比度",
cfg_scale=4,
num_inference_steps=50, height=1024, width=1024,
)
image.save("5.jpg")
torch.manual_seed(1)
image = pipe(
prompt="二次元,一个紫色长发小女孩穿着粉色吊带漏肩连衣裙,在练习室练习唱歌,手持话筒",
negative_prompt="丑陋、变形、嘈杂、模糊、低对比度",
cfg_scale=4,
num_inference_steps=50, height=1024, width=1024,
)
image.save("6.jpg")
torch.manual_seed(7)
image = pipe(
prompt="二次元,紫色长发少女,穿着黑色连衣裙,试衣间,心情忐忑",
negative_prompt="丑陋、变形、嘈杂、模糊、低对比度",
cfg_scale=4,
num_inference_steps=50, height=1024, width=1024,
)
image.save("7.jpg")
torch.manual_seed(0)
image = pipe(
prompt="二次元,紫色长发少女,穿着黑色礼服,连衣裙,在台上唱歌",
negative_prompt="丑陋、变形、嘈杂、模糊、低对比度",
cfg_scale=4,
num_inference_steps=50, height=1024, width=1024,
)
image.save("8.jpg")
import numpy as np
from PIL import Image
images =
image = np.concatenate([
np.concatenate(images, axis=1),
np.concatenate(images, axis=1),
np.concatenate(images, axis=1),
np.concatenate(images, axis=1),
], axis=0)
image = Image.fromarray(image).resize((1024, 2048))
image
下面进利用用通义千问明白代码框架https://i-blog.csdnimg.cn/direct/4227ab9395ce4e16923d5c50faee8b81.png
后续我还继续向它提问,它可以逐行分析并且加上注释,非常智能。
(以前只是利用AI帮忙查找资料,本日学习到了利用AI帮忙做注释和分析,我又拓展了自己的眼界,并且学习到了方法,以后会要多加进行运用。)
2.借助AI助手生成prompt
我:你是一个文生图专家,我们现在要做一个实战项目,就是要编排一个文生图话剧 话剧由8张场景图片生成,你需要输出每张图片的生图提示词 这个故事讲的是一个小时候喜欢弹钢琴的女孩,但是在妈妈的要求下学习数学,她学习了计算机,但最后她用计算机制作音乐,实现童年愿望的故事。
通义千问:
好的,让我们来为这个故事创作八个场景,并为每个场景编写生成图像的提示词。这里是一个简要概述:
[*]女孩小时候弹钢琴的场景 - 展示女孩坐在钢琴前,专注地弹奏着,周围有玩具和册本,营造出温馨的家庭氛围。
[*]女孩在母亲指导下学习数学的场景 - 形貌女孩与母亲一起在书桌前学习数学讲义的情形,母亲正耐心地解释数学题目。
[*]女孩上大学学习计算机科学的场景 - 呈现女孩在大学教室里,围绕着笔记本电脑和其他学生,背景是充满科技感的环境。
[*]女孩在图书馆研究编程的场景 - 展示女孩沉浸在图书馆的书堆中,正在阅读有关编程的册本和技能手册。
[*]女孩开始探索音乐制作软件的场景 - 刻画女孩在家里使用电脑和音乐制作软件创作音乐的画面。
[*]女孩在工作室录制音乐的场景 - 展示女孩在一个专业的音乐工作室里,用计算机合成器和其他音乐设备录制歌曲。
[*]女孩在音乐会现场演出的场景 - 形貌女孩站在舞台上,使用电子乐器演奏自己创作的音乐,观众在欣赏。
[*]女孩实现了童年愿望的场景 - 呈现女孩在钢琴旁,周围是计算机设备和音乐制作工具,脸上洋溢着幸福的笑脸。
接下来,我将给出具体的提示词:
[*] 场景一:
[*]“一个小女孩穿着睡衣,坐在豁亮的房间内的一架钢琴前,专注地弹奏着,旁边散落着一些玩具和册本。”
[*] 场景二:
[*]“一位母亲坐在书桌旁,手拿数学讲义,耐心地向她的女儿解释一道难题,女儿认真听着。”
[*] 场景三:
[*]“一位年轻女性坐在大学教室里,周围是其他学生,她眼前放着一台笔记本电脑,屏幕上显示着编程代码,教室装饰着各种计算机科学相干的海报。”
[*] 场景四:
[*]“女孩独自一人在图书馆的书桌前,周围堆满了关于编程和音乐理论的册本,她正在认真地做笔记。”
[*] 场景五:
[*]“女孩坐在家中,眼前摆放着一台打开的笔记本电脑,电脑上显示着音乐制作软件,女孩正用键盘和鼠标创作音乐。”
[*] 场景六:
[*]“女孩站在一个专业的音乐工作室里,戴着耳机,面对一台音乐合成器,旁边有一台笔记本电脑和录音设备。”
[*] 场景七:
[*]“女孩在一场音乐会的舞台上,使用电子钢琴和计算机设备进行现场演出,观众们围坐在舞台前,聚精会神地聆听。”
[*] 场景八:
[*]“女孩坐在一台钢琴旁,周围摆放着各种计算机设备和音乐制作工具,她正微笑着弹奏钢琴,墙上挂着她的音乐会海报。”
我们可以按照这些提示词来生成
结果展示
https://i-blog.csdnimg.cn/direct/ba3cba9ed3f345d1aacd0765a03f64b3.jpeg
https://i-blog.csdnimg.cn/direct/eb7ec31f084544edb42f32ba82617371.jpeg
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]