Akegarasu / lora-scripts

LoRA & Dreambooth training scripts & GUI use kohya-ss's trainer, for diffusion model.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

关于图片实际分辨率与训练分辨率配置的疑问

brian95827 opened this issue · comments

假设配置时设置分辨率为 512x512
image

但是我发现数据集的数据都是1024x1024也能训练,请问这是在训练的时候把图片进行了缩放吗?

请问程序的处理方式是裁剪(只取512x512范围内的画面,丢弃以外的)还是等比缩放成512x512全部读取呢?

如果是等比缩放的话,那么是不是意味着如果是1920x1080的图片也会被缩放成512x512,也就是说原本16:9的图片会变成1:1的比例?这样是不是意味着图片被扭曲了?

如果可以的话,想问一下相关代码在哪个文件?哪个位置?

我的代码能力还是有待提高,只读到train_network.py就读不下去了,读了两三个小时,也还不知道参数传哪去了,也不知道是在哪里处理的图片。。。

谢谢大佬。。。
6562A5D4

找到了代码了! 代码说图片会先等比缩放,缩放以后如果还比设置的分辨率高,那就删掉一部分图片,保证最终图片的分辨率和设置的是一样大小。

# 画像を読み込む。戻り値はnumpy.ndarray,(original width, original height),(crop left, crop top, crop right, crop bottom)
def trim_and_resize_if_required(
    random_crop: bool, image: Image.Image, reso, resized_size: Tuple[int, int]
) -> Tuple[np.ndarray, Tuple[int, int], Tuple[int, int, int, int]]:
    image_height, image_width = image.shape[0:2]
    original_size = (image_width, image_height)  # size before resize

    if image_width != resized_size[0] or image_height != resized_size[1]:
        # リサイズする
        image = cv2.resize(image, resized_size, interpolation=cv2.INTER_AREA)  # INTER_AREAでやりたいのでcv2でリサイズ

    image_height, image_width = image.shape[0:2]

    if image_width > reso[0]:
        trim_size = image_width - reso[0]
        p = trim_size // 2 if not random_crop else random.randint(0, trim_size)
        # print("w", trim_size, p)
        image = image[:, p : p + reso[0]]
    if image_height > reso[1]:
        trim_size = image_height - reso[1]
        p = trim_size // 2 if not random_crop else random.randint(0, trim_size)
        # print("h", trim_size, p)
        image = image[p : p + reso[1]]

    # random cropの場合のcropされた値をどうcrop left/topに反映するべきか全くアイデアがない
    # I have no idea how to reflect the cropped value in crop left/top in the case of random crop

    crop_ltrb = BucketManager.get_crop_ltrb(reso, original_size)

    assert image.shape[0] == reso[1] and image.shape[1] == reso[0], f"internal error, illegal trimmed size: {image.shape}, {reso}"
    return image, original_size, crop_ltrb

还有,图片训练是不训练透明度的,也不知道为什么。。之前我自己处理图片都是保留了RGBA的。。看来是多此一举了。

def load_image(image_path):
    image = Image.open(image_path)
    if not image.mode == "RGB":
        image = image.convert("RGB")
    img = np.array(image, np.uint8)
    return img

不知道正确的读代码找关键信息的方式是什么,我是直接全局搜索resize然后打断点运行,哪个地方停,就看哪个地方的代码。。直接读train_network.py的话,训练过程又贼复杂,看不懂。。。