2023-06-14 16:57:02 +00:00
|
|
|
from transformers import CLIPVisionModelWithProjection, CLIPVisionConfig, CLIPImageProcessor, modeling_utils
|
2023-04-02 03:19:15 +00:00
|
|
|
from .utils import load_torch_file, transformers_convert
|
|
|
|
import os
|
2023-04-09 19:47:35 +00:00
|
|
|
import torch
|
2023-08-28 19:26:29 +00:00
|
|
|
import contextlib
|
|
|
|
|
2023-06-15 00:13:08 +00:00
|
|
|
import comfy.ops
|
2023-08-28 19:26:29 +00:00
|
|
|
import comfy.model_patcher
|
|
|
|
import comfy.model_management
|
2023-04-02 03:19:15 +00:00
|
|
|
|
|
|
|
class ClipVisionModel():
|
|
|
|
def __init__(self, json_config):
|
|
|
|
config = CLIPVisionConfig.from_json_file(json_config)
|
2023-08-28 19:26:29 +00:00
|
|
|
self.load_device = comfy.model_management.text_encoder_device()
|
|
|
|
offload_device = comfy.model_management.text_encoder_offload_device()
|
|
|
|
self.dtype = torch.float32
|
|
|
|
if comfy.model_management.should_use_fp16(self.load_device, prioritize_performance=False):
|
|
|
|
self.dtype = torch.float16
|
|
|
|
|
|
|
|
with comfy.ops.use_comfy_ops(offload_device, self.dtype):
|
2023-06-15 00:13:08 +00:00
|
|
|
with modeling_utils.no_init_weights():
|
|
|
|
self.model = CLIPVisionModelWithProjection(config)
|
2023-08-28 19:26:29 +00:00
|
|
|
self.model.to(self.dtype)
|
|
|
|
|
|
|
|
self.patcher = comfy.model_patcher.ModelPatcher(self.model, load_device=self.load_device, offload_device=offload_device)
|
2023-04-02 03:19:15 +00:00
|
|
|
self.processor = CLIPImageProcessor(crop_size=224,
|
|
|
|
do_center_crop=True,
|
|
|
|
do_convert_rgb=True,
|
|
|
|
do_normalize=True,
|
|
|
|
do_resize=True,
|
|
|
|
image_mean=[ 0.48145466,0.4578275,0.40821073],
|
|
|
|
image_std=[0.26862954,0.26130258,0.27577711],
|
|
|
|
resample=3, #bicubic
|
|
|
|
size=224)
|
|
|
|
|
|
|
|
def load_sd(self, sd):
|
2023-06-19 01:21:17 +00:00
|
|
|
return self.model.load_state_dict(sd, strict=False)
|
2023-04-02 03:19:15 +00:00
|
|
|
|
|
|
|
def encode_image(self, image):
|
2023-08-14 20:54:05 +00:00
|
|
|
img = torch.clip((255. * image), 0, 255).round().int()
|
2023-08-16 15:36:22 +00:00
|
|
|
img = list(map(lambda a: a, img))
|
2023-08-14 20:54:05 +00:00
|
|
|
inputs = self.processor(images=img, return_tensors="pt")
|
2023-08-28 19:26:29 +00:00
|
|
|
comfy.model_management.load_model_gpu(self.patcher)
|
|
|
|
pixel_values = inputs['pixel_values'].to(self.load_device)
|
|
|
|
|
|
|
|
if self.dtype != torch.float32:
|
|
|
|
precision_scope = torch.autocast
|
|
|
|
else:
|
|
|
|
precision_scope = lambda a, b: contextlib.nullcontext(a)
|
|
|
|
|
|
|
|
with precision_scope(comfy.model_management.get_autocast_device(self.load_device), torch.float32):
|
|
|
|
outputs = self.model(pixel_values=pixel_values)
|
2023-08-28 20:26:11 +00:00
|
|
|
|
|
|
|
for k in outputs:
|
|
|
|
t = outputs[k]
|
|
|
|
if t is not None:
|
|
|
|
outputs[k] = t.cpu()
|
2023-04-02 03:19:15 +00:00
|
|
|
return outputs
|
|
|
|
|
2023-06-22 17:03:50 +00:00
|
|
|
def convert_to_transformers(sd, prefix):
|
2023-04-02 03:19:15 +00:00
|
|
|
sd_k = sd.keys()
|
2023-06-22 17:03:50 +00:00
|
|
|
if "{}transformer.resblocks.0.attn.in_proj_weight".format(prefix) in sd_k:
|
2023-04-02 03:19:15 +00:00
|
|
|
keys_to_replace = {
|
2023-06-22 17:03:50 +00:00
|
|
|
"{}class_embedding".format(prefix): "vision_model.embeddings.class_embedding",
|
|
|
|
"{}conv1.weight".format(prefix): "vision_model.embeddings.patch_embedding.weight",
|
|
|
|
"{}positional_embedding".format(prefix): "vision_model.embeddings.position_embedding.weight",
|
|
|
|
"{}ln_post.bias".format(prefix): "vision_model.post_layernorm.bias",
|
|
|
|
"{}ln_post.weight".format(prefix): "vision_model.post_layernorm.weight",
|
|
|
|
"{}ln_pre.bias".format(prefix): "vision_model.pre_layrnorm.bias",
|
|
|
|
"{}ln_pre.weight".format(prefix): "vision_model.pre_layrnorm.weight",
|
2023-04-02 03:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for x in keys_to_replace:
|
|
|
|
if x in sd_k:
|
|
|
|
sd[keys_to_replace[x]] = sd.pop(x)
|
|
|
|
|
2023-06-22 17:03:50 +00:00
|
|
|
if "{}proj".format(prefix) in sd_k:
|
|
|
|
sd['visual_projection.weight'] = sd.pop("{}proj".format(prefix)).transpose(0, 1)
|
2023-04-02 03:19:15 +00:00
|
|
|
|
2023-08-18 15:13:29 +00:00
|
|
|
sd = transformers_convert(sd, prefix, "vision_model.", 48)
|
2023-04-02 03:19:15 +00:00
|
|
|
return sd
|
|
|
|
|
2023-06-23 05:08:05 +00:00
|
|
|
def load_clipvision_from_sd(sd, prefix="", convert_keys=False):
|
|
|
|
if convert_keys:
|
|
|
|
sd = convert_to_transformers(sd, prefix)
|
2023-08-18 15:13:29 +00:00
|
|
|
if "vision_model.encoder.layers.47.layer_norm1.weight" in sd:
|
|
|
|
json_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), "clip_vision_config_g.json")
|
|
|
|
elif "vision_model.encoder.layers.30.layer_norm1.weight" in sd:
|
2023-04-02 03:19:15 +00:00
|
|
|
json_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), "clip_vision_config_h.json")
|
|
|
|
else:
|
|
|
|
json_config = os.path.join(os.path.dirname(os.path.realpath(__file__)), "clip_vision_config_vitl.json")
|
|
|
|
clip = ClipVisionModel(json_config)
|
2023-06-19 01:21:17 +00:00
|
|
|
m, u = clip.load_sd(sd)
|
2023-08-18 15:13:29 +00:00
|
|
|
if len(m) > 0:
|
|
|
|
print("missing clip vision:", m)
|
2023-06-19 01:21:17 +00:00
|
|
|
u = set(u)
|
|
|
|
keys = list(sd.keys())
|
|
|
|
for k in keys:
|
|
|
|
if k not in u:
|
|
|
|
t = sd.pop(k)
|
|
|
|
del t
|
2023-04-02 03:19:15 +00:00
|
|
|
return clip
|
|
|
|
|
|
|
|
def load(ckpt_path):
|
|
|
|
sd = load_torch_file(ckpt_path)
|
2023-08-18 15:13:29 +00:00
|
|
|
if "visual.transformer.resblocks.0.attn.in_proj_weight" in sd:
|
|
|
|
return load_clipvision_from_sd(sd, prefix="visual.", convert_keys=True)
|
|
|
|
else:
|
|
|
|
return load_clipvision_from_sd(sd)
|