nodes_upscale_model.py 1.73 KB
Newer Older
1
2
import os
from comfy_extras.chainner_models import model_loading
3
from comfy import model_management
4
import torch
5
import comfy.utils
6
import folder_paths
7
from tqdm.auto import tqdm
8
9
10
11

class UpscaleModelLoader:
    @classmethod
    def INPUT_TYPES(s):
12
        return {"required": { "model_name": (folder_paths.get_filename_list("upscale_models"), ),
13
14
15
16
17
18
19
                             }}
    RETURN_TYPES = ("UPSCALE_MODEL",)
    FUNCTION = "load_model"

    CATEGORY = "loaders"

    def load_model(self, model_name):
20
        model_path = folder_paths.get_full_path("upscale_models", model_name)
21
        sd = comfy.utils.load_torch_file(model_path)
22
23
24
25
26
27
28
29
30
31
32
33
34
        out = model_loading.load_state_dict(sd).eval()
        return (out, )


class ImageUpscaleWithModel:
    @classmethod
    def INPUT_TYPES(s):
        return {"required": { "upscale_model": ("UPSCALE_MODEL",),
                              "image": ("IMAGE",),
                              }}
    RETURN_TYPES = ("IMAGE",)
    FUNCTION = "upscale"

35
    CATEGORY = "image/upscaling"
36
37

    def upscale(self, upscale_model, image):
38
        device = model_management.get_torch_device()
39
40
        upscale_model.to(device)
        in_img = image.movedim(-1,-3).to(device)
41
42
43
44
45
46

        tile = 128 + 64
        overlap = 8
        its = -(in_img.shape[2] // -(tile - overlap)) * -(in_img.shape[3] // -(tile - overlap))
        pbar = tqdm(total=its)
        s = comfy.utils.tiled_scale(in_img, lambda a: upscale_model(a), tile_x=tile, tile_y=tile, overlap=overlap, upscale_amount=upscale_model.scale, pbar=pbar)
47
48
49
50
51
52
53
54
        upscale_model.cpu()
        s = torch.clamp(s.movedim(-3,-1), min=0, max=1.0)
        return (s,)

NODE_CLASS_MAPPINGS = {
    "UpscaleModelLoader": UpscaleModelLoader,
    "ImageUpscaleWithModel": ImageUpscaleWithModel
}