-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoycaption.py
237 lines (203 loc) · 7.95 KB
/
joycaption.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import os
import torch
from loguru import logger
from torchvision import transforms
from transformers import (
AutoModel,
AutoModelForCausalLM,
AutoProcessor,
AutoTokenizer,
PreTrainedTokenizer,
PreTrainedTokenizerFast,
)
import folder_paths
VLM_PROMPT = "A beautiful descriptive caption for this image:\n"
JOYCAPTION_PATH = os.path.join(folder_paths.models_dir, "joycaption")
CLIP_PATH = os.path.join(JOYCAPTION_PATH, "google-siglip-so400m-patch14-384")
MODEL_PATH = os.path.join(JOYCAPTION_PATH, "Meta-Llama-3.1-8B-Instruct")
ADAPTER_MODEL_PATH = os.path.join(JOYCAPTION_PATH, "joy-caption-pre-alpha")
class ImageAdapter(torch.nn.Module):
def __init__(self, input_features: int, output_features: int):
super().__init__()
self.linear1 = torch.nn.Linear(input_features, output_features)
self.activation = torch.nn.GELU()
self.linear2 = torch.nn.Linear(output_features, output_features)
def forward(self, vision_outputs: torch.Tensor):
x = self.linear1(vision_outputs)
x = self.activation(x)
x = self.linear2(x)
return x
class LoadJoyCaption:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
}
}
RETURN_TYPES = ("JoyCaptionMODEL",)
RETURN_NAMES = ("models",)
FUNCTION = "loadmodel"
CATEGORY = "JoyCaption"
def loadmodel(self):
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
clip_processor = AutoProcessor.from_pretrained(CLIP_PATH)
clip_model = AutoModel.from_pretrained(CLIP_PATH)
clip_model = clip_model.vision_model
clip_model.eval()
clip_model.requires_grad_(False)
clip_model.to(device)
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, use_fast=False)
assert isinstance(tokenizer, PreTrainedTokenizer) or isinstance(
tokenizer, PreTrainedTokenizerFast
), f"Tokenizer is of type {type(tokenizer)}"
text_model = AutoModelForCausalLM.from_pretrained(
MODEL_PATH, device_map=device, torch_dtype=torch.bfloat16
)
text_model.eval()
image_adapter = ImageAdapter(
clip_model.config.hidden_size, text_model.config.hidden_size
)
image_adapter.load_state_dict(
torch.load(f"{ADAPTER_MODEL_PATH}/image_adapter.pt", map_location="cpu")
)
image_adapter.eval()
image_adapter.to(device)
JoyCaptionMODEL = (clip_processor,tokenizer,clip_model, image_adapter,text_model)
return (JoyCaptionMODEL,)
class JoyImageCaption:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"models": ("JoyCaptionMODEL",),
"do_sample": ("BOOLEAN", {"default": True}),
"max_new_tokens": ("INT", {"default": 300, "min": 1, "max": 0xfffffff}),
"temperature": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 1.0, "step": 0.01}),
"top_k": ("INT",{"default": 10}),
"suppress_tokens": (
"STRING",
{
"default": "",
"multiline": True,
"dynamicPrompts": True,
},
),
}
}
RETURN_TYPES = ("STRING",)
# RETURN_NAMES =("text",)
FUNCTION = "imagecaption"
CATEGORY = "JoyCaption"
def imagecaption(self, image, do_sample, max_new_tokens,temperature, top_k, suppress_tokens, models):
if torch.cuda.is_available():
device = "cuda"
else:
device = "cpu"
SIZE_LIMIT = 1536
logger.debug(f"{do_sample=}, {max_new_tokens=}, {temperature=}")
# suppress_tokens = [part.strip() for part in suppress_tokens.split(',')]
clip_processor,tokenizer,clip_model, image_adapter,text_model = models
if len(image.shape) == 4:
b, height, width, _ = image.shape # b, h, w, c in ComfyUI
if b != 1:
error_msg = f"Image batch size should be 1, got {b}"
logger.warning(error_msg)
return {
"type": "bizyair",
"error": error_msg,
}
image = image.squeeze(0)
image = image.permute(2, 0, 1)
elif len(image.shape) == 3: # c, h, w in PIL, for test python script
_, height, width = image.shape
if not (width <= SIZE_LIMIT and height <= SIZE_LIMIT):
error_msg = f"Image size should be less than {SIZE_LIMIT}x{SIZE_LIMIT}, got {width}x{height}"
logger.warning(error_msg)
return {
"type": "bizyair",
"error": error_msg,
}
to_pil = transforms.ToPILImage()
pil_img = to_pil(image)
image = clip_processor(images=pil_img, return_tensors="pt").pixel_values
image = image.to(device)
# Tokenize the prompt
prompt = tokenizer.encode(
VLM_PROMPT,
return_tensors="pt",
padding=False,
truncation=False,
add_special_tokens=False,
)
# Embed image
with torch.amp.autocast_mode.autocast(device, enabled=True):
vision_outputs = clip_model(
pixel_values=image, output_hidden_states=True
)
image_features = vision_outputs.hidden_states[-2]
embedded_images = image_adapter(image_features)
embedded_images = embedded_images.to(device)
# Embed prompt
prompt_embeds = text_model.model.embed_tokens(prompt.to(device))
assert prompt_embeds.shape == (
1,
prompt.shape[1],
text_model.config.hidden_size,
), f"Prompt shape is {prompt_embeds.shape}, expected {(1, prompt.shape[1], self.text_model.config.hidden_size)}"
embedded_bos = text_model.model.embed_tokens(
torch.tensor(
[[tokenizer.bos_token_id]],
device=text_model.device,
dtype=torch.int64,
)
)
# Construct prompts
inputs_embeds = torch.cat(
[
embedded_bos.expand(embedded_images.shape[0], -1, -1),
embedded_images.to(dtype=embedded_bos.dtype),
prompt_embeds.expand(embedded_images.shape[0], -1, -1),
],
dim=1,
)
input_ids = torch.cat(
[
torch.tensor([[tokenizer.bos_token_id]], dtype=torch.long),
torch.zeros((1, embedded_images.shape[1]), dtype=torch.long),
prompt,
],
dim=1,
).to(device)
attention_mask = torch.ones_like(input_ids)
suppress_tokens = tokenizer.encode(suppress_tokens, return_tensors='pt', padding=False, truncation=False, add_special_tokens=False)
suppress_tokens=[aa.tolist() for aa in suppress_tokens]
generate_ids = text_model.generate(
input_ids,
inputs_embeds=inputs_embeds,
attention_mask=attention_mask,
max_new_tokens=max_new_tokens,
do_sample=do_sample,
top_k = top_k,
temperature=temperature,
suppress_tokens=suppress_tokens,
)
# Trim off the prompt
generate_ids = generate_ids[:, input_ids.shape[1] :]
if generate_ids[0][-1] == tokenizer.eos_token_id:
generate_ids = generate_ids[:, :-1]
caption = tokenizer.batch_decode(
generate_ids, skip_special_tokens=False, clean_up_tokenization_spaces=False
)[0]
return (caption.strip(),)
NODE_CLASS_MAPPINGS = {
"LoadJoyCaptionModel": LoadJoyCaption,
"JoyImageCaption": JoyImageCaption,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"LoadJoyCaptionModel": "LoadJoyCaption",
"JoyImageCaption": "ImageJoyCaption",
}