1038lab commited on
Commit
c3aaf76
·
verified ·
1 Parent(s): 3e6631c

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +111 -0
README.md ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model:
3
+ - meta-llama/Llama-3.1-8B-Instruct
4
+ - google/siglip2-so400m-patch14-384
5
+ tags:
6
+ - captioning
7
+ pipeline_tag: image-text-to-text
8
+ library_name: transformers
9
+ ---
10
+ # Model Card for Llama JoyCaption Beta One
11
+
12
+ [Github](https://github.com/fpgaminer/joycaption)
13
+
14
+ JoyCaption is an image captioning Visual Language Model (VLM) built from the ground up as a free, open, and uncensored model for the community to use in training Diffusion models.
15
+
16
+ Key Features:
17
+ - **Free and Open**: Always released for free, open weights, no restrictions, and just like [bigASP](https://www.reddit.com/r/StableDiffusion/comments/1dbasvx/the_gory_details_of_finetuning_sdxl_for_30m/), will come with training scripts and lots of juicy details on how it gets built.
18
+ - **Uncensored**: Equal coverage of SFW and NSFW concepts. No "cylindrical shaped object with a white substance coming out on it" here.
19
+ - **Diversity**: All are welcome here. Do you like digital art? Photoreal? Anime? Furry? JoyCaption is for everyone. Pains are being taken to ensure broad coverage of image styles, content, ethnicity, gender, orientation, etc.
20
+ - **Minimal Filtering**: JoyCaption is trained on large swathes of images so that it can understand almost all aspects of our world. almost. Illegal content will never be tolerated in JoyCaption's training.
21
+
22
+
23
+ ## Motivation
24
+
25
+ Automated descriptive captions enable the training and finetuning of diffusion models on a wider range of images, since trainers are no longer required to either find images with already associated text or write the descriptions themselves. They also improve the quality of generations produced by Text-to-Image models trained on them (ref: DALL-E 3 paper). But to-date, the community has been stuck with ChatGPT, which is expensive and heavily censored; or alternative models, like CogVLM, which are weaker than ChatGPT and have abysmal performance outside of the SFW domain.
26
+
27
+ I'm building JoyCaption to help fill this gap by performing near or on-par with GPT4o in captioning images, while being free, unrestricted, and open.
28
+
29
+
30
+ ## How to Get Started with the Model
31
+
32
+ Please see the [Github](https://github.com/fpgaminer/joycaption) for more details.
33
+
34
+ Example usage:
35
+
36
+ ```
37
+ import torch
38
+ from PIL import Image
39
+ from transformers import AutoProcessor, LlavaForConditionalGeneration
40
+
41
+
42
+ IMAGE_PATH = "image.jpg"
43
+ PROMPT = "Write a long descriptive caption for this image in a formal tone."
44
+ MODEL_NAME = "1038lab/llama-joycaption-beta-one"
45
+
46
+
47
+ # Load JoyCaption
48
+ # bfloat16 is the native dtype of the LLM used in JoyCaption (Llama 3.1)
49
+ # device_map=0 loads the model into the first GPU
50
+ processor = AutoProcessor.from_pretrained(MODEL_NAME)
51
+ llava_model = LlavaForConditionalGeneration.from_pretrained(MODEL_NAME, torch_dtype="bfloat16", device_map=0)
52
+ llava_model.eval()
53
+
54
+ with torch.no_grad():
55
+ # Load image
56
+ image = Image.open(IMAGE_PATH)
57
+
58
+ # Build the conversation
59
+ convo = [
60
+ {
61
+ "role": "system",
62
+ "content": "You are a helpful image captioner.",
63
+ },
64
+ {
65
+ "role": "user",
66
+ "content": PROMPT,
67
+ },
68
+ ]
69
+
70
+ # Format the conversation
71
+ # WARNING: HF's handling of chat's on Llava models is very fragile. This specific combination of processor.apply_chat_template(), and processor() works
72
+ # but if using other combinations always inspect the final input_ids to ensure they are correct. Often times you will end up with multiple <bos> tokens
73
+ # if not careful, which can make the model perform poorly.
74
+ convo_string = processor.apply_chat_template(convo, tokenize = False, add_generation_prompt = True)
75
+ assert isinstance(convo_string, str)
76
+
77
+ # Process the inputs
78
+ inputs = processor(text=[convo_string], images=[image], return_tensors="pt").to('cuda')
79
+ inputs['pixel_values'] = inputs['pixel_values'].to(torch.bfloat16)
80
+
81
+ # Generate the captions
82
+ generate_ids = llava_model.generate(
83
+ **inputs,
84
+ max_new_tokens=512,
85
+ do_sample=True,
86
+ suppress_tokens=None,
87
+ use_cache=True,
88
+ temperature=0.6,
89
+ top_k=None,
90
+ top_p=0.9,
91
+ )[0]
92
+
93
+ # Trim off the prompt
94
+ generate_ids = generate_ids[inputs['input_ids'].shape[1]:]
95
+
96
+ # Decode the caption
97
+ caption = processor.tokenizer.decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)
98
+ caption = caption.strip()
99
+ print(caption)
100
+ ```
101
+
102
+
103
+ ## vLLM
104
+
105
+ vLLM provides the highest performance inference for JoyCaption, and an OpenAI compatible API so JoyCaption can be used like any other VLMs. Example usage:
106
+
107
+ ```
108
+ vllm serve 1038lab/llama-joycaption-beta-one --max-model-len 4096 --enable-prefix-caching
109
+ ```
110
+
111
+ VLMs are a bit finicky on vLLM, and vLLM is memory hungry, so you may have to adjust settings for your particular environment, such as forcing eager mode, adjusting max-model-len, adjusting gpu_memory_utilization, etc.