Files
CavinHuang-comfyui-nodes-docs/docs/LoadLatent.md
2024-05-15 08:09:49 +08:00

1.9 KiB

Documentation

该节点旨在从指定目录中检索和处理潜在数据,确保数据被正确缩放和格式化以便在系统中进一步使用。

Input types

Required

  • latent
    • ‘latent’参数至关重要,因为它指定了要加载的潜在文件,其中包含节点操作所需的数据。
    • Comfy dtype: COMBO[string]
    • Python dtype: List[str]

Output types

  • samples
    • ‘samples’输出提供了处理后的潜在数据,已经过缩放,准备在系统中使用。
    • Comfy dtype: dict
    • Python dtype: Dict[str, torch.Tensor]

Usage tips

  • Infra type: CPU

Source code

class LoadLatent:

    @classmethod
    def INPUT_TYPES(s):
        input_dir = folder_paths.get_input_directory()
        files = [f for f in os.listdir(input_dir) if os.path.isfile(os.path.join(input_dir, f)) and f.endswith('.latent')]
        return {'required': {'latent': [sorted(files)]}}
    CATEGORY = '_for_testing'
    RETURN_TYPES = ('LATENT',)
    FUNCTION = 'load'

    def load(self, latent):
        latent_path = folder_paths.get_annotated_filepath(latent)
        latent = safetensors.torch.load_file(latent_path, device='cpu')
        multiplier = 1.0
        if 'latent_format_version_0' not in latent:
            multiplier = 1.0 / 0.18215
        samples = {'samples': latent['latent_tensor'].float() * multiplier}
        return (samples,)

    @classmethod
    def IS_CHANGED(s, latent):
        image_path = folder_paths.get_annotated_filepath(latent)
        m = hashlib.sha256()
        with open(image_path, 'rb') as f:
            m.update(f.read())
        return m.digest().hex()

    @classmethod
    def VALIDATE_INPUTS(s, latent):
        if not folder_paths.exists_annotated_filepath(latent):
            return 'Invalid latent file: {}'.format(latent)
        return True