Datawhale X 魔搭 AI夏令营-AIGC文生图方向Task2笔记

目录

前言

代码分析

​编辑

第一部分:安装和卸载依赖

第二部分:加载数据集

第三部分:数据预处理

第四部分:数据过滤

第五部分:数据整理

第六部分:模型评估

第七部分:创建PyTorch数据集

第八部分:图像生成

第九部分:组合生成的图像

代码修改 进阶提升

1.修改图像尺寸——生成要求的尺寸

2.修改提示词(Prompt)——生成要求的画面

3.调整生成参数 ——调节图像精致度

5.设置随机种子——设置图像随机程度

6.修改图像的组合方式——改变排序、大小等


前言

经过上一期task01的学习,我学习到了文生图的发展历程、文生图涉及到的相关知识点和重要名词、尝试通过baseline修改提示词并微调生成了属于自己的第一份图片。在第二阶段,我根据社区的资料进一步学习到了AI生图的前沿领域并且“从AI到AI”借助现在的大模型技术对baseline代码进行模块划分和理解,进一步对文生图的原理和并且呈现到该笔记中,同时尝试了scepter webui进行模型训练。

代码分析

下方展示的为baseline中的所有代码

!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"/mnt/workspace/kolors/data/lora_dataset/train/{data_id}.jpg"]}
        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"][0])

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 = [Image.open(img_path) for img_path in df["file_name"]]
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 = [Image.open(img_path) for img_path in df["file_name"]]
        self.processor = processor

    def __len__(self):
        return len(self.texts)

    def __getitem__(self, idx):
        inputs = self.processor(text=self.texts[idx], images=self.images[idx], 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[0]

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 = [np.array(Image.open(f"{i}.jpg")) for i in range(1, 9)]
image = np.concatenate([
    np.concatenate(images[0:2], axis=1),
    np.concatenate(images[2:4], axis=1),
    np.concatenate(images[4:6], axis=1),
    np.concatenate(images[6:8], axis=1),
], axis=0)
image = Image.fromarray(image).resize((1024, 2048))
image

第一部分:安装和卸载依赖

使用!pip命令安装和卸载所需的Python包。

第二部分:加载数据集

使用ModelScope库加载名为lowres_anime的数据集,并将其保存到指定路径。

第三部分:数据预处理

将数据集中的图像转换为RGB格式,并将每张图像及对应的标签(文本描述)写入到JSONL文件中,为后续的数据处理做准备。

第四部分:数据过滤

创建配置文件data_juicer_config.yaml,定义数据处理的参数和流程。

使用data-juicer工具根据配置文件对数据进行过滤,例如筛选出特定尺寸和宽高比的图像。

第五部分:数据整理

读取经过过滤后的数据,将图像路径和对应的文本描述存储到Pandas DataFrame中,并保存为CSV文件。

第六部分:模型评估

加载CLIP模型和处理器,并使用它们来计算图像和文本之间的相似性得分。

第七部分:创建PyTorch数据集

定义一个自定义的PyTorch数据集类CustomDataset,用于加载图像和文本数据。

创建数据加载器DataLoader,以便在训练过程中高效地迭代数据集。

第八部分:图像生成

使用StableDiffusionPipeline从预训练模型生成图像。

根据不同的提示(prompt)和负面提示(negative prompt),生成多张具有特定内容的二次元风格图像。

第九部分:组合生成的图像

将生成的图像合并成一张大图,并调整大小。

代码修改 进阶提升

随着如今大模型技术的飞速发展,各种成品AI生图工具变得越来越普及和强大。但是自定义代码提仍然具有一些无法取代的优点,首先就是我们通过阅读和逐步执行代码的过程中可以增加我们对AI生图底层原理的了解并进行实验和创新,而不受限于预设软件的功能和限制,并且我们可以根据自己的需求精确调整数据处理流程、模型参数和生成设置,从而实现更精细的控制和优化。这种灵活性和可扩展性使得代码在我哦们的研究和开发阶段尤为宝贵,能够满足更专业和个性化的需求。

因此接下来我们介绍代码中可以简单进行进一步修改的部分,从而生成更加符合自己要求的优质图像

1.修改图像尺寸——生成要求的尺寸

在生成图像的部分,我们可以设置heightwidth参数来改变输出图像的尺寸。

# 修改高度和宽度
image = pipe(
    prompt=prompt,
    negative_prompt=negative_prompt,
    guidance_scale=guidance_scale,
    num_inference_steps=num_inference_steps,
    height=1024,  # 设置高度
    width=1024,   # 设置宽度
).images[0]

2.修改提示词(Prompt)——生成要求的画面

该部分即task1中提到的我们可以通过更改prompt变量的内容,你控制生成图像的主题和风格例如,更改描述人物的细节或者背景环境等,从而生成符合自己要求的图片。

# 更改prompt
prompt = "二次元,一个紫色长发小女孩穿着粉色吊带漏肩连衣裙,在练习室练习唱歌,手持话筒"
# 更改negative_prompt
negative_prompt = "丑陋、变形、嘈杂、模糊、低对比度"

需要注意的是在我们实际使用提示词时要尽量做到具体明确(使用简单直白的语言,AI可能不完全理解某些复杂的语言结构或文化隐喻),逻辑连贯,考虑上下文,考虑生成模型的能力。在生成图片后不断测试和调整,根据反馈不断修改提示词,最终得到自己满意的图片。

3.调整生成参数 ——调节图像精致度

# 调整生成参数
guidance_scale = 4
num_inference_steps = 50

注:guidance_scale为指导比例,较高的值会使生成的图像更符合提示。

num_inference_steps为推理步数,更多的步数通常会产生更精细的结果,但也会增加生成时间。

5.设置随机种子——设置图像随机程度

如果使生成结果可重复,可以设置固定的随机种子。这样每次运行相同的代码都会得到相同的结果。

# 设置随机种子
torch.manual_seed(1)

在编程中,"随机种子"(random seed)是用来初始化伪随机数生成器的一个初始值。设置随机种子可以让所谓的“随机”过程可重复。当你使用相同的种子值初始化随机数生成器时,无论何时运行程序,它都会产生相同的随机数序列。

在PyTorch等机器学习框架中,设置随机种子是非常重要的,特别是在开发和调试过程中。这可以确保实验的可重复性,即每次运行相同的代码片段时,模型初始化的权重、数据集的划分以及任何其他依赖随机性的部分都将保持一致。

6.修改图像的组合方式——改变排序、大小等

# 修改图像组合方式
images = [np.array(Image.open(f"{i}.jpg")) for i in range(1, 9)]
image = np.concatenate([
    np.concatenate(images[0:2], axis=1),
    np.concatenate(images[2:4], axis=1),
    np.concatenate(images[4:6], axis=1),
    np.concatenate(images[6:8], axis=1),
], axis=0)
image = Image.fromarray(image).resize((1024, 2048))

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值